What is Kubectl
Kubectl is the command-line tool designed to interact with a Kubernetes cluster. It can manage, inspect, and operate Kubernetes clusters and resources.
Installation of Kubectl
Before you start using Kubectl, you need to install it. Below are the steps for installing Kubectl on different operating systems:
Windows
- Download the latest Kubectl executable from Kubernetes' release page.
- Add the Kubectl executable to your system’s PATH.
MacOS
You can use Homebrew to install Kubectl.
$ brew install kubectl
Linux
Use a package manager like apt
for Ubuntu/Debian or yum
for CentOS/Red Hat. Example for Ubuntu is:
$ sudo apt-get install -y kubectl
Basic Kubectl Commands
I will cover some of the most fundamental Kubectl commands.
List Resources (kubectl get)
The kubectl get
command is used for listing various resources in your cluster, such as nodes, pods, services, etc.
$ kubectl get pods
You can also use flags to alter the output format. For instance, to view the resource in YAML format:
$ kubectl get pod my-pod -o yaml
Describe Resources (kubectl describe)
The kubectl describe
command shows detailed information about a specific resource. It’s particularly useful for troubleshooting as it displays a high-level overview alongside detailed data of the specified resource.
$ kubectl describe pod my-pod
Viewing Cluster Configuration (kubectl config view)
You can use the kubectl config view
command to display the kubeconfig settings, which include the cluster configuration, user, and context.
$ kubectl config view
This command is especially helpful to check how Kubectl is configured to interact with different clusters.
Displaying Cluster Information (kubectl cluster-info)
To see the basic information about your Kubernetes cluster, including the master and services with their associated URLs, use the kubectl cluster-info
command.
$ kubectl cluster-info
This command helps you understand the high-level structure of your Kubernetes cluster.
Create Resources (kubectl create)
You can use the kubectl create
command to create a resource from a YAML or JSON configuration file.
$ kubectl create -f my-pod.yaml
Apply Configuration to Resources (kubectl apply)
The kubectl apply
command is used for applying configuration changes to resources. It is particularly useful for updating resources with configuration files.
$ kubectl apply -f my-pod.yaml
Delete Resources (kubectl delete)
The kubectl delete
command allows you to delete resources in your Kubernetes cluster.
$ kubectl delete pod my-pod
View Logs (kubectl logs)
To see the logs for a running container, you can use the kubectl logs
command. This is helpful for debugging purposes.
$ kubectl logs my-pod
Execute Commands in a Container (kubectl exec)
The kubectl exec
command allows you to execute commands inside a container that is part of a pod.
To list files in the root directory of a container:
$ kubectl exec my-pod -- ls /
Accessing a Shell in a Container (kubectl exec -it)
You can access a shell inside a container using the following command:
$ kubectl exec -it my-pod -- /bin/bash
Exposing Services (kubectl expose)
The kubectl expose
command is used to expose pods to the public internet.
$ kubectl expose deployment my-deployment --type=LoadBalancer --port=8080
Scaling Resources (kubectl scale)
The kubectl scale command allows you to scale up or down the number of replicas of a resource.
$ kubectl scale deployment my-deployment --replicas=3
References