Kubernetes Kubernetes Cheat Sheet Estimated time to read: 10 minutes
K8s Istio Helm Kubernetes Cluster Management Command Syntax Example Description Get cluster info kubectl cluster-info kubectl cluster-info Display addresses of the master and services Get cluster nodes kubectl get nodes kubectl get nodes List all nodes in the cluster Get node details kubectl describe node NODE_NAME kubectl describe node my-node Get detailed information about a node
Kubernetes Namespace Management Command Syntax Example Description Get namespaces kubectl get namespaces kubectl get namespaces List all namespaces in the cluster Create namespace kubectl create namespace NAMESPACE_NAME kubectl create namespace mynamespace Create a new namespace Set namespace context kubectl config set-context --current --namespace=NAMESPACE_NAME kubectl config set-context --current --namespace=mynamespace Set the namespace for the current context
Kubernetes Deployment Management Command Syntax Example Description Get deployments kubectl get deployments kubectl get deployments List all deployments in the current namespace Create deployment kubectl create deployment DEPLOYMENT_NAME --image=IMAGE_NAME kubectl create deployment nginx --image=nginx Create a new deployment with the specified image Update deployment kubectl set image deployment/DEPLOYMENT_NAME CONTAINER_NAME=NEW_IMAGE_NAME kubectl set image deployment/nginx nginx=nginx:1.19 Update the image for a deployment Rollback deployment kubectl rollout undo deployment DEPLOYMENT_NAME kubectl rollout undo deployment nginx Rollback a deployment to the previous revision
Kubernetes Service Management Command Syntax Example Description Get services kubectl get services kubectl get services List all services in the current namespace Create service kubectl expose deployment DEPLOYMENT_NAME --type=SERVICE_TYPE --port=PORT kubectl expose deployment nginx --type=LoadBalancer --port=80 Create a new service for a deployment Update service kubectl patch svc SERVICE_NAME -p '{"spec": {"ports": [{"port": NEW_PORT, "targetPort": NEW_TARGET_PORT}]}}' kubectl patch svc nginx -p '{"spec": {"ports": [{"port": 8080, "targetPort": 80}]}}' Update the port and target port for a service Delete service kubectl delete service SERVICE_NAME kubectl delete service nginx Delete a service
Kubernetes ConfigMap and Secret Management Command Syntax Example Description Create ConfigMap kubectl create configmap CONFIGMAP_NAME --from-file=FILE_PATH kubectl create configmap myconfig --from-file=config.txt Create a new ConfigMap from a file Create Secret kubectl create secret generic SECRET_NAME --from-file=FILE_PATH kubectl create secret generic mysecret --from-file=secret.txt Create a new Secret from a file Get ConfigMaps and Secrets kubectl get configmaps,secrets kubectl get configmaps,secrets List all ConfigMaps and Secrets in the current namespace
Helm Commands Command Syntax Example Description Install chart helm install RELEASE_NAME CHART_NAME helm install my-release stable/nginx Install a Helm chart Upgrade release helm upgrade RELEASE_NAME CHART_NAME helm upgrade my-release stable/nginx Upgrade a Helm release to a new version of the chart Rollback release helm rollback RELEASE_NAME REVISION helm rollback my-release 1 Rollback a Helm release to a specific revision List releases helm list helm list List all Helm releases Get release history helm history RELEASE_NAME helm history my-release Get the history of a Helm release Uninstall release helm uninstall RELEASE_NAME helm uninstall my-release Uninstall a Helm release
Istio Commands Command Syntax Example Description Install Istio istioctl install istioctl install Install Istio using the default profile Check Istio installation istioctl verify-install istioctl verify-install Verify that Istio has been installed and is running correctly Analyze configuration istioctl analyze istioctl analyze Analyze Istio configuration in the current namespace Add a namespace label kubectl label namespace NAMESPACE_NAME istio-injection=enabled kubectl label namespace mynamespace istio-injection=enabled Add a label to enable automatic sidecar injection for a namespace
Miscellaneous Commands Command Syntax Example Description Get all resources kubectl get all kubectl get all List all resources in the current namespace Get resource details kubectl describe RESOURCE_TYPE RESOURCE_NAME kubectl describe pod my-pod Get detailed information about a specific resource Delete resource kubectl delete RESOURCE_TYPE RESOURCE_NAME kubectl delete pod my-pod Delete a specific resource Apply a configuration kubectl apply -f FILE_PATH kubectl apply -f my-config.yaml Apply a configuration from a file Get resource YAML kubectl get RESOURCE_TYPE RESOURCE_NAME -o yaml kubectl get pod my-pod -o yaml Get the YAML configuration of a specific resource Watch resource changes kubectl get RESOURCE_TYPE --watch kubectl get pods --watch Watch for changes to a specific resource type Autoscale deployment kubectl autoscale deployment DEPLOYMENT_NAME --min=MIN_REPLICAS --max=MAX_REPLICAS --cpu-percent=CPU_PERCENT kubectl autoscale deployment nginx --min=2 --max=5 --cpu-percent=80 Autoscale a deployment based on CPU usage
Kubernetes Ingress Management Command Syntax Example Description Get ingresses kubectl get ingresses kubectl get ingresses List all ingresses in the current namespace Create ingress kubectl apply -f FILE_PATH kubectl apply -f my-ingress.yaml Create a new ingress from a file Update ingress kubectl apply -f FILE_PATH kubectl apply -f updated-ingress.yaml Update an existing ingress using a file Delete ingress kubectl delete ingress INGRESS_NAME kubectl delete ingress my-ingress Delete a specific ingress
Kubernetes Persistent Volume and Persistent Volume Claim Management Command Syntax Example Description Get persistent volumes kubectl get pv kubectl get pv List all persistent volumes in the cluster Create persistent volume kubectl apply -f FILE_PATH kubectl apply -f my-pv.yaml Create a new persistent volume from a file Get persistent volume claims kubectl get pvc kubectl get pvc List all persistent volume claims in the current namespace Create persistent volume claim kubectl apply -f FILE_PATH kubectl apply -f my-pvc.yaml Create a new persistent volume claim from a file
Kubernetes StatefulSet Management Command Syntax Example Description Get statefulsets kubectl get statefulsets kubectl get statefulsets List all statefulsets in the current namespace Create statefulset kubectl apply -f FILE_PATH kubectl apply -f my-statefulset.yaml Create a new statefulset from a file Update statefulset kubectl apply -f FILE_PATH kubectl apply -f updated-statefulset.yaml Update an existing statefulset using a file Delete statefulset kubectl delete statefulset STATEFULSET_NAME kubectl delete statefulset my-statefulset Delete a specific statefulset
Kubernetes Job and CronJob Management Command Syntax Example Description Get jobs kubectl get jobs kubectl get jobs List all jobs in the current namespace Create job kubectl apply -f FILE_PATH kubectl apply -f my-job.yaml Create a new job from a file Delete job kubectl delete job JOB_NAME kubectl delete job my-job Delete a specific job Get cronjobs kubectl get cronjobs kubectl get cronjobs List all cronjobs in the current namespace Create cronjob kubectl apply -f FILE_PATH kubectl apply -f my-cronjob.yaml Create a new cronjob from a file Update cronjob kubectl apply -f FILE_PATH kubectl apply -f updated-cronjob.yaml Update an existing cronjob using a file Delete cronjob kubectl delete cronjob CRONJOB_NAME kubectl delete cronjob my-cronjob Delete a specific cronjob
Kubernetes Horizontal Pod Autoscaler (HPA) Management Command Syntax Example Description Get HPAs kubectl get hpa kubectl get hpa List all Horizontal Pod Autoscalers in the current namespace Create HPA kubectl autoscale deployment DEPLOYMENT_NAME --min=MIN_REPLICAS --max=MAX_REPLICAS --cpu-percent=CPU_PERCENT kubectl autoscale deployment nginx --min=2 --max=5 --cpu-percent=80 Create a new Horizontal Pod Autoscaler for a deployment Update HPA kubectl patch hpa HPA_NAME -p '{"spec": {"minReplicas": NEW_MIN_REPLICAS, "maxReplicas": NEW_MAX_REPLICAS, "targetCPUUtilizationPercentage": NEW_CPU_PERCENT}}' kubectl patch hpa nginx-hpa -p '{"spec": {"minReplicas": 3, "maxReplicas": 8, "targetCPUUtilizationPercentage": 75}}' Update an existing Horizontal Pod Autoscaler Delete HPA kubectl delete hpa HPA_NAME kubectl delete hpa nginx-hpa Delete a specific Horizontal Pod Autoscaler
Kubernetes Resource Quota Management Command Syntax Example Description Get resource quotas kubectl get quota kubectl get quota List all resource quotas in the current namespace Create resource quota kubectl apply -f FILE_PATH kubectl apply -f my-quota.yaml Create a new resource quota from a file Update resource quota kubectl apply -f FILE_PATH kubectl apply -f updated-quota.yaml Update an existing resource quota using a file Delete resource quota kubectl delete quota QUOTA_NAME kubectl delete quota my-quota Delete a specific resource quota
Kubernetes Network Policy Management Command Syntax Example Description Get network policies kubectl get networkpolicies kubectl get networkpolicies List all network policies in the current namespace Create network policy kubectl apply -f FILE_PATH kubectl apply -f my-network-policy.yaml Create a new network policy from a file Update network policy kubectl apply -f FILE_PATH kubectl apply -f updated-network-policy.yaml Update an existing network policy using a file Delete network policy kubectl delete networkpolicy POLICY_NAME kubectl delete networkpolicy my-network-policy Delete a specific network policy
Kubernetes Custom Resource Definition (CRD) Management Command Syntax Example Description Get CRDs kubectl get crd kubectl get crd List all Custom Resource Definitions in the cluster Create CRD kubectl apply -f FILE_PATH kubectl apply -f my-crd.yaml Create a new Custom Resource Definition from a file Update CRD kubectl apply -f FILE_PATH kubectl apply -f updated-crd.yaml Update an existing Custom Resource Definition using a file Delete CRD kubectl delete crd CRD_NAME kubectl delete crd my-crd Delete a specific Custom Resource Definition
Logs and Troubleshooting Command Syntax Example Description Get pod logs 'kubectl logs POD_NAME' kubectl logs my-pod Get logs of a specific pod (stdout) Get container logs kubectl logs POD_NAME -c CONTAINER_NAME
kubectl logs my-pod -c my-container
Get logs of a specific container in a multi-container pod (stdout) Stream pod logs kubectl logs -f POD_NAME kubectl logs -f my-pod Stream logs of a specific pod (stdout) Stream container logs kubectl logs -f POD_NAME -c CONTAINER_NAME kubectl logs -f my-pod -c my-container Stream logs of a specific container in a multi-container pod (stdout) Stream logs with labels kubectl logs -f -l LABEL_KEY=LABEL_VALUE --all-containers kubectl logs -f -l app=myapp --all-containers Stream logs of all pods with a specific label (stdout) Get previous container logs kubectl logs POD_NAME -c CONTAINER_NAME --previous kubectl logs my-pod -c my-container --previous Get logs of a previous instantiation of a specific container (stdout) Get pod events kubectl describe pod POD_NAME kubectl describe pod my-pod Get events and other information of a specific pod Get resource status kubectl get RESOURCE_TYPE RESOURCE_NAME kubectl get pod my-pod Get the current status of a specific resource Debug running pod kubectl exec -it POD_NAME -- COMMAND kubectl exec -it my-pod -- /bin/bash Run a command in a running pod (interactive mode)
Kubernetes Troubleshooting Commands Command Syntax Example Description Check component statuses kubectl get componentstatuses kubectl get componentstatuses Get the status of Kubernetes components (e.g., etcd, controller manager, etc.) Get cluster events kubectl get events kubectl get events List all events in the cluster Get cluster events (sorted) kubectl get events --sort-by='.metadata.creationTimestamp' kubectl get events --sort-by='.metadata.creationTimestamp' List all events in the cluster sorted by timestamp Get cluster events for a namespace kubectl get events --namespace=NAMESPACE kubectl get events --namespace=my-namespace List all events for a specific namespace Check for configuration issues kubectl get nodes -o json kubectl get nodes -o json Get detailed information about nodes to diagnose issues Check control plane logs kubectl logs -n kube-system -l component=COMPONENT_NAME kubectl logs -n kube-system -l component=kube-apiserver Check logs of control plane components (e.g., kube-apiserver, kube-controller-manager, etc.)
Istio Troubleshooting Commands Command Syntax Example Description Check Istio components kubectl get pods -n istio-system kubectl get pods -n istio-system List all Istio components and their status Get Istio component logs kubectl logs -n istio-system POD_NAME -c CONTAINER_NAME kubectl logs -n istio-system istio-ingressgateway-12345 -c istio-proxy Get logs of a specific Istio component Check Istio configuration istioctl analyze istioctl analyze Analyze Istio configuration in the current
These commands cover a broad range of Kubernetes operations, from cluster management to workload and network management. Be sure to consult the official documentation for each tool (Kubernetes, Helm, and Istio) for more information on advanced usage, custom configurations, and best practices.