Table of contents
- ๐ช kubernetes Commands
- ๐จโ๐ป Check Kubectl Version
- ๐จโ๐ป Set Context for Cluster in different cloud providers
- ๐จโ๐ป kubeconfig file
- ๐จโ๐ป ๐ ๐ฎ๐ป๐ถ๐ณ๐ฒ๐๐ ๐๐ถ๐น๐ฒ - ๐๐ฟ๐ฒ๐ฎ๐๐ถ๐ผ๐ป, ๐๐ฒ๐น๐ฒ๐๐ถ๐ผ๐ป, ๐ฎ๐ป๐ฑ ๐๐ฑ๐ถ๐๐ถ๐ป๐ด ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
- ๐จโ๐ป ๐๐๐ง ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐ (๐ฆ๐๐ฎ๐๐๐)
- ๐จโ๐ป ๐๐๐ฏ๐ฒ๐ฐ๐๐น ๐๐ฒ๐๐ฐ๐ฟ๐ถ๐ฏ๐ฒ
- โ ๐๐บ๐ฝ๐ฒ๐ฟ๐ฎ๐๐ถ๐๐ฒ ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
- ๐จโ๐ป ๐๐๐ฏ๐ฒ๐ฐ๐๐น ๐๐ฟ๐ฒ๐ฎ๐๐ฒ ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
- ๐จโ๐ป ๐๐ฟ๐ฒ๐ฎ๐๐ฒ ๐ฎ ๐ฃ๐ผ๐ฑ
- ๐จโ๐ป ๐๐ฑ๐ถ๐ ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
- ๐จโ๐ป ๐๐๐ฏ๐ฒ๐ฐ๐๐น ๐ฅ๐ฒ๐ฝ๐น๐ฎ๐ฐ๐ฒ - ๐๐ผ๐ฟ๐ฐ๐ฒ
- ๐จโ๐ป ๐๐ฒ๐ฏ๐๐ด๐ด๐ถ๐ป๐ด ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
- ๐จโ๐ป ๐๐ผ๐ด๐ด๐ถ๐ป๐ด ๐ฎ๐ป๐ฑ ๐ ๐ผ๐ป๐ถ๐๐ผ๐ฟ๐ถ๐ป๐ด ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
- ๐ ๐ธ๐๐ฏ๐ฒ๐ฐ๐๐น ๐๐ต๐ฒ๐ฎ๐ ๐ฆ๐ต๐ฒ๐ฒ๐ ๐ฏ๐ ๐๐๐ฏ๐ฒ๐ฟ๐ป๐ฒ๐๐ฒ๐
๐ช kubernetes Commands
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available. Kubernetes provides you with a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, canary deployments, and more. In this blog post, I will mention Kubernetes commands which we need for most of the use-cases.
๐จโ๐ป Check Kubectl Version
This command checks the version of the installed Kubectl client
kubectl version --client
๐จโ๐ป Set Context for Cluster in different cloud providers
These commands set the context for the specified cloud-managed Kubernetes clusters, allowing subsequent commands to interact with the chosen cluster.
# Connect to EKS
aws eks --region <your-region> update-kubeconfig --name <your-cluster-name>
#Connect to AKS
az aks get-credentials --resource-group <your-resource-group> --name <your-aks-cluster-name>
#Connect to GKE
gcloud container clusters get-credentials <your-gke-cluster-name> --region <your-region>
๐จโ๐ป kubeconfig file
The kubeconfig file is a crucial part of managing Kubernetes configurations, stored at ~/.kube/config by default. It defines clusters, users, and contexts.
This command displays the name of the current Kubernetes context, which represents the cluster that kubectl commands will interact with.
# Check the current Kubernetes context
kubectl config current-context
๐จโ๐ป ๐ ๐ฎ๐ป๐ถ๐ณ๐ฒ๐๐ ๐๐ถ๐น๐ฒ - ๐๐ฟ๐ฒ๐ฎ๐๐ถ๐ผ๐ป, ๐๐ฒ๐น๐ฒ๐๐ถ๐ผ๐ป, ๐ฎ๐ป๐ฑ ๐๐ฑ๐ถ๐๐ถ๐ป๐ด ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
Kubernetes manifest file is a short YAML or JSON document that defines how a specific cluster resource should be configured and managed. It declares the desired state, allowing Kubernetes to maintain that state in the cluster.
Deployment is a blueprint for running and managing application instances, ensuring the specified number of pods are running. It also manages aspects like the container image, configurations, and enables seamless updates, scalability, and self-healing.
# Create a Deployment using Manifest file
kubectl apply -f deployment.yaml
# Delete an object using Manifest file
kubectl delete -f deployment.yaml
# Edit a Deployment using Manifest file
kubectl apply -f deployment.yaml
๐จโ๐ป ๐๐๐ง ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐ (๐ฆ๐๐ฎ๐๐๐)
These commands retrieve information about different Kubernetes resources, such as pods, services, and deployments.
# Get commands to check the status
kubectl get nodes
kubectl get pods
kubectl get services
kubectl get deployments
Get everything in the cluster (statefulset, deployment, pods, nodes, services) in default namespace
kubectl get all
kubectl get all -o yaml
kubectl get all -o wide (this command is used to give detailed information)
Get everything in the cluster (statefulset, deployment, pods, nodes, services) in particulat namespace (ex: dev)
kubectl get namespaces
kubectl get all -n dev
kubectl get all -o wide -n dev (this command is used to give detailed information)
These commands show how to retrieve information in different formats for more detailed view with better readability or automation.
# Get information in YAML format
kubectl get pods -o yaml
# Get information in JSON format
kubectl get services -o json
๐จโ๐ป ๐๐๐ฏ๐ฒ๐ฐ๐๐น ๐๐ฒ๐๐ฐ๐ฟ๐ถ๐ฏ๐ฒ
This command provides detailed information about a specific Kubernetes resource, aiding in troubleshooting and understanding its configuration.
# Describe a resource
kubectl describe pod <pod-name>
โ ๐๐บ๐ฝ๐ฒ๐ฟ๐ฎ๐๐ถ๐๐ฒ ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
Imperative commands allow quick actions without the need for manifest files.
๐จโ๐ป ๐๐๐ฏ๐ฒ๐ฐ๐๐น ๐๐ฟ๐ฒ๐ฎ๐๐ฒ ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
These commands demonstrate creating deployments with specific images and replica counts.
# Create an Nginx Deployment
kubectl create deployment nginx-deploy --image=nginx
# Create a Deployment with Redis image and 2 replicas
kubectl create deployment redis-deploy --image=redis --replicas=2
# Dry run command will not actually create the Deployment but will generate the YAML configuration file.
kubectl create deployment mongod-deploy --image mongo --replicas 3 --dry-run=client -o yaml > mongod.yaml
๐จโ๐ป ๐๐ฟ๐ฒ๐ฎ๐๐ฒ ๐ฎ ๐ฃ๐ผ๐ฑ
The kubectl run command creates a pod imperatively, allowing quick pod deployment without the need for a manifest file.
# Create a Pod
kubectl run <pod-name> --image=<image-name>
kubectl run nginx-pod --image nginx
๐จโ๐ป ๐๐ฑ๐ถ๐ ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
Edit commands are used to modify existing deployments, either imperatively or by scaling the number of replicas.
# Edit a Deployment imperatively
kubectl edit "type" "name"
kubectl edit deployment <deployment-name>
# Scale a Deployment imperatively
kubectl scale deployment <deployment-name> --replicas=3
๐จโ๐ป ๐๐๐ฏ๐ฒ๐ฐ๐๐น ๐ฅ๐ฒ๐ฝ๐น๐ฎ๐ฐ๐ฒ - ๐๐ผ๐ฟ๐ฐ๐ฒ
This command forcefully updates an object by replacing it with a new configuration.
# Replace and force update an object
kubectl replace --force -f <manifest_file>
๐ Certain fields of a resource, like its name or type, cannot be edited using kubectl edit as they are immutable. For such cases, the kubectl replace --force command is used to enforce an update by deleting and recreating the resource with the new configuration.
๐จโ๐ป ๐๐ฒ๐ฏ๐๐ด๐ด๐ถ๐ป๐ด ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
These commands demonstrate debugging techniques, including viewing pod logs and executing commands inside a container.
# Create a Deployment for debugging
kubectl create deployment debug-deploy --image=alpine
# View logs of a pod
kubectl logs <pod-name>
# Execute a command inside a container
kubectl exec -it <pod-name> -- "command"
kubectl exec -it <pod-name> -- /bin/bash
kubectl exec -it <pod-name> -- ls/etc/
๐จโ๐ป ๐๐ผ๐ด๐ด๐ถ๐ป๐ด ๐ฎ๐ป๐ฑ ๐ ๐ผ๐ป๐ถ๐๐ผ๐ฟ๐ถ๐ป๐ด ๐๐ผ๐บ๐บ๐ฎ๐ป๐ฑ๐
These commands showcase how to view logs of a pod and display resource usage information for pods.
# View logs of a pod
kubectl logs <pod-name>
# Display Resource Usage (CPU and Memory) of Pods
kubectl top pods
kubectl top nodes
๐ ๐ธ๐๐ฏ๐ฒ๐ฐ๐๐น ๐๐ต๐ฒ๐ฎ๐ ๐ฆ๐ต๐ฒ๐ฒ๐ ๐ฏ๐ ๐๐๐ฏ๐ฒ๐ฟ๐ป๐ฒ๐๐ฒ๐
Kubernetes Quick Reference: https://kubernetes.io/docs/reference/kubectl/quick-reference/
Getting Started: https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#-em-deployment-em-
\...................................................................................................................................................
The above information is up to my understanding. Suggestions are always welcome. Thanks for reading this article.
#docker #aws #cloudcomputing #Devops #kubernetes #devsecops #TrainWithShubham #90daysofdevopsc #happylearning
Follow for many such contents: