Kubectl

Kubectl is a command line interface for running commands against Kubernetes clusters.

Installing

The kubectl version has to be within one minor version difference of the Kubernetes cluster. For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master.

Kubectl can be installed on Ubuntu, Debian, CentOS, RedHat, MacOS and Windows operating systems.

sudo apt-get update && sudo apt-get install -y apt-transport-https
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

For further information about Kubectl installation method, please refer to the Kubernetes documentation.

Completion

To easy manage the Kubernetes resources thanks to the command line Kubectl, the shell completion can be added to the shell profile to easily navigate in command line.

# Installing bash completion on macOS using homebrew
## If running Bash 3.2 included with macOS
brew install bash-completion
## or, if running Bash 4.1+
brew install bash-completion@2
## If kubectl is installed via homebrew, this should start working immediately.
## If you've installed via other means, you may need add the completion to your completion directory
kubectl completion bash > $(brew --prefix)/etc/bash_completion.d/kubectl


# Installing bash completion on Linux
## Load the kubectl completion code for bash into the current shell
source <(kubectl completion bash)
## Write bash completion code to a file and source if from .bash_profile
kubectl completion bash > ~/.kube/completion.bash.inc
printf "
# Kubectl shell completion
source '$HOME/.kube/completion.bash.inc'
" >> $HOME/.bash_profile
source $HOME/.bash_profile

# Load the kubectl completion code for zsh[1] into the current shell
source <(kubectl completion zsh)
# Set the kubectl completion code for zsh[1] to autoload on startup
kubectl completion zsh > "${fpath[1]}/_kubectl"

A project called kube-shell give an alternative to the configuration of the autocompletion of kubectl. Kube-shell integrate shell for working with the Kubernetes CLI. Under the hood kube-shell still calls kubectl. KuIt aims to provide ease-of-use of kubectl and increasing productivity.

Syntax

Kubectl is a powerful tool to manage each object on a Kubernetes cluster. The command has a simple and unique syntax to manage everything :

  • command : specifies the operation that you want to perform on one or more resources (create, get, describe, delete)

  • type : specifies the resource type. Resource types are case-insensitive and you can specify the singular, plural, or abbreviated forms

  • name : specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed

  • flags : specifies optional flags.

Operations

The following table includes short descriptions and general syntax for all kubectl operations :

Operation

Description

annotate

Update the annotations on a resource

api-resources

Print the supported API versions on the server, in the form of "group/version"

apply

Apply a configuration to a resource by filename or stdin

attach

Attach to a running container

auth

Inspect authorization

autoscale

Auto-scale a Deployment, ReplicaSet, or ReplicationController

certificate

Modify certificate resources

cluster-info

Display cluster info

config

Modify kubeconfig files

convert

Convert config files between different API versions

cordon

Mark node as unschedulable

cp

Copy files and directories to and from containers

create

Create a resource from a file or from stdin

delete

Delete resources by filenames, stdin, resources and names, or by resources and label selector

describe

Show details of a specific resource or group of resources

drain

Drain node in preparation for maintenance

edit

Edit a resource on the server

exec

Execute a command in a container

explain

Documentation of resources

expose

Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service

get

Display one or many resources

label

Update the labels on a resource

logs

Print the logs for a container in a pod

patch

Update field(s) of a resource using strategic merge patch

plugin

Runs a command-line plugin

port-forward

Forward one or more local ports to a pod

proxy

Run a proxy to the Kubernetes API server

replace

Replace a resource by filename or stdin

rolling-update

Perform a rolling update of the given ReplicationController (Deprecated)

rollout

Manage the rollout of a resource

run

Run a particular image on the cluster

scale

Set a new size for a Deployment, ReplicaSet, Replication Controller, or Job

set

Set specific features on objects

taint

Update the taints on one or more nodes

top

Display Resource (CPU/Memory/Storage) usage

uncordon

Mark node as schedulable

version

Print the client and server version information

Resource Types

The following table includes a list of all the supported resource types and their abbreviated aliases :

Resource type

Abbreviated alias

all

all

certificatesigningrequests

csr

clusterrolebindings

clusterrolebindings

