What is Kubectl?
Kubectl is a command-line tool for Kubernetes. It allows us to execute Kubernetes operations via the API. We can use Kubectl to deploy apps, check logs as well as manage all the other resources of the cluster.
Kubernetes uses an HTTP-based REST API which is the actual Kubernetes user interface employed to manage it. This means that every Kubernetes operation is represented as an API endpoint and can be carried out based on an HTTP-request sent to the endpoint.
In this article, we will review Kubectl, and outline its installation, configuration, and use.
What is Kubernetes?
The name Kubernetes has its origins from the original Greek term for helmsman or pilot. Kubernetes, or ‘k8s’ (pronounced “Kate’s”) is an open-source software tool that was created by Google and is used for scaling, deploying and coordinating containerized applications into easy to manage groups. It supports multiple containerization technologies as well as orchestrates hardware virtualization.
To manage a Kubernetes server cluster effectively, we utilize kubectl as the command-line tool of choice. Basically, kubectl communicates with the master Kubernetes node(s) which in turn submits commands to the worker nodes to manage the cluster. A Kubernetes cluster basically consists of two types of resources.
- Master server – a master server organizes the cluster
- Node server – Nodes are the workers that contain and run the applications
Each node contains a Kubelet, which is the agent for managing the node and communicating with the master. We can use kubectl to deploy, explore, review and remove Kubernetes objects (like nodes, images or containers).
Initially, Kubernetes was designed and developed by Google engineers to employ and utilize containers for its workload management. Google generates more than 2 billion containers deployments a week which was provided for by their internal platform code-named Borg (the predecessor to Kubernetes). During Borg’s development, the experience gained was one of the main factors that influenced a significant part of Kubernetes current technologies. Currently, Kubernetes is maintained by the Cloud Native Computing Foundation.
How to Install Kubectl?
The easiest way to install kubectl is to use one of the default package managers for a Linux OS.
sudo apt-get update && sudo apt-get install -y apt-transport-https gnupg2 curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add - echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list sudo apt-get update sudo apt-get install -y kubectl
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64 enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg EOF yum install -y kubectl
Linux Source Installation
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl" chmod +x ./kubectl sudo mv ./kubectl /usr/local/bin/kubectl
Verify Kubectl Installation
To verify all install versions, we use the following command.
kubectl version --client
Kubectl Configuration Commands
To check the kubectl configuration (or to gain access to the cluster), a kubeconfig file is needed. This file is created automatically during the creation of the cluster via the kube-up.sh script. By default, the cluster configuration file is located in ~/.kube/config.
We use this command to check the state of the cluster and verify that kubectl is correctly configured.
$ kubectl cluster-info
When we see a URL-output, then kubectl is configured correctly to work with the cluster.
If the output contains the following error message, then kubectl is either misconfigured or cannot connect to the cluster.
The connection to the server <server-name:port> was refused - did you specify the right host or port?
Otherwise, when kube cluster-info command returns URL output, but we still cannot connect to the cluster, we run the following command to check whether it is appropriately configured.
$ kubectl cluster-info dump
The autofill configuration is one of the most important and convenient kubectl commands as it allows us to automatically fill in some parts of a command by simply pressing the Tab key. This feature works for subcommands, options, arguments, and more complicated entries, like resource names. This configuration works for shells like Bash and Zsh. A full tutorial on how to configure that can be found on the official web page.
Beginning with version 1.12, kubectl supports plugins which let us expand its functionality. Kubectl plugins are simply executable files using a naming convention which begins with kubectl-*. For example, a plugin called hello, is named kubectl-hello. To install a kubectl plugin, we move the file to any location with our PATH variable and make it executable.
Kubectl Command Syntax
The syntax of a kubectl commands is as follows.
$ kubectl [command] [TYPE] [NAME] [flags]
- command - defines the operation carried out on one or more resources, e.g., create, get, or delete.
- TYPE - characterizes the type of resource. It is case-insensitive and used in a single, plural, or as an abbreviated manner.
- name - denotes the identity of a resource and is case-sensitive.
- flags - A flag sets options on a command.
Basic Kubectl Commands
There are multiple commands that can be used, but we are going to look through some basic ones to have a general understanding and see some examples. We are going to use test.yaml file noted below.
kubectl exec POD_NAME -- bash -c "date && echo 1" kubectl exec POD_NAME -- bash -c "date && \ echo 1 && \ echo 2"
- $ kubectl create -f test.yaml - creates resource from the file or input.
- $ kubectl run test.yaml - start a deployment.
- -f - can mention resources with a file or files, e.g., f file1 -f file2.
- $ kubectl edit -f test.yaml - change the resource thanks to the test.yaml file.
- $ kubectl describe -f test.yaml - detailed description of the state of one or several resources.
- $ kubectl delete -f test.yaml - delete resources from the file.
- $ kubectl logs test.yaml - print logs.
- $ kubectl options - A list of global parameters of the command line, applied to all commands.
In this article, we have learned what Kubernetes and kubectl are, how to install and configure kubectl, also we have provided a quick overview of some basic kubectl commands. Kubernetes is an excellent tool with many useful features.
Get Started Today!
Contact us today at 1.800.580.4985 to speak to a knowledgeable Solutions Provider who can get you the info you need, to make an informed decision right away.
Too busy to talk? Click HERE to open a quick chat with us to find out more. Would you like the information in an email you can review at your leisure? Email us today to get solid advice on which product in our lineup would best suit your business needs.
We look forward to hearing from you!
- ChatGPT Integration — How to Create a Plugin for ChatGPT
- Stable Diffusion AI Image Generator (SDXL) — Using the Web UI
- How to Install VMware Tools on Ubuntu: Step-by-Step Guide
- How to Install WordPress on Linux (AlmaLinux)
- What is CentOS? Everything You Need to Know
- Virtual Desktop Environment — Configuring Kasm Workspaces
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.