Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cmcconnell1/0d77d09e4f7509cd5f65794576e90d4e to your computer and use it in GitHub Desktop.
Save cmcconnell1/0d77d09e4f7509cd5f65794576e90d4e to your computer and use it in GitHub Desktop.

Kubectl Cheat Sheet

  • focusing on kubectl commands commonly used for development, debugging, and maintenance tasks.
  • This can be imported as markdown and saved as a Confluence page etc. for easy reference.

Overview

This cheat sheet covers some essential kubectl commands for platform engineers. It includes syntax examples for inspecting resources, managing configurations, debugging, and performing essential tasks.


1. Port Forwarding

Basic Port Forwarding

Forward a local port to a port on a pod, allowing access to internal services without exposing them publicly.

kubectl port-forward pod/<pod-name> <local-port>:<pod-port>
# Example
kubectl port-forward pod/my-pod 8080:80

Service Port Forwarding

Forward a local port to a port on a Kubernetes service.

kubectl port-forward service/<service-name> <local-port>:<service-port>
# Example
kubectl port-forward service/my-service 8080:80

2. Executing Commands Inside a Pod

Running a Command in a Pod

Use exec to run commands in a container within a pod, useful for debugging or checking logs.

kubectl exec -it <pod-name> -- <command>
# Example
kubectl exec -it my-pod -- /bin/bash

3. Describing Resources

Describe a Pod

Get detailed information about a specific pod, including events and environment variables.

kubectl describe pod <pod-name>
# Example
kubectl describe pod my-pod

Describe a Service

View detailed information about a service, including its endpoint mappings.

kubectl describe service <service-name>
# Example
kubectl describe service my-service

4. Listing Resources

List All Pods in a Namespace

Display all pods in a given namespace.

kubectl get pods -n <namespace>
# Example
kubectl get pods -n default

List All Services

Show all services in the current namespace.

kubectl get services

List Nodes

Check the nodes in the Kubernetes cluster.

kubectl get nodes

5. Logs and Monitoring

View Pod Logs

Access logs for a specific container in a pod.

kubectl logs <pod-name> -c <container-name>
# Example
kubectl logs my-pod -c my-container

View Logs for All Containers in a Pod

Use the --all-containers flag to get logs from all containers.

kubectl logs <pod-name> --all-containers=true
# Example
kubectl logs my-pod --all-containers=true

Stream Logs Continuously

Stream logs in real-time.

kubectl logs -f <pod-name> -c <container-name>
# Example
kubectl logs -f my-pod -c my-container

6. Managing Deployments

Create a Deployment

Create a deployment directly from an image.

kubectl create deployment <deployment-name> --image=<image-name>
# Example
kubectl create deployment my-deployment --image=nginx

Scale a Deployment

Scale a deployment up or down to a specific number of replicas.

kubectl scale deployment <deployment-name> --replicas=<number>
# Example
kubectl scale deployment my-deployment --replicas=3

Update a Deployment Image

Change the image of a deployment.

kubectl set image deployment/<deployment-name> <container-name>=<new-image>
# Example
kubectl set image deployment/my-deployment nginx=nginx:1.19

7. Namespace Management

List Namespaces

Get a list of all namespaces in the cluster.

kubectl get namespaces

Switch to a Different Namespace

You can set a specific namespace as the default for commands using a context update.

kubectl config set-context --current --namespace=<namespace>
# Example
kubectl config set-context --current --namespace=production

8. Applying Configuration Files

Apply a Manifest

Apply configuration from a YAML file to create or update resources.

kubectl apply -f <file.yaml>
# Example
kubectl apply -f deployment.yaml

Delete Resources from a File

Delete resources defined in a manifest file.

kubectl delete -f <file.yaml>
# Example
kubectl delete -f deployment.yaml

9. Inspecting and Troubleshooting

Check Events in a Namespace

View all events in a namespace to troubleshoot issues.

kubectl get events -n <namespace>
# Example
kubectl get events -n default

Debug a Pod by Copying Files

Copy files to and from a pod for further inspection.

# Copy from local to pod
kubectl cp <local-file-path> <pod-name>:<pod-file-path>

# Copy from pod to local
kubectl cp <pod-name>:<pod-file-path> <local-file-path>
# Example
kubectl cp my-pod:/var/log/app.log ./app.log

10. Working with Secrets

Create a Secret from Literal Values

Create a new Kubernetes secret from literal values (e.g., storing credentials).

kubectl create secret generic <secret-name> --from-literal=<key>=<value>
# Example
kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=secret

View Secret Data in Base64

Retrieve the secret data in a base64-encoded format.

kubectl get secret <secret-name> -o jsonpath='{.data.<key>}' | base64 --decode
# Example
kubectl get secret my-secret -o jsonpath='{.data.password}' | base64 --decode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment