3 - Pod design Flashcards
Create 3 pods with names nginx1,nginx2,nginx3. All of them should have the label app=v1
for i in seq 1 3
; do kubectl run nginx$i –image=nginx -l app=v1 ; done
Show all labels of the pods
kubectl get po –show-labels
Change the labels of pod ‘nginx2’ to be app=v2
kubectl label po nginx2 app=v2 –overwrite
Get the label ‘app’ for the pods (show a column with APP labels)
kubectl get po -L app
# or
kubectl get po –label-columns=app
Get only the ‘app=v2’ pods
kubectl get po -l app=v2
# or
kubectl get po -l ‘app in (v2)’
# or
kubectl get po –selector=app=v2
Add a new label tier=web to all pods having ‘app=v2’ or ‘app=v1’ labels
kubectl label po -l “app in(v1,v2)” tier=web
Add an annotation ‘owner: marketing’ to all pods having ‘app=v2’ label
kubectl annotate po -l “app=v2” owner=marketing
Remove the ‘app’ label from the pods we created before
kubectl label po nginx1 nginx2 nginx3 app-
# or
kubectl label po nginx{1..3} app-
# or
kubectl label po -l app app-
Annotate pods nginx1, nginx2, nginx3 with “description=’my description’” value
or
kubectl annotate po nginx1 nginx2 nginx3 description=’my description’
kubectl annotate po nginx{1..3} description=’my description’
Check the annotations for pod nginx1
kubectl annotate pod nginx1 –list
or
kubectl describe po nginx1 | grep -i ‘annotations’
or
kubectl get po nginx1 -o custom-columns=Name:metadata.name,ANNOTATIONS:metadata.annotations.description
Remove the annotations for these three pods
kubectl annotate po nginx{1..3} description- owner-
Remove these pods to have a clean state in your cluster
kubectl delete po nginx{1..3}
Create a pod that will be deployed to a Node that has the label ‘accelerator=nvidia-tesla-p100’
kubectl label nodes <your-node-name> accelerator=nvidia-tesla-p100
kubectl get nodes --show-labels</your-node-name>
apiVersion: v1
kind: Pod
metadata:
name: cuda-test
spec:
containers:
- name: cuda-test
image: “k8s.gcr.io/cuda-vector-add:v0.1”
nodeSelector: # add this
accelerator: nvidia-tesla-p100 # the selection label
kubectl explain po.spec
apiVersion: v1
kind: Pod
metadata:
name: affinity-pod
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: accelerator
operator: In
values:
- nvidia-tesla-p100
containers:
…
Taint a node with key tier and value frontend with the effect NoSchedule. Then, create a pod that tolerates this taint.
kubectl taint node node1 tier=frontend:NoSchedule # key=value:Effect
kubectl describe node node1 # view the taints on a node
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
containers:
- name: nginx
image: nginx
tolerations:
- key: “tier”
operator: “Equal”
value: “frontend”
effect: “NoSchedule”
Create a pod that will be placed on node controlplane. Use nodeSelector and tolerations.
apiVersion: v1
kind: Pod
metadata:
name: frontend
spec:
containers:
- name: nginx
image: nginx
nodeSelector:
kubernetes.io/hostname: controlplane
tolerations:
- key: “node-role.kubernetes.io/control-plane”
operator: “Exists”
effect: “NoSchedule”
Create a deployment with image nginx:1.18.0, called nginx, having 2 replicas, defining port 80 as the port that this container exposes (don’t create a service for this deployment)
change the replicas field from 1 to 2
kubectl create deployment nginx –image=nginx:1.18.0 –dry-run=client -o yaml > deploy.yaml
vi deploy.yaml
# add this section to the container spec and save the deploy.yaml file
# ports:
# - containerPort: 80
kubectl apply -f deploy.yaml
kubectl create deploy nginx –image=nginx:1.18.0 –replicas=2 –port=80
View the YAML of this deployment
kubectl get deploy nginx -o yaml
View the YAML of the replica set that was created by this deployment
kubectl describe deploy nginx # you’ll see the name of the replica set on the Events section and in the ‘NewReplicaSet’ property
# OR you can find rs directly by:
kubectl get rs -l run=nginx # if you created deployment by ‘run’ command
kubectl get rs -l app=nginx # if you created deployment by ‘create’ command
# you could also just do kubectl get rs
kubectl get rs nginx-7bf7478b77 -o yaml
Get the YAML for one of the pods
kubectl get po # get all the pods
# OR you can find pods directly by:
kubectl get po -l run=nginx # if you created deployment by ‘run’ command
kubectl get po -l app=nginx # if you created deployment by ‘create’ command
kubectl get po nginx-7bf7478b77-gjzp8 -o yaml