kubernetes.io > Documentation > Reference > kubectl CLI > kubectl Cheat Sheet
kubernetes.io > Documentation > Tasks > Monitoring, Logging, and Debugging > Get a Shell to a Running Container
kubernetes.io > Documentation > Tasks > Access Applications in a Cluster > Configure Access to Multiple Clusters
kubernetes.io > Documentation > Tasks > Access Applications in a Cluster > Accessing Clusters using API
kubernetes.io > Documentation > Tasks > Access Applications in a Cluster > Use Port Forwarding to Access Applications in a Cluster
show
kubectl create namespace mynamespace
kubectl run nginx --image=nginx --restart=Never -n mynamespace
show
Easily generate YAML with:
kubectl run nginx --image=nginx --restart=Never --dry-run=client -n mynamespace -o yaml > pod.yaml
cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
namespace: mynamespace
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
kubectl create -f pod.yaml
Alternatively, you can run in one line
kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml | kubectl create -n mynamespace -f -
show
kubectl run busybox --image=busybox --command --restart=Never -it --rm -- env # -it will help in seeing the output, --rm will immediately delete the pod after it exits
# or, just run it without -it
kubectl run busybox --image=busybox --command --restart=Never -- env
# and then, check its logs
kubectl logs busybox
show
# create a YAML template with this command
kubectl run busybox --image=busybox --restart=Never --dry-run=client -o yaml --command -- env > envpod.yaml
# see it
cat envpod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- command:
- env
image: busybox
name: busybox
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
# apply it and then see the logs
kubectl apply -f envpod.yaml
kubectl logs busybox
show
kubectl create namespace myns -o yaml --dry-run=client
Get the YAML for a new ResourceQuota called 'myrq' with hard limits of 1 CPU, 1G memory and 2 pods without creating it
show
kubectl create quota myrq --hard=cpu=1,memory=1G,pods=2 --dry-run=client -o yaml
show
kubectl get po --all-namespaces
Alternatively
kubectl get po -A
show
kubectl run nginx --image=nginx --restart=Never --port=80
Change pod's image to nginx:1.7.1. Observe that the container will be restarted as soon as the image gets pulled
show
Note: The RESTARTS
column should contain 0 initially (ideally - it could be any number)
# kubectl set image POD/POD_NAME CONTAINER_NAME=IMAGE_NAME:TAG
kubectl set image pod/nginx nginx=nginx:1.7.1
kubectl describe po nginx # you will see an event 'Container will be killed and recreated'
kubectl get po nginx -w # watch it
Note: some time after changing the image, you should see that the value in the RESTARTS
column has been increased by 1, because the container has been restarted, as stated in the events shown at the bottom of the kubectl describe pod
command:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
[...]
Normal Killing 100s kubelet, node3 Container pod1 definition changed, will be restarted
Normal Pulling 100s kubelet, node3 Pulling image "nginx:1.7.1"
Normal Pulled 41s kubelet, node3 Successfully pulled image "nginx:1.7.1"
Normal Created 36s (x2 over 9m43s) kubelet, node3 Created container pod1
Normal Started 36s (x2 over 9m43s) kubelet, node3 Started container pod1
Note: you can check pod's image by running
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
show
kubectl get po -o wide # get the IP, will be something like '10.1.1.131'
# create a temp busybox pod
kubectl run busybox --image=busybox --rm -it --restart=Never -- wget -O- 10.1.1.131:80
Alternatively you can also try a more advanced option:
# Get IP of the nginx pod
NGINX_IP=$(kubectl get pod nginx -o jsonpath='{.status.podIP}')
# create a temp busybox pod
kubectl run busybox --image=busybox --env="NGINX_IP=$NGINX_IP" --rm -it --restart=Never -- sh -c 'wget -O- $NGINX_IP:80'
Or just in one line:
kubectl run busybox --image=busybox --rm -it --restart=Never -- wget -O- $(kubectl get pod nginx -o jsonpath='{.status.podIP}:{.spec.containers[0].ports[0].containerPort}')
show
kubectl get po nginx -o yaml
# or
kubectl get po nginx -oyaml
# or
kubectl get po nginx --output yaml
# or
kubectl get po nginx --output=yaml
show
kubectl describe po nginx
show
kubectl logs nginx
show
kubectl logs nginx -p
# or
kubectl logs nginx --previous
show
kubectl exec -it nginx -- /bin/sh
show
kubectl run busybox --image=busybox -it --restart=Never -- echo 'hello world'
# or
kubectl run busybox --image=busybox -it --restart=Never -- /bin/sh -c 'echo hello world'
show
kubectl run busybox --image=busybox -it --rm --restart=Never -- /bin/sh -c 'echo hello world'
kubectl get po # nowhere to be found :)
Create an nginx pod and set an env value as 'var1=val1'. Check the env value existence within the pod
show
kubectl run nginx --image=nginx --restart=Never --env=var1=val1
# then
kubectl exec -it nginx -- env
# or
kubectl exec -it nginx -- sh -c 'echo $var1'
# or
kubectl describe po nginx | grep val1
# or
kubectl run nginx --restart=Never --image=nginx --env=var1=val1 -it --rm -- env