Important Kubernetes Commands In 2024

You are currently viewing Important Kubernetes Commands In 2024

Kubernetes is an open-source container orchestration system that automates the deployment, scaling, and management of containerized applications. One of the key features of Kubernetes is its command-line interface, or kubectl, which allows users to easily manage their Kubernetes clusters and the applications running on them. In this article, we will discuss some of the most important kubectl commands that every Kubernetes user should know.

kubectl get – This command retrieves information about the resources running in a Kubernetes cluster. You can use kubectl to list all the pods, services, and deployments running in a cluster, or to get specific information about a particular resource. For example, kubectl get pods will list all the pods running in a cluster, while kubectl get pods my-pod will give you detailed information about a specific pod named “my-pod”.

kubectl describe – This command is used to get detailed information about a specific resource. It is similar to kubectl get, but it provides more in-depth information, such as the current state of the resource, its events, and its associated labels and annotations. For example,

kubectl describe pods my-pod 

will give you detailed information about the pod named “my-pod”.

kubectl create – This command is used to create new resources in a Kubernetes cluster. It can be used to create pods, services, deployments, and custom resource definitions (CRDs). For example,

kubectl create -f my-pod.yaml 

will create a new pod based on the configuration specified in the “my-pod.yaml” file.

kubectl delete – This command is used to delete resources from a Kubernetes cluster. It can be used to delete pods, services, deployments, and custom resource definitions (CRDs). For example,

kubectl delete pod my-pod 

will delete the pod named “my-pod”.

kubectl logs – This command is used to view the logs of a specific pod. It is useful for troubleshooting and debugging. For example,

kubectl logs my-pod 

will display the logs of the pod named “my-pod”.

kubectl exec – This command is used to execute a command in a specific pod. This can be useful for troubleshooting and debugging, as well as for running one-off commands in a pod. For example,

kubectl exec -it my-pod -- /bin/bash 

it will open a bash shell in the “my-pod” pod, allowing you to run commands within it.

iamidentitymapping – This command is used to allow an IAM role/user to execute commands in AWS EKS.

eksctl create iamidentitymapping --cluster $CLUSTER_NAME --arn $IAM_ROLE --group "system:masters" --username "codebuild-service"

The above command to add the IAM role to aws-auth config map

kubectl scale – This command is used to scale the number of replicas of a deployment, replica set, or stateful set. For example,

kubectl scale deployment my-deployment --replicas=5 

will scale the “my-deployment” deployment to 5 replicas.

kubectl rollout – This command is used to manage and interact with the rollout of a deployment. You can use it to view the history of a deployment’s revisions, view the details of a particular revision, or roll back to a previous revision. For example,

kubectl rollout history deployment my-deployment 

will show the history of the “my-deployment” deployment.

kubectl port-forward – This command is used to forward a local port to a port on a pod running in a cluster. This allows you to access a service running in a cluster from your local machine. For example,

kubectl port-forward pod/my-pod 8080:80 

will forward the local port 8080 to port 80 on the “my-pod” pod.

aws eks update-kubeconfig – This AWS CLI command is used to update the kubeconfig for an EKS cluster, which allows you to easily access and manage the cluster using kubectl. This command requires the name of the cluster and the region where the cluster is running. For example,

aws eks update-kubeconfig --name my-cluster --region us-west-2 

updates the kubeconfig to connect to the “my-cluster” cluster in the “us-west-2” region.

aws eks create-fargate-profile – This AWS CLI command is used to create a new Fargate profile in an EKS cluster, which allows running pods on AWS Fargate. It requires the name of the profile, the name of the EKS cluster, and the subnets and security group to be used. For example,

aws eks create-fargate-profile --cluster-name my-cluster --fargate-profile-name my-profile --subnets subnet-a subnet-b --security-groups sg-a

create a fargate profile “my-profile” for the “my-cluster” cluster using the subnets “subnet-a” and “subnet-b” and security group “sg-a”

kubectl apply – This command is used to create or update resources in a Kubernetes cluster. It reads a configuration file, which can be a YAML or JSON file, and applies it to the cluster. This is useful when you have a set of resources to be deployed in a particular order, or if you have a templatized configuration that you want to reuse. For example,

kubectl apply -f my-config.yml 

creates or updates the resources defined in the “my-config.yml” file

Conclusion

These are just a few of the most important kubectl commands for managing Kubernetes clusters and the resources running on them. There are many more commands and options available, and it’s worth taking some time to learn the basics and how they can be used to manage and troubleshoot your applications.

Atiqur Rahman

I am MD. Atiqur Rahman graduated from BUET and is an AWS-certified solutions architect. I have successfully achieved 6 certifications from AWS including Cloud Practitioner, Solutions Architect, SysOps Administrator, and Developer Associate. I have more than 8 years of working experience as a DevOps engineer designing complex SAAS applications.

Leave a Reply