Home Kubernetes Kubernetes commands? No more mistakes

Kubernetes commands? No more mistakes

by Mohamed Imthiyaz
kubernetes commands

As a powerful and popular container orchestration platform, Kubernetes offers a wide range of commands that can be used to manage and monitor your containerized applications.

In this blog post, we will cover some of the most important and useful Kubernetes commands, along with brief examples of how to use them.

kubectl

The kubectl command-line tool is the primary way to interact with a Kubernetes cluster. It can be used to create, modify, and delete resources in a cluster, as well as to view and manage the cluster itself.

Here are some examples of useful kubectl commands:

kubectl get: This command is used to retrieve information about resources in a Kubernetes cluster. For example,

kubectl get svc     //  –  To list one or more services
kubectl get deployments     //  –  To list all the deployments
kubectl get pods    //  –  To list all the pods in a cluster.
kubectl get pods -o=json      //  – Output format in JSON.
kubectl get pods -o=yaml      // – Output format in YAML.
kubectl get pods -o=wide      // – Output in a plain-text formate for pods, name of the node and any additional information
kubectl get pods  -n namespace     // – To get pods from a particular namespace
kubectl get rc  -n namespace     // – To list the replication controllers by namespace

kubectl describe: This command provides detailed information about a specific resource in a cluster. For example, you can use

kubectl describe pod my-pod            //   –To view detailed information about a pod named my-pod.
kubectl describe  nodes my-node   // – To view detailed information about the node named my-node
kubectl describe  services   // – To view detailed information and state about a service

If you are using vSphere with Tanzu or have deployed Tanzu Kubernetes cluster you can use

kubectl describe  tkc cluster-name -n namespace  // – This will give you a detailed view of your Tanzu kubernetes cluster

kubectl apply: This command is used to create or update resources in a Kubernetes cluster. It takes a YAML or JSON configuration file as input, which defines the desired state of the resource. For example,

kubectl apply -f deployment.yaml  // – To create or update a deployment according to the config file deployment.yaml

kubectl delete: This command is used to delete resources from a Kubernetes cluster. For example,

kubectl delete deployement my-deployment  // – To delete a deployment named my-deployment

Secrets

Create a secret

kubectl create secret

List secrets

kubectl get secrets

List details about secrets

kubectl describe secrets

Delete a secret

kubectl delete secret secret_name

kubeadm

The kubeadm command is used to bootstrap a Kubernetes cluster. It can be used to initialize a cluster, join nodes to the cluster, and upgrade the cluster to a new version.

Here are some examples of useful kubeadm commands:

  • kubeadm init: This command is used to initialize a Kubernetes cluster. It creates the control plane and initializes the cluster to a ready state.
  • kubeadm join: This command is used to add a new node to an existing Kubernetes cluster. It is typically run on the new node, and it connects the node to the cluster and installs the necessary components.
  • kubeadm upgrade: This command is used to upgrade an existing Kubernetes cluster to a new version. It performs a rolling upgrade of the control plane components and ensures that the cluster remains available and functional throughout the upgrade process.

kubectl logs

The kubectl logs command is used to view the logs of a specific pod in a Kubernetes cluster. This can be useful for troubleshooting and debugging applications that are running in the cluster.

Here is an example of how to use the kubectl logs command:

kubectl logs my-pod

This command will output the logs of the pod named my-pod. You can also specify a specific container within the pod by using the -c flag, like this:

kubectl logs my-pod -c

Optional: Enable kubectl commands autocompletion

For bash, you can install it with the below command

apt-get install bash-completion
echo 'source <(kubectl completion bash)' >>~/.bashrc

More Detailed document for autocompletion

Few references are from official kubernetes docs, hope this helps you in your day around with kubernetes.

You may also like

Leave a Comment