How to Install and Configure Kubectl: A Tutorial

Reading Time: 4 minutes

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. 

Debian/Ubuntu

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

Redhat/CentOS

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

Autofill

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

Plugins

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.yamlcreates 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.yamlchange the resource thanks to the test.yaml file.
  • $ kubectl describe -f test.yamldetailed 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 optionsA list of global parameters of the command line, applied to all commands. 

Conclusion

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!

What is a Virtual Machine? A Tutorial

Reading Time: 7 minutes
What is a Virtual Machine?

A virtual machine is a simulated computer system which runs on a physical computer. In other words, a virtual machine is a computer inside a computer. Virtual machines allocate memory, a virtual CPU, disk storage space, and a network interface. This means that we can have a Windows computer that runs multiple virtual machines composed of Ubuntu Linux, macOS, Windows 10, Solaris, and CentOS, and they will all be completely separate from our parent Windows operating system.

Continue reading “What is a Virtual Machine? A Tutorial”

How to Install Minikube On CentOS

Reading Time: 5 minutes

What Is Minikube?

Minikube is the name of a software program written in Go, which can build a local Kubernetes cluster on a single host. It uses a meager amount of resources to run a mini Kubernetes deployment. Minikube is mainly used for testing purposes using different scenarios or versions of Kubernetes

Continue reading “How to Install Minikube On CentOS”

Virtualization vs. Containerization

Reading Time: 6 minutes

With the constant technological development we are witnessing in the world of hosting, it is necessary to familiarize ourselves with the terminology and meaning of the systems we are using. An important distinction that we need to make is between virtual machines and containers, as they are both widely used in the hosting industry, and yet they are often confused.

Continue reading “Virtualization vs. Containerization”

The Benefits Of Containerization

Reading Time: 3 minutes

Fast, Portable and Secure.

Containerization is a technology that is taking the IT world by storm! It’s no surprise considering the considerable benefits of shifting to a container based deployment.

container ship

Today we’ll be discussing the top benefits of containers, and why you might want to consider containers for your next web app. Let’s dig in!

Continue reading “The Benefits Of Containerization”