Javatpoint Logo
Javatpoint Logo

Install Kubectl on Ubuntu

Introduction to Kubectl

Kubernetes offers a command-line tool to communicate with the control plane of the Kubernetes cluster with the help of the Kubernetes API. The tool is called kubectl.

Kubectl finds a file that is known as config in the directory, i.e., $HOME/.kube for configuration. We can define other files of kubeconfig by setting the --kubeconfig flag or KUBECONFIG environment variable.

From the point of view of a user, kubectl is our cockpit for controlling Kubernetes. It permits us to perform all possible operations of Kubernetes. From the point of view of a technical user, kubectl is the client for the Kubernetes API. The API is the actual user interface of Kubernetes.

Kubernetes is completely controlled by this API. It means that all Kubernetes operations are disclosed as an API endpoint and could be run by an HTTP request to the endpoint. The primary aim of kubectl is to consequently implement HTTP requests for the Kubernetes API.

Syntax of kubectl

We can use the below syntax to run the commands of kubectl from our terminal:

Explanation:

Command- It describes the operation that we wish to implement on multiple resources.

For instance, create, describe, delete, get, etc.

TYPE- It describes the type of the resource. These types of resources are case-insensitive and we can define the abbreviated, singular, or plural forms.

For instance, the below commands generate the same result:

NAME- It describes the resource name. The names of the resource are case-sensitive. Details for every resource are shown if a name is missing.

For instance, kubectl get pods.

We can define all resources by name and type or define multiple files when implementing an operation on one or more resources:

  • To individually define more than one resource by types:

For example, kubectl get pod/example-pod1 replicationcontroller/example-rc1

  • To define resources by name and type:

For group resources when they're all of the similar types:

For example, kubectl get pod example-pod1 example-pod2

flags- It describes optional flags.

For instance, we can use the --server or -s flags for specifying the port and address of the Kubernetes API server.

Note: Flags override the related environment variables and default values that we define from the command line.

Namespace overrides and in-cluster authentication

First of all, kubectl will by default determine if it's running in a pod, and hence in a cluster. It begins by inspecting the environment variables, i.e., KUBERNETES_SERVICE_PORT and KUBERNETES_SERVICE_HOST, and the presence of a service account token file over /var/run/secrets/kubernetes.io.serviceaccount/token. In-cluster authentication is pretended if each three is found.

For maintaining backward compatibility, when the environment variable, i.e., POD_NAMESPACE is set at the time of in-cluster authentication, then it would override the namespace (default) from a service account token. The tools or manifests depending on the defaulting of the namespace will be impacted by it.

POD_NAMESPACE environment variable. On namespace resources, cli operations will default to the value of a variable if the environment variable is set.

How kubectl managed tokens of ServiceAccount

If:

  • The environment variable, KUBERNETES_SERVICE_PORT is set
  • The environment variable, KUBERNETES_SERVICE_HOST is set
  • There is a token file of the Kubernetes service account located at /var/run/secrets/kubernetes.io.serviceaccount/token

We do not specify any namespace explicitly on the command line of kubectl

Then, kubectl pretends it's running in our cluster. The tool, i.e., kubectl finds a namespace of the ServiceAccount (it is similar to the pad namespace) and operates against the namespace. It is distinct from what's outside happens to the cluster. The command of kubectl operates against the namespace (default) if kubectl outside runs a cluster and we don't define a namespace.

Operations of kubectl

The following are some operations of kubectl along with their syntax and description:

Install Kubectl on Ubuntu

alpha- It lists the present commands that are related to the features of alpha that are not activated by default in the Kubernetes cluster.

Syntax:

annotate- It updates or adds the annotations of multiple resources.

Syntax:

api-resources- It lists the resources of API that are present.

Syntax:

api-versions- It lists the versions of API that are present.

Syntax:

apply- It applies the change of a configuration to a resource using a stdin or file.

Syntax:

attach- It attaches to an active container either to interact with the stdin (container) or view the final stream.

Syntax:

auth- Inspect authorization.

Syntax:

autoscale- It automatically scales the group of pods that are handled by a replication controller.

Syntax:

certificate- Change certificate resources.

Syntax:

cluster-info- It shows the information of the endpoint about the services and master in the cluster.

Syntax:

completion- Final code of shell completion for the described shell (zsh or bash).

Syntax:

config- It changes kubeconfig files.

Syntax:

convert- It transforms config files between distinct API versions. Both JSON and YAML formats are accepted.

Important: It needs a plugin, i.e., kubectl-convert to be installed.

Syntax:

cordon- It marks the node as unscheduled.

Syntax:

cp- It copies directories and files from and to containers.

Syntax:

create- It create multiple resources from a stdin or file.

Syntax:

delete- It deletes resources either through a stdin, file, or specifying label names, selectors, resources, or resource selectors.

Syntax:

describe- It shows the detailed state of multiple resources.

Syntax:

diff- Diff file or stdin against live configuration.

Syntax:

Install kubectl on Ubuntu

Kubectl is a tool that is open-source and essential in container orchestration. Kubernetes operates by managing and orchestrating clusters at scale around several cloud environments or on-premise servers.

Clusters are a group of hosts meant to run containerized services and applications. A cluster requires a minimum of two different nodes to operate- one worker node and a master node. We have an option for expanding the cluster with as various worker nodes as needed keeping scalability in mind.

In Kubernetes, a node refers to the server. A server is a master node that handles the cluster state. The worker nodes are the servers that execute the workloads- typically, these are containerized services and applications.

Prerequisites

We will need to arrange two servers, executing on the Ubuntu system. For Kubernetes, the minimum requirement of the system is 2 CPUs and 2 GB RAM for the best performance. One server will be a master node, then another one will be a worker node.

  • Ensure that we add the user along with sudo privileges on the nodes that we will apply to execute the commands.
  • Network connectivity- In the cluster, the servers should be capable of communicating. They will be by default linked to the Internet using a public IP when we deploy our Virtual Machines from CloudSigma. We may need to edit our file, i.e., /etc/hosts in all servers, and appropriately connect them if we are working through a local network.
  • We will need to enable and install Docker on all nodes. Kubernetes depends on a container runtime for running containers within the pods. Docker will give the runtime environment required by Ubuntu.

Enable snaps and install kubectl

A snap is the applications packaged with each of their dependency to execute on every famous Linux distro from an individual build. Automatically, they gracefully update and rollback. Snaps are installable and discoverable from the Snap Store and the app store along with millions of audiences.

Enable snapd

If we are running Focal Fossa (Ubuntu 20.04), Bionic Beaver (Ubuntu 18.04), and Xenial Xerus (Ubuntu 16.04), we don't require to do anything. A snap is installed and ready to proceed.

For Ubuntu versions between Wily Werewolf (Ubuntu 15.10) and Trusty Tahr (Ubuntu 14.04 LTS), as well as flavors of Ubuntu that by default do not include snap, snap could be installed using the Ubuntu Software Center by finding for snapd. Snapd could be alternatively installed with the help of the command-line:


Install Kubectl on Ubuntu

Install Kubectl on Ubuntu

Either back in and log out, or restart our system, to ensure the paths of snap are correctly updated.

Installing kubectl

We can simply run the following command for installing kubectl:


Install Kubectl on Ubuntu

Uninstalling Kubectl

We can remove or uninstall kubectl by using the following command in the terminal window:


Install Kubectl on Ubuntu
Next TopicTLP Ubuntu





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA