Getting started with Kubectl

Muhammad Faheem Muhammadi
5 min readDec 6, 2022

--

Important concepts and commands to have on fingertips.

Kubernetes with Kubectl (Figma)

Blog Content:

  • What is Kubectl?
  • What You Can Do with kubectl?
  • Pre-requisites for Kubectl
  • Kubectl Query Structure
  • Kubectl Sample Command Examples
  • Kubectl: Behind the Scenes?
  • Deploying Container App into Kubernetes Cluster
  • Kubernetes Services
  • Service Deletion

Before we get into Kubectl, it is important to know what is Kubernetes?

Kubernetes is one of the hot areas in the world of computing where people all over the world are investing their time and intellect contributing into the creation of a system that automates the processes involved in the production environment to make lives easier.

The Official statement for Kubernetes is:

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

CNCF open-source: Kubernetes.

Kubernetes is one of the Scheduling & Orchestration layer’s open-source project initiated by Cloud Native Computing Foundation (CNCF).

So, in order to use this system, Kubectl is one of the tools for using Kubernetes.

What is Kubectl?

Kubectl is a command-line tool used for interacting and managing Kubernetes (k8s) clusters.

What you can do with it?

It allows you to deploy your containerized applications to the k8s cluster, inspect and manage k8s resources, and view logs.

Pre-requisites for Kubectl

For kubectl to be able to interact with a K8s cluster, you need to set up a cluster. This can be done in many ways. One of them is setting it up on your local machine. This is can be achieved either by installing Minikube cluster or enabling a kubernetes cluster on Docker-Desktop for Windows users.

Minikube is a tool that sets up a K8s cluster with a single node (a machine within the cluster where container aplications are deployed). You can install it from here.

Docker-Destop also provides a standalone single-node Kubernetes server and client, as well as Docker CLI integration that runs on your machine. It runs within a Docker container on your local system, and is only for local testing. To install Docker-Desktop and enable Kubernetes, follow here.

Kubectl Query Structure

After successfully installing Kubectl from Kubernetes.io for your desired platform, you can get started with it using a command-line terminal. Kubectl command goes as:

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

So, the command starts with the keyword: kubectl. This keyword requires some arguments for its execution for letting it know what action to perform on a specific resource by specifying its name followed by flags that are optional arguments. In short:

  1. [command]: operation you perform on a resource.
  2. [TYPE]: resource type from a set of different resource types.
  3. [NAME]: specific name you have named your resource.
  4. [flags]: optional flags you pass

Kubectl Sample Command Examples:

Below are three operations performed using kubectl on a command-line terminal.

kubectl get pod my-pod --ignore-not-found=true // displays a pod
kubectl get namespaces // lists all the namespaces
kubectl api-resources // lists all k8s resources

The first example arguments are explained below:

get: displays the resource
pod: resource type
my-pod: resource name
— ignore-not-found=true: flag to ignore error message if resource doesn’t exist

Kubectl: Behind the scenes?

Kubectl interacts Kubernetes Cluster.

You have a k8s cluster up and running on your local machine, lets say on Docker-Desktop over Windows. After executing a kubectl command on the terminal, it communicates with k8s cluster’s control plane’s component called Api-Server. For now, just think of the api-server as the middle person between kubectl and k8s cluster. Kubectl interacts and manages the cluster through the api-server. With Kubectl you can create, add, run, delete your containerized applications in the cluster.

Deploying Container App into K8s Cluster

To deploy a container into the cluster, following steps need to be followed:

  1. Create an image file of your application. One way of achieving this is by using Docker. Docker enables you for creating image files for your application by writing Dockerfile.
  2. Store your image file in docker hub registry.
  3. Create a Deployment using the image file of your container application using the following command:
kubectl create deployment my-deployment --image=[image]

You should replace the [image] with the path for your image file e.g. if the image is in Dockerhub registry: docker.io/your_username/file_name.

The deployment creates a pod and adds the container to the pod. Deployment is a recommended way of creating a pod for ease of managing pod creation and scaling purposes. If a pod crashes, then deployment takes care of it by restarting the pod.

Once the deployment is created, you can use the get operation to see the status of your deployment:

kubectl get my-deployment // displays the status of a deployment

But, the deployed app is only accessible within the cluster. There is no access to the application over the internet unless you convert the resource into a kubernetes Service.

Kubernetes Services

A service is an abstraction which defines a logical set of Pods and a policy by which to access them. In simple words, it is an exposed resource to the outside world of Kubernetes cluster. To expose a resource as a service, the following command is used:

kubectl expose deployment my-deployment --type=LoadBalancer --port=8080

The load-balancer flag indicates that you want to expose your Service outside of the cluster. If your application runs on a specific port, then the port flag should be assigned that port number otherwise clients won’t be able to connect.

Use the get service command to see the status of your service:

kubectl get services

You will be able to find the IP address of the service in the output of the above operation.

Service Deletion

To delete your created services and deployments, you can run the following commands to close the chapter:

kubectl delete service my-deployment
kubectl delete deployment my-deployment

That was all from this blog. I hope it be of any help to people who may read it!

Thank you!

--

--

Muhammad Faheem Muhammadi

Computer Science student, becoming a Pro day by day, to bring easy reads on diverse subjects to save my readers’ time and energy. Your Feedback is Appreciated!!