13 | 14 | ``` 15 | kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx.yaml 16 | 17 | // edit the label app: my-nginx and create the pod 18 | apiVersion: v1 19 | kind: Pod 20 | metadata: 21 | creationTimestamp: null 22 | labels: 23 | app: my-nginx 24 | name: nginx 25 | spec: 26 | containers: 27 | - image: nginx 28 | name: nginx 29 | ports: 30 | - containerPort: 80 31 | resources: {} 32 | dnsPolicy: ClusterFirst 33 | restartPolicy: Never 34 | status: {} 35 | 36 | kubectl create -f nginx.yaml 37 | ``` 38 |
39 |44 | 45 | ``` 46 | // create the below service 47 | apiVersion: v1 48 | kind: Service 49 | metadata: 50 | name: my-service 51 | spec: 52 | selector: 53 | app: my-nginx 54 | ports: 55 | - protocol: TCP 56 | port: 80 57 | targetPort: 9376 58 | 59 | kubectl create -f nginx-svc.yaml 60 | ``` 61 |
62 |67 | 68 | ``` 69 | // get the pod with labels 70 | kubectl get po nginx --show-labels 71 | 72 | // get the service and chekc the selector column 73 | kubectl get svc my-service -o wide 74 | ``` 75 |
76 |81 | 82 | ``` 83 | // delete the service 84 | kubectl delete svc my-service 85 | 86 | // create the service again 87 | kubectl expose po nginx --port=80 --target-port=9376 88 | 89 | // verify the label 90 | kubectl get svc -l app=my-nginx 91 | ``` 92 |
93 |98 | 99 | ``` 100 | // delete the service 101 | kubectl delete svc nginx 102 | 103 | // create service with expose command 104 | kubectl expose po nginx --port=80 --type=NodePort 105 | ``` 106 |
107 |
112 |
113 | ```
114 | // get the clusterIP from this command
115 | kubectl get svc nginx -o wide
116 |
117 | // create temporary busybox to check the nodeport
118 | kubectl run busybox --image=busybox --restart=Never -it --rm -- wget -o-
126 | 127 | ``` 128 | apiVersion: networking.k8s.io/v1 129 | kind: NetworkPolicy 130 | metadata: 131 | name: default-deny 132 | spec: 133 | podSelector: {} 134 | policyTypes: 135 | - Ingress 136 | ``` 137 |
138 |13 | 14 | ``` 15 | kubectl get pv 16 | ``` 17 |
18 |23 | 24 | ``` 25 | // task-pv-volume.yaml 26 | 27 | apiVersion: v1 28 | kind: PersistentVolume 29 | metadata: 30 | name: task-pv-volume 31 | labels: 32 | type: local 33 | spec: 34 | storageClassName: manual 35 | capacity: 36 | storage: 10Gi 37 | accessModes: 38 | - ReadWriteOnce 39 | hostPath: 40 | path: "/mnt/data" 41 | 42 | kubectl create -f task-pv-volume.yaml 43 | 44 | kubectl get pv 45 | ``` 46 |
47 |53 | 54 | ``` 55 | // task-pv-claim.yaml 56 | 57 | apiVersion: v1 58 | kind: PersistentVolumeClaim 59 | metadata: 60 | name: task-pv-claim 61 | spec: 62 | storageClassName: manual 63 | accessModes: 64 | - ReadWriteOnce 65 | resources: 66 | requests: 67 | storage: 3Gi 68 | 69 | kubectl create -f task-pv-claim.yaml 70 | 71 | kubectl get pvc 72 | ``` 73 |
74 |79 | 80 | ``` 81 | kubectl delete pvc task-pv-claim 82 | kubectl delete pv task-pv-volume 83 | ``` 84 |
85 |90 | 91 | ``` 92 | // emptyDir is the volume that lasts for the life of the pod 93 | 94 | apiVersion: v1 95 | kind: Pod 96 | metadata: 97 | name: redis 98 | spec: 99 | containers: 100 | - name: redis 101 | image: redis 102 | volumeMounts: 103 | - name: redis-storage 104 | mountPath: /data/redis 105 | volumes: 106 | - name: redis-storage 107 | emptyDir: {} 108 | 109 | kubectl create -f redis-storage.yaml 110 | ``` 111 |
112 |117 | 118 | ``` 119 | // first terminal 120 | kubectl exec -it redis-storage /bin/sh 121 | cd /data/redis 122 | echo 'This is called the file' > file.txt 123 | 124 | //open another tab 125 | kubectl exec -it redis-storage /bin/sh 126 | cat /data/redis/file.txt 127 | ``` 128 |
129 |134 | 135 | ``` 136 | kubectl delete pod redis 137 | 138 | kubectl create -f redis-storage.yaml 139 | kubectl exec -it redis-storage /bin/sh 140 | cat /data/redis/file.txt // file doesn't exist 141 | ``` 142 |
143 |148 | 149 | ``` 150 | kubectl create -f task-pv-volume.yaml 151 | kubectl create -f task-pv-claim.yaml 152 | 153 | kubectl get pv 154 | kubectl get pvc 155 | ``` 156 |
157 |162 | 163 | ``` 164 | // task-pv-pod.yaml 165 | 166 | apiVersion: v1 167 | kind: Pod 168 | metadata: 169 | name: task-pv-pod 170 | spec: 171 | volumes: 172 | - name: task-pv-storage 173 | persistentVolumeClaim: 174 | claimName: task-pv-claim 175 | containers: 176 | - name: task-pv-container 177 | image: nginx 178 | ports: 179 | - containerPort: 80 180 | name: "http-server" 181 | volumeMounts: 182 | - mountPath: "/usr/share/nginx/html" 183 | name: task-pv-storage 184 | 185 | kubectl create -f task-pv-pod.yaml 186 | ``` 187 |
188 |11 | 12 | ``` 13 | // first create single container pod with dry run flag 14 | kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml 15 | 16 | // edit the pod like below 17 | 18 | apiVersion: v1 19 | kind: Pod 20 | metadata: 21 | creationTimestamp: null 22 | labels: 23 | run: busybox 24 | name: busybox 25 | spec: 26 | containers: 27 | - args: 28 | - bin/sh 29 | - -c 30 | - ls; sleep 3600 31 | image: busybox 32 | name: busybox1 33 | resources: {} 34 | - args: 35 | - bin/sh 36 | - -c 37 | - echo Hello world; sleep 3600 38 | image: busybox 39 | name: busybox2 40 | resources: {} 41 | - args: 42 | - bin/sh 43 | - -c 44 | - echo this is third container; sleep 3600 45 | image: busybox 46 | name: busybox3 47 | resources: {} 48 | dnsPolicy: ClusterFirst 49 | restartPolicy: Never 50 | status: {} 51 | 52 | // create it 53 | kubectl create -f multi-container.yaml 54 | 55 | kubectl get po busybox 56 | ``` 57 |
58 |62 | 63 | ``` 64 | kubectl logs busybox -c busybox1 65 | kubectl logs busybox -c busybox2 66 | kubectl logs busybox -c busybox3 67 | ``` 68 |
69 |73 | 74 | ``` 75 | kubectl logs busybox -c busybox2 --previous 76 | ``` 77 |
78 |82 | 83 | ``` 84 | kubectl exec busybox -c busybox3 -- ls 85 | ``` 86 |
87 |91 | 92 | ``` 93 | kubectl top pod busybox --containers 94 | 95 | // putting them into file 96 | kubectl top pod busybox --containers > file.log 97 | cat file.log 98 | ``` 99 |
100 |105 | 106 | ``` 107 | // create an initial yaml file with this 108 | kubectl run multi-cont-pod --image=busbox --restart=Never --dry-run -o yaml > multi-container.yaml 109 | 110 | // edit the yml as below and create it 111 | apiVersion: v1 112 | kind: Pod 113 | metadata: 114 | creationTimestamp: null 115 | labels: 116 | run: multi-cont-pod 117 | name: multi-cont-pod 118 | spec: 119 | volumes: 120 | - name: var-logs 121 | emptyDir: {} 122 | containers: 123 | - image: busybox 124 | command: ["/bin/sh"] 125 | args: ["-c", "while true; do echo 'Hi I am from Main container' >> /var/log/index.html; sleep 5;done"] 126 | name: main-container 127 | resources: {} 128 | volumeMounts: 129 | - name: var-logs 130 | mountPath: /var/log 131 | - image: nginx 132 | name: sidecar-container 133 | resources: {} 134 | ports: 135 | - containerPort: 80 136 | volumeMounts: 137 | - name: var-logs 138 | mountPath: /usr/share/nginx/html 139 | dnsPolicy: ClusterFirst 140 | restartPolicy: Never 141 | status: {} 142 | 143 | kubectl create -f multi-container.yaml 144 | 145 | kubectl get po multi-cont-pod 146 | ``` 147 |
148 |153 | 154 | ``` 155 | // exec into main container 156 | kubectl exec -it multi-cont-pod -c main-container -- sh 157 | cat /var/log/main.txt 158 | 159 | // exec into sidecar container 160 | kubectl exec -it multi-cont-pod -c sidecar-container -- sh 161 | cat /usr/share/nginx/html/index.html 162 | 163 | // install curl and get default page 164 | kubectl exec -it multi-cont-pod -c sidecar-container -- sh 165 | # apt-get update && apt-get install -y curl 166 | # curl localhost 167 | ``` 168 |
169 |12 | 13 | ``` 14 | kubectl get namespaces 15 | 16 | kubectl get ns 17 | ``` 18 |
19 |23 | 24 | ``` 25 | kubectl get po --all-namespaces 26 | ``` 27 |
28 |
32 |
33 | ```
34 | kubectl get po -n
41 |
42 | ```
43 | kubectl get svc -n
50 | 51 | ``` 52 | kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}" 53 | ``` 54 |
55 |59 | 60 | ``` 61 | // creating a pod 62 | kubectl run nginx --image=nginx --restart=Never 63 | 64 | // List the pod 65 | kubectl get po 66 | ``` 67 |
68 |73 | 74 | ``` 75 | // get the yaml file with --dry-run flag 76 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yaml 77 | 78 | // cat nginx-pod.yaml 79 | apiVersion: v1 80 | kind: Pod 81 | metadata: 82 | creationTimestamp: null 83 | labels: 84 | run: nginx 85 | name: nginx 86 | spec: 87 | containers: 88 | - image: nginx 89 | name: nginx 90 | resources: {} 91 | dnsPolicy: ClusterFirst 92 | restartPolicy: Never 93 | status: {} 94 | 95 | // create a pod 96 | kubectl create -f nginx-pod.yaml 97 | ``` 98 |
99 |104 | 105 | ``` 106 | kubectl get po nginx -o yaml 107 | ``` 108 |
109 |114 | 115 | ``` 116 | kubectl get po nginx -o yaml --export 117 | ``` 118 |
119 |124 | 125 | ``` 126 | kubectl describe pod nginx 127 | ``` 128 |
129 |134 | 135 | ``` 136 | kubectl delete po nginx 137 | 138 | kubectl delete -f nginx-pod.yaml 139 | ``` 140 |
141 |146 | 147 | ``` 148 | kubectl delete po nginx --grace-period=0 --force 149 | ``` 150 |
151 |156 | 157 | ``` 158 | kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80 159 | ``` 160 |
161 |166 | 167 | ``` 168 | kubectl set image pod/nginx nginx=nginx:1.15-alpine 169 | 170 | kubectl describe po nginx 171 | 172 | // another way it will open vi editor and change the version 173 | kubeclt edit po nginx 174 | 175 | kubectl describe po nginx 176 | ``` 177 |
178 |183 | 184 | ``` 185 | kubectl set image pod/nginx nginx=nginx:1.17.1 186 | 187 | kubectl describe po nginx 188 | 189 | kubectl get po nginx -w # watch it 190 | ``` 191 |
192 |197 | 198 | ``` 199 | kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}' 200 | ``` 201 |
202 |207 | 208 | ``` 209 | // creating a pod 210 | kubectl run nginx --image=nginx --restart=Never 211 | 212 | // exec into the pod 213 | kubectl exec -it nginx /bin/sh 214 | ``` 215 |
216 |221 | 222 | ``` 223 | kubectl get po nginx -o wide 224 | ``` 225 |
226 |230 | 231 | ``` 232 | kubectl run busybox --image=busybox --restart=Never -- ls 233 | 234 | kubectl logs busybox 235 | ``` 236 |
237 |242 | 243 | ``` 244 | kubectl logs busybox -p 245 | ``` 246 |
247 |252 | 253 | ``` 254 | kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600" 255 | ``` 256 |
257 |
262 |
263 | ```
264 | kubectl get po nginx -o wide
265 |
266 | // check the connection
267 | kubectl exec -it busybox -- wget -o-
275 | 276 | ``` 277 | kubectl run busybox --image=nginx --restart=Never -it -- echo "How are you" 278 | 279 | kubectl delete po busybox 280 | ``` 281 |
282 |287 | 288 | ``` 289 | // notice the --rm flag 290 | kubectl run busybox --image=nginx --restart=Never -it --rm -- echo "How are you" 291 | ``` 292 |
293 |298 | 299 | ``` 300 | // create a pod 301 | kubectl run nginx --image=nginx --restart=Never --port=80 302 | 303 | // List the pod with different verbosity 304 | kubectl get po nginx --v=7 305 | kubectl get po nginx --v=8 306 | kubectl get po nginx --v=9 307 | ``` 308 |
309 |314 | 315 | ``` 316 | kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state" 317 | ``` 318 |
319 |324 | 325 | ``` 326 | kubectl get pods --sort-by=.metadata.name 327 | ``` 328 |
329 |334 | 335 | ``` 336 | kubectl get pods--sort-by=.metadata.creationTimestamp 337 | ``` 338 |
339 |14 | 15 | ``` 16 | kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml 17 | 18 | // add the readinessProbe section and create 19 | apiVersion: v1 20 | kind: Pod 21 | metadata: 22 | creationTimestamp: null 23 | labels: 24 | run: nginx 25 | name: nginx 26 | spec: 27 | containers: 28 | - image: nginx 29 | name: nginx 30 | ports: 31 | - containerPort: 80 32 | readinessProbe: 33 | httpGet: 34 | path: / 35 | port: 80 36 | resources: {} 37 | dnsPolicy: ClusterFirst 38 | restartPolicy: Never 39 | status: {} 40 | 41 | kubectl create -f nginx-pod.yaml 42 | 43 | // verify 44 | kubectl describe pod nginx | grep -i readiness 45 | kubectl delete po nginx 46 | ``` 47 |
48 |53 | 54 | ``` 55 | kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml 56 | 57 | // add the livenessProbe section and create 58 | apiVersion: v1 59 | kind: Pod 60 | metadata: 61 | creationTimestamp: null 62 | labels: 63 | run: nginx 64 | name: nginx 65 | spec: 66 | containers: 67 | - image: nginx 68 | name: nginx 69 | ports: 70 | - containerPort: 80 71 | livenessProbe: 72 | httpGet: 73 | path: /healthz 74 | port: 80 75 | resources: {} 76 | dnsPolicy: ClusterFirst 77 | restartPolicy: Never 78 | status: {} 79 | 80 | kubectl create -f nginx-pod.yaml 81 | 82 | // verify 83 | kubectl describe pod nginx | grep -i readiness 84 | kubectl delete po nginx 85 | ``` 86 |
87 |92 | 93 | ``` 94 | kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml 95 | 96 | // add the livenessProbe and readiness section and create 97 | apiVersion: v1 98 | kind: Pod 99 | metadata: 100 | creationTimestamp: null 101 | labels: 102 | run: nginx 103 | name: nginx 104 | spec: 105 | containers: 106 | - image: nginx 107 | name: nginx 108 | ports: 109 | - containerPort: 80 110 | livenessProbe: 111 | httpGet: 112 | path: /healthz 113 | port: 80 114 | readinessProbe: 115 | httpGet: 116 | path: / 117 | port: 80 118 | resources: {} 119 | dnsPolicy: ClusterFirst 120 | restartPolicy: Never 121 | status: {} 122 | 123 | kubectl create -f nginx-pod.yaml 124 | 125 | // verify 126 | kubectl describe pod nginx | grep -i readiness 127 | kubectl describe pod nginx | grep -i liveness 128 | ``` 129 |
130 |135 | 136 | ``` 137 | kubectl explain Pod.spec.containers.livenessProbe 138 | kubectl explain Pod.spec.containers.readinessProbe 139 | ``` 140 |
141 |146 | 147 | ``` 148 | // nginx-pod.yaml 149 | 150 | apiVersion: v1 151 | kind: Pod 152 | metadata: 153 | creationTimestamp: null 154 | labels: 155 | run: nginx 156 | name: nginx 157 | spec: 158 | containers: 159 | - image: nginx 160 | name: nginx 161 | ports: 162 | - containerPort: 80 163 | livenessProbe: 164 | initialDelaySeconds: 20 165 | periodSeconds: 25 166 | httpGet: 167 | path: /healthz 168 | port: 80 169 | readinessProbe: 170 | initialDelaySeconds: 20 171 | periodSeconds: 25 172 | httpGet: 173 | path: / 174 | port: 80 175 | resources: {} 176 | dnsPolicy: ClusterFirst 177 | restartPolicy: Never 178 | status: {} 179 | 180 | kubectl create -f nginx-pod.yaml 181 | ``` 182 |
183 |188 | 189 | ``` 190 | kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "echo I am from busybox pod; sleep 3600;" 191 | 192 | kubectl logs busybox 193 | ``` 194 |
195 |200 | 201 | ``` 202 | kubectl logs busybox > busybox-logs.txt 203 | 204 | cat busybox-logs.txt 205 | ``` 206 |
207 |212 | 213 | ``` 214 | kubectl get events --sort-by=.metadata.creationTimestamp 215 | 216 | // putting them into file.log 217 | kubectl get events --sort-by=.metadata.creationTimestamp > file.log 218 | 219 | cat file.log 220 | ``` 221 |
222 |227 | 228 | ``` 229 | // create the pod 230 | kubectl run hello --image=alpine --restart=Never -- /bin/sh -c "while true; do echo 'Hi I am from Alpine'; sleep 5;done" 231 | 232 | // verify and follow the logs 233 | kubectl logs --follow hello 234 | ``` 235 |
236 |241 | 242 | ``` 243 | // create the pod 244 | kubectl create -f https://gist.githubusercontent.com/bbachi/212168375b39e36e2e2984c097167b00/raw/1fd63509c3ae3a3d3da844640fb4cca744543c1c/not-running.yml 245 | 246 | // get the pod 247 | kubectl get pod not-running 248 | kubectl describe po not-running 249 | 250 | // it clearly says ImagePullBackOff something wrong with image 251 | kubectl edit pod not-running // it will open vim editor 252 | or 253 | kubectl set image pod/not-running not-running=nginx 254 | ``` 255 |
256 |261 | 262 | ``` 263 | kubectl create -f https://gist.githubusercontent.com/bbachi/1f001f10337234d46806929d12245397/raw/84b7295fb077f15de979fec5b3f7a13fc69c6d83/problem-pod.yaml 264 | 265 | // get all the pods in all namespaces 266 | kubectl get po --all-namespaces 267 | 268 | // find out which pod is not running 269 | kubectl get po -n namespace2 270 | 271 | // update the image 272 | kubectl set image pod/pod2 pod2=nginx -n namespace2 273 | 274 | // verify again 275 | kubectl get po -n namespace2 276 | ``` 277 |
278 |283 | 284 | ``` 285 | // get the top 3 hungry pods 286 | kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3 287 | 288 | // putting into file 289 | kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3 > cpu-usage.txt 290 | 291 | // verify 292 | cat cpu-usage.txt 293 | ``` 294 |
295 |17 | 18 | ``` 19 | kubectl get cm 20 | or 21 | kubectl get configmap 22 | ``` 23 |
24 |29 | 30 | ``` 31 | kubectl create cm myconfigmap --from-literal=appname=myapp 32 | ``` 33 |
34 |39 | 40 | ``` 41 | // you will see under data 42 | kubectl get cm -o yaml 43 | or 44 | kubectl describe cm 45 | ``` 46 |
47 |52 | 53 | ``` 54 | kubectl delete cm myconfigmap 55 | ``` 56 |
57 |62 | 63 | ``` 64 | cat >> config.txt << EOF 65 | key1=value1 66 | key2=value2 67 | EOF 68 | 69 | cat config.txt 70 | ``` 71 |
72 |77 | 78 | ``` 79 | kubectl create cm keyvalcfgmap --from-file=config.txt 80 | 81 | kubectl get cm keyvalcfgmap -o yaml 82 | ``` 83 |
84 |90 | 91 | ``` 92 | // first run this command to save the pod yml 93 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml 94 | 95 | // edit the yml to below file and create 96 | apiVersion: v1 97 | kind: Pod 98 | metadata: 99 | creationTimestamp: null 100 | labels: 101 | run: nginx 102 | name: nginx 103 | spec: 104 | containers: 105 | - image: nginx 106 | name: nginx 107 | resources: {} 108 | envFrom: 109 | - configMapRef: 110 | name: keyvalcfgmap 111 | dnsPolicy: ClusterFirst 112 | restartPolicy: Never 113 | status: {} 114 | 115 | kubectl create -f nginx-pod.yml 116 | 117 | // verify 118 | kubectl exec -it nginx -- env 119 | kubectl delete po nginx 120 | ``` 121 |
122 |127 | 128 | ``` 129 | echo var1=val1 > file.env 130 | cat file.env 131 | 132 | kubectl create cm envcfgmap --from-env-file=file.env 133 | kubectl get cm envcfgmap -o yaml --export 134 | ``` 135 |
136 |141 | 142 | ``` 143 | // first run this command to save the pod yml 144 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml 145 | 146 | // edit the yml to below file and create 147 | apiVersion: v1 148 | kind: Pod 149 | metadata: 150 | creationTimestamp: null 151 | labels: 152 | run: nginx 153 | name: nginx 154 | spec: 155 | containers: 156 | - image: nginx 157 | name: nginx 158 | resources: {} 159 | env: 160 | - name: ENVIRONMENT 161 | valueFrom: 162 | configMapKeyRef: 163 | name: envcfgmap 164 | key: environment 165 | dnsPolicy: ClusterFirst 166 | restartPolicy: Never 167 | status: {} 168 | 169 | kubectl create -f nginx-pod.yml 170 | 171 | // verify 172 | kubectl exec -it nginx -- env 173 | kubectl delete po nginx 174 | ``` 175 |
176 |181 | 182 | ``` 183 | // first create a configmap cfgvolume 184 | kubectl create cm cfgvolume --from-literal=var1=val1 --from-literal=var2=val2 185 | 186 | // verify the configmap 187 | kubectl describe cm cfgvolume 188 | 189 | // create the config map 190 | apiVersion: v1 191 | kind: Pod 192 | metadata: 193 | creationTimestamp: null 194 | labels: 195 | run: nginx 196 | name: nginx 197 | spec: 198 | volumes: 199 | - name: nginx-volume 200 | configMap: 201 | name: cfgvolume 202 | containers: 203 | - image: nginx 204 | name: nginx 205 | resources: {} 206 | volumeMounts: 207 | - name: nginx-volume 208 | mountPath: /etc/cfg 209 | dnsPolicy: ClusterFirst 210 | restartPolicy: Never 211 | status: {} 212 | 213 | kubectl create -f nginx-volume.yml 214 | 215 | // exec into the pod 216 | kubectl exec -it nginx -- /bin/sh 217 | 218 | // check the path 219 | cd /etc/cfg 220 | ls 221 | ``` 222 |
223 |228 | 229 | ``` 230 | // create yml file with dry-run 231 | kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml 232 | 233 | // edit the pod like below and create 234 | apiVersion: v1 235 | kind: Pod 236 | metadata: 237 | creationTimestamp: null 238 | labels: 239 | run: secbusybox 240 | name: secbusybox 241 | spec: 242 | securityContext: # add security context 243 | runAsUser: 1000 244 | runAsGroup: 2000 245 | containers: 246 | - args: 247 | - /bin/sh 248 | - -c 249 | - sleep 3600; 250 | image: busybox 251 | name: secbusybox 252 | resources: {} 253 | dnsPolicy: ClusterFirst 254 | restartPolicy: Never 255 | status: {} 256 | 257 | kubectl create -f busybox.yml 258 | 259 | // verify 260 | kubectl exec -it secbusybox -- sh 261 | id // it will show the id and group 262 | ``` 263 |
264 |269 | 270 | ``` 271 | // create yml file with dry-run 272 | kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml 273 | 274 | // edit the pod like below and create 275 | apiVersion: v1 276 | kind: Pod 277 | metadata: 278 | creationTimestamp: null 279 | labels: 280 | run: secbusybox 281 | name: secbusybox 282 | spec: 283 | securityContext: 284 | runAsUser: 1000 285 | containers: 286 | - args: 287 | - /bin/sh 288 | - -c 289 | - sleep 3600; 290 | image: busybox 291 | securityContext: 292 | runAsUser: 2000 293 | name: secbusybox 294 | resources: {} 295 | dnsPolicy: ClusterFirst 296 | restartPolicy: Never 297 | status: {} 298 | 299 | kubectl create -f busybox.yml 300 | 301 | // verify 302 | kubectl exec -it secbusybox -- sh 303 | id // you can see container securityContext overides the Pod level 304 | ``` 305 |
306 |311 | 312 | ``` 313 | // create the yaml file 314 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml 315 | 316 | // edit as below and create pod 317 | apiVersion: v1 318 | kind: Pod 319 | metadata: 320 | creationTimestamp: null 321 | labels: 322 | run: nginx 323 | name: nginx 324 | spec: 325 | containers: 326 | - image: nginx 327 | securityContext: 328 | capabilities: 329 | add: ["SYS_TIME", "NET_ADMIN"] 330 | name: nginx 331 | resources: {} 332 | dnsPolicy: ClusterFirst 333 | restartPolicy: Never 334 | status: {} 335 | 336 | kubectl create -f nginx.yml 337 | 338 | // exec and verify 339 | kubectl exec -it nginx -- sh 340 | cd /proc/1 341 | cat status 342 | 343 | // you should see these values 344 | CapPrm: 00000000aa0435fb 345 | CapEff: 00000000aa0435fb 346 | ``` 347 |
348 |353 | 354 | ``` 355 | // create a yml file 356 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml 357 | 358 | // add the resources section and create 359 | apiVersion: v1 360 | kind: Pod 361 | metadata: 362 | creationTimestamp: null 363 | labels: 364 | run: nginx 365 | name: nginx 366 | spec: 367 | containers: 368 | - image: nginx 369 | name: nginx 370 | resources: 371 | requests: 372 | memory: "100Mi" 373 | limits: 374 | memory: "200Mi" 375 | dnsPolicy: ClusterFirst 376 | restartPolicy: Never 377 | status: {} 378 | 379 | kubectl create -f nginx.yml 380 | 381 | // verify 382 | kubectl top pod 383 | ``` 384 |
385 |390 | 391 | ``` 392 | // create a yml file 393 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml 394 | 395 | // add the resources section and create 396 | apiVersion: v1 397 | kind: Pod 398 | metadata: 399 | creationTimestamp: null 400 | labels: 401 | run: nginx 402 | name: nginx 403 | spec: 404 | containers: 405 | - image: nginx 406 | name: nginx 407 | resources: 408 | requests: 409 | cpu: "0.5" 410 | limits: 411 | cpu: "1" 412 | dnsPolicy: ClusterFirst 413 | restartPolicy: Never 414 | status: {} 415 | 416 | kubectl create -f nginx.yml 417 | 418 | // verify 419 | kubectl top pod 420 | ``` 421 |
422 |427 | 428 | ``` 429 | // create a yml file 430 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml 431 | 432 | // add the resources section and create 433 | apiVersion: v1 434 | kind: Pod 435 | metadata: 436 | creationTimestamp: null 437 | labels: 438 | run: nginx 439 | name: nginx 440 | spec: 441 | containers: 442 | - image: nginx 443 | name: nginx 444 | resources: 445 | requests: 446 | memory: "100Mi" 447 | cpu: "0.5" 448 | limits: 449 | memory: "200Mi" 450 | cpu: "1" 451 | dnsPolicy: ClusterFirst 452 | restartPolicy: Never 453 | status: {} 454 | 455 | kubectl create -f nginx.yml 456 | 457 | // verify 458 | kubectl top pod 459 | ``` 460 |
461 |466 | 467 | ``` 468 | // create a yml file 469 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml 470 | 471 | // add the resources section and create 472 | apiVersion: v1 473 | kind: Pod 474 | metadata: 475 | creationTimestamp: null 476 | labels: 477 | run: nginx 478 | name: nginx 479 | spec: 480 | containers: 481 | - image: nginx 482 | name: nginx 483 | resources: 484 | requests: 485 | memory: "100Gi" 486 | cpu: "0.5" 487 | limits: 488 | memory: "200Gi" 489 | cpu: "1" 490 | dnsPolicy: ClusterFirst 491 | restartPolicy: Never 492 | status: {} 493 | 494 | kubectl create -f nginx.yml 495 | 496 | // verify 497 | kubectl describe po nginx // you can see pending state 498 | ``` 499 |
500 |505 | 506 | ``` 507 | kubectl create secret generic my-secret --from-literal=username=user --from-literal=password=mypassword 508 | ``` 509 |
510 |515 | 516 | ``` 517 | kubectl get secret --all-namespaces 518 | ``` 519 |
520 |525 | 526 | ``` 527 | kubectl get secret my-secret -o yaml 528 | ``` 529 |
530 |535 | 536 | ``` 537 | // create a yml file 538 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml 539 | 540 | // add env section below and create 541 | apiVersion: v1 542 | kind: Pod 543 | metadata: 544 | creationTimestamp: null 545 | labels: 546 | run: nginx 547 | name: nginx 548 | spec: 549 | containers: 550 | - image: nginx 551 | name: nginx 552 | env: 553 | - name: USER_NAME 554 | valueFrom: 555 | secretKeyRef: 556 | name: my-secret 557 | key: username 558 | resources: {} 559 | dnsPolicy: ClusterFirst 560 | restartPolicy: Never 561 | status: {} 562 | 563 | kubectl create -f nginx.yml 564 | 565 | //verify 566 | kubectl exec -it nginx -- env 567 | ``` 568 |
569 |574 | 575 | ``` 576 | // create a yml file 577 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml 578 | 579 | // add env section below and create 580 | apiVersion: v1 581 | kind: Pod 582 | metadata: 583 | creationTimestamp: null 584 | labels: 585 | run: nginx 586 | name: nginx 587 | spec: 588 | containers: 589 | - image: nginx 590 | name: nginx 591 | envFrom: 592 | - secretRef: 593 | name: my-secret 594 | resources: {} 595 | dnsPolicy: ClusterFirst 596 | restartPolicy: Never 597 | status: {} 598 | 599 | kubectl create -f nginx.yml 600 | 601 | //verify 602 | kubectl exec -it nginx -- env 603 | ``` 604 |
605 |610 | 611 | ``` 612 | kubectl get sa 613 | ``` 614 |
615 |620 | 621 | ``` 622 | kubectl get sa --all-namespaces 623 | ``` 624 |
625 |630 | 631 | ``` 632 | kubectl create sa admin 633 | ``` 634 |
635 |640 | 641 | ``` 642 | kubectl get sa admin -o yaml 643 | ``` 644 |
645 |650 | 651 | ``` 652 | kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600" > busybox.yml 653 | 654 | kubectl create -f busybox.yml 655 | 656 | apiVersion: v1 657 | kind: Pod 658 | metadata: 659 | creationTimestamp: null 660 | labels: 661 | run: busybox 662 | name: busybox 663 | spec: 664 | serviceAccountName: admin 665 | containers: 666 | - args: 667 | - /bin/sh 668 | - -c 669 | - sleep 3600 670 | image: busybox 671 | name: busybox 672 | resources: {} 673 | dnsPolicy: ClusterFirst 674 | restartPolicy: Never 675 | status: {} 676 | 677 | // verify 678 | kubectl describe po busybox 679 | ``` 680 |
681 |15 | 16 | ``` 17 | kubectl get pods --show-labels 18 | ``` 19 |
20 |25 | 26 | ``` 27 | kubectl run nginx-dev1 --image=nginx --restart=Never --labels=env=dev 28 | kubectl run nginx-dev2 --image=nginx --restart=Never --labels=env=dev 29 | kubectl run nginx-dev3 --image=nginx --restart=Never --labels=env=dev 30 | kubectl run nginx-prod1 --image=nginx --restart=Never --labels=env=prod 31 | kubectl run nginx-prod2 --image=nginx --restart=Never --labels=env=prod 32 | ``` 33 |
34 |39 | 40 | ``` 41 | kubeclt get pods --show-labels 42 | ``` 43 |
44 |48 | 49 | ``` 50 | kubectl get pods -l env=dev 51 | ``` 52 |
53 |57 | 58 | ``` 59 | kubectl get pods -l env=dev --show-labels 60 | ``` 61 |
62 |66 | 67 | ``` 68 | kubectl get pods -l env=prod 69 | ``` 70 |
71 |75 | 76 | ``` 77 | kubectl get pods -l env=prod --show-labels 78 | ``` 79 |
80 |85 | 86 | ``` 87 | kubectl get pods -L env 88 | ``` 89 |
90 |95 | 96 | ``` 97 | kubectl get pods -l 'env in (dev,prod)' 98 | ``` 99 |
100 |105 | 106 | ``` 107 | kubectl get pods -l 'env in (dev,prod)' --show-labels 108 | ``` 109 |
110 |115 | 116 | ``` 117 | kubectl label pod/nginx-dev3 env=uat --overwrite 118 | 119 | kubectl get pods --show-labels 120 | ``` 121 |
122 |127 | 128 | ``` 129 | kubectl label pod nginx-dev{1..3} env- 130 | kubectl label pod nginx-prod{1..2} env- 131 | 132 | kubectl get po --show-labels 133 | ``` 134 |
135 |140 | 141 | ``` 142 | kubectl label pod nginx-dev{1..3} app=nginx 143 | kubectl label pod nginx-prod{1..2} app=nginx 144 | 145 | kubectl get po --show-labels 146 | ``` 147 |
148 |153 | 154 | ``` 155 | kubectl get nodes --show-labels 156 | ``` 157 |
158 |163 | 164 | ``` 165 | kubectl label node minikube nodeName=nginxnode 166 | ``` 167 |
168 |173 | 174 | ``` 175 | kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml 176 | 177 | // add the nodeSelector like below and create the pod 178 | 179 | apiVersion: v1 180 | kind: Pod 181 | metadata: 182 | creationTimestamp: null 183 | labels: 184 | run: nginx 185 | name: nginx 186 | spec: 187 | nodeSelector: 188 | nodeName: nginxnode 189 | containers: 190 | - image: nginx 191 | name: nginx 192 | resources: {} 193 | dnsPolicy: ClusterFirst 194 | restartPolicy: Never 195 | status: {} 196 | 197 | kubectl create -f pod.yaml 198 | ``` 199 |
200 |205 | 206 | ``` 207 | kubectl describe po nginx | grep Node-Selectors 208 | ``` 209 |
210 |215 | 216 | ``` 217 | kubectl describe po nginx | grep Labels 218 | ``` 219 |
220 |225 | 226 | ``` 227 | kubectl annotate pod nginx-dev{1..3} name=webapp 228 | kubectl annotate pod nginx-prod{1..2} name=webapp 229 | ``` 230 |
231 |236 | 237 | ``` 238 | kubectl describe po nginx-dev{1..3} | grep -i annotations 239 | kubectl describe po nginx-prod{1..2} | grep -i annotations 240 | ``` 241 |
242 |248 | 249 | ``` 250 | kubectl annotate pod nginx-dev{1..3} name- 251 | kubectl annotate pod nginx-prod{1..2} name- 252 | 253 | kubectl describe po nginx-dev{1..3} | grep -i annotations 254 | kubectl describe po nginx-prod{1..2} | grep -i annotations 255 | ``` 256 |
257 |262 | 263 | ``` 264 | kubectl delete po --all 265 | ``` 266 |
267 |272 | 273 | ``` 274 | kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml 275 | 276 | // change the replicas to 5 in the yaml and create it 277 | 278 | apiVersion: apps/v1 279 | kind: Deployment 280 | metadata: 281 | creationTimestamp: null 282 | labels: 283 | app: webapp 284 | name: webapp 285 | spec: 286 | replicas: 5 287 | selector: 288 | matchLabels: 289 | app: webapp 290 | strategy: {} 291 | template: 292 | metadata: 293 | creationTimestamp: null 294 | labels: 295 | app: webapp 296 | spec: 297 | containers: 298 | - image: nginx 299 | name: nginx 300 | resources: {} 301 | status: {} 302 | 303 | kubectl create -f webapp.yaml 304 | ``` 305 |
306 |311 | 312 | ``` 313 | kubectl get deploy webapp --show-labels 314 | ``` 315 |
316 |321 | 322 | ``` 323 | kubectl get deploy webapp -o yaml 324 | ``` 325 |
326 |331 | 332 | ``` 333 | // get the label of the deployment 334 | kubectl get deploy --show-labels 335 | 336 | // get the pods with that label 337 | kubectl get pods -l app=webapp 338 | ``` 339 |
340 |345 | 346 | ``` 347 | kubectl scale deploy webapp --replicas=20 348 | 349 | kubectl get po -l app=webapp 350 | ``` 351 |
352 |357 | 358 | ``` 359 | kubectl rollout status deploy webapp 360 | ``` 361 |
362 |367 | 368 | ``` 369 | kubectl get rs -l app=webapp 370 | ``` 371 |
372 |377 | 378 | ``` 379 | kubectl get rs -l app=webapp -o yaml 380 | 381 | kubectl get po -l app=webapp -o yaml 382 | ``` 383 |
384 |389 | 390 | ``` 391 | kubectl delete deploy webapp 392 | 393 | kubectl get po -l app=webapp -w 394 | ``` 395 |
396 |401 | 402 | ``` 403 | kubectl create deploy webapp --image=nginx:1.17.1 --dry-run -o yaml > webapp.yaml 404 | 405 | // add the port section and create the deployment 406 | 407 | apiVersion: apps/v1 408 | kind: Deployment 409 | metadata: 410 | creationTimestamp: null 411 | labels: 412 | app: webapp 413 | name: webapp 414 | spec: 415 | replicas: 1 416 | selector: 417 | matchLabels: 418 | app: webapp 419 | strategy: {} 420 | template: 421 | metadata: 422 | creationTimestamp: null 423 | labels: 424 | app: webapp 425 | spec: 426 | containers: 427 | - image: nginx:1.17.1 428 | name: nginx 429 | ports: 430 | - containerPort: 80 431 | resources: {} 432 | status: {} 433 | 434 | kubectl create -f webapp.yaml 435 | 436 | // verify 437 | kubectl describe deploy webapp | grep Image 438 | ``` 439 |
440 |445 | 446 | ``` 447 | kubectl set image deploy/webapp nginx=nginx:1.17.4 448 | 449 | kubectl describe deploy webapp | grep Image 450 | ``` 451 |
452 |457 | 458 | ``` 459 | kubectl rollout history deploy webapp 460 | 461 | kubectl get deploy webapp --show-labels 462 | kubectl get rs -l app=webapp 463 | kubectl get po -l app=webapp 464 | ``` 465 |
466 |471 | 472 | ``` 473 | kubectl rollout undo deploy webapp 474 | 475 | kubectl describe deploy webapp | grep Image 476 | ``` 477 |
478 |484 | 485 | ``` 486 | kubectl set image deploy/webapp nginx=nginx:1.16.1 487 | 488 | kubectl describe deploy webapp | grep Image 489 | 490 | kubectl rollout history deploy webapp 491 | ``` 492 |
493 |499 | 500 | ``` 501 | kubectl rollout undo deploy webapp --to-revision=3 502 | 503 | kubectl describe deploy webapp | grep Image 504 | 505 | kubectl rollout status deploy webapp 506 | ``` 507 |
508 |513 | 514 | ``` 515 | kubectl set image deploy/webapp nginx=nginx:1.100 516 | 517 | kubectl rollout status deploy webapp (still pending state) 518 | 519 | kubectl get pods (ImagePullErr) 520 | ``` 521 |
522 |527 | 528 | ``` 529 | kubectl rollout undo deploy webapp 530 | kubectl rollout status deploy webapp 531 | 532 | kubectl get pods 533 | ``` 534 |
535 |540 | 541 | ``` 542 | kubectl rollout history deploy webapp --revision=7 543 | ``` 544 |
545 |551 | 552 | ``` 553 | kubectl rollout pause deploy webapp 554 | ``` 555 |
556 |561 | 562 | ``` 563 | kubectl set image deploy/webapp nginx=nginx:latest 564 | 565 | kubectl rollout history deploy webapp (No new revision) 566 | ``` 567 |
568 |574 | 575 | ``` 576 | kubectl rollout resume deploy webapp 577 | ``` 578 |
579 |585 | 586 | ``` 587 | kubectl rollout history deploy webapp 588 | 589 | kubectl rollout history deploy webapp --revision=9 590 | ``` 591 |
592 |598 | 599 | ``` 600 | kubectl autoscale deploy webapp --min=10 --max=20 --cpu-percent=85 601 | 602 | kubectl get hpa 603 | 604 | kubectl get pod -l app=webapp 605 | ``` 606 |
607 |612 | 613 | ``` 614 | kubectl delete deploy webapp 615 | 616 | kubectl delete hpa webapp 617 | ``` 618 |
619 |624 | 625 | ``` 626 | kubectl create job nodeversion --image=node -- node -v 627 | 628 | kubectl get job -w 629 | kubectl get pod 630 | ``` 631 |
632 |
638 |
639 | ```
640 | kubectl logs
649 | 650 | ``` 651 | kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" 652 | ``` 653 |
654 |659 | 660 | ``` 661 | kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml 662 | 663 | kubectl create -f hello-job.yaml 664 | ``` 665 |
666 |671 | 672 | ``` 673 | kubectl get job 674 | kubectl get po 675 | 676 | kubectl logs hello-job-* 677 | ``` 678 |
679 |685 | 686 | ``` 687 | kubectl delete job hello-job 688 | ``` 689 |
690 |695 | 696 | ``` 697 | kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml 698 | 699 | // edit the yaml file to add completions: 10 700 | 701 | apiVersion: batch/v1 702 | kind: Job 703 | metadata: 704 | creationTimestamp: null 705 | name: hello-job 706 | spec: 707 | completions: 10 708 | template: 709 | metadata: 710 | creationTimestamp: null 711 | spec: 712 | containers: 713 | - command: 714 | - echo 715 | - Hello I am from job 716 | image: busybox 717 | name: hello-job 718 | resources: {} 719 | restartPolicy: Never 720 | status: {} 721 | 722 | kubectl create -f hello-job.yaml 723 | ``` 724 |
725 |730 | 731 | ``` 732 | kubectl get job -w 733 | kubectl get po 734 | 735 | kubectl delete job hello-job 736 | ``` 737 |
738 |744 | 745 | ``` 746 | kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml 747 | 748 | // edit the yaml file to add parallelism: 10 749 | 750 | apiVersion: batch/v1 751 | kind: Job 752 | metadata: 753 | creationTimestamp: null 754 | name: hello-job 755 | spec: 756 | parallelism: 10 757 | template: 758 | metadata: 759 | creationTimestamp: null 760 | spec: 761 | containers: 762 | - command: 763 | - echo 764 | - Hello I am from job 765 | image: busybox 766 | name: hello-job 767 | resources: {} 768 | restartPolicy: Never 769 | status: {} 770 | 771 | kubectl create -f hello-job.yaml 772 | ``` 773 |
774 |780 | 781 | ``` 782 | kubectl get job -w 783 | kubectl get po 784 | 785 | kubectl delete job hello-job 786 | ``` 787 |
788 |794 | 795 | ``` 796 | kubectl create cronjob date-job --image=busybox --schedule="*/1 * * * *" -- bin/sh -c "date; echo Hello from kubernetes cluster" 797 | ``` 798 |
799 |805 | 806 | ``` 807 | kubectl get cj date-job -o yaml 808 | ``` 809 |
810 |
816 |
817 | ```
818 | kubectl get job
819 | kubectl get po
820 |
821 | kubectl logs date-job-
830 | 831 | ``` 832 | kubectl delete cj date-job 833 | 834 | // verify pods and jobs 835 | kubectl get po 836 | kubectl get job 837 | ``` 838 |
839 |