Kubernetes (Kubectl) Cheatsheet: Essential Commands for Managing Your Clusters

Featured Image
Kubernetes

Share Post Now :

HOW TO GET HIGH PAYING JOBS IN AWS CLOUD

Even as a beginner with NO Experience Coding Language

Explore Free course Now

Table of Contents

Loading

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It provides a flexible and reliable infrastructure for managing microservices and distributed systems. However, working with Kubernetes can be challenging for developers and system administrators due to its complexity and the number of commands required to manage Kubernetes clusters. In this blog post, we will provide you with a Kubernetes cheat sheet containing essential commands to help you manage your Kubernetes clusters more efficiently.

Cluster Management

1. Display endpoint details for the cluster’s master and services.

$ kubectl cluster-info

2. Show the Kubernetes version that is active on the server and client

$ kubectl version

3. Get the cluster’s configuration

$ kubectl config view

4. List the available API resources

$ kubectl api-resources

5. List everything

$ kubectl get all --all-namespaces

👉 Learn How to create a K8s cluster.

Node Management

1. List the nodes

$ kubectl get node

2. Update the taints on nodes

$ kubectl taint node <node_name>

3. Delete a node

$ kubectl delete node <node_name>

👉 Learn more about Kubernetes Architecture & Components

Pod Management

1. List pods

$ kubectl get pod

2. Delete a pod

$ kubectl delete pod <pod_name>

3. See detailed state of a pods

$ kubectl describe pod <pod_name>

4. Create a pod

$ kubectl create pod <pod_name>

5. Run a command for a container inside a pod

$ kubectl exec <pod_name> -c <container_name> <command>

6. Get interactive shell on a  pod

$ kubectl exec -it <pod_name> /bin/sh

7. See resource usage (CPU/Memory/Storage) for pods

$ kubectl top pod

8. Add/update the annotations of a pod

$ kubectl annotate pod <pod_name> <annotation>

9. Add/update the label of a pod

$ kubectl label pod <pod_name>

👉 Learn more about K8s Pods

Replication Controllers

1. View the list of replication controllers

$ kubectl get rc

2. View the list of replication controllers by namespace

$ kubectl get rc --namespace=”<namespace_name>”

ReplicaSet Management

List all ReplicaSets

$ kubectl get replicasets

2. View the detailed state of one or more ReplicaSets

$ kubectl describe replicasets <replicaset_name>

3. Scale a ReplicaSet

$ kubectl scale --replicas=<expected_replica_num> replicaset <name>

Deployment Management

1. List the deployments

$ kubectl get deployment

2. Show the precise status of single or multiple deployments.

$ kubectl describe deployment <deployment_name>

3. Edit and update the deployment.

$ kubectl edit deployment <deployment_name>

4. Create a new deployment

$ kubectl create deployment <deployment_name>

5. Delete a deployment

$ kubectl delete deployment <deployment_name>

6. Check the rollout status of a deployment

$ kubectl rollout status deployment <deployment_name>

7. Display Resource usage (CPU/Memory/Storage) for nodes

$ kubectl top node

8. See resource allocation per node

$ kubectl describe nodes | grep Allocated -A 5

9. List the pods running on a node

$ kubectl get pods -o wide | grep <node_name>

10. To annotate a node

$ kubectl annotate node <node_name>

11. Add or update the labels of a node

$ kubectl label nodes <your-node-name> <label>

12. Scale a Deployment

$ kubectl scale deployment <deployment-name> --replicas=<number-of-replicas>

👉 Learn more about K8s Deployments

Secrets

1. Create a secret

$ kubectl create secret <name>

2. List secrets

$ kubectl get secrets

3. View details about secrets

$ kubectl describe secrets <name>

4. Delete a secret

$ kubectl delete secret <secret_name>

Services

1. List the services

$ kubectl get services

2. View the detailed state of a service

$ kubectl describe services <name>

3. Expose a replication controller, deployment or pod as a new Kubernetes service

$ kubectl expose deployment [deployment_name]

4. Edit/update the definition of a service

$ kubectl edit service <name>

👉 Learn more about Networking & Services

Common Options

Commands in Kubectl can include optional flags. Here are a few examples of the most popular and helpful ones.

1. –o Format of output. (Suppose you wanted to list all of the pods in ps output format with additional information.)

$ kubectl get pods -o wide

2. Create any resource(pod/replicaset/deployment, etc) using a yaml/json file.

$ kubectl apply -f <xyz.yaml>

Conclusion

Kubernetes is a powerful tool for managing containerized applications, but with great power comes great complexity. The K8s cheatsheet commands presented in this blog post are just a small subset of the commands available for managing Kubernetes clusters, but they cover some of the most essential operations that developers and system administrators need to perform on a regular basis. By mastering these commands, you can manage your Kubernetes clusters more efficiently, troubleshoot issues more effectively, and get more value out of your containerized applications. We hope this cheat sheet helps you in your Kubernetes journey and makes your life a little bit easier.

Related/References

Join FREE Masterclass

Discover the Power of Kubernetes, Docker & DevOpsJoin Our Free Masterclass. Unlock the secrets of Kubernetes, Docker, and DevOps in our exclusive, no-cost masterclass. Take the first step towards building highly sought-after skills and securing lucrative job opportunities. Click on the below image to Register Our FREE Masterclass Now!

Mastering Kubernetes Docker & DevOps

 

Picture of mike

mike

I started my IT career in 2000 as an Oracle DBA/Apps DBA. The first few years were tough (<$100/month), with very little growth. In 2004, I moved to the UK. After working really hard, I landed a job that paid me £2700 per month. In February 2005, I saw a job that was £450 per day, which was nearly 4 times of my then salary.