- Understand Kubernetes API Primitives
- Create and Configure Basic Pods
List all the namespaces in the cluster
kubectl get namespaces
kubectl get ns
List all the pods in all namespaces
kubectl get po --all-namespaces
List all the pods in the particular namespace
kubectl get po -n <namespace name>
List all the services in the particular namespace
kubectl get svc -n <namespace name>
List all the pods showing name and namespace with a json path expression
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}"
Create an nginx pod in a default namespace and verify the pod running
// creating a pod
kubectl run nginx --image=nginx --restart=Never
// List the pod
kubectl get po
Create the same nginx pod with a yaml file
// 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
Output the yaml file of the pod you just created
kubectl get po nginx -o yaml
Output the yaml file of the pod you just created without the cluster-specific information
kubectl get po nginx -o yaml --export
Get the complete details of the pod you just created
kubectl describe pod nginx
Delete the pod you just created
kubectl delete po nginx
kubectl delete -f nginx-pod.yaml
Delete the pod you just created without any delay (force delete)
kubectl delete po nginx --grace-period=0 --force
Create the nginx pod with version 1.17.4 and expose it on port 80
kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80
Change the Image version to 1.15-alpine for the pod you just created and verify the image version is updated
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
Change the Image version back to 1.17.1 for the pod you just updated and observe the changes
kubectl set image pod/nginx nginx=nginx:1.17.1
kubectl describe po nginx
kubectl get po nginx -w # watch it
Check the Image version without the describe command
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
Create the nginx pod and execute the simple shell on the pod
// creating a pod
kubectl run nginx --image=nginx --restart=Never
// exec into the pod
kubectl exec -it nginx /bin/sh
Get the IP Address of the pod you just created
kubectl get po nginx -o wide
Create a busybox pod and run command ls while creating it and check the logs
kubectl run busybox --image=busybox --restart=Never -- ls
kubectl logs busybox
If pod crashed check the previous logs of the pod
kubectl logs busybox -p
Create a busybox pod with command sleep 3600
kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600"
Check the connection of the nginx pod from the busybox pod
kubectl get po nginx -o wide
// check the connection
kubectl exec -it busybox -- wget -o- <IP Address>
Create a busybox pod and echo message ‘How are you’ and delete it manually
kubectl run busybox --image=nginx --restart=Never -it -- echo "How are you"
kubectl delete po busybox
Create a busybox pod and echo message ‘How are you’ and have it deleted immediately
// notice the --rm flag
kubectl run busybox --image=nginx --restart=Never -it --rm -- echo "How are you"
Create an nginx pod and list the pod with different levels of verbosity
// 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
List the nginx pod with custom columns POD_NAME and POD_STATUS
kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state"
List all the pods sorted by name
kubectl get pods --sort-by=.metadata.name
List all the pods sorted by created timestamp
kubectl get pods--sort-by=.metadata.creationTimestamp