A.core Concepts
A.core Concepts
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
Create a namespace called 'mynamespace' and a pod with image nginx called nginx on
this namespace
show
show
kubectl run nginx --image=nginx --restart=Never --dry-run=client -n mynamespace -o yaml > pod.ya
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 run nginx --image=nginx --restart=Never --dry-run=client -o yaml | kubectl create -n myn
Create a busybox pod (using kubectl command) that runs the command "env". Run it and
see the output
show
kubectl run busybox --image=busybox --command --restart=Never -it --rm -- env # -it will help
# or, just run it without -it
kubectl run busybox --image=busybox --command --restart=Never -- env
# and then, check its logs
kubectl logs busybox
Create a busybox pod (using YAML) that runs the command "env". Run it and see the
output
show
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: {}
show
Create the YAML for a new ResourceQuota called 'myrq' with hard limits of 1 CPU, 1G
memory and 2 pods without creating it
show
show
Alternatively
kubectl get po -A
Create a pod with image nginx called nginx and expose traffic on port 80
show
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)
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,
Normal Pulling 100s kubelet, node3 Pulling image "nginx:1.7.1"
Normal Pulled 41s kubelet, node3 Successfully pulled image "nginx:1.
Normal Created 36s (x2 over 9m43s) kubelet, node3 Created container pod1
Normal Started 36s (x2 over 9m43s) kubelet, node3 Started container pod1
Get nginx pod's ip created in previous step, use a temp busybox image to wget its '/'
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
kubectl run busybox --image=busybox --rm -it --restart=Never -- wget -O- $(kubectl get pod nginx
show
Get information about the pod, including details about potential issues (e.g. pod hasn't
started)
show
show
If pod crashed and restarted, get logs about the previous instance
show
show
Create a busybox pod that echoes 'hello world' and then exits
show
Do the same, but have the pod deleted automatically when it's completed
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