Skip to main content

Kubectl

In this guide, we will help you understand how to use kubectl in a Runme Notebook interact with your kubernetes cluster.

Prerequisites

brew install kubectl

Verify kubectl is Installed

kubectl version --client

Create a cluster (if you don't have one)

kind delete cluster --name kubectl-runme

Create a cluster (locally), using kind, named kubectl-runme

kind create cluster --name kubectl-runme

Check if your cluster is running

kubectl get namespaces

Basic commands

Here are some essential Kubernetes commands to help you interact with and monitor your cluster:

Get Cluster Info

kubectl cluster-info

Viewing Nodes

Nodes are the physical or virtual machines in your cluster.

kubectl get nodes

Viewing Pods

Pods are the smallest deployable units in Kubernetes. To list all pods:

kubectl get pods

For more detailed information about a pod:

kubectl describe pod <pod-name>

Viewing Services

Services allow networking between different components in your cluster:

kubectl get services

Working with Deployments

Deployments in Kubernetes allow you to automate the process of scaling, updating, and managing the lifecycle of your applications. Whether you’re deploying a new app or updating an existing one, deployments offer a reliable way to manage your applications with ease. Below are some key commands to help you create, scale, and manage your deployments.

Create a Deployment

You can create a deployment using a YAML file or via the command line. Here’s an example of creating a deployment for an NGINX server:

kubectl create deployment nginx --image=nginx

Check Deployment Status

kubectl get deployments

Scale the Deployment

You can easily scale your deployment to multiple replicas:

kubectl scale deployment nginx --replicas=3

Rolling Updates

To update the image version for a deployment:

kubectl set image deployment/nginx nginx=nginx:1.19

To monitor the progress of the update:

kubectl rollout status deployment/nginx

To rollback in case of an issue:

kubectl rollout undo deployment/nginx

Using YAML Configuration Files

Most Kubernetes objects can be created using YAML files. Here’s an example of a simple pod definition:


cat <<EOF | sudo tee ./runme-pod.yaml > /dev/null
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
spec:
containers:
- name: myapp-container
image: nginx
ports:
- containerPort: 80

EOF

To create this pod, save it to a file (e.g., runme-pod.yaml) and apply it using:

kubectl apply -f runme-pod.yaml

To delete resources defined by a YAML file:

kubectl delete -f runme-pod.yaml

Working with Pods

Running a Pod

You can run a pod manually using the following command:

kubectl run myrunmeapp --image=nginx

To check the logs of a running pod:

kubectl logs <pod-name>

If the pod has multiple containers, specify the container:

kubectl logs <pod-name> -c <container-name>

Executing Commands Inside a Pod

kubectl exec -it <pod-name> -- /bin/bash

This gives you a shell session in the pod's container.

Services and Exposing Pods

To expose your deployment as a service:

kubectl expose deployment nginx --type=LoadBalancer --port=80

You can also expose it using NodePort for testing purposes on your local machine:

kubectl expose deployment nginx --type=NodePort --port=80

To view your exposed services:

kubectl get services

Monitoring and Debugging

Get Logs

To get logs from a pod:

kubectl logs <pod-name>

Debugging

If a pod crashes, inspect the last few logs:

kubectl logs <pod-name> --previous

Check Resource Usage

To check CPU and memory usage of nodes and pods:

kubectl top nodes
kubectl top pod