- -``` -kubectl get cm - or -kubectl get configmap -``` -
-- -``` -kubectl create cm myconfigmap --from-literal=appname=myapp -``` -
-
-
-```
-// you will see under data
-kubectl get cm -o yaml
+1.
+
+ ```
+ kubectl get cm
or
-kubectl describe cm
-```
-
-
-```
-kubectl delete cm myconfigmap
-```
-
-
-```
-cat >> config.txt << EOF
-key1=value1
-key2=value2
-EOF
-
-cat config.txt
-```
-
-
-```
-kubectl create cm keyvalcfgmap --from-file=config.txt
-
-kubectl get cm keyvalcfgmap -o yaml
-```
-
-
-```
-// first run this command to save the pod yml
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml
-
-// edit the yml to below file and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- resources: {}
- envFrom:
- - configMapRef:
- name: keyvalcfgmap
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx-pod.yml
-
-// verify
-kubectl exec -it nginx -- env
-kubectl delete po nginx
-```
-
-
-```
-echo var1=val1 > file.env
-cat file.env
-
-kubectl create cm envcfgmap --from-env-file=file.env
-kubectl get cm envcfgmap -o yaml --export
-```
-
-
-```
-// first run this command to save the pod yml
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml
-
-// edit the yml to below file and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- resources: {}
- env:
- - name: ENVIRONMENT
- valueFrom:
- configMapKeyRef:
- name: envcfgmap
- key: environment
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx-pod.yml
-
-// verify
-kubectl exec -it nginx -- env
-kubectl delete po nginx
-```
-
-
-```
-// first create a configmap cfgvolume
-kubectl create cm cfgvolume --from-literal=var1=val1 --from-literal=var2=val2
-
-// verify the configmap
-kubectl describe cm cfgvolume
-
-// create the config map
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- volumes:
- - name: nginx-volume
- configMap:
- name: cfgvolume
- containers:
- - image: nginx
- name: nginx
- resources: {}
- volumeMounts:
- - name: nginx-volume
- mountPath: /etc/cfg
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx-volume.yml
-
-// exec into the pod
-kubectl exec -it nginx -- /bin/sh
-
-// check the path
-cd /etc/cfg
-ls
-```
-
-
-```
-// create yml file with dry-run
-kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml
-
-// edit the pod like below and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: secbusybox
- name: secbusybox
-spec:
- securityContext: # add security context
- runAsUser: 1000
- runAsGroup: 2000
- containers:
- - args:
- - /bin/sh
- - -c
- - sleep 3600;
- image: busybox
- name: secbusybox
- resources: {}
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f busybox.yml
-
-// verify
-kubectl exec -it secbusybox -- sh
-id // it will show the id and group
-```
-
-
-```
-// create yml file with dry-run
-kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml
-
-// edit the pod like below and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: secbusybox
- name: secbusybox
-spec:
- securityContext:
- runAsUser: 1000
- containers:
- - args:
- - /bin/sh
- - -c
- - sleep 3600;
- image: busybox
- securityContext:
- runAsUser: 2000
- name: secbusybox
- resources: {}
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f busybox.yml
-
-// verify
-kubectl exec -it secbusybox -- sh
-id // you can see container securityContext overides the Pod level
-```
-
-
-```
-// create the yaml file
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml
-
-// edit as below and create pod
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- securityContext:
- capabilities:
- add: ["SYS_TIME", "NET_ADMIN"]
- name: nginx
- resources: {}
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx.yml
-
-// exec and verify
-kubectl exec -it nginx -- sh
-cd /proc/1
-cat status
-
-// you should see these values
-CapPrm: 00000000aa0435fb
-CapEff: 00000000aa0435fb
-```
-
-
-```
-// create a yml file
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml
-
-// add the resources section and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- resources:
- requests:
- memory: "100Mi"
- limits:
- memory: "200Mi"
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx.yml
-
-// verify
-kubectl top pod
-```
-
-
-```
-// create a yml file
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml
-
-// add the resources section and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- resources:
- requests:
- cpu: "0.5"
- limits:
- cpu: "1"
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx.yml
-
-// verify
-kubectl top pod
-```
-
-
-```
-// create a yml file
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml
-
-// add the resources section and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- resources:
- requests:
- memory: "100Mi"
- cpu: "0.5"
- limits:
- memory: "200Mi"
- cpu: "1"
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx.yml
-
-// verify
-kubectl top pod
-```
-
-
-```
-// create a yml file
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml
-
-// add the resources section and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- resources:
- requests:
- memory: "100Gi"
- cpu: "0.5"
- limits:
- memory: "200Gi"
- cpu: "1"
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx.yml
-
-// verify
-kubectl describe po nginx // you can see pending state
-```
-
-
-```
-kubectl create secret generic my-secret --from-literal=username=user --from-literal=password=mypassword
-```
-
-
-```
-kubectl get secret --all-namespaces
-```
-
-
-```
-kubectl get secret my-secret -o yaml
-```
-
-
-```
-// create a yml file
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml
-
-// add env section below and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- env:
- - name: USER_NAME
- valueFrom:
- secretKeyRef:
- name: my-secret
- key: username
- resources: {}
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx.yml
-
-//verify
-kubectl exec -it nginx -- env
-```
-
-
-```
-// create a yml file
-kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml
-
-// add env section below and create
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: nginx
- name: nginx
-spec:
- containers:
- - image: nginx
- name: nginx
- envFrom:
- - secretRef:
- name: my-secret
- resources: {}
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-kubectl create -f nginx.yml
-
-//verify
-kubectl exec -it nginx -- env
-```
-
-
-```
-kubectl get sa
-```
-
-
-```
-kubectl get sa --all-namespaces
-```
-
-
-```
-kubectl create sa admin
-```
-
-
-```
-kubectl get sa admin -o yaml
-```
-
-
-```
-kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600" > busybox.yml
-
-kubectl create -f busybox.yml
-
-apiVersion: v1
-kind: Pod
-metadata:
- creationTimestamp: null
- labels:
- run: busybox
- name: busybox
-spec:
- serviceAccountName: admin
- containers:
- - args:
- - /bin/sh
- - -c
- - sleep 3600
- image: busybox
- name: busybox
- resources: {}
- dnsPolicy: ClusterFirst
- restartPolicy: Never
-status: {}
-
-// verify
-kubectl describe po busybox
-```
-List all the configmaps in the cluster
+ delete the configmap myconfigmap we just created
-Create a file called config.txt with two values key1=value1 and key2=value2 and verify the file
-Create a configmap named keyvalcfgmap and read data from the file config.txt and verify that configmap is created correctly
-Create an nginx pod and load environment values from the above configmap keyvalcfgmap and exec into the pod and verify the environment variables and delete the pod
-Create an env file file.env with var1=val1 and create a configmap envcfgmap from this env file and verify the configmap
-Create an nginx pod and load environment values from the above configmap envcfgmap and exec into the pod and verify the environment variables and delete the pod
-Create a configmap called cfgvolume with values var1=val1, var2=val2 and create an nginx pod with volume nginx-volume which reads data from this configmap cfgvolume and put it on the path /etc/cfg
-Create a pod called secbusybox with the image busybox which executes command sleep 3600 and makes sure any Containers in the Pod, all processes run with user ID 1000 and with group id 2000 and verify.
-Create the same pod as above this time set the securityContext for the container as well and verify that the securityContext of container overrides the Pod level securityContext.
-Create pod with an nginx image and configure the pod with capabilities NET_ADMIN and SYS_TIME verify the capabilities
-Create a Pod nginx and specify a memory request and a memory limit of 100Mi and 200Mi respectively.
-Create a Pod nginx and specify a CPU request and a CPU limit of 0.5 and 1 respectively.
-Create a Pod nginx and specify both CPU, memory requests and limits together and verify.
-Create a Pod nginx and specify a memory request and a memory limit of 100Gi and 200Gi respectively which is too big for the nodes and verify pod fails to start because of insufficient memory
-Create a secret mysecret with values user=myuser and password=mypassword
-List the secrets in all namespaces
-Output the yaml of the secret created above
-Create an nginx pod which reads username as the environment variable
-Create an nginx pod which loads the secret as environment variables
-List all the service accounts in the default namespace
-List all the service accounts in all namespaces
-Create a service account called admin
-Output the YAML file for the service account we just created
-Create a busybox pod which executes this command sleep 3600 with the service account admin and verify
-
+ + ``` + kubectl create cm myconfigmap --from-literal=appname=myapp + ``` +
++ + ``` + // you will see under data + kubectl get cm -o yaml + or + kubectl describe cm + ``` +
++ + ``` + kubectl delete cm myconfigmap + ``` +
++ + ``` + cat >> config.txt << EOF + key1=value1 + key2=value2 + EOF + + cat config.txt + ``` +
++ + ``` + kubectl create cm keyvalcfgmap --from-file=config.txt + + kubectl get cm keyvalcfgmap -o yaml + ``` +
++ + ``` + // first run this command to save the pod yml + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml + + // edit the yml to below file and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + resources: {} + envFrom: + - configMapRef: + name: keyvalcfgmap + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx-pod.yml + + // verify + kubectl exec -it nginx -- env + kubectl delete po nginx + ``` +
++ + ``` + echo var1=val1 > file.env + cat file.env + + kubectl create cm envcfgmap --from-env-file=file.env + kubectl get cm envcfgmap -o yaml --export + ``` +
++ + ``` + // first run this command to save the pod yml + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml + + // edit the yml to below file and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + resources: {} + env: + - name: ENVIRONMENT + valueFrom: + configMapKeyRef: + name: envcfgmap + key: environment + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx-pod.yml + + // verify + kubectl exec -it nginx -- env + kubectl delete po nginx + ``` +
++ + ``` + // first create a configmap cfgvolume + kubectl create cm cfgvolume --from-literal=var1=val1 --from-literal=var2=val2 + + // verify the configmap + kubectl describe cm cfgvolume + + // create the config map + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + volumes: + - name: nginx-volume + configMap: + name: cfgvolume + containers: + - image: nginx + name: nginx + resources: {} + volumeMounts: + - name: nginx-volume + mountPath: /etc/cfg + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx-volume.yml + + // exec into the pod + kubectl exec -it nginx -- /bin/sh + + // check the path + cd /etc/cfg + ls + ``` +
++ + ``` + // create yml file with dry-run + kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml + + // edit the pod like below and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: secbusybox + name: secbusybox + spec: + securityContext: # add security context + runAsUser: 1000 + runAsGroup: 2000 + containers: + - args: + - /bin/sh + - -c + - sleep 3600; + image: busybox + name: secbusybox + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f busybox.yml + + // verify + kubectl exec -it secbusybox -- sh + id // it will show the id and group + ``` +
++ + ``` + // create yml file with dry-run + kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml + + // edit the pod like below and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: secbusybox + name: secbusybox + spec: + securityContext: + runAsUser: 1000 + containers: + - args: + - /bin/sh + - -c + - sleep 3600; + image: busybox + securityContext: + runAsUser: 2000 + name: secbusybox + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f busybox.yml + + // verify + kubectl exec -it secbusybox -- sh + id // you can see container securityContext overides the Pod level + ``` +
++ + ``` + // create the yaml file + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml + + // edit as below and create pod + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + securityContext: + capabilities: + add: ["SYS_TIME", "NET_ADMIN"] + name: nginx + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yml + + // exec and verify + kubectl exec -it nginx -- sh + cd /proc/1 + cat status + + // you should see these values + CapPrm: 00000000aa0435fb + CapEff: 00000000aa0435fb + ``` +
++ + ``` + // create a yml file + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml + + // add the resources section and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + resources: + requests: + memory: "100Mi" + limits: + memory: "200Mi" + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yml + + // verify + kubectl top pod + ``` +
++ + ``` + // create a yml file + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml + + // add the resources section and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + resources: + requests: + cpu: "0.5" + limits: + cpu: "1" + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yml + + // verify + kubectl top pod + ``` +
++ + ``` + // create a yml file + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml + + // add the resources section and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + resources: + requests: + memory: "100Mi" + cpu: "0.5" + limits: + memory: "200Mi" + cpu: "1" + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yml + + // verify + kubectl top pod + ``` +
++ + ``` + // create a yml file + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml + + // add the resources section and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + resources: + requests: + memory: "100Gi" + cpu: "0.5" + limits: + memory: "200Gi" + cpu: "1" + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yml + + // verify + kubectl describe po nginx // you can see pending state + ``` +
++ + ``` + kubectl create secret generic my-secret --from-literal=username=user --from-literal=password=mypassword + ``` +
++ + ``` + kubectl get secret --all-namespaces + ``` +
++ + ``` + kubectl get secret my-secret -o yaml + ``` +
++ + ``` + // create a yml file + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml + + // add env section below and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + env: + - name: USER_NAME + valueFrom: + secretKeyRef: + name: my-secret + key: username + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yml + + //verify + kubectl exec -it nginx -- env + ``` +
++ + ``` + // create a yml file + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml + + // add env section below and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + envFrom: + - secretRef: + name: my-secret + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yml + + //verify + kubectl exec -it nginx -- env + ``` +
++ + ``` + kubectl get sa + ``` +
++ + ``` + kubectl get sa --all-namespaces + ``` +
++ + ``` + kubectl create sa admin + ``` +
++ + ``` + kubectl get sa admin -o yaml + ``` +
++ + ``` + kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600" > busybox.yml + + kubectl create -f busybox.yml + + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: busybox + name: busybox + spec: + serviceAccountName: admin + containers: + - args: + - /bin/sh + - -c + - sleep 3600 + image: busybox + name: busybox + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + // verify + kubectl describe po busybox + ``` +
+- -``` -kubectl get namespaces - -kubectl get ns -``` -
-- -``` -kubectl get po --all-namespaces -``` -
-
-
-```
-kubectl get po -n
-
-```
-kubectl get svc -n
- -``` -kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}" -``` -
-- -``` -// creating a pod -kubectl run nginx --image=nginx --restart=Never - -// List the pod -kubectl get po -``` -
-- -``` -// get the yaml file with --dry-run flag -kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yaml - -// cat nginx-pod.yaml -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: nginx - name: nginx -spec: - containers: - - image: nginx - name: nginx - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -// create a pod -kubectl create -f nginx-pod.yaml -``` -
-- -``` -kubectl get po nginx -o yaml -``` -
-- -``` -kubectl get po nginx -o yaml --export -``` -
-- -``` -kubectl describe pod nginx -``` -
-- -``` -kubectl delete po nginx - -kubectl delete -f nginx-pod.yaml -``` -
-- -``` -kubectl delete po nginx --grace-period=0 --force -``` -
-- -``` -kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80 -``` -
-- -``` -kubectl set image pod/nginx nginx=nginx:1.15-alpine - -kubectl describe po nginx - -// another way it will open vi editor and change the version -kubeclt edit po nginx - -kubectl describe po nginx -``` -
-- -``` -kubectl set image pod/nginx nginx=nginx:1.17.1 - -kubectl describe po nginx - -kubectl get po nginx -w # watch it -``` -
-- -``` -kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}' -``` -
-- -``` -// creating a pod -kubectl run nginx --image=nginx --restart=Never - -// exec into the pod -kubectl exec -it nginx /bin/sh -``` -
-- -``` -kubectl get po nginx -o wide -``` -
-- -``` -kubectl run busybox --image=busybox --restart=Never -- ls - -kubectl logs busybox -``` -
-- -``` -kubectl logs busybox -p -``` -
-- -``` -kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600" -``` -
-
-
-```
-kubectl get po nginx -o wide
-
-// check the connection
-kubectl exec -it busybox -- wget -o-
- -``` -kubectl run busybox --image=nginx --restart=Never -it -- echo "How are you" - -kubectl delete po busybox -``` -
-- -``` -// notice the --rm flag -kubectl run busybox --image=nginx --restart=Never -it --rm -- echo "How are you" -``` -
-- -``` -// create a pod -kubectl run nginx --image=nginx --restart=Never --port=80 - -// List the pod with different verbosity -kubectl get po nginx --v=7 -kubectl get po nginx --v=8 -kubectl get po nginx --v=9 -``` -
-- -``` -kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state" -``` -
-- -``` -kubectl get pods --sort-by=.metadata.name -``` -
-- -``` -kubectl get pods--sort-by=.metadata.creationTimestamp -``` -
-+ + ``` + kubectl get namespaces + + kubectl get ns + ``` +
++ + ``` + kubectl get po --all-namespaces + ``` +
+
+
+ ```
+ kubectl get po -n
+
+ ```
+ kubectl get svc -n
+ + ``` + kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}" + ``` +
++ + ``` + // creating a pod + kubectl run nginx --image=nginx --restart=Never + + // List the pod + kubectl get po + ``` +
++ + ``` + // get the yaml file with --dry-run flag + kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yaml + + // cat nginx-pod.yaml + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + // create a pod + kubectl create -f nginx-pod.yaml + ``` +
++ + ``` + kubectl get po nginx -o yaml + ``` +
++ + ``` + kubectl get po nginx -o yaml --export + ``` +
++ + ``` + kubectl describe pod nginx + ``` +
++ + ``` + kubectl delete po nginx + + kubectl delete -f nginx-pod.yaml + ``` +
++ + ``` + kubectl delete po nginx --grace-period=0 --force + ``` +
++ + ``` + kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80 + ``` +
++ + ``` + kubectl set image pod/nginx nginx=nginx:1.15-alpine + + kubectl describe po nginx + + // another way it will open vi editor and change the version + kubeclt edit po nginx + + kubectl describe po nginx + ``` +
++ + ``` + kubectl set image pod/nginx nginx=nginx:1.17.1 + + kubectl describe po nginx + + kubectl get po nginx -w # watch it + ``` +
++ + ``` + kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}' + ``` +
++ + ``` + // creating a pod + kubectl run nginx --image=nginx --restart=Never + + // exec into the pod + kubectl exec -it nginx /bin/sh + ``` +
++ + ``` + kubectl get po nginx -o wide + ``` +
++ + ``` + kubectl run busybox --image=busybox --restart=Never -- ls + + kubectl logs busybox + ``` +
++ + ``` + kubectl logs busybox -p + ``` +
++ + ``` + kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600" + ``` +
+
+
+ ```
+ kubectl get po nginx -o wide
+
+ // check the connection
+ kubectl exec -it busybox -- wget -o-
+ + ``` + kubectl run busybox --image=nginx --restart=Never -it -- echo "How are you" + + kubectl delete po busybox + ``` +
++ + ``` + // notice the --rm flag + kubectl run busybox --image=nginx --restart=Never -it --rm -- echo "How are you" + ``` +
++ + ``` + // create a pod + kubectl run nginx --image=nginx --restart=Never --port=80 + + // List the pod with different verbosity + kubectl get po nginx --v=7 + kubectl get po nginx --v=8 + kubectl get po nginx --v=9 + ``` +
++ + ``` + kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state" + ``` +
++ + ``` + kubectl get pods --sort-by=.metadata.name + ``` +
++ + ``` + kubectl get pods--sort-by=.metadata.creationTimestamp + ``` +
+- -``` -// first create single container pod with dry run flag -kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml - -// edit the pod like below - -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: busybox - name: busybox -spec: - containers: - - args: - - bin/sh - - -c - - ls; sleep 3600 - image: busybox - name: busybox1 - resources: {} - - args: - - bin/sh - - -c - - echo Hello world; sleep 3600 - image: busybox - name: busybox2 - resources: {} - - args: - - bin/sh - - -c - - echo this is third container; sleep 3600 - image: busybox - name: busybox3 - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -// create it -kubectl create -f multi-container.yaml - -kubectl get po busybox -``` -
-- -``` -kubectl logs busybox -c busybox1 -kubectl logs busybox -c busybox2 -kubectl logs busybox -c busybox3 -``` -
-- -``` -kubectl logs busybox -c busybox2 --previous -``` -
-- -``` -kubectl exec busybox -c busybox3 -- ls -``` -
-- -``` -kubectl top pod busybox --containers - -// putting them into file -kubectl top pod busybox --containers > file.log -cat file.log -``` -
-- -``` -// create an initial yaml file with this -kubectl run multi-cont-pod --image=busbox --restart=Never --dry-run -o yaml > multi-container.yaml - -// edit the yml as below and create it -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: multi-cont-pod - name: multi-cont-pod -spec: - volumes: - - name: var-logs - emptyDir: {} - containers: - - image: busybox - command: ["/bin/sh"] - args: ["-c", "while true; do echo 'Hi I am from Main container' >> /var/log/index.html; sleep 5;done"] - name: main-container - resources: {} - volumeMounts: - - name: var-logs - mountPath: /var/log - - image: nginx - name: sidecar-container - resources: {} - ports: - - containerPort: 80 - volumeMounts: - - name: var-logs - mountPath: /usr/share/nginx/html - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -kubectl create -f multi-container.yaml - -kubectl get po multi-cont-pod -``` -
-- -``` -// exec into main container -kubectl exec -it multi-cont-pod -c main-container -- sh -cat /var/log/main.txt - -// exec into sidecar container -kubectl exec -it multi-cont-pod -c sidecar-container -- sh -cat /usr/share/nginx/html/index.html - -// install curl and get default page -kubectl exec -it multi-cont-pod -c sidecar-container -- sh -# apt-get update && apt-get install -y curl -# curl localhost -``` -
-+ + ``` + // first create single container pod with dry run flag + kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml + + // edit the pod like below + + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: busybox + name: busybox + spec: + containers: + - args: + - bin/sh + - -c + - ls; sleep 3600 + image: busybox + name: busybox1 + resources: {} + - args: + - bin/sh + - -c + - echo Hello world; sleep 3600 + image: busybox + name: busybox2 + resources: {} + - args: + - bin/sh + - -c + - echo this is third container; sleep 3600 + image: busybox + name: busybox3 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + // create it + kubectl create -f multi-container.yaml + + kubectl get po busybox + ``` +
++ + ``` + kubectl logs busybox -c busybox1 + kubectl logs busybox -c busybox2 + kubectl logs busybox -c busybox3 + ``` +
++ + ``` + kubectl logs busybox -c busybox2 --previous + ``` +
++ + ``` + kubectl exec busybox -c busybox3 -- ls + ``` +
++ + ``` + kubectl top pod busybox --containers + + // putting them into file + kubectl top pod busybox --containers > file.log + cat file.log + ``` +
++ + ``` + // create an initial yaml file with this + kubectl run multi-cont-pod --image=busbox --restart=Never --dry-run -o yaml > multi-container.yaml + + // edit the yml as below and create it + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: multi-cont-pod + name: multi-cont-pod + spec: + volumes: + - name: var-logs + emptyDir: {} + containers: + - image: busybox + command: ["/bin/sh"] + args: ["-c", "while true; do echo 'Hi I am from Main container' >> /var/log/index.html; sleep 5;done"] + name: main-container + resources: {} + volumeMounts: + - name: var-logs + mountPath: /var/log + - image: nginx + name: sidecar-container + resources: {} + ports: + - containerPort: 80 + volumeMounts: + - name: var-logs + mountPath: /usr/share/nginx/html + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f multi-container.yaml + + kubectl get po multi-cont-pod + ``` +
++ + ``` + // exec into main container + kubectl exec -it multi-cont-pod -c main-container -- sh + cat /var/log/main.txt + + // exec into sidecar container + kubectl exec -it multi-cont-pod -c sidecar-container -- sh + cat /usr/share/nginx/html/index.html + + // install curl and get default page + kubectl exec -it multi-cont-pod -c sidecar-container -- sh + # apt-get update && apt-get install -y curl + # curl localhost + ``` +
+- -``` -kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml - -// add the readinessProbe section and create -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: nginx - name: nginx -spec: - containers: - - image: nginx - name: nginx - ports: - - containerPort: 80 - readinessProbe: - httpGet: - path: / - port: 80 - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -kubectl create -f nginx-pod.yaml - -// verify -kubectl describe pod nginx | grep -i readiness -kubectl delete po nginx -``` -
-- -``` -kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml - -// add the livenessProbe section and create -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: nginx - name: nginx -spec: - containers: - - image: nginx - name: nginx - ports: - - containerPort: 80 - livenessProbe: - httpGet: - path: /healthz - port: 80 - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -kubectl create -f nginx-pod.yaml - -// verify -kubectl describe pod nginx | grep -i readiness -kubectl delete po nginx -``` -
-- -``` -kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml - -// add the livenessProbe and readiness section and create -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: nginx - name: nginx -spec: - containers: - - image: nginx - name: nginx - ports: - - containerPort: 80 - livenessProbe: - httpGet: - path: /healthz - port: 80 - readinessProbe: - httpGet: - path: / - port: 80 - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -kubectl create -f nginx-pod.yaml - -// verify -kubectl describe pod nginx | grep -i readiness -kubectl describe pod nginx | grep -i liveness -``` -
-- -``` -kubectl explain Pod.spec.containers.livenessProbe -kubectl explain Pod.spec.containers.readinessProbe -``` -
-- -``` -// nginx-pod.yaml - -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: nginx - name: nginx -spec: - containers: - - image: nginx - name: nginx - ports: - - containerPort: 80 - livenessProbe: - initialDelaySeconds: 20 - periodSeconds: 25 - httpGet: - path: /healthz - port: 80 - readinessProbe: - initialDelaySeconds: 20 - periodSeconds: 25 - httpGet: - path: / - port: 80 - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -kubectl create -f nginx-pod.yaml -``` -
-- -``` -kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "echo I am from busybox pod; sleep 3600;" - -kubectl logs busybox -``` -
-- -``` -kubectl logs busybox > busybox-logs.txt - -cat busybox-logs.txt -``` -
-- -``` -kubectl get events --sort-by=.metadata.creationTimestamp - -// putting them into file.log -kubectl get events --sort-by=.metadata.creationTimestamp > file.log - -cat file.log -``` -
-- -``` -// create the pod -kubectl run hello --image=alpine --restart=Never -- /bin/sh -c "while true; do echo 'Hi I am from Alpine'; sleep 5;done" - -// verify and follow the logs -kubectl logs --follow hello -``` -
-- -``` -// create the pod -kubectl create -f https://gist.githubusercontent.com/bbachi/212168375b39e36e2e2984c097167b00/raw/1fd63509c3ae3a3d3da844640fb4cca744543c1c/not-running.yml - -// get the pod -kubectl get pod not-running -kubectl describe po not-running - -// it clearly says ImagePullBackOff something wrong with image -kubectl edit pod not-running // it will open vim editor - or -kubectl set image pod/not-running not-running=nginx -``` -
-- -``` -kubectl create -f https://gist.githubusercontent.com/bbachi/1f001f10337234d46806929d12245397/raw/84b7295fb077f15de979fec5b3f7a13fc69c6d83/problem-pod.yaml - -// get all the pods in all namespaces -kubectl get po --all-namespaces - -// find out which pod is not running -kubectl get po -n namespace2 - -// update the image -kubectl set image pod/pod2 pod2=nginx -n namespace2 - -// verify again -kubectl get po -n namespace2 -``` -
-- -``` -// get the top 3 hungry pods -kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3 - -// putting into file -kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3 > cpu-usage.txt - -// verify -cat cpu-usage.txt -``` -
-+ + ``` + kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml + + // add the readinessProbe section and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + ports: + - containerPort: 80 + readinessProbe: + httpGet: + path: / + port: 80 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx-pod.yaml + + // verify + kubectl describe pod nginx | grep -i readiness + kubectl delete po nginx + ``` +
++ + ``` + kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml + + // add the livenessProbe section and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + ports: + - containerPort: 80 + livenessProbe: + httpGet: + path: /healthz + port: 80 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx-pod.yaml + + // verify + kubectl describe pod nginx | grep -i readiness + kubectl delete po nginx + ``` +
++ + ``` + kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx-pod.yaml + + // add the livenessProbe and readiness section and create + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + ports: + - containerPort: 80 + livenessProbe: + httpGet: + path: /healthz + port: 80 + readinessProbe: + httpGet: + path: / + port: 80 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx-pod.yaml + + // verify + kubectl describe pod nginx | grep -i readiness + kubectl describe pod nginx | grep -i liveness + ``` +
++ + ``` + kubectl explain Pod.spec.containers.livenessProbe + kubectl explain Pod.spec.containers.readinessProbe + ``` +
++ + ``` + // nginx-pod.yaml + + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + run: nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + ports: + - containerPort: 80 + livenessProbe: + initialDelaySeconds: 20 + periodSeconds: 25 + httpGet: + path: /healthz + port: 80 + readinessProbe: + initialDelaySeconds: 20 + periodSeconds: 25 + httpGet: + path: / + port: 80 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx-pod.yaml + ``` +
++ + ``` + kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "echo I am from busybox pod; sleep 3600;" + + kubectl logs busybox + ``` +
++ + ``` + kubectl logs busybox > busybox-logs.txt + + cat busybox-logs.txt + ``` +
++ + ``` + kubectl get events --sort-by=.metadata.creationTimestamp + + // putting them into file.log + kubectl get events --sort-by=.metadata.creationTimestamp > file.log + + cat file.log + ``` +
++ + ``` + // create the pod + kubectl run hello --image=alpine --restart=Never -- /bin/sh -c "while true; do echo 'Hi I am from Alpine'; sleep 5;done" + + // verify and follow the logs + kubectl logs --follow hello + ``` +
++ + ``` + // create the pod + kubectl create -f https://gist.githubusercontent.com/bbachi/212168375b39e36e2e2984c097167b00/raw/1fd63509c3ae3a3d3da844640fb4cca744543c1c/not-running.yml + + // get the pod + kubectl get pod not-running + kubectl describe po not-running + + // it clearly says ImagePullBackOff something wrong with image + kubectl edit pod not-running // it will open vim editor + or + kubectl set image pod/not-running not-running=nginx + ``` +
++ + ``` + kubectl create -f https://gist.githubusercontent.com/bbachi/1f001f10337234d46806929d12245397/raw/84b7295fb077f15de979fec5b3f7a13fc69c6d83/problem-pod.yaml + + // get all the pods in all namespaces + kubectl get po --all-namespaces + + // find out which pod is not running + kubectl get po -n namespace2 + + // update the image + kubectl set image pod/pod2 pod2=nginx -n namespace2 + + // verify again + kubectl get po -n namespace2 + ``` +
++ + ``` + // get the top 3 hungry pods + kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3 + + // putting into file + kubectl top pod --all-namespaces | sort --reverse --key 3 --numeric | head -3 > cpu-usage.txt + + // verify + cat cpu-usage.txt + ``` +
+- -``` -kubectl get pods --show-labels -``` -
-- -``` -kubectl run nginx-dev1 --image=nginx --restart=Never --labels=env=dev -kubectl run nginx-dev2 --image=nginx --restart=Never --labels=env=dev -kubectl run nginx-dev3 --image=nginx --restart=Never --labels=env=dev -kubectl run nginx-prod1 --image=nginx --restart=Never --labels=env=prod -kubectl run nginx-prod2 --image=nginx --restart=Never --labels=env=prod -``` -
-- -``` -kubeclt get pods --show-labels -``` -
-- -``` -kubectl get pods -l env=dev -``` -
-- -``` -kubectl get pods -l env=dev --show-labels -``` -
-- -``` -kubectl get pods -l env=prod -``` -
-- -``` -kubectl get pods -l env=prod --show-labels -``` -
-- -``` -kubectl get pods -L env -``` -
-- -``` -kubectl get pods -l 'env in (dev,prod)' -``` -
-- -``` -kubectl get pods -l 'env in (dev,prod)' --show-labels -``` -
-- -``` -kubectl label pod/nginx-dev3 env=uat --overwrite - -kubectl get pods --show-labels -``` -
-- -``` -kubectl label pod nginx-dev{1..3} env- -kubectl label pod nginx-prod{1..2} env- - -kubectl get po --show-labels -``` -
-- -``` -kubectl label pod nginx-dev{1..3} app=nginx -kubectl label pod nginx-prod{1..2} app=nginx - -kubectl get po --show-labels -``` -
-- -``` -kubectl get nodes --show-labels -``` -
-- -``` -kubectl label node minikube nodeName=nginxnode -``` -
-- -``` -kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml - -// add the nodeSelector like below and create the pod - -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - run: nginx - name: nginx -spec: - nodeSelector: - nodeName: nginxnode - containers: - - image: nginx - name: nginx - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -kubectl create -f pod.yaml -``` -
-- -``` -kubectl describe po nginx | grep Node-Selectors -``` -
-- -``` -kubectl describe po nginx | grep Labels -``` -
-- -``` -kubectl annotate pod nginx-dev{1..3} name=webapp -kubectl annotate pod nginx-prod{1..2} name=webapp -``` -
-- -``` -kubectl describe po nginx-dev{1..3} | grep -i annotations -kubectl describe po nginx-prod{1..2} | grep -i annotations -``` -
-- -``` -kubectl annotate pod nginx-dev{1..3} name- -kubectl annotate pod nginx-prod{1..2} name- - -kubectl describe po nginx-dev{1..3} | grep -i annotations -kubectl describe po nginx-prod{1..2} | grep -i annotations -``` -
-- -``` -kubectl delete po --all -``` -
-
-
-```
-kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml
-
-// change the replicas to 5 in the yaml and create it
-
-apiVersion: apps/v1
-kind: Deployment
-metadata:
- creationTimestamp: null
- labels:
- app: webapp
- name: webapp
-spec:
- replicas: 5
- selector:
- matchLabels:
- app: webapp
- strategy: {}
- template:
+1.
+
+ ```
+ kubectl get pods --show-labels
+ ```
+
+
+ ```
+ kubectl run nginx-dev1 --image=nginx --restart=Never --labels=env=dev
+ kubectl run nginx-dev2 --image=nginx --restart=Never --labels=env=dev
+ kubectl run nginx-dev3 --image=nginx --restart=Never --labels=env=dev
+ kubectl run nginx-prod1 --image=nginx --restart=Never --labels=env=prod
+ kubectl run nginx-prod2 --image=nginx --restart=Never --labels=env=prod
+ ```
+
+
+ ```
+ kubeclt get pods --show-labels
+ ```
+
+
+ ```
+ kubectl get pods -l env=dev
+ ```
+
+
+ ```
+ kubectl get pods -l env=dev --show-labels
+ ```
+
+
+ ```
+ kubectl get pods -l env=prod
+ ```
+
+
+ ```
+ kubectl get pods -l env=prod --show-labels
+ ```
+
+
+ ```
+ kubectl get pods -L env
+ ```
+
+
+ ```
+ kubectl get pods -l 'env in (dev,prod)'
+ ```
+
+
+ ```
+ kubectl get pods -l 'env in (dev,prod)' --show-labels
+ ```
+
+
+ ```
+ kubectl label pod/nginx-dev3 env=uat --overwrite
+
+ kubectl get pods --show-labels
+ ```
+
+
+ ```
+ kubectl label pod nginx-dev{1..3} env-
+ kubectl label pod nginx-prod{1..2} env-
+
+ kubectl get po --show-labels
+ ```
+
+
+ ```
+ kubectl label pod nginx-dev{1..3} app=nginx
+ kubectl label pod nginx-prod{1..2} app=nginx
+
+ kubectl get po --show-labels
+ ```
+
+
+ ```
+ kubectl get nodes --show-labels
+ ```
+
+
+ ```
+ kubectl label node minikube nodeName=nginxnode
+ ```
+
+
+ ```
+ kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml
+
+ // add the nodeSelector like below and create the pod
+
+ apiVersion: v1
+ kind: Pod
metadata:
creationTimestamp: null
labels:
- app: webapp
+ run: nginx
+ name: nginx
spec:
+ nodeSelector:
+ nodeName: nginxnode
containers:
- image: nginx
name: nginx
resources: {}
-status: {}
-
-kubectl create -f webapp.yaml
-```
-
-
-```
-kubectl get deploy webapp --show-labels
-```
-
-
-```
-kubectl get deploy webapp -o yaml
-```
-
-
-```
-// get the label of the deployment
-kubectl get deploy --show-labels
-
-// get the pods with that label
-kubectl get pods -l app=webapp
-```
-
-
-```
-kubectl scale deploy webapp --replicas=20
-
-kubectl get po -l app=webapp
-```
-
-
-```
-kubectl rollout status deploy webapp
-```
-
-
-```
-kubectl get rs -l app=webapp
-```
-
-
-```
-kubectl get rs -l app=webapp -o yaml
-
-kubectl get po -l app=webapp -o yaml
-```
-
-
-```
-kubectl delete deploy webapp
-
-kubectl get po -l app=webapp -w
-```
-
-
-```
-kubectl create deploy webapp --image=nginx:1.17.1 --dry-run -o yaml > webapp.yaml
-
-// add the port section and create the deployment
-
-apiVersion: apps/v1
-kind: Deployment
-metadata:
- creationTimestamp: null
- labels:
- app: webapp
- name: webapp
-spec:
- replicas: 1
- selector:
- matchLabels:
- app: webapp
- strategy: {}
- template:
+ dnsPolicy: ClusterFirst
+ restartPolicy: Never
+ status: {}
+
+ kubectl create -f pod.yaml
+ ```
+
+
+ ```
+ kubectl describe po nginx | grep Node-Selectors
+ ```
+
+
+ ```
+ kubectl describe po nginx | grep Labels
+ ```
+
+
+ ```
+ kubectl annotate pod nginx-dev{1..3} name=webapp
+ kubectl annotate pod nginx-prod{1..2} name=webapp
+ ```
+
+
+ ```
+ kubectl describe po nginx-dev{1..3} | grep -i annotations
+ kubectl describe po nginx-prod{1..2} | grep -i annotations
+ ```
+
+
+ ```
+ kubectl annotate pod nginx-dev{1..3} name-
+ kubectl annotate pod nginx-prod{1..2} name-
+
+ kubectl describe po nginx-dev{1..3} | grep -i annotations
+ kubectl describe po nginx-prod{1..2} | grep -i annotations
+ ```
+
+
+ ```
+ kubectl delete po --all
+ ```
+
+
+ ```
+ kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml
+
+ // change the replicas to 5 in the yaml and create it
+
+ apiVersion: apps/v1
+ kind: Deployment
metadata:
creationTimestamp: null
labels:
app: webapp
+ name: webapp
spec:
- containers:
- - image: nginx:1.17.1
- name: nginx
- ports:
- - containerPort: 80
- resources: {}
-status: {}
-
-kubectl create -f webapp.yaml
-
-// verify
-kubectl describe deploy webapp | grep Image
-```
-
-
-```
-kubectl set image deploy/webapp nginx=nginx:1.17.4
-
-kubectl describe deploy webapp | grep Image
-```
-
-
-```
-kubectl rollout history deploy webapp
-
-kubectl get deploy webapp --show-labels
-kubectl get rs -l app=webapp
-kubectl get po -l app=webapp
-```
-
-
-```
-kubectl rollout undo deploy webapp
-
-kubectl describe deploy webapp | grep Image
-```
-
-
-```
-kubectl set image deploy/webapp nginx=nginx:1.16.1
-
-kubectl describe deploy webapp | grep Image
-
-kubectl rollout history deploy webapp
-```
-
-
-```
-kubectl rollout undo deploy webapp --to-revision=3
-
-kubectl describe deploy webapp | grep Image
-
-kubectl rollout status deploy webapp
-```
-
-
-```
-kubectl set image deploy/webapp nginx=nginx:1.100
-
-kubectl rollout status deploy webapp (still pending state)
-
-kubectl get pods (ImagePullErr)
-```
-
-
-```
-kubectl rollout undo deploy webapp
-kubectl rollout status deploy webapp
-
-kubectl get pods
-```
-
-
-```
-kubectl rollout history deploy webapp --revision=7
-```
-
-
-```
-kubectl rollout pause deploy webapp
-```
-
-
-```
-kubectl set image deploy/webapp nginx=nginx:latest
-
-kubectl rollout history deploy webapp (No new revision)
-```
-
-
-```
-kubectl rollout resume deploy webapp
-```
-
-
-```
-kubectl rollout history deploy webapp
-
-kubectl rollout history deploy webapp --revision=9
-```
-
-
-```
-kubectl autoscale deploy webapp --min=10 --max=20 --cpu-percent=85
-
-kubectl get hpa
-
-kubectl get pod -l app=webapp
-```
-
-
-```
-kubectl delete deploy webapp
-
-kubectl delete hpa webapp
-```
-
-
-```
-kubectl create job nodeversion --image=node -- node -v
-
-kubectl get job -w
-kubectl get pod
-```
-
-
-```
-kubectl logs
-
-```
-kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job"
-```
-
-
-```
-kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml
-
-kubectl create -f hello-job.yaml
-```
-
-
-```
-kubectl get job
-kubectl get po
-
-kubectl logs hello-job-*
-```
-
-
-```
-kubectl delete job hello-job
-```
-
-
-```
-kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml
-
-// edit the yaml file to add completions: 10
-
-apiVersion: batch/v1
-kind: Job
-metadata:
- creationTimestamp: null
- name: hello-job
-spec:
- completions: 10
- template:
+ replicas: 5
+ selector:
+ matchLabels:
+ app: webapp
+ strategy: {}
+ template:
+ metadata:
+ creationTimestamp: null
+ labels:
+ app: webapp
+ spec:
+ containers:
+ - image: nginx
+ name: nginx
+ resources: {}
+ status: {}
+
+ kubectl create -f webapp.yaml
+ ```
+
+
+ ```
+ kubectl get deploy webapp --show-labels
+ ```
+
+
+ ```
+ kubectl get deploy webapp -o yaml
+ ```
+
+
+ ```
+ // get the label of the deployment
+ kubectl get deploy --show-labels
+
+ // get the pods with that label
+ kubectl get pods -l app=webapp
+ ```
+
+
+ ```
+ kubectl scale deploy webapp --replicas=20
+
+ kubectl get po -l app=webapp
+ ```
+
+
+ ```
+ kubectl rollout status deploy webapp
+ ```
+
+
+ ```
+ kubectl get rs -l app=webapp
+ ```
+
+
+ ```
+ kubectl get rs -l app=webapp -o yaml
+
+ kubectl get po -l app=webapp -o yaml
+ ```
+
+
+ ```
+ kubectl delete deploy webapp
+
+ kubectl get po -l app=webapp -w
+ ```
+
+
+ ```
+ kubectl create deploy webapp --image=nginx:1.17.1 --dry-run -o yaml > webapp.yaml
+
+ // add the port section and create the deployment
+
+ apiVersion: apps/v1
+ kind: Deployment
metadata:
creationTimestamp: null
+ labels:
+ app: webapp
+ name: webapp
spec:
- containers:
- - command:
- - echo
- - Hello I am from job
- image: busybox
- name: hello-job
- resources: {}
- restartPolicy: Never
-status: {}
-
-kubectl create -f hello-job.yaml
-```
-
-
-```
-kubectl get job -w
-kubectl get po
-
-kubectl delete job hello-job
-```
-
-
-```
-kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml
-
-// edit the yaml file to add parallelism: 10
-
-apiVersion: batch/v1
-kind: Job
-metadata:
- creationTimestamp: null
- name: hello-job
-spec:
- parallelism: 10
- template:
+ replicas: 1
+ selector:
+ matchLabels:
+ app: webapp
+ strategy: {}
+ template:
+ metadata:
+ creationTimestamp: null
+ labels:
+ app: webapp
+ spec:
+ containers:
+ - image: nginx:1.17.1
+ name: nginx
+ ports:
+ - containerPort: 80
+ resources: {}
+ status: {}
+
+ kubectl create -f webapp.yaml
+
+ // verify
+ kubectl describe deploy webapp | grep Image
+ ```
+
+
+ ```
+ kubectl set image deploy/webapp nginx=nginx:1.17.4
+
+ kubectl describe deploy webapp | grep Image
+ ```
+
+
+ ```
+ kubectl rollout history deploy webapp
+
+ kubectl get deploy webapp --show-labels
+ kubectl get rs -l app=webapp
+ kubectl get po -l app=webapp
+ ```
+
+
+ ```
+ kubectl rollout undo deploy webapp
+
+ kubectl describe deploy webapp | grep Image
+ ```
+
+
+ ```
+ kubectl set image deploy/webapp nginx=nginx:1.16.1
+
+ kubectl describe deploy webapp | grep Image
+
+ kubectl rollout history deploy webapp
+ ```
+
+
+ ```
+ kubectl rollout undo deploy webapp --to-revision=3
+
+ kubectl describe deploy webapp | grep Image
+
+ kubectl rollout status deploy webapp
+ ```
+
+
+ ```
+ kubectl set image deploy/webapp nginx=nginx:1.100
+
+ kubectl rollout status deploy webapp (still pending state)
+
+ kubectl get pods (ImagePullErr)
+ ```
+
+
+ ```
+ kubectl rollout undo deploy webapp
+ kubectl rollout status deploy webapp
+
+ kubectl get pods
+ ```
+
+
+ ```
+ kubectl rollout history deploy webapp --revision=7
+ ```
+
+
+ ```
+ kubectl rollout pause deploy webapp
+ ```
+
+
+ ```
+ kubectl set image deploy/webapp nginx=nginx:latest
+
+ kubectl rollout history deploy webapp (No new revision)
+ ```
+
+
+ ```
+ kubectl rollout resume deploy webapp
+ ```
+
+
+ ```
+ kubectl rollout history deploy webapp
+
+ kubectl rollout history deploy webapp --revision=9
+ ```
+
+
+ ```
+ kubectl autoscale deploy webapp --min=10 --max=20 --cpu-percent=85
+
+ kubectl get hpa
+
+ kubectl get pod -l app=webapp
+ ```
+
+
+ ```
+ kubectl delete deploy webapp
+
+ kubectl delete hpa webapp
+ ```
+
+
+ ```
+ kubectl create job nodeversion --image=node -- node -v
+
+ kubectl get job -w
+ kubectl get pod
+ ```
+
+
+ ```
+ kubectl logs
+
+ ```
+ kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job"
+ ```
+
+
+ ```
+ kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml
+
+ kubectl create -f hello-job.yaml
+ ```
+
+
+ ```
+ kubectl get job
+ kubectl get po
+
+ kubectl logs hello-job-*
+ ```
+
+
+ ```
+ kubectl delete job hello-job
+ ```
+
+
+ ```
+ kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml
+
+ // edit the yaml file to add completions: 10
+
+ apiVersion: batch/v1
+ kind: Job
metadata:
creationTimestamp: null
+ name: hello-job
spec:
- containers:
- - command:
- - echo
- - Hello I am from job
- image: busybox
- name: hello-job
- resources: {}
- restartPolicy: Never
-status: {}
-
-kubectl create -f hello-job.yaml
-```
-
-
-```
-kubectl get job -w
-kubectl get po
-
-kubectl delete job hello-job
-```
-
-
-```
-kubectl create cronjob date-job --image=busybox --schedule="*/1 * * * *" -- bin/sh -c "date; echo Hello from kubernetes cluster"
-```
-
-
-```
-kubectl get cj date-job -o yaml
-```
-
-
-```
-kubectl get job
-kubectl get po
-
-kubectl logs date-job-
-
-```
-kubectl delete cj date-job
-
-// verify pods and jobs
-kubectl get po
-kubectl get job
-```
-Get the pods with label information
+ Create 5 nginx pods in which two of them is labeled env=prod and three of them is labeled env=dev
+ Verify all the pods are created with correct labels
+ Get the pods with label env=dev
+ Get the pods with label env=dev and also output the labels
+ Get the pods with label env=prod
+ Get the pods with label env=prod and also output the labels
+ Get the pods with label env
+ Get the pods with labels env=dev and env=prod
+ Get the pods with labels env=dev and env=prod and output the labels as well
+ Change the label for one of the pod to env=uat and list all the pods to verify
+ Remove the labels for the pods that we created now and verify all the labels are removed
+ Let’s add the label app=nginx for all the pods and verify
+ Get all the nodes with labels (if using minikube you would get only master node)
+ Label the node (minikube if you are using) nodeName=nginxnode
+ Create a Pod that will be deployed on this node with the label nodeName=nginxnode
+ Get the deployment you just created with labels
-Output the yaml file of the deployment you just created
-Get the pods of this deployment
-Scale the deployment from 5 replicas to 20 replicas and verify
-Get the deployment rollout status
-Get the replicaset that created with this deployment
-Get the yaml of the replicaset and pods of this deployment
-Delete the deployment you just created and watch all the pods are also being deleted
-Create a deployment of webapp with image nginx:1.17.1 with container port 80 and verify the image version
-Verify the pod that it is scheduled with the node selector
+ Verify the pod nginx that we just created has this label
+ Annotate the pods with name=webapp
+ Verify the pods that have been annotated correctly
+ Remove the annotations on the pods and verify
+ Remove all the pods that we created so far
+ Create a deployment called webapp with image nginx with 5 replicas
+ Update the deployment with the image version 1.17.4 and verify
-Check the rollout history and make sure everything is ok after the update
-Undo the deployment to the previous version 1.17.1 and verify Image has the previous version
-Update the deployment with the image version 1.16.1 and verify the image and also check the rollout history
-Update the deployment to the Image 1.17.1 and verify everything is ok
-Update the deployment with the wrong image version 1.100 and verify something is wrong with the deployment
-Undo the deployment with the previous version and verify everything is Ok
-Check the history of the specific revision of that deployment
-Pause the rollout of the deployment
-Update the deployment with the image version latest and check the history and verify nothing is going on
-Resume the rollout of the deployment
-Check the rollout history and verify it has the new version
-
-Apply the autoscaling to this deployment with minimum 10 and maximum 20 replicas and target CPU of 85% and verify hpa is created and replicas are increased to 10 from 1
-
-Clean the cluster by deleting deployment and hpa you just created
-Create a Job with an image node which prints node version and also verifies there is a pod created for this job
-Get the logs of the job just created
-Output the yaml file for the Job with the image busybox which echos “Hello I am from job”
-Copy the above YAML file to hello-job.yaml file and create the job
-Verify the job and the associated pod is created and check the logs as well
-Delete the job we just created
-Create the same job and make it run 10 times one after one
-Get the deployment you just created with labels
+ Output the yaml file of the deployment you just created
+ Get the pods of this deployment
+ Scale the deployment from 5 replicas to 20 replicas and verify
+ Get the deployment rollout status
+ Get the replicaset that created with this deployment
+ Get the yaml of the replicaset and pods of this deployment
+ Delete the deployment you just created and watch all the pods are also being deleted
+ Create a deployment of webapp with image nginx:1.17.1 with container port 80 and verify the image version
+ Watch the job that runs 10 times one by one and verify 10 pods are created and delete those after it’s completed
-Create the same job and make it run 10 times parallel
-Update the deployment with the image version 1.17.4 and verify
+ Check the rollout history and make sure everything is ok after the update
+ Undo the deployment to the previous version 1.17.1 and verify Image has the previous version
+ Update the deployment with the image version 1.16.1 and verify the image and also check the rollout history
+ Update the deployment to the Image 1.17.1 and verify everything is ok
+ Update the deployment with the wrong image version 1.100 and verify something is wrong with the deployment
+ Undo the deployment with the previous version and verify everything is Ok
+ Check the history of the specific revision of that deployment
+ Pause the rollout of the deployment
+ Update the deployment with the image version latest and check the history and verify nothing is going on
+ Resume the rollout of the deployment
+ Check the rollout history and verify it has the new version
+
+ Apply the autoscaling to this deployment with minimum 10 and maximum 20 replicas and target CPU of 85% and verify hpa is created and replicas are increased to 10 from 1
+
+ Clean the cluster by deleting deployment and hpa you just created
+ Create a Job with an image node which prints node version and also verifies there is a pod created for this job
+ Get the logs of the job just created
+ Output the yaml file for the Job with the image busybox which echos “Hello I am from job”
+ Copy the above YAML file to hello-job.yaml file and create the job
+ Verify the job and the associated pod is created and check the logs as well
+ Delete the job we just created
+ Create the same job and make it run 10 times one after one
+ Watch the job that runs 10 times parallelly and verify 10 pods are created and delete those after it’s completed
-Create a Cronjob with busybox image that prints date and hello from kubernetes cluster message for every minute
-Output the YAML file of the above cronjob
-Verify that CronJob creating a separate job and pods for every minute to run and verify the logs of the pod
-Delete the CronJob and verify all the associated jobs and pods are also deleted
-
+ + ``` + kubectl get job -w + kubectl get po + + kubectl delete job hello-job + ``` +
++ + ``` + kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml + + // edit the yaml file to add parallelism: 10 + + apiVersion: batch/v1 + kind: Job + metadata: + creationTimestamp: null + name: hello-job + spec: + parallelism: 10 + template: + metadata: + creationTimestamp: null + spec: + containers: + - command: + - echo + - Hello I am from job + image: busybox + name: hello-job + resources: {} + restartPolicy: Never + status: {} + + kubectl create -f hello-job.yaml + ``` +
++ + ``` + kubectl get job -w + kubectl get po + + kubectl delete job hello-job + ``` +
++ + ``` + kubectl create cronjob date-job --image=busybox --schedule="*/1 * * * *" -- bin/sh -c "date; echo Hello from kubernetes cluster" + ``` +
++ + ``` + kubectl get cj date-job -o yaml + ``` +
+
+
+ ```
+ kubectl get job
+ kubectl get po
+
+ kubectl logs date-job-
+ + ``` + kubectl delete cj date-job + + // verify pods and jobs + kubectl get po + kubectl get job + ``` +
+- -``` -kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx.yaml - -// edit the label app: my-nginx and create the pod -apiVersion: v1 -kind: Pod -metadata: - creationTimestamp: null - labels: - app: my-nginx - name: nginx -spec: - containers: - - image: nginx - name: nginx - ports: - - containerPort: 80 - resources: {} - dnsPolicy: ClusterFirst - restartPolicy: Never -status: {} - -kubectl create -f nginx.yaml -``` -
-- -``` -// create the below service -apiVersion: v1 -kind: Service -metadata: - name: my-service -spec: - selector: - app: my-nginx - ports: - - protocol: TCP - port: 80 - targetPort: 9376 - -kubectl create -f nginx-svc.yaml -``` -
-- -``` -// get the pod with labels -kubectl get po nginx --show-labels - -// get the service and chekc the selector column -kubectl get svc my-service -o wide -``` -
-- -``` -// delete the service -kubectl delete svc my-service - -// create the service again -kubectl expose po nginx --port=80 --target-port=9376 - -// verify the label -kubectl get svc -l app=my-nginx -``` -
-- -``` -// delete the service -kubectl delete svc nginx - -// create service with expose command -kubectl expose po nginx --port=80 --type=NodePort -``` -
-
-
-```
-// get the clusterIP from this command
-kubectl get svc nginx -o wide
-
-// create temporary busybox to check the nodeport
-kubectl run busybox --image=busybox --restart=Never -it --rm -- wget -o-
- -``` -apiVersion: networking.k8s.io/v1 -kind: NetworkPolicy -metadata: - name: default-deny -spec: - podSelector: {} - policyTypes: - - Ingress -``` -
-+ + ``` + kubectl run nginx --image=nginx --restart=Never --port=80 --dry-run -o yaml > nginx.yaml + + // edit the label app: my-nginx and create the pod + apiVersion: v1 + kind: Pod + metadata: + creationTimestamp: null + labels: + app: my-nginx + name: nginx + spec: + containers: + - image: nginx + name: nginx + ports: + - containerPort: 80 + resources: {} + dnsPolicy: ClusterFirst + restartPolicy: Never + status: {} + + kubectl create -f nginx.yaml + ``` +
++ + ``` + // create the below service + apiVersion: v1 + kind: Service + metadata: + name: my-service + spec: + selector: + app: my-nginx + ports: + - protocol: TCP + port: 80 + targetPort: 9376 + + kubectl create -f nginx-svc.yaml + ``` +
++ + ``` + // get the pod with labels + kubectl get po nginx --show-labels + + // get the service and chekc the selector column + kubectl get svc my-service -o wide + ``` +
++ + ``` + // delete the service + kubectl delete svc my-service + + // create the service again + kubectl expose po nginx --port=80 --target-port=9376 + + // verify the label + kubectl get svc -l app=my-nginx + ``` +
++ + ``` + // delete the service + kubectl delete svc nginx + + // create service with expose command + kubectl expose po nginx --port=80 --type=NodePort + ``` +
+
+
+ ```
+ // get the clusterIP from this command
+ kubectl get svc nginx -o wide
+
+ // create temporary busybox to check the nodeport
+ kubectl run busybox --image=busybox --restart=Never -it --rm -- wget -o-
+ + ``` + apiVersion: networking.k8s.io/v1 + kind: NetworkPolicy + metadata: + name: default-deny + spec: + podSelector: {} + policyTypes: + - Ingress + ``` +
+- -``` -kubectl get pv -``` -
-- -``` -// task-pv-volume.yaml - -apiVersion: v1 -kind: PersistentVolume -metadata: - name: task-pv-volume - labels: - type: local -spec: - storageClassName: manual - capacity: - storage: 10Gi - accessModes: - - ReadWriteOnce - hostPath: - path: "/mnt/data" - -kubectl create -f task-pv-volume.yaml - -kubectl get pv -``` -
-- -``` -// task-pv-claim.yaml - -apiVersion: v1 -kind: PersistentVolumeClaim -metadata: - name: task-pv-claim -spec: - storageClassName: manual - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 3Gi - -kubectl create -f task-pv-claim.yaml - -kubectl get pvc -``` -
-- -``` -kubectl delete pvc task-pv-claim -kubectl delete pv task-pv-volume -``` -
-- -``` -// emptyDir is the volume that lasts for the life of the pod - -apiVersion: v1 -kind: Pod -metadata: - name: redis -spec: - containers: - - name: redis - image: redis - volumeMounts: - - name: redis-storage - mountPath: /data/redis - volumes: - - name: redis-storage - emptyDir: {} - -kubectl create -f redis-storage.yaml -``` -
-- -``` -// first terminal -kubectl exec -it redis-storage /bin/sh -cd /data/redis -echo 'This is called the file' > file.txt - -//open another tab -kubectl exec -it redis-storage /bin/sh -cat /data/redis/file.txt -``` -
-- -``` -kubectl delete pod redis - -kubectl create -f redis-storage.yaml -kubectl exec -it redis-storage /bin/sh -cat /data/redis/file.txt // file doesn't exist -``` -
-- -``` -kubectl create -f task-pv-volume.yaml -kubectl create -f task-pv-claim.yaml - -kubectl get pv -kubectl get pvc -``` -
-
-
-```
-// task-pv-pod.yaml
-
-apiVersion: v1
-kind: Pod
-metadata:
- name: task-pv-pod
-spec:
- volumes:
- - name: task-pv-storage
- persistentVolumeClaim:
- claimName: task-pv-claim
- containers:
- - name: task-pv-container
- image: nginx
- ports:
- - containerPort: 80
- name: "http-server"
- volumeMounts:
- - mountPath: "/usr/share/nginx/html"
- name: task-pv-storage
+1.
+
+ ```
+ kubectl get pv
+ ```
+
+
+ ```
+ // task-pv-volume.yaml
+
+ apiVersion: v1
+ kind: PersistentVolume
+ metadata:
+ name: task-pv-volume
+ labels:
+ type: local
+ spec:
+ storageClassName: manual
+ capacity:
+ storage: 10Gi
+ accessModes:
+ - ReadWriteOnce
+ hostPath:
+ path: "/mnt/data"
+
+ kubectl create -f task-pv-volume.yaml
+
+ kubectl get pv
+ ```
+
+
+ ```
+ // task-pv-claim.yaml
+
+ apiVersion: v1
+ kind: PersistentVolumeClaim
+ metadata:
+ name: task-pv-claim
+ spec:
+ storageClassName: manual
+ accessModes:
+ - ReadWriteOnce
+ resources:
+ requests:
+ storage: 3Gi
-kubectl create -f task-pv-pod.yaml
-```
-List Persistent Volumes in the cluster
+ Create a hostPath PersistentVolume named task-pv-volume with storage 10Gi, access modes ReadWriteOnce, storageClassName manual, and volume at /mnt/data and verify
+ Create a PersistentVolumeClaim of at least 3Gi storage and access mode ReadWriteOnce and verify status is Bound
+
+ + ``` + kubectl delete pvc task-pv-claim + kubectl delete pv task-pv-volume + ``` +
++ + ``` + // emptyDir is the volume that lasts for the life of the pod + + apiVersion: v1 + kind: Pod + metadata: + name: redis + spec: + containers: + - name: redis + image: redis + volumeMounts: + - name: redis-storage + mountPath: /data/redis + volumes: + - name: redis-storage + emptyDir: {} + + kubectl create -f redis-storage.yaml + ``` +
++ + ``` + // first terminal + kubectl exec -it redis-storage /bin/sh + cd /data/redis + echo 'This is called the file' > file.txt + + //open another tab + kubectl exec -it redis-storage /bin/sh + cat /data/redis/file.txt + ``` +
++ + ``` + kubectl delete pod redis + + kubectl create -f redis-storage.yaml + kubectl exec -it redis-storage /bin/sh + cat /data/redis/file.txt // file doesn't exist + ``` +
++ + ``` + kubectl create -f task-pv-volume.yaml + kubectl create -f task-pv-claim.yaml + + kubectl get pv + kubectl get pvc + ``` +
++ + ``` + // task-pv-pod.yaml + + apiVersion: v1 + kind: Pod + metadata: + name: task-pv-pod + spec: + volumes: + - name: task-pv-storage + persistentVolumeClaim: + claimName: task-pv-claim + containers: + - name: task-pv-container + image: nginx + ports: + - containerPort: 80 + name: "http-server" + volumeMounts: + - mountPath: "/usr/share/nginx/html" + name: task-pv-storage + + kubectl create -f task-pv-pod.yaml + ``` +
+