Cover image for The Top 10 Kubectl Commands Every DevOps Engineer Needs to Know
User avatar
Posted on

The Top 10 Kubectl Commands Every DevOps Engineer Needs to Know

If you are a new or experienced DevOps engineer and just got into the Kubernetes universe, here are the top 10 kubectl commands you’ll use most often and have to get familiar with.

  1. kubectl get - This command allows you to retrieve information about the objects in your Kubernetes cluster. For example, you can use kubectl get pods to get a list of all the pods in your cluster, or kubectl get deployments to get a list of all the deployments.

    Example output:

    $ kubectl get pods
    NAME                              READY   STATUS    RESTARTS   AGE
    my-app-7c5f9fd5b8-2lkf7            1/1     Running   0          22m
    my-app-7c5f9fd5b8-d8vbt            1/1     Running   0          22m

  2. kubectl describe - This command allows you to get detailed information about a specific object in your Kubernetes cluster. For example, you can use kubectl describe pod <pod-name> to get detailed information about a specific pod, including its status, IP address, and the containers it is running.

    Example output:

    $ kubectl describe pod my-app-7c5f9fd5b8-2lkf7
    Name:         my-app-7c5f9fd5b8-2lkf7
    Namespace:    default
    Priority:     0
    Node:         k8s-worker-1/10.240.0.5
    Start Time:   Sun, 15 Sep 2019 18:42:23 +0000
    Labels:       app=my-app
                  pod-template-hash=7c5f9fd5b8
    Annotations:  <none>
    Status:       Running
    IP:           10.244.2.3
    Containers:
      my-app:
        Container ID:   docker://827983f47f9ce9a7b3bbf3a7d3aee081c72fa8f2dcc69f7b6a9b9f9d03ec8c6d
        Image:          my-app:1.0.0
        Image ID:       docker-pullable://my-app@sha256:cbbf2f9a99b47fc460d422812b6a5adff7dfee951d8fa2e4a98caa0382cfbdbf
        Port:           <none>
        Host Port:      <none>
        State:          Running
          Started:      Sun, 15 Sep 2019 18:42:24 +0000
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from default-token-zfqc5 (ro)
    Conditions:
    ...

  3. kubectl create - This command allows you to create new objects in your Kubernetes cluster. For example, you can use kubectl create deployment <deployment-name> --image=<image-name> to create a new deployment that runs the specified Docker image.

    Example output:

    $ kubectl create deployment my-app --image=my-app:1.0.0
    deployment.apps/my-app created

  4. kubectl delete - This command allows you to delete objects from your Kubernetes cluster. For example, you can use kubectl delete deployment <deployment-name> to delete a deployment and all the replicas it manages.

    Example output:

    $ kubectl delete deployment my-app
    deployment.apps "my-app" deleted

  5. kubectl scale - This command allows you to change the number of replicas in a deployment. For example, you can use kubectl scale deployment <deployment-name> --replicas=3 to scale a deployment to run 3 replicas of the specified Docker image.

    Example output:

    $ kubectl scale deployment my-app --replicas=3
    deployment.apps/my-app scaled

  6. kubectl apply - This command allows you to apply changes to your Kubernetes cluster. For example, you can use kubectl apply -f <config-file.yaml> to apply the changes specified in a YAML configuration file to your cluster.

    Example output:

    $ kubectl apply -f my-app-config.yaml
    deployment.apps/my-app configured

  7. kubectl exec - This command allows you to run a command inside a container in a pod. For example, you can use kubectl exec -it <pod-name> -- bash to open a Bash shell inside a container in a specific pod.

    Example output:

    $ kubectl exec -it my-app-7c5f9fd5b8-2lkf7 -- bash
    root@my-app-7c5f9fd5b8-2lkf7:/#

  8. kubectl logs - This command allows you to retrieve the logs from a container in a pod. For example, you can use kubectl logs <pod-name> to get the logs from all the containers in a specific pod.

    Example output:

    $ kubectl logs my-app-7c5f9fd5b8-2lkf7
    2019-09-15 18:42:24,123 INFO Starting my-app version 1.0.0
    2019-09-15 18:42:24,123 INFO Listening on port 8080

  9. kubectl port-forward - This command allows you to forward a local port to a port on a pod. For example, you can use kubectl port-forward <pod-name> 8080:80 to forward local port 8080 to port 80 on the specified pod.

    Example output:

    $ kubectl port-forward my-app-7c5f9fd5b8-2lkf7 8080:80
    Forwarding from 127.0.0.1:8080 -> 80
    Forwarding from [::1]:8080 -> 80

  10. kubectl rollout - This command allows you to manage the rollout of a deployment or other resource type in a Kubernetes cluster. For example, you can use kubectl rollout status deployment <deployment-name> to check the status of a deployment:

    Example output:

    $ kubectl rollout status deployment my-deployment
    deployment "my-deployment" successfully rolled out

In my opinion these 10 commands are essential for any DevOps Engineer or anyone who works with Kubernetes. They provide the basic functionality needed to manage and deploy applications on a Kubernetes cluster. By using these commands, you can easily retrieve information about your cluster, create and delete objects, and apply changes to your applications.