Monday, February 7, 2022

Day7 - k8s RC, RS, Deployment, Taint tolerance ..

2/07/2022 - Class Notes

ReCap from last class

- k8s
  - Architecture
  - Master node
  - Worker Node
  - Client (Your PC)

  - Configure Master, Worker, Client
  - kubectl 
    - using ad-hoc command
    - using yaml file

  - run command
  - expose command

yaml files
- pod.yaml
- service.yaml
- nginx.yml

yaml file contenet
apiVersion:
Kind
Metadata
spec:


pod has pod definition
service file has service definition

Go ahead and start aws instance or your VM.
$ ssh -i rsa_user user@ip

# kubectl get node
Master -> control-plane, master

# kubectl get pod

# kubectl get svc
  exposing service

# kc get svc nsserv -o yaml
# kc get pod ns -0 yaml



a service is a ogical set of pods and acts as a gateway, allowing ..

- we need multiple pods. How can we service multiple pods? how many ways we can create pods?

We are going to use replication set, replica-set or deployment.

service can decide how to manage pods. 

search for workload resources: 

https://kubernetes.io/docs/concepts/workloads/

Replication controller
a replicationcontroller rnsures that a specified number of pod replicas are running at any one itme. in other works, a replicationcontroller 


search for pod 
-> look for pod definition.

Search for replication controller

eg,

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:  # replication control spec
  replicas: 3
  selector: 
    name: nginx
  template: # pod definition
    metadata:
      name: nginx
      labels:
        app: nginx
    spec: # pod definition
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Would they be running on same node or on different?

schedular will decide on what node to create?

# vi rc.yml
# kc delete pod n1
# kc delete svc n1serviec
# kc get svc

# kc apply -f rc.yaml

# kc get -f rc.yml
you see desire, current, and ready
# kc get rc
# kc get pod

under name section, you see name attach..

RC is tightly coupled here. job is going to run at particular node only. if node is not available, job is on the queue.
multiple node with label?

instead if selector, for multiple label, we will use replica set.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
spec:
  # modify replicas according to your case
  replicas: 3
  selector:
    matchLabels:
      app: nginx 
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
        env: dev
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
# vi rs.yml

# kc apply -f rs.yml

# kc get pod

# kc delete -f rc.yml

# kc get rs
# kc get rs -o yaml # yaml file output

# kc get rs (replica set)
# kc describe rs 

# kc get pod
# kc delete pod nginx-abdfd
# kc get pod

# same number of pods.
cluster will always maintain 3 replicas all the time.


if you want to update,
just change the image: nginx:latest

# kc apply -f rs.yml

# kc get pod

# kc get pod nginx-dfssd -o yaml

look at the image version of the output.
spec"
 containers:
 - image: nginx:version

# kc get pod

# kc delete -f rs.yaml
# kc apply -f rs.yml

updating is a problem with replica set. All of the pods will be updated. your system may be down. couple of sec/minutes of downtime.

There is another method called 'deployment' All three options are good but 

lets say you want to update canery or blue greeen , or percent wise update. that time, you want to update on control rate rather then one time. 

you can use 'deployment'

only different is that you will be using object on deployment.

$ cat dep.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3 # rollout
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

# kc delete -f rs.yml

# kc get pods
nothing is there

# vi deploy.yml
paste the content above.

# kc apply -f deploy.yml

deployment is created.

# kc get deployments

# kc rollout status deployment/nginx-deployment

# kc get deployments

# kc get rs  # replica set, it will show there as well.

modify

# kc get pod
there pods are running


# kc set image deployment.v1.spps/nginx-deployment nginx=nginx:1.16.1

# kc edit deployment/nginx-deployment

image: 1.16.1 # change the versio under spec: containers:

its modified automatically

# kc get pod

kc rolllout status deployment/nginx-dployment

you will see message -  old replicas are on pending termination 


# kc get rs
update is graceful shutdown. when deployment is running, it will wait until all job on the pod is completed.

so, we see we will use deployment to update your product.

search for service on k8s documentation.


apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

# cat deploy.yaml

# kc get expose --help

you can use possible resource.

we have 
selector:
  app: myapp


expose outside
$ cat svc.yaml

spec:
  type: NodePort

# kc apply -f svc.yaml
# kc get svc



Dashboard
Simple example

How to create a dash board and deploy a micro service.

google for k8s dashboard

$ cat dashboard.yml 

ServiceAccount - object
apiVersion: Namespace
metadata:
  name: kubernetes-dashboard

what is role?
- its a permission, priviledge.
it contens roles, resources.

user define rules

cluster level roles
- clusterrole
and we bind with user.

assign permision based on the role to the user.

role binding - service account 
 - service account is just like a user. it is binded with certain roles, clusterRolebinding, role binding.

# kc apply -f recommended.yaml

# kc get pod -n kubernetes-dashboard

# kc get svc -n kubernetes-dashboard

how to modify?
download and apply or use the edit command

# kc edit svc -n kubernetes-svc kubernetes-dashboard 

Change from clusterip to NodePort

# kc get svc -n kubernetes-dashboard

you see type and ports different now..

port: 31687

get the ip of your node and use ip to access
https://ip:port

option
- token
- kubeconfig

how to get token

follow the guide create an authentication roken (rbac)
https://github.com/kubernetes/dashboard


create clusterrole binding

# vi user.yml

add service bindng

# kc -f user.yml 

# kc -n kubernetes-dashboard get secret $(kubectl - kubernetes-dashboard get sa/admin-user -o jsonoath="{.secrets[0].name}) =o go-template='{{.data.token | base64decode}}"

# kc -n kubentes-dashboard get sa/admin-user
# kc -n kubernetes-dashboard get sa/admin-user -o yaml

# kc -n kubernetes-dashbard get secret admin-user-token-mbkg8 -o yaml

# vi token
# cat token | base64decode

copy the =token and go to dash board and paste under token section 

now, you have access to dashboard.

where the pods are deployed?
# kc get pod
# kc describe pod <pod-name>

you will see under events what node deployed to.

or you can go to dashboard
go to default dashboard
go to pods and you will find it.


how do I deploy windows app (.net app?)?

or a linux server with 16gb of ram.

you want to select the node rather then randomly selecting it. You want to select your node based on your requirement.

- We will use nodeDelector option.

how to declare nodeSelector?
go to k8s web page and serch nodeselector

assigning pods to Nodes.


nodelecector:
  disktype: ssd

# kc get node

how to know what node to select?
use selector, level

# kc describe node <node-name> 

you will see namespace, allocated repsources, system resources, capacities.
look for labels

you have to specify values based on key-> value paid.

nodeSelector

multiple filter, 
afinity/anti-affinity

provides multiple options.


To select the pod, you can use these three options,
- nodeSelector
- Node affinity
- node anti-affinity

There is another option
Taint and tolerations

taint -> 
tolerations ->

k8s.io  - search for taints

search for taints and tolerations

# kc taint nodes node1 key1=value1:Noschedule

read about taint and toleration

======================
tomorrow,
configmap
pv
pvc
storageclass
ingress
statefulset
jobs

================

Today, we created
- pod, svc, rc, rs, deployment
- dashboard, tocken
- Controlling purpose -> NodeSelector, affinity, taint, toleration

# kc taint ...
# kc describe 

No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...