2022-06-02

Kubectl

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

  1. Download the latest Kubectl executable from Kubernetes' release page.

https://kubernetes.io/docs/tasks/tools/install-kubectl-windows/

  1. Add the Kubectl executable to your system’s PATH.

MacOS

You can use Homebrew to install Kubectl.

bash
$ brew install kubectl

Linux

Use a package manager like apt for Ubuntu/Debian or yum for CentOS/Red Hat. Example for Ubuntu is:

bash
$ 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.

bash
$ kubectl get pods

You can also use flags to alter the output format. For instance, to view the resource in YAML format:

bash
$ 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.

bash
$ 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.

bash
$ 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.

bash
$ 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.

bash
$ 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.

bash
$ kubectl apply -f my-pod.yaml

Delete Resources (kubectl delete)

The kubectl delete command allows you to delete resources in your Kubernetes cluster.

bash
$ 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.

bash
$ 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:

bash
$ 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:

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

Exposing Services (kubectl expose)

The kubectl expose command is used to expose pods to the public internet.

bash
$ 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.

bash
$ kubectl scale deployment my-deployment --replicas=3

References

https://kubernetes.io/docs/reference/kubectl/

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!