Cheat Sheet¶
A cheat sheet for Kubernetes commands.
Kubectl Command¶
Alias¶
Completion¶
Add kubectl auto completion
Windows¶
Cluster Info¶
Get Clusters¶
Get Cluster Info¶
Contexts¶
A context is a cluster, namespace and user
Get a list of contexts¶
Get the current context¶
Switch current context¶
To switch between contexts, you can also install and use kubectx.
Get Commands¶
kubectl get all
kubectl get namespaces
kubectl get configmaps
kubectl get nodes
kubectl get pods
kubectl get rs
kubectl get svc kuard
kubectl get endpoints kuard
Additional switches that can be added to the above commands:
-o wide- Show more information.--watchor-w- watch for changes.
Namespaces¶
Set default namespace¶
You can set the default namespace for the current context like so
--namespace - Get a resource for a specific namespace
To switch namespaces, you can also install and use kubens
Labels¶
Get pods showing labels¶
Get pods by label¶
kubectl get pods -l environment=production,tier!=frontend
kubectl get pods -l 'environment in (production,test),tier notin (frontend,backend)'
Describe¶
kubectl describe nodes [id]
kubectl describe pods [id]
kubectl describe rs [id]
kubectl describe svc kuard [id]
kubectl describe endpoints kuard [id]
Delete¶
kubectl delete nodes [id]
kubectl delete pods [id]
kubectl delete rs [id]
kubectl delete svc kuard [id]
kubectl delete endpoints kuard [id]
Force a deletion of a pod without waiting for it to gracefully shut down
Create vs Apply¶
kubectl create can be used to create new resources while kubectl apply inserts or updates resources while maintaining any manual changes made like scaling pods.
--record- Add the current command as an annotation to the resource.--recursive- Recursively look for yaml in the specified directory.
Create Pod¶
kubectl run kuard --generator=run-pod/v1 --image=gcr.io/kuar-demo/kuard-amd64:1 --output yaml --export --dry-run > kuard-pod.yml
kubectl apply -f kuard-pod.yml
Create Deployment¶
kubectl run kuard --image=gcr.io/kuar-demo/kuard-amd64:1 --output yaml --export --dry-run > kuard-deployment.yml
kubectl apply -f kuard-deployment.yml
Create Service¶
kubectl expose deployment kuard --port 8080 --target-port=8080 --output yaml --export --dry-run > kuard-service.yml
kubectl apply -f kuard-service.yml
Export YAML¶
For New Pod¶
kubectl run my-cool-app —-image=me/my-cool-app:v1 --output yaml --export --dry-run > my-cool-app.yaml
For Existing Object¶
Logs¶
Get logs¶
Get logs for previously terminated container¶
Watch logs in real time¶
Copy files out of pod¶
Requires tar binary in container
You can also install and use kail.
Port Forward¶
Scaling¶
Update replicas¶
Autoscaling¶
Set autoscaling config¶
Rollout¶
Get rollout status¶
kubectl rollout status deployment/nginx-deployment
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
deployment "nginx-deployment" successfully rolled out
Get rollout history¶
kubectl rollout history deployment/nginx-deployment
kubectl rollout history deployment/nginx-deployment --revision=2
Undo a rollout¶
kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment --to-revision=2
Pause/resume a rollout¶
Examples¶
Pod¶
apiVersion: v1
kind: Pod
metadata:
name: cuda-test
spec:
containers:
- name: cuda-test
image: "k8s.gcr.io/cuda-vector-add:v0.1"
resources:
limits:
nvidia.com/gpu: 1
nodeSelector:
accelerator: nvidia-tesla-p100
Deployment¶
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: my-ns
labels:
environment: production
tier: frontend
annotations:
key1: value1
key2: value2
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
Dashboard¶
- Enable proxy
Azure Kubernetes Service¶
Show Dashboard¶
Secure the dashboard like this. Then run:
Get Credentials¶
Upgrade¶
Get updates
Get Node Resources Info¶
The Node Feature Discovery operator is a tool for Kubernetes administrators that makes it easy to detect and understand the hardware features and configurations of a cluster's nodes. With this operator, administrators can easily gather information about their nodes that can be used for scheduling, resource management, and more by controlling the life cycle of NFD.