Kubernetes Essential Commands

Here are some essential commands using kubectl.

1- Get worker nodes status and verify if kubernetes worker nodes are ready.

Commands Description
az aks get-credentials --resource-group aks-resgrp --name demo   Configure Cluster Creds (kube config) for Azure AKS Clusters  
kubectl get nodes   Get Worker Node Status  
kubectl get nodes -o wide   Get Worker Node Status with wide option  

2- Create Kubernetes Pod

Commands Description
kubectl run <pod-name> --image <Docker-Container-Image>   Create Kubernetes pod by providing pod name and docker container image  
Eg. kubectl run demo-pod --image asif120/demodocker:1.0.0    Replace Pod Name,Docker Container Image In above given commands
  1. Kubernetes created a pod
  2. Pulled the docker image from docker hub
  3. Created the container in the pod
  4. Started the container present in the pod

3- Get the list of pods

Commands Description
kubectl get pods   Get the list of pods  
kubectl get pods -o wide    Get the list of pods with wide option which also provide Node information on which Pod is running
kubectl get po Giving alias name for pods is po

4- Describe pods helps during troubleshooting.

Commands Description
kubectl get pods   Get the list of pods  
kubectl describe pod <Pod-Name>   Describe the pod details .Events will be shown it will be helpful during troubleshooting
Eg . kubectl describe pod demo-pod   Replace pod name in above given command.

5- Delete pod details

Commands Description
kubectl get pods   Get the list of pods  
kubectl delete pod <Pod-Name>   Delete the pod
Eg . kubectl delete pod demo-pod Replace pod name in above given command

6- Expose pods with service (Load Balancer Service) to access application externally  (from internet) .Currently we can access this application only inside worker nodes.To access it externally, we need to create a NodePort or Load Balancer Service.

Ports

  • Port: Port on which node port service listens in Kubernetes cluster internally
  • targetPort: We define container port here on which our application is running.
Commands Description
kubectl expose pod <Pod-Name>  --type=LoadBalancer --port=80 --name=<Service-Name> Expose Pod as a Service
Eg . kubectl expose pod pod-demo  --type=LoadBalancer --port=80 --name=demo-service Replace pod and service name in above commands
kubectl get service Get Service Info
kubectl get svc  Get Service Info
kubectl describe service demo-service Describe Service to get more details for troubleshooting
http://<External-IP-from-get-service-output> Access Application

Verify Azure Standard Load Balancer created for Azure AKS Cluster

  • Frontend IP Configuration
  • Load Balancing Rules
  • Azure Public IP

7- Get more pod logs for troubleshooting using kubectl

Commands Description
kubectl get po Get Pod Name
kubectl logs <pod-name>
kubectl logs demo-pod
Dump Pod logs
kubectl logs <pod-name>
kubectl logs -f demo-pod
Stream pod logs with -f option and access application to see logs

Reference for Interacting with running Pods for additional log options: kubectl Cheat Sheet | Kubernetes

8- Get YAML Output of Pod & Service

Commands Description
kubectl get pod my-first-pod -o yaml Get pod definition YAML output
kubectl get service my-first-service -o yaml Get service definition YAML output

9- Other useful commands

Commands Description
kubectl exec -it <pod-name> -- /bin/bash
kubectl exec -it demo-pod -- /bin/bash
Connect to a ngnix Container in POD and execute commands
kubectl exec -it <pod-name> -- env
kubectl exec -it demo-pod -- env
kubectl exec -it demo-pod -- ls
Execute some running individual commands in a Container
kubectl get all Get all Objects in default namespace
kubectl delete svc demo-service Delete Services
kubectl delete pod demo-pod Delete Pod

Leave a Reply

Your email address will not be published. Required fields are marked *