Sunday, December 13, 2020

Kubernetes - imperative and declarative way of managinv infracture


Imperative way of managing infracture is like 

A. Create objects
1. Create a pod
# kc run --image-nginx nginx
2. Create deployment
# kc create deployment --image=nginx nginx
3. Create a service to expose a deployment 
# kc expose deployment nginx --port 80

B. Update objects
4. Edit an existing object
# kc edit deployment nginx
5. Scale the deployment replica set
# kc scale deployment nginx --replicas=5
6. Update in image on deployment 
# kc set image deployment nginx nginx=nginx:1.18
These above command remain on session history
7. Update configuration
# kc create -f nginx.yaml
8. Editing an object using replace command
# kc replace -f nginx.yaml
9. Delete object
# kc delete -f nginx.yaml

declrartive approache for infracture
# kc apply -f nginx.yaml


A. Create objects
# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app: myapp
    type: front-end
  sepc: 
    containers:
    - name: nginx-container
      image: nginx
# kc create 0f naginx.yaml

B. Update object
If youlike to update any value, use edit..
# kc edit deployment nginx
when you run edit command, it opens with some additional fields, such as status:
which stires the status about the POD,
if you edit and save the file, its a live change. your local copy might not be upto date..
to resolve this proble, first edit the local copy of the file and make changes. and run the replace command to update the object.
# kc replace -f nginx.yaml
or you can complately delete and recreate object.
# kc replace --force -f ngins.yaml

This is still an imperative approach.
when you run replace option, make sure object exists before running the replac ecommand.
other wise it will fail.


Declarative approach.
Create approach - using apply command. it will create object if its already not created.
# kc apply -f nginx.yaml
or you can specify a directory rather than a file. This way all the files are created.
# kc apply -f /path-to_config_files/

Update objects
if you need to make change, you make change t config file and run the apply command
# kc apply -f nginx.yaml
if object exists, it wil not throw error. adding or updating will not have any problem,

In the exam, use imperative approach to create pod or deployment.
to edit you can use edit but for long time use, use declarative approach.

By default, when you run the command, resources will be created. Before you create resources,
you would like to test your commands, use the option --dry-run=client option.
This will not create resources but will check if your command is right.
you can also use -o yaml to see the resource definition output in yaml format on the screen.
 
POD
1. create an nginx pod
# kc run nginx --image-nginx
2. Generate POD manifest YAM lfile (use -o yaml  and --dry-run option to not to create object)
# kc run nginx --image=nginx --dry-run=client -o yaml

Deployment
1. create a deployment
# kc create deployment --image=nginx nginx
2. Generate deployment yam file (using -o yam and --dry-run option)
# kc create deployment -image=nginx nginx --dry-run -o yaml
3. Generate Deployment with 4 replicas
# kc create deployment nginx --image=nginx --replicas=4
4. Scale your deployment
# kc scale deployment nigix --replicas=4
or you can also create a yaml file and use it later
# kc create deployment nginx --image=nginx --dry-run=client -o yaml > nginx-deployment.yaml
Using this file, you can update the change with replicas or any other values before creating the deployment.

Service
1. Create a service called redis-service of type ClusterIP to expose the pod redis on port 6379
# kc expose pod redis --pod=6379 --name redis-service --dry-run=client -o yaml
it will automatically use the POD's label as a selector.
or
# kc create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
This will not use pod labels as a selector, instead it assume selectors as app=redis.
Note: It does not work if your pod has a different lable set so first generate a file and modify the selector before createing the service.
2. Create a service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes:
# kc expose pod nginx --port=80 --name nginx-service --type=NodePort --dry-run=client -o yaml
This will automatically use the pod's label as selectors but you can not specify the node port. You have to generate a definition file and then add the node port in manually before creating the service with the pod.
or 
# kc create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml
This iwll not use pods label as selectors
Here both ommands are not perfect. One can not accept a selector while other can not accept node port. 
Use kubectl expose command and generate a file. manually update nodeport before creating the service.
source: https://kubernetes.io/docs/reference/kubectl/conventions/

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. ...