Kubernetes

Essential Kubectl Commands for Managing Kubernetes

Essential kubectl commands for Kubernetes management
Written by MilanMaximo

When working with Kubernetes, one of the most essential tools at your disposal is kubectl. It’s the command-line interface that allows you to interact with your Kubernetes cluster, whether you’re deploying applications, managing pods, scaling services, or configuring networking. Mastering kubectl commands is key to efficiently managing your Kubernetes environments, and this guide covers the most important commands you’ll need.

Whether you’re just starting out or you’re an experienced Kubernetes user looking to refine your skills, this blog will provide a comprehensive overview of key kubectl commands. From basic operations like creating and deleting pods to more advanced tasks like managing ConfigMaps, secrets, and deployments, we’ll walk you through the essential commands for effective Kubernetes management.

Dive in, and learn how to streamline your Kubernetes workflows with this hands-on guide!

Pods

minikube start

Starts a Minikube cluster

kubectl create deployment my-app --image=my-image

Creates a deployment with the given image

kubectl get pods

Lists all the pods in the cluster

kubectl delete pod my-pod

Deletes a pod with the given name

kubectl logs my-pod

Displays the logs of the given pod

Deployments

kubectl create deployment my-app --image=my-image

Creates a deployment with the given image

kubectl scale deployment my-app --replicas=3

Scales the deployment to have 3 replicas

kubectl set image deployment/my-app my-app=my-image:v2

Updates the deployment to use the new version of the image

Services

kubectl expose deployment my-app --port=80 --target-port=8080

Creates a service for the given deployment on port 80, forwarding traffic to port 8080 of the pods

kubectl get services

Lists all the services in the cluster

kubectl delete service my-service

Deletes the service with the given name

ConfigMaps

kubectl create configmap my-config --from-file=my-config-file

Creates a ConfigMap with the contents of the given file

kubectl get configmaps

Lists all the ConfigMaps in the cluster

kubectl delete configmap my-config

Deletes the ConfigMap with the given name

Roles

kubectl create role my-role --verb=get,list --resource=pods

Creates a role with the given permissions

kubectl get roles

Lists all the roles in the cluster

kubectl delete role my-role

Deletes the role with the given name

Role Bindings

kubectl create rolebinding my-rolebinding --role=my-role --user=my-user

Creates a role binding that binds the given role to the given user

kubectl get rolebindings

Lists all the role bindings in the cluster

kubectl delete rolebinding my-rolebinding

Deletes the role binding with the given name

Network Policies

kubectl create networkpolicy my-networkpolicy --ingress

Creates a network policy that allows ingress traffic

kubectl get networkpolicies

Lists all the network policies in the cluster

kubectl delete networkpolicy my-networkpolicy

Deletes the network policy with the given name

Events

kubectl get events

Lists all the events in the cluster

Namespaces

kubectl create namespace my-namespace

Creates a namespace with the given name

kubectl get namespaces

Lists all the namespaces in the cluster

kubectl delete namespace my-namespace

Deletes the namespace with the given name

Jobs

kubectl create job my-job --image=my-image

Creates a job with the given image

kubectl get jobs

Lists all the jobs in the cluster

kubectl delete job my-job

Deletes the job with the given name

Cron Jobs

kubectl create cronjob my-cronjob --image=my-image --schedule="*/1 * * * *" --restart=OnFailure

Creates a cron job that runs every minute

kubectl get cronjobs

Lists all the cron jobs in the cluster

kubectl delete cronjob my-cronjob

Deletes the cron job with the given name

Daemon Sets

kubectl create daemonset my-daemonset --image=my-image

Creates a daemon set with the given image

kubectl get daemonsets

Lists all the daemon sets in the cluster

kubectl delete daemonset my-daemonset

Deletes the daemon set with the given name

Replica Sets

kubectl create replicaset my-replicaset --image=my-image

Creates a replica set with the given image

kubectl get replicasets

Lists all the replica sets in the cluster

kubectl delete replicaset my-replicaset

Deletes the replica set with the given name

Ingress

kubectl create ingress my-ingress --rule=my-rule

Creates an ingress with the given rule

kubectl get ingresses

Lists all the ingresses in the cluster

kubectl delete ingress my-ingress

Deletes the ingress with the given name

Endpoints

kubectl get endpoints

Lists all the endpoints in the cluster

Horizontal Pod Autoscalers

kubectl create hpa my-hpa --cpu-percent=80 --min=2 --max=10

Creates a horizontal pod autoscaler with the given CPU utilization percentage, minimum and maximum number of replicas

kubectl get hpa

Lists all the horizontal pod autoscalers in the cluster

kubectl delete hpa my-hpa

Deletes the horizontal pod autoscaler with the given name

Pod Templates

kubectl create podtemplate my-podtemplate --image=my-image

Creates a pod template with the given image

kubectl get podtemplates

Lists all the pod templates in the cluster

kubectl delete podtemplate my-podtemplate

Deletes the pod template with the given name

Service Accounts

kubectl create serviceaccount my-serviceaccount

Creates a service account with the given name

kubectl get serviceaccounts

Lists all the service accounts in the cluster

kubectl delete serviceaccount my-serviceaccount

