Kubernetes Exercises
Notes
Try the following exercises by using imperative commands instead of starting off with yaml declaration files.
Setup ~/.vimrc file with the following: - set ts=2 sw=2 - set number - set expandtab
Exercises
Ex 1: Deploy a pod named nginx-pod using the nginx:alpine image.
Ex 2: Deploy a redis pod using the redis:alpine image with the labels set to tier=db.
Ex 3: Create a service redis-service to expose the redis application within the cluster on port 6379
Ex 4: Create a deployment named webapp using the image kodekloud/webapp-color with 3 replicas.
Ex 5: How do you label nodes and specify pods to only use nodes with that label?
Ex 6: Create a deployment from the nginx image with 3 replicas affiliated to node deb-k8node02 which is labelled colour=red
Ex 7: Create a https enabled nginx depoyment, creating the https keys, placing them into the ng-secret secrets, similarly creating the required nginx configuration to support this with the following attributes:
Image: nginx:alpine
Web host name: nginx.bigtom.local
Requests for .2 CPU and label app=nginx
Expose container port 443 for https only
Create the following nginx configuration into the ng-config configuration map:
server { listen 80; listen 443 ssl; server_name nginx.bigtom.local; ssl_certificate certs/https.cert; ssl_certificate_key certs/https.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; location / { root /usr/share/nginx/html; index index.html index.htm; } }
Answers
- Ans 1:
$ kubectl run --generator=run-pod/v1 nginx-pod --image=nginx:alpine
- Ans 2:
$ kubectl run --generator=run-pod/v1 redis --image=redis --dry-run -o yaml > redis.yaml
Now edit the redis.yaml file and add
tier: db
under thelabels:
dictionary.
- Ans 3:
$ kubectl expose pod/redis --name redis-service --port=6379
- Ans 4:
$ kubectl create deploy webapp --image=kodekloud/webapp-color --dry-run -o yaml > webapp.yaml
Now, edit the
webapp.yaml
file and create a linereplicas=3
and save it.$ kubectl apply -f webapp.yaml
- Ans 5:
$ kubectl label nodes deb-k8node02 gpu=nvidia
to label a node.
spec: containers: - name: myapp image: nginx nodeSelector: gpu: nvidia
The block above shows how the pod specifications should be created in yaml to use the node deb-k8node02 which has the label
gpu=nvidia
.
Ans 7:
- Create the private key: $ openssl genrsa -out https.key 2048
- Create the https certificate: $ openssl req -new -x509 -key https.key -out https.cert -days 365 -subj /CN=nginx.bigtom.local
- Create the ng-secret
secret from the https keys: $ kubectl create secret generic ng-secret --from-file=https.key --from-file=https.cert
- Create the config map, deployment and service from a yaml file.
A place to practice Kubernetes
URL: https://labs.play-with-k8s.com
Initialise the master node with: - kubeadm init –apiserver-advertise-address $(hostname -i)
Initialise cluster networking with: - kubeectl apply -n kube-system -f “https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d ‘n’)”
Untainting a node to get it to schedule: - kubectl taint nodes node1 node-role.kubernetes.io/master:NoSchedule-