Getting started with Kubectl
Overview
Teaching: 10 min
Exercises: 0 minQuestions
What is Kubectl?
How to use Kubectl commands?
Objectives
Learn what the kubectl command can do
Learn how to set up different services/resources to get the most of your cluster
K8s - Imperative vs Declarative programming
In the context of Kubernetes, imperative and declarative are two different paradigms used to define and manage the desired state of resources within a cluster. While imperative commands are useful for ad-hoc tasks or interactive exploration, the declarative approach is more suitable for managing and maintaining resources in a Kubernetes cluster efficiently. Let’s explore each approach! But first, we need a tool to interact with our cluster.
Kubectl
The kubectl command-line tool is a powerful utility provided by Kubernetes that allows you to interact with and manage Kubernetes clusters. Both Minikube and K8s on Docker Desktop come with a built-in kubectl installation. Use the following syntax to run kubectl commands from your terminal window:
kubectl [command] [TYPE] [NAME] [flags]
Where:
- command: Specifies the operation you want to perform on one or more Kubernetes resources. Some commonly used commands include
create
,get
,describe
,delete
,apply
, andscale
. Each command has its own set of options and subcommands. - TYPE: Indicates the type of Kubernetes resource you want to interact with. It can be a single resource type like
Pod, Deployment, Service
, or a more general term likeall
,nodes
,namespaces
, etc. - NAME: Specifies the name of the specific resource you want to operate on.
- flags: These are optional flags that modify the behavior of the command. Flags can be used to specify additional parameters, control output formats, apply labels, set resource limits, etc. Flags are specific to each command and can be listed by running
kubectl [command] --help
.
Do not forget to go through the setup episode to get your environment up and running. Also, check out the kubectl cheat sheet.
Windows Users - Reminder
To enable Kubernetes on WSL2, you have two options: activating Kubernetes in Docker Desktop or installing Minikube following the Linux option on WSL2 Ubuntu. It’s important to note that the Windows instructions for the Minikube installation guide users to PowerShell, but running the CMSSW container there will cause issues. Therefore, it is necessary to execute those commands within the Ubuntu shell.
Docker desktop needs to be running in the background in order for Minikube to start. From a terminal with administrator access (but not logged in as root), run:
minikube start
kubectl
and it is pointing to some other environment, such as docker-desktop or a GKE cluster, ensure you change the context so that kubectl is pointing to minikube:
kubectl config use-context minikube
If minikube fails to start, see the drivers page for help setting up a compatible container or virtual-machine manager.
Congratulations! You have successfully activated Minikube. You can now use Kubernetes to deploy and manage containerized applications on your local machine. Remember to familiarize yourself with Kubernetes concepts and commands to make the most of its capabilities.
- From the Docker Dashboard, select the Settings.
- Select Kubernetes from the left sidebar.
- Next to Enable Kubernetes, select the checkbox.
- Select Apply & Restart to save the settings and then click Install to confirm. This instantiates images required to run the Kubernetes server as containers, and installs the
/usr/local/bin/kubectl
command on your machine.
kubectl
and it is pointing to some other environment, such as minikube or a GKE cluster, ensure you change the context so that kubectl is pointing to docker-desktop:
kubectl config use-context docker-desktop
kubectl get nodes
Congratulations! You have successfully activated Docker Desktop Kubernetes. You can now use Kubernetes to deploy and manage containerized applications on your local machine. Remember to familiarize yourself with Kubernetes concepts and commands to make the most of its capabilities.
Imperative Approach
In the imperative approach, you specify the exact sequence of commands or actions to be performed to create or modify Kubernetes resources. You interact with the Kubernetes API by issuing explicit instructions.
Let’s first create a node running Nginx by using the imperative way.
Create the pod using the Imperative way
kubectl run mynginx --image=nginx
Get a list of pods and their status
kubectl get pods
Output
Get more info
kubectl get pods -o wide
Output
kubectl describe pod mynginx
Output
Delete the pod
kubectl delete pod mynginx
Declarative Approach
In the declarative approach, you define the desired state of Kubernetes resources in a declarative configuration file (e.g., YAML or JSON). Rather than specifying the steps to achieve that state, you describe the desired outcome and let Kubernetes handle the internal details.
Create a pod using the declarative way
Download the file:
wget https://cms-opendata-workshop.github.io/workshop2023-lesson-introcloud/files/kubectl/myapp.yaml
YAML File
Now, let’s create a pod using the YAML file
kubectl create -f myapp.yaml
Get some info
kubectl get pods -o wide
kubectl describe pod myapp-pod
Open a shell in the running pod
kubectl exec -it myapp-pod -- bash
Output
Print the DBCON environment variable that was set in the YAML file.
echo $DBCON
Output
Exit from the container
exit
Delete the pod
kubectl delete -f myapp.yaml
The declarative approach is the recommended way to manage resources in Kubernetes. It promotes consistency, reproducibility, and automation. You can easily version control the configuration files, track changes over time, and collaborate with team members more effectively.
Let’s run a few examples.
Kubernetes namespaces partition resources in a cluster, creating isolated virtual clusters. They allow multiple teams or applications to coexist while maintaining separation and preventing conflicts.
Get the currently configured namespaces:
kubectl get namespaces
kubectl get ns
Both commands are equivalent and will retrieve the list of namespaces in your Kubernetes cluster.
Get the pods list:
Get a list of all the installed pods.
kubectl get pods
You get the pods from the default namespace. Try getting the pods from the docker namespace. You will get a different list.
kubectl get pods -n kube-system
Output
Get nodes information:
Get a list of all the installed nodes. Using Docker Desktop or Minikube, there should be only one.
kubectl get nodes
Get some info about the node.
kubectl describe node
Output
Run your first deployment
A Deployment is a higher-level resource that provides declarative updates and manages the deployment of Pods. It allows you to define the desired state of your application, including the number of replicas, container images, and resource requirements.
Download the file:
wget https://cms-opendata-workshop.github.io/workshop2023-lesson-introcloud/files/kubectl/deploy-example.yaml
YAML File
Create the Deployment:
kubectl apply -f deploy-example.yaml
Get the pods list
kubectl get pods -o wide
Output
Get more details about the pod
kubectl describe pod deploy-example
Output
Get the Deployment info
kubectl get deploy
Output
Get the ReplicaSet name:
A ReplicaSet is a lower-level resource that ensures a specified number of replicas of a Pod are running at all times.
kubectl get rs
Output
In summary, a Deployment provides a higher-level abstraction for managing and updating the desired state of Pods, while a ReplicaSet is a lower-level resource that ensures the specified number of Pod replicas are maintained. Deployments use ReplicaSets under the hood to achieve the desired state and handle scaling and rolling updates.
Cleanup
Delete the pod
kubectl delete -f deploy-example.yaml
Key Points
kubectl
is the ruler of GKE