clusterroles

clusterroles

componentstatuses

cs

configmaps

cm

controllerrevisions

controllerrevisions

cronjobs

cronjobs

customresourcedefinition

crd

daemonsets

ds

deployments

deploy

endpoints

ep

events

ev

horizontalpodautoscalers

hpa

ingresses

ing

jobs

jobs

limitranges

limits

namespaces

ns

networkpolicies

netpol

nodes

no

persistentvolumeclaims

pvc

persistentvolumes

pv

poddisruptionbudgets

pdb

podpreset

podpreset

pods

po

podsecuritypolicies

psp

podtemplates

podtemplates

replicasets

rs

replicationcontrollers

rc

resourcequotas

quota

rolebindings

rolebindings

roles

roles

secrets

secrets

serviceaccounts

sa

services

svc

statefulsets

sts

storageclasses

sc

Basic commands

Create

Create a resource from a file or from stdin.

Delete

Delete resources by filenames, stdin, resources and names, or by resources and label selector.

Edit

Edit a resource from the default editor.

Expose

Expose a resource as a new Kubernetes service.

Get

Display one or many resources.

Run

Create and run a particular image, possibly replicated.

Set

Configure application resources.

Deploy commands

Autoscale

Creates an autoscaler that automatically chooses and sets the number of pods that run in a kubernetes cluster.

Rollout

Manage the rollout of a resource.

Scale

Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet.

Cluster management commands

Cluster-info

Display addresses of the master and services with label kubernetes.io/cluster-service=true To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Cordon / Uncordon

Mark node as (un)schedulable.

Drain

Drain node in preparation for maintenance.

Taint

Update the taints on one or more nodes

Top

Display Resource (CPU/Memory/Storage) usage.

Troubleshooting and debugging commands

Describe

Show details of a specific resource or group of resources.

Exec

Execute a command in a container.

Logs

Print the logs for a container in a pod or specified resource. If the pod has only one container, the container name is optional.

Proxy

Creates a proxy server or application-level gateway between localhost and the Kubernetes API Server. It also allows serving static content over specified HTTP path. All incoming data enters through one port and gets forwarded to the remote kubernetes API Server port, except for the path matching the static content path.

Advanced commands

Apply

Apply a configuration to a resource by filename or stdin. The resource name must be specified. This resource will be created if it doesn't exist yet. To use 'apply', always create the resource initially with either 'apply' or 'create --save-config'.

Settings commands

Label

Update the labels on a resource.

Other commands

Config

Modify kubeconfig files using subcommands like "kubectl config set current-context my-context".

Version

Print the client and server version information for the current context.

Customization

Kubectl has some limitation when we talk about management of multiple clusters with multiple context with multiple namespace. Sometimes it is more convenient to be faster in the command line.

Namespace reminder

Some tools have been developed to let the terminal remember the current namespace where the kubectl command has to be execute. Here are convenient helper tools :

  • kubensx a simple tool to easily switch between context, user and namespace in command line.

  • change-ns is a plugin that can be installed with the kubectl Krew package manager to remember the current namespace.

Manage aliases

Like each command line, the autocompletion sometimes is not enough to be faster as possible in the maintenance of the cluster objects.

Manage some Kubectl aliases can be a good alternative to the autocompletion. This Github project defined automatically some useful aliases to easily manage the Kubernetes objects.

Here are some example of aliases :

Highlighter

Customize the bash or zsh prompt can be useful to easily get information without executing commands.

The project kube-ps1 is a script that adds the current Kubernetes context and namespace configured on kubectl to the Bash/Zsh prompt strings.

Here is an example of that useful tool :

Plugins

Google has developed an extension to the Kubectl command to easily manage his plugin as apt or yum can do on a Linux operating system.

Krew is a tool that makes it easy to use kubectl plugins. It helps to discover plugins, install and manage them on a machine. It is similar to tools like apt, dnf or brew.

Krew can be installed on Linux, Mac and Windows, everything is explain on the Github web page.

Here are some example of using Kubectl to manage plugins :

External documentation

To go further in the management of Kubectl, please refer to these documentations :

Last updated

Was this helpful?