A beginner's guide for K8s setup using Kubectl

in Kubernetes

A beginner's guide for K8s setup using Kubectl

What is Kubectl? How to install it? How to use it?

    Definition - What is Kubectl?

    Kubernetes is a great solution for automating the deployment, scaling, and management of containerized applications - explained in more detail in “How to get started with Kubernetes” blog post.

    One of the main tools in Kubernetes is Kubectl. This is the main command-line tool for administering and interacting with your Kubernetes installation; think of kubectl as the Bash environment of Kubernetes.

    Other useful Kubernetes components are kubeadm and kubelet - you will most likely install and use these 3 together when setting up your first Kubernetes cluster. And read this article for a full overview of Kubernetes components and this other article for a sample Kubernetes deployment.

    Kubectl Installation

    You can install kubectl, on Linux, macOS, and Windows. But as detailed in the official Kubernetes site: “You must use a kubectl version that is within one minor version difference of your cluster. For example, a v1.2 client should work with v1.1, v1.2, and v1.3 master. Using the latest version of kubectl helps avoid unforeseen issues.”

    The most common installation is on Linux, so let’s go through the kubectl installation via both curl and via native package manager in Linux in a bit more detail:

    Using curl:

    The first step is to confirm the latest stable release at this URL: https://storage.googleapis.com/kubernetes-release/release/stable.txt

    Next, download that latest stable release with this command:
    curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"

    If instead, you want to use an older release, say v1.16.4, use this command instead:

    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.16.4/bin/linux/amd64/kubectl

    Next, you make the binary executable, add it to your PATH, and check that kubectl is running ok and up to date, using these 3 commands respectively:

    chmod +x ./kubectl

    sudo mv ./kubectl /usr/local/bin/kubectl

    kubectl version --client

    Using a Native Package Manager

    Use these commands for Ubuntu/ Debian installation:

    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

    The steps for kubectl installation on Windows can be found here, and for macOS here.

    After a successful installation, kubectl now needs to find and access a Kubernetes cluster. For this, it needs a kubeconfig file, which was created automatically when you created a cluster using “kube-up.sh” or successfully deployed a Minikube cluster (what is Minikube? Find out more here). By default, the kubectl configuration file is in this path: ~/.kube/config.

    Finally, confirm that kubectl is properly configured. Display the cluster state with this command:

    kubectl cluster-info

    This should return a URL response. But if the error message below is displayed instead, then kubectl is not configured correctly or your cluster setup was incorrect: The connection to the server <server-name:port> was refused - did you specify the right host or port?

    You can get more verbose info on your cluster setup using this command:

    kubectl cluster-info dump

    And you can view your overall kubectl configuration using this command:

    kubectl config view

    Kubectl Options & Flags

    Just like bash, there are some kubectl options that can make life easier. For instance, you can enable command autocompletion in kubectl on bash or zsh by following this guide.

    You can also enable a number of flags in kubectl, by simply using this syntax: kubectl [flags]

    Quick example: kubectl --alsologtostderr

    These flags are explained in full in the official kubectl reference guide, and a few of the more useful and common ones are listed in the table below:

    Flag Purpose
    --alsologtostderr log to standard error as well as log files
    --application-metrics-count-limit int Maximum number of application metrics to store (per container): default 100
    --cluster clustername The name of the cluster to use. Useful in multiple-cluster environments
    -h, --help help
    --log-dir path-to-logdirectory If non-empty, write log files in this directory
    -s, --server servername The address and port of the Kubernetes API server

    Kubectl Syntax and Commands

    We have talked about kubectl being the command-line utility for Kubernetes. Now let’s take a closer look at the kubectl syntax and some useful Kubectl commands. These are also explained in detail in the official guide.

    Kubectl commands generally follow this order:

    kubectl [command] [TYPE] [NAME] [flags]

    command: Specifies the operation that you want to perform on one or more resources, such as create, get, describe, delete

    TYPE: Specifies the resource type. Resource types are case-insensitive and you can specify the singular, plural, or even abbreviated forms. For example, the following commands produce the same output:

    kubectl get pod pod1

    kubectl get pods pod1

    kubectl get po pod1

    NAME: Specifies the name of the resource. Names are case-sensitive. If the name is omitted, details for all resources are displayed, for example, kubectl get pods

    flags: Specifies optional flags, as explained earlier. Note that any flags you specify at the command line will override the default values in a configuration file and in any environment variables.

    The actual kubectl commands are simply too many to list here. You can go the official guide and the command reference for the full list, but below are some of the most useful everyday commands that you will almost certainly use as a Kubernetes admin:

    • kubectl get <resourcename>

    Use ‘kubectl get’ to pull a list of resources you have currently on your cluster. The types of resources you can get include: Namespace, Pod, Node, Deployment, Service, ReplicaSets

    Example: kubectl get nodes to list all nodes.

    • kubectl create <resourcename>

    As you may have guessed, this is the command to create resources such as service, deployment, job, namespace (ns)

    Example: kubectl create ns my-new-namespace

    • kubectl describe <resourcename>

    This shows the details of the resource you're looking at. The most common use of this is to describe a pod or node to check that it’s running ok or if there’s an error.

    Example: kubectl describe pod15

    • kubectl apply

    This command is used to apply a configuration in a specific config file.

    Example: kubectl apply -f /kube/config2.yaml

    And remember, you can always get some help and tips using kubectl help


    This is just an intro to Kubernetes. You can use this guide to familiarize yourself with Kubernetes in general and kubectl in particular. But remember you also always have the option of not spending time learning all of this, by using a fully hosted Kubernetes solution such as Cloudplex.

    Get started with Cloudplex here, and try out some useful hands-on labs here.

    Get similar stories in your inbox weekly, for free

    Share this story:

    Founder and CEO of Cloudplex - We make Kubernetes easy for developers.

    Latest stories

    How ManageEngine Applications Manager Can Help Overcome Challenges In Kubernetes Monitoring

    We tested ManageEngine Applications Manager to monitor different Kubernetes clusters. This post shares our review …

    AIOps with Site24x7: Maximizing Efficiency at an Affordable Cost

    In this post we'll dive deep into integrating AIOps in your business suing Site24x7 to …

    A Review of Zoho ManageEngine

    Zoho Corp., formerly known as AdventNet Inc., has established itself as a major player in …

    Should I learn Java in 2023? A Practical Guide

    Java is one of the most widely used programming languages in the world. It has …

    The fastest way to ramp up on DevOps

    You probably have been thinking of moving to DevOps or learning DevOps as a beginner. …

    Why You Need a Blockchain Node Provider

    In this article, we briefly cover the concept of blockchain nodes provider and explain why …

    Top 5 Virtual desktop Provides in 2022

    Here are the top 5 virtual desktop providers who offer a range of benefits such …

    Why Your Business Should Connect Directly To Your Cloud

    Today, companies make the most use of cloud technology regardless of their size and sector. …

    7 Must-Watch DevSecOps Videos

    Security is a crucial part of application development and DevSecOps makes it easy and continuous.The …