Deletes the service account with the given name

Volume Snapshots

kubectl create volumesnapshot my-volumesnapshot --snapshotter=my-snapshotter --source=pvc/my-pvc

Creates a volume snapshot with the given snapshotter and source

kubectl get volumesnapshots

Lists all the volume snapshots in the cluster

kubectl delete volumesnapshot my-volumesnapshot

Deletes the volume snapshot with the given name

Volume Snapshot Classes

kubectl create volumesnapshotclass my-volumesnapshotclass --driver=my-driver

Creates a volume snapshot class with the given driver

kubectl get volumesnapshotclasses

Lists all the volume snapshot classes in the cluster

kubectl delete volumesnapshotclass my-volumesnapshotclass

Deletes the volume snapshot class with the given name

Volume Attachments

kubectl create volumeattachment my-volumeattachment --source=my-node --target=my-pv

Creates a volume attachment with the given node and PV

kubectl get volumeattachments

Lists all the volume attachments in the cluster

kubectl delete volumeattachment my-volumeattachment

Deletes the volume attachment with the given name

CSI Drivers

kubectl create csidriver my-csidriver --nodepluginpath=/path/to/node/plugin --controllerpluginpath=/path/to/controller/plugin

Creates a CSI driver with the given node and controller plugin paths

kubectl get csidrivers

Lists all the CSI drivers in the cluster

kubectl delete csidriver my-csidriver

Deletes the CSI driver with the given name

Certificate Signing Requests

kubectl create csr my-csr --cert=my-cert --key=my-key

Creates a certificate signing request with the given cert and key

kubectl get csr

Lists all the certificate signing requests in the cluster

kubectl delete csr my-csr

Deletes the certificate signing request with the given name

Custom Resource Definitions (CRDs)

kubectl create crd my-crd --kind=my-kind --api-version=my-version

Creates a custom resource definition with the given kind and API version

kubectl get crd

Lists all the custom resource definitions in the cluster

kubectl delete crd my-crd

Deletes the custom resource definition with the given name

Pod Disruption Budgets (PDBs)

kubectl create pdb my-pdb --selector=my-selector --min-available=my-min-available

Creates a pod disruption budget with the given selector and minimum available count

kubectl get pdb

Lists all the pod disruption budgets in the cluster

kubectl delete pdb my-pdb

Deletes the pod disruption budget with the given name

Stateful Sets

kubectl create statefulset my-statefulset --image=my-image --replicas=3

Creates a stateful set with the given image and number of replicas

kubectl get statefulsets

Lists all the stateful sets in the cluster

kubectl scale statefulset my-statefulset --replicas=5

Scales up or down the number of replicas for the given stateful set

Network Policies

kubectl create networkpolicy my-networkpolicy --ingress-from=my-namespace

Creates a network policy with the given ingress rule from a namespace

kubectl get networkpolicies

Lists all the network policies in the cluster

kubectl delete networkpolicy my-networkpolicy

Deletes the network policy with the given name

Pod Presets

kubectl create podpreset my-podpreset --env=MY_ENV=my-value

Creates a pod preset with the given environment variable

kubectl get podpresets

Lists all the pod presets in the cluster

kubectl delete podpreset my-podpreset

Deletes the pod preset with the given name

Mutating and Validating Webhooks

kubectl create validatingwebhook my-validatingwebhook --namespace=my-namespace --image=my-image

Creates a validating webhook with the given namespace and image

kubectl get validatingwebhooks

Lists all the validating webhooks in the cluster

kubectl delete validatingwebhook my-validatingwebhook

Deletes the validating webhook with the given name

kubectl create mutatingwebhook my-mutatingwebhook --namespace=my-namespace --image=my-image

Creates a mutating webhook with the given namespace and image

kubectl get mutatingwebhooks

Lists all the mutating webhooks in the cluster

kubectl delete mutatingwebhook my-mutatingwebhook

Deletes the mutating webhook with the given name

Secrets

kubectl create secret generic my-secret --from-literal=MY_SECRET=my-value

Creates a generic secret with the given data

kubectl get secrets

Lists all the secrets in the cluster

kubectl delete secret my-secret

Deletes the secret with the given name

Persistent Volumes (PVs)

kubectl create pv my-pv --capacity=1Gi --access-modes=ReadWriteOnce --hostPath=/data

Creates a persistent volume with the given capacity, access modes and host path

kubectl get pv

Lists all the persistent volumes in the cluster

kubectl delete pv my-pv

Deletes the persistent volume with the given name

Persistent Volume Claims (PVCs)

kubectl create pvc my-pvc --capacity=1Gi --access-modes=ReadWriteOnce --storageClassName=standard

Creates a persistent volume claim with the given capacity, access modes and storage class name

kubectl get pvc

Lists all the persistent volume claims in the cluster

kubectl delete pvc my-pvc

Deletes the persistent volume claim with the given name

Resource Quotas

kubectl create quota my-quota --hard=pods=10,services=10

Creates a resource quota with the given limits on the number of pods and services

kubectl get quota

Lists all the resource quotas in the cluster

kubectl delete quota my-quota

Deletes the resource quota with the given name

About the author

MilanMaximo

Leave a Comment