๐ŸŽกKubernetes commands you
should definitely know!

๐ŸŽกKubernetes commands you should definitely know!

ยท

5 min read

Table of contents

๐ŸŽช 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.

imperative commands

๐Ÿ‘จโ€๐Ÿ’ป ๐—ž๐˜‚๐—ฏ๐—ฒ๐—ฐ๐˜๐—น ๐—–๐—ฟ๐—ฒ๐—ฎ๐˜๐—ฒ ๐—–๐—ผ๐—บ๐—บ๐—ฎ๐—ป๐—ฑ๐˜€

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:

LinkedIn: linkedin.com/in/dushyant-kumar-dk

Blog: dushyantkumark.hashnode.dev

ย