Essential kubectl Commands for Beginners

Saravanakumar Arunachalam
3 min readJan 8, 2025

If you’re new to Kubernetes, mastering kubectl is your first step toward managing clusters like a pro. This quick guide will help you learn the most essential commands every beginner should know. And remember, for operations specific to a namespace, use the -n <namespace> flag to simplify your workflow.

Context

List all k8s Context

kubectl config get-contexts

Describe the context

kubectl config get-contexts my-context

Use the Context

kubectl config use-context CONTEXT_NAME

Show the KubeConfig

kubectl config view

Namespace Management

Namespaces allow you to organize and manage resources within your cluster.

List all namespaces:

kubectl get namespaces

Create a new namespace:

kubectl create namespace <namespace-name>

Delete a namespace:

kubectl delete namespace <namespace-name>

Set the default namespace for the current context:

kubectl config set-context --current --namespace=<namespace-name>

Pod Operations

Pods are the fundamental units of deployment in Kubernetes.

List all pods in the current namespace:

kubectl get pods

Delete a specific pod:

kubectl delete pod <pod-name>

Execute a command inside a pod

kubectl exec -it <pod-name> -- <command>

Example:

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

Print environment variables of a pod:

kubectl exec -it <pod-name> -- env

Filter pods by label:

kubectl get pods -l <label-key>=<label-value>

Example:

kubectl get pods -l app=webapp

Create a pod with a specific image:

kubectl run <pod-name> --image=<image-name> --restart=Never

Example:

kubectl run <pod-name> --image=<image-name> --restart=Never

Generate pod YAML without creating it:

kubectl run <pod-name> --image=<image-name> --restart=Never --dry-run=client -o yaml

Example:

kubectl run web --image=nginx --restart=Never --dry-run=client -o yaml

Port-forward a pod to localhost:

kubectl port-forward pod/<pod-name> <local-port>:<pod-port>

Example:

kubectl port-forward pod/web 8080:80

Deployment Management

Deployments manage the deployment and scaling of pod sets.

List all deployments:

kubectl get deployments

Create a deployment:

kubectl create deployment <deployment-name> --image=<image-name>

Example:

kubectl create deployment webapp --image=nginx

Update the image of a deployment:

kubectl set image deployment/<deployment-name> <container-name>=<new-image>

Example:

kubectl set image deployment/webapp nginx=nginx:1.19

Scale a deployment

kubectl scale deployment/<deployment-name> --replicas=<number-of-replicas>

Example:

kubectl scale deployment/webapp --replicas=3

Roll back a deployment to a previous revision:

kubectl rollout undo deployment/<deployment-name>

Example:

kubectl rollout undo deployment/webapp

Service Management

Services expose your applications to the network.

List all services:

kubectl get services

Create a service to expose a deployment:

kubectl expose deployment/<deployment-name> --port=<port> --target-port=<target-port> --type=<service-type>

Example:

kubectl expose deployment/webapp --port=80 --target-port=8080 --type=LoadBalancer

Delete a service

kubectl delete service <service-name>

Port-Forward a Service

kubectl port-forward service/myservice 8443:https

ConfigMap and Secret Management

ConfigMaps and Secrets manage configuration data and sensitive information.

Create a ConfigMap from a file:

kubectl create configmap <configmap-name> --from-file=<file-path>

Example

kubectl create configmap app-config --from-file=config.yaml

Create a Secret from literal values:

kubectl create secret generic <secret-name> --from-literal=<key>=<value>

Example:

kubectl create secret generic db-secret --from-literal=username=admin --from-literal=password=secret

View a ConfigMap:

kubectl get configmap <configmap-name> -o yaml

View a Secret (base64 encoded):

kubectl get secret <secret-name> -o yaml

Logs and Events

Monitoring logs and events is crucial for debugging.

View logs of a pod:

kubectl logs <pod-name>

Stream logs of a pod:

kubectl logs -f <pod-name>

View events in the current namespace:

kubectl get events

Node Management

Nodes are the worker machines in a Kubernetes cluster.

List all nodes:

kubectl get nodes

View detailed information about a node:

kubectl describe node <node-name>

Cordon a node (mark as unschedulable):

kubectl cordon <node-name>

Uncordon a node (mark as schedulable):

kubectl uncordon <node-name>

Drain a node (safely evict all pods):

kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data

Pro Tip

For any operation specific to a namespace, always include the -n <namespace> flag. For example:

kubectl get pods -n dev

Ready to Start?

These commands are your entry point into the powerful world of Kubernetes. Start experimenting with them today, and you’ll be on your way to becoming a Kubernetes expert!

For more commands please reference: https://kubernetes.io/docs/reference/kubectl/

--

--

No responses yet