K8s kubectl CheatSheet

?
R
Bash

CheatSheet for kubectl. Updated regularly. kubectl: https://kubernetes.io/docs/reference/kubectl/overview/ https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#autoscale Official CheatSheet: https://kubernetes.io/docs/user-guide/kubectl-cheatsheet/ Kubectl: https://kubernetes-v1-4.github.io/docs/user-guide/kubectl/kubectl_patch/ Amazon EKS kubectl: https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-06-05/bin/darwin/amd64/kubectl Warnings: Always version control the manifest.yml files!!

1# Interactive shell on a running pod
2kubectl exec --stdin --tty <pod-name> -- /bin/bash
3
4# Kubeconfig generation from AWS EKS
5aws eks update-kubeconfig --name my-cool-cluster --region=us-west-1
6
7# Start a Support Pod
8 kubectl run -it --rm debug --image=busybox --restart=Never -- sh
9
10# Delete pods
11kubectl get pods --no-headers=true | awk '/api/{print $1}'| xargs  kubectl delete  pod
12
13# Delete resources 
14kubectl delete -f <filename>
15
16# Troubleshooting
17kubectl events
18
19# Pretty Check Nodes Pressure (CPU and Memory)
20kubectl describe nodes | grep -A 2 -e "^\\s*Allocated"
21
22# Check if metrics-server is installed
23kubectl -n kube-system get deployment/metrics-server
24
25# View autoscalling
26kubectl get horizontalpodautoscaler.autoscaling/XXX
27
28# Cluster Autoscaling logs
29kubectl -n kube-system logs -f deployment.apps/cluster-autoscaler
30
31# Create namespace
32kubectl create namespace xpto
33
34# Get a Daemonset
35kubectl get daemonset --namespace=kube-system
36
37# Delete a Daemonset
38kubectl delete ds fluentd-cloudwatch --namespace=kube-system
39
40# Get Configmap in YAML
41kubectl get configmap nginx-configuration --namespace ingress-nginx --export -o yaml
42
43# Update Deployment Image
44kubectl set image deployments/code-recipes-deployment codeapp=code-recipes:1.1.3
45
46### Context ###
47kubectl config current-context
48
49# Copy secret between contexts (namespaces)
50kubectl get secret gitlab-registry --namespace=revsys-com --export -o yaml |\
51   kubectl apply --namespace=devspectrum-dev -f -
52
53### Horizontal Pod Autoscaler ###
54kubectl autoscale deployment optimize-api-deployment --min=2 --max=5 --cpu-percent=80
55kubectl get hpa
56kubectl describe hpa
57
58############## LIFECYCLE ##############
59# Quick delete
60kubectl delete rc/redis-master rc/redis-slave rc/guestbook svc/redis-master svc/redis-slave svc/guestbook
61# Quick overview
62kubectl get svc,rc,po
63watch kubectl get svc,rc,po
64
65############## NODES (WORKERS) ###############
66# View node load
67kubectl top node
68# Add a label to a node with a performance tag
69kubectl label node ip-192-168-214-9.ec2.internal performance=low
70kubectl describe node ip-192-168-214-9.ec2.internal
71
72
73############## PODs ##############
74# List Pods by Restart Count
75kubectl get pods --sort-by=.status.containerStatuses[0].restartCount --all-namespaces
76# Get a Pod
77kubectl get pod pod1
78# Get Pods by Namespace
79kubectl get pods --namespace=nasa
80
81############## CLUSTER ADMIN ##############
82# Watch Worker Nodes
83kubectl get nodes --watch
84# Version
85kubectl version --short --client
86# Describe Nodes
87kubectl describe nodes | grep -A 2 -e "^\\s*CPU Requests"
88
89############## AWS ##############
90# View the ConfigMap (IAM)
91kubectl describe configmap -n kube-system aws-auth
92# Edit a ConfigMap
93kubectl edit -n kube-system configmap/aws-auth
94
95############## SERVICES ##############
96# View all Services with selectors
97kubectl get services -o wide
98
99# Check service Load Balancer endpoint
100kubectl describe svc http
101
102# Expose a Deployment (container port 80, host port 8000)
103kubectl expose deployment my-deployment --external-ip="172.17.0.35" --port=8000 --target-port=80
104
105############## DEPLOYMENTS ##############
106# Scale the pod replicates
107kubectl scale --replicas=5 deployment my-deployment
108
109
110############## AIO ##############
111# Create a deployment and expose it as a single command (container port: 80)
112## This exposes the Pod via Docker Port Mapping, you won't get a service
113kubectl run httpexposed --image=katacoda/docker-http-server:latest --replicas=1 --port=80 --hostport=8001
114curl http://172.17.0.35:8001
115
116# Rollout Forced
117kubectl rollout restart deployment sampleapp-deployment
118
119# Rollback
120kubectl rollout history deployment sampleapp-deployment
121kubectl rollout undo deployment sampleapp-deployment --to-revision=1
122
123# Check deployment rollout history
124kubectl rollout history deployment sampleapp-deployment
125
126# Check deployment rollout status
127kubectl rollout status deployment sampleapp-deployment
128
129# Describe a Replica Set
130kubectl describe rs
131
132# Describe a Deployment
133kubectl describe deploy sampleapp-deployment
134
135# Upload a YML to the apiserver {}
136kubectl create -f my-depoyment.yml
137
138# Re-Upload a YML to the apiserver {} **ensure record flag is passed at all times
139kubectl apply -f my-depoyment.yml --record
140
141# Get all Replication Controllers
142kubectl get rc
143
144# Get all Replica Sets *for deployments only
145kubectl get rs
146
147# Delete a RC
148kubectl delete rc my-rc
149
150# Updates (via RC)
151kubectl rolling-update -f updaterc.yml
152
153# Inser annotations
154kubectl annotate svc beta --namespace=beta annotation=value
155
156# View all service annotations
157 kubectl get svc -o 'go-template={{range .items}}{{.metadata.name}}{{"\n"}}{{range $key,$value := .metadata.annotations}}{{$key}}: {{$value}}{{"\n"}}{{end}}{{"\n"}}{{end}}' --namespace=client
158client
159
160# Get Namespaces
161kubectl get namespaces
162kubectl get namespaces --show-labels
163
164# Get cluster info (IP Address, etc..)
165kubectl cluster-info
166
167# Get Cluster IP (Internal)
168export CLUSTER_IP=$(kubectl get services/webapp1-clusterip-svc -o go-template='{{(index .spec.clusterIP)}}')
169
170# Describe a Service
171kubectl describe svc deis-router --namespace=deis     
172
173# stream pod logs (stdout)
174kubectl logs -f my-pod                            
175kubectl --namespace=nasa logs -f my-pod             
176
177# Nginx logs
178kubectl exec --stdin --tty <pod-name> -- /bin/bash
179kubectl exec -it api-cmd-2764659056-1n283 -- /bin/bash
180tail -f /var/log/nginx/error.log
181
182# Nginx config
183kubectl exec -it api-cmd-2764659056-1n283 -- /bin/bash
184cd /etc/nginx/
185
186# Metrics for Pod and its containers
187kubectl top pod POD_NAME --containers
188
189# Get Replication Controllers, Replica Sets and Pods
190kubectl --namespace=nasa get pods
191kubectl --namespace=nasa get rs
192kubectl --namespace=nasa get rc
193
194# Delete Pod (Manually)
195kubectl --namespace=nasa delete pod nasa-4141478638-m0le0
196kubectl delete pod --grace-period=0 --force --namespace
197
198

Created on 11/13/2017