This covers the API primitives and how to create and configure basic Pods
- YAML
- POD
- ReplicaSet
- Deployment
- Namespaces
First of all, make sure to configure kubectl autocomplete, it will easy your life
# BASH
source <(kubectl completion bash) # setup autocomplete in bash into the current shell, bash-completion package should be installed first.
echo "source <(kubectl completion bash)" >> ~/.bashrc # add autocomplete permanently to your bash shell.
# ZSH
source <(kubectl completion zsh) # setup autocomplete in zsh into the current shell
echo "if [ $commands[kubectl] ]; then source <(kubectl completion zsh); fi" >> ~/.zshrc # add autocomplete permanently to your zsh shell
show
# Create the namespace if not exist
kubectl get ns
kubectl create ns webtier
# Create the pod
kubectl run nginx --image=nginx --restart=Never -n webtier
==================================================================================================
show
kubectl run nginx --image=nginx --dry-run
==================================================================================================
show
# Create the namespace if not exist
kubectl get ns
kubectl create ns dev
# An easy way to generate YAML is to use imperactive command with dry-run option
kubectl run nginx --image=nginx --restart=Never -n dev --dry-run -o yaml > nginx-pod.yaml
cat nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
# Edit the file by adding namespace dev
```yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
namespace: dev
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
# Another way to add namespace dev to the command
kubeclt create -f nginx-pod.yaml -n dev
==================================================================================================
show
kubectl get po --all-namespaces
==================================================================================================
show
```bash kubectl run hazelcast --image=hazelcast --port=5701 ```
==================================================================================================
5. 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 -- env # -it will help in seeing the output
# Another way is to run it without -it
kubectl run busybox --image=busybox --command --restart=Never -- env
# and then, check its logs
kubectl logs busybox
==================================================================================================
6. Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and "POD_NAMESPACE=default" in the container.
show
```bash kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default" ``` ``` # Verify it via kubectl logs hazelcast ```
==================================================================================================
7. Using kubectl command, create a pod using busybox image, set env value var1=val1. Verify the existance of the env inside the pod
show
# This is an easy way
kubectl run busybox --image=busybox --restart=Never --env=var1=val1 -it -- env
# Another way is to run it without -it
kubectl run busybox --image=busybox --restart=Never --env=var1=val1
# and then
kubectl exec -it busybox -- env
# or
kubectl describe po busybox | grep val1
==================================================================================================
8. Using kubectl command, create a pod using nginx image, set env value var2=val2, print it and make sure the pod is automatically deleted after it's completed.
show
kubectl run nginx --image=nginx --restart=Never --env=var2=val2 -it --rm -- env
==================================================================================================
Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from JSON.
show
kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
==================================================================================================
show
kubectl run busybox --image=busybox -it --restart=Never -- echo 'hello world'
# Another way
kubectl run busybox --image=busybox -it --restart=Never -- /bin/sh -c 'echo hello world'
==================================================================================================
show
kubectl create ns production --dry-run -o yaml
==================================================================================================
12. Using kubectl command, create a pod using kubectl command that uses perl to calculate pi to 2000 digits, then stops and print the output
show
kubectl run pi --image=perl --restart=OnFailure --dry-run -- perl -Mbignum=bpi -wle 'print bpi(2000)'
kubectl logs pi
==================================================================================================
show
kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
kubectl logs pi
==================================================================================================
show
kubectl create resourcequota myrq -o yaml --dry-run
==================================================================================================
14. Change pod's image to nginx:1.7.1. Observe that the pod will be killed and recreated as soon as the image gets pulled
show
# 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
==================================================================================================
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 -- sh
# run wget on specified IP:Port
wget -O- 10.1.1.131:80
exit
==================================================================================================
show
kubectl get po nginx -o yaml --export
==================================================================================================
17. Get information about the pod, including details about potential issues (e.g. pod hasn't started)
show
kubectl describe po nginx
==================================================================================================
show
kubectl logs nginx
==================================================================================================
show
kubectl logs nginx -p
==================================================================================================
show
kubectl exec -it nginx -- /bin/sh
==================================================================================================
Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container.
show
kubectl run hazelcast --image=hazelcast --labels="app=hazelcast,env=prod"
==================================================================================================
show
kubectl run nginx --image=nginx --replicas=5
==================================================================================================
11. Start a single instance of nginx, but overload the spec of the deployment with a partial set of values parsed from JSON.
show
kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
==================================================================================================
show
kubectl run busybox --image=busybox -it --restart=Never -- echo 'hello world'
# Another way
kubectl run busybox --image=busybox -it --restart=Never -- /bin/sh -c 'echo hello world'
==================================================================================================
show
kubectl create ns production --dry-run -o yaml
==================================================================================================
15. Using kubectl command, create a pod using kubectl command that uses perl to calculate pi to 2000 digits, then stops and print the output
show
kubectl run pi --image=perl --restart=OnFailure --dry-run -- perl -Mbignum=bpi -wle 'print bpi(2000)'
kubectl logs pi
==================================================================================================
show
kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
kubectl logs pi
==================================================================================================
show
kubectl create resourcequota myrq -o yaml --dry-run
==================================================================================================
18. Change pod's image to nginx:1.7.1. Observe that the pod will be killed and recreated as soon as the image gets pulled
show
# 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
==================================================================================================
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 -- sh
# run wget on specified IP:Port
wget -O- 10.1.1.131:80
exit
==================================================================================================
show
kubectl get po nginx -o yaml --export
==================================================================================================
21. Get information about the pod, including details about potential issues (e.g. pod hasn't started)
show
kubectl describe po nginx
==================================================================================================
show
kubectl logs nginx
==================================================================================================
show
kubectl logs nginx -p
==================================================================================================
show
kubectl exec -it nginx -- /bin/sh
==================================================================================================
25. Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the container.
show
kubectl run hazelcast --image=hazelcast --labels="app=hazelcast,env=prod"
==================================================================================================
show
kubectl run nginx --image=nginx --replicas=5