Skip to content

Pod Design

Yasser Sinjab edited this page Apr 8, 2019 · 10 revisions

Labels and Annotations

Labels is one of the best features in kubernetes for managing not only pods but even kubernetes resources.

Create a pod with two labels (app=frontend) and (env=prod):

kubectl run nginx --image=nginx --restart=Never --labels="app=frontend,env=prod"

Get the pod with all labels:

kubectl get po nginx --show-labels 

Get the pod with specific label (app):

kubectl get po nginx -L app

Label a running pod:

kubectl run nginx --image=nginx --restart=Never
kubectl label po nginx env=prod

Update a label for a current pod:

kubectl label po nginx env=dev --overwrite
kubectl get po nginx -L env

Create two pods with labels and let's do some selection:

kubectl run nginx2 --image=nginx --restart=Never --labels="app=frontend,env=dev"
kubectl run nginx1 --image=nginx --restart=Never --labels="app=frontend,env=prod"
# List all pods that has env=prod and app=frontend
kubectl get po -l env=prod,app=frontend

# List all pods that has app=frontend but not env=prod:
kubectl get po -l env!=prod,app=frontend --show-labels

# List all pods that has env either (dev) or prod:
kubectl get po -l 'env in (dev, prod)'

Use label to schedule a pod on a specific node. Let's explain node selector (you can use nodename too if you like)

# I'm using minikube
kubectl label node minikube gpu=false
kubectl explain pod.spec | grep node

Edit the pod.yaml and add node selector:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  nodeSelector:
    gpu: "false"
  containers:
  - image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Never
status: {}

Now annotations: They are similar to labels but they are not meant to hold identifying information, but they can hold large pieces of information.

kubectl run nginx --image=nginx --restart=Never
kubectl annotate po nginx description='My lovely nginx'
kubectl describe po nginx | grep Annotations
# Annotations:        description: My lovely nginx

You can annotate multiple pods (nginx1, nginx2, nginx3) by simply adding them together in the same command:

kubectl run nginx1 --image=nginx --restart=Never
kubectl run nginx2 --image=nginx --restart=Never
kubectl run nginx3 --image=nginx --restart=Never
kubectl annotate po nginx1 nginx2 nginx3 description='My lovely nginx'
# Or
kubectl annotate po nginx{1..3} description='My lovely nginx'

To remove label or annotations just add "-" after the label or annotation name:

kubectl annotate po nginx description-
kubectl label po nginx app-

Delete all pods

kubectl delete po --all

Deployments

To create a deployment using run command:

kubectl run nginx --image=nginx

Create nginx deployment with replica 3:

kubectl run nginx --image=nginx --replicas=3 

Create deployment for nginx, then update the image to nginx:1.14.2, display the revision number and the command that cause the change:

kubectl run nginx --image=nginx
kubectl set image deploy nginx nginx=nginx:1.14.2 --record
kubectl describe deploy nginx

#
...
Annotations:            deployment.kubernetes.io/revision: 2
                        kubernetes.io/change-cause: kubectl set image deploy nginx nginx=nginx:1.14.2 --record=true
...

Scale the previous deployment to have 4 replicas and print the roll out status

kubectl scale deploy nginx --replicas=4
kubectl rollout status deploy nginx

#Result
Waiting for deployment "nginx" rollout to finish: 1 of 4 updated replicas are available...
Waiting for deployment "nginx" rollout to finish: 2 of 4 updated replicas are available...
Waiting for deployment "nginx" rollout to finish: 3 of 4 updated replicas are available...
deployment "nginx" successfully rolled out

Get the replicaset generated by previous nginx deployment:

kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
nginx-5d769b96b5   4         4         4       6m41s

Create nginx deployment, change the image to nginx:1.14.2, then rollback to the previous deployment:

kubectl run nginx --image=nginx
kubectl set image deploy nginx nginx=nginx:1.14.2 --record
kubectl rollout undo deploy nginx

# to view the history of rollout use
kubectl rollout history deployment nginx
REVISION  CHANGE-CAUSE
2         kubectl set image deploy nginx nginx=nginx:1.14.2 --record=true
3         <none>

Rollback to a specific revision for the previous deployment:

kubectl rollout undo deploy nginx --to-revision=2

Create nginx deployment with 4 replicas, change the image to nginx:1.14.2 so it will perform rolling update by default, pause the process, check the rollout status, then resume the process:

kubectl run nginx --image=nginx --replicas=4 
kubectl set image deploy nginx nginx=nginx:1.14.2

kubectl rollout pause deploy nginx
kubectl rollout status deploy nginx

kubectl describe po | grep Image:

kubectl rollout resume deploy nginx

Create nginx deployment with 2 replicas, and set the deployment strategy to be Recreate:

kubectl explain deploy.spec.strategy.type
kubectl run nginx --image=nginx --replicas=2 -o yaml --dry-run > deployment.yaml

# Edit the yaml and set strategy type under spec
...
spec:
  strategy:
    type: Recreate
...

Configure the rolling update strategy for a deployment to have 50% of the number of replicas above the replica numbers while the update process:

kubectl explain deploy.spec.strategy.rollingUpdate
kubectl run nginx --image=nginx --replicas=4 -o yaml --dry-run > deployment.yaml

# Edit the yaml and set rollingUpdate's maxSurge under spec to 50%
...
spec:
  strategy:
    rollingUpdate:
      maxSurge: 50%
...

Create an auto-scaler for nginx deployemnt based on CPU usage percentage on 50% with minimum pods 1 and maximum 4, and get the HPA:

kubectl run nginx --image=nginx
kubectl autoscale deploy nginx --cpu-percent=50 --min=1 --max=5
kubectl get hpa

Jobs

Create a busybox job that echo 'Hello World!', check the logs of the job, and finally delete the job

kubectl run busybox --image=busybox --restart=OnFailure -- /bin/sh -c 'echo Hello World!'
kubectl logs jobs/busybox
kubectl delete jobs busybox

Jobs may be configured to create more than one pod instance and run them in parallel (parallelism) or sequentially (completions).

kubectl explain job.spec.parallelism
kubectl explain job.spec.completions

Create a job that that run 6 times sequentially but it allow 2 to run in parallel:

kubectl run busybox --image=busybox --restart=OnFailure --dry-run -o yaml -- /bin/sh -c 'echo Hello World!' > job.yaml

# Edit the job.yaml and add this under the spec
...
spec:
  completions: 6
  parallelism: 2
...

Create a busybox jon that sleep for 15 seconds, set the limit of time allowed for a Job pod to complete to 5 seconds and make sure that kubernetes terminated the job:

kubectl explain job.spec.activeDeadlineSeconds
kubectl run busybox --image=busybox --restart=OnFailure --dry-run -o yaml -- /bin/sh -c 'sleep 15;' > job.yaml

# Edit the job.yaml and add this under the spec
...
spec:
  activeDeadlineSeconds: 5
...

kubectl describe jobs busybox

Cronjobs

Create a busybox cronjob that run every one minute and print date:

kubectl run busybox --image=busybox --restart=OnFailure --schedule="*/1 * * * *" -- /bin/sh -c 'date;'
kubectl get cronjobs

#watch the jobs there will be a job every one minute
kubectl get jobs --watch
Clone this wiki locally