Skip to content

Core Concepts

Yasser Sinjab edited this page Apr 7, 2019 · 9 revisions

Let's start by creating a namespace called nyx

kubectl create ns nyx

Create a pod in a specific namespace would be

kubectl run busybox --image=busybox --restart=Never --namespace=nyx
kubectl get po --namespace=nyx

From now on I will keep using the default namespace for make the other steps simpler.

Kubectl run command has different results depends on the arguments passed:

kubectl run nginx --image=nginx --dry-run -o yaml # Deployment
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml # Pod
kubectl run busybox --image=busybox --restart=OnFailure --dry-run -o yaml -- /bin/sh -c 'echo Hello world!' # Job
kubectl run busybox --image=busybox --restart=OnFailure --schedule="0/5 * * * ?" --dry-run -o yaml -- /bin/sh -c 'echo Hello world!' # CronJob

It is easier to use these commands in the exam since it will generate the YAML template for you. Pipe it to a file as the following:

kubectl run nginx --image=nginx --dry-run -o yaml > pod.yaml

Kubectl explain command is your documentation in case you don't want to leave the terminal. It is very handy and useful.

kubectl explain pods
kubectl explain pod.spec
kubectl explain pod.spec.containers
kubectl explain pod.spec.containers.image

Now let's do the following:

  • Create an nginx pod with environment variable
kubectl run nginx --image=nginx --restart=Never --env=MY_VAR=MY_VAL
  • Describe it
kubectl describe po nginx
  • Print environment variables
kubectl exec nginx -- env | grep MY_VAR
  • Delete and clean
kubectl delete po nginx

Kubectl logs command is used to view the logs of the pod. Create a job that echo hello world and view it through logs:

kubectl run busybox --image=busybox --restart=OnFailure -- /bin/sh -c 'echo Hello world!'
kubectl get jobs
kubectl logs logs jobs/busybox
# Hello world!
Clone this wiki locally