Kubernetes Hands-on Workshop
Kubernetes is used to maintain, scale and deploy containerised applications. It is one of the most used open-source application that accelerate the application lifecycle. In this blog, I am going to teach Kubernetes via a hands-on workshop. The contents of the workshop;
- Install Minikube
- Kubernetes common features
- Run a pod
- Run the cron job
- Create a secret
- Kubernetes logs
- Create config map
- Create a pod with json
- Create service
- Kubernetes service types
- Create load balancer
- Nodeport example
- Creating Kubernetes Deployment
- Update the image for deployment
- e2e application via Kubernetes
- Create Kubernetes cluster on AWS
Install minikube (Docker)
The workshop will be done on minikube, you can also use any Kubernetes cloud provider like AWS, Azure, Google..
https://minikube.sigs.k8s.io/docs/start/
Step 1 — Start cluster using Docker
Start a cluster using the docker driver:
minikube start — driver=docker
Step 2 — Start minikube
minikube start
Step 3 — Test sample command
kubectl get pod
Run a pod
A pod consist of multiple application that can be deployed to Kubernetes
Step 1 — Run the pod with the following command
kubectl run nginx — image=nginx
- First parameter is pod name
- Second parameter is the image name
Step 2 — List the pod
kubectl get pod
Create namespace
It allow to separate the containerised applications through different virtual clusters
kubectl create ns nginx-ns
Run a pod via file definition
Prepare the pod with the following definition
As a first step, you need to create a namespace
kubectl create ns nginx-ns
Create a pod with the required specifications
kubectl apply -f pod.yaml
You can check whether the pod is running in the nginx-ns
namespace
kubectl get pod --namespace=nginx-ns
Create a cron job
Let’s create a cron job with the following specifications
- name of job : cron
- time interval : every 5 minutes
- container name : busyboxcron
- image: busybox
The container is supposed to print the date in the system shell
Tip : When you see this kind of question, please search cron job in the documentation
Create the cronjob with the following yaml
You can check the pod
kubectl get pods
Create a pod with secret
Create a secret with the following command
kubectl create secret generic db-secret — from-literal=username=serkan — from-literal=password=serkan123
Let’s create a pod with the following specifications
- secret name= db-secret
- readonly = true
- pod name = mypod
- container name = redis
- image = redis
- mountPath = “/etc/foo”
kubectl apply -f pod_secret.yaml
Connect to the pod
kubectl exec mypod -it sh
Locate to the folder
cd /etc/foo
List the files
ls -l
Create a config map
Create a config map with the following command
kubectl create configmap myconfig — from-literal=dbType=DynamoDB — from-literal=progLanguage=Java
Create a POD with the following features, use the myconfig as a ConfigMap.
- pod name : podconfig
- container name : busybox
- image : k8s.gcr.io/busybox
Define the following e pod environment variables from myconfig
- DATABASE_TYPE from dbType
- PROGRAMMING_LANGUAGE from progLanguage
Use the following pod definition
Create the pod with the following command
Create a service
Please create the following pod before starting to the question
Create a Service that expose the pod on port 443. The name of the service should be myservice
Expose the pod via following command
kubectl expose pod static-web --port=443 —-name=myservice
You can check via kubectl get services
command
Create a service ( NodePort)
Please run the pod before starting to the exercise
kubectl run nginx — image=nginx
Create a service for nginx pod which serves on port 80 and the type should be NodePort. The service name should be nginx-service
Expose the pod with kubectl
command
kubectl expose pod nginx --port=80 --type=NodePort --name=nginx-service
You can check whether the service is created
kubectl get service nginx-service
Kubernetes Deployment
Please see the sample configuration for deployment
kubectl apply -f deployment.yaml
Kubernetes Deployment ( Update max.unavailable )
Please deploy the following deployment for that exercise
Deploy the provided yaml in the question
kubectl apply -f deployment.yaml
In the nginx-deployment, please make a required configuration that specifies the maximum 2 Pods that can be unavailable during the update process.
.spec.strategy.rollingUpdate.maxUnavailable is an optional field that specifies the maximum number of Pods that can be unavailable during the update process. !!!!
In order to make an update in the deployment, execute this comment
kubectl edit deployment nginx-deployment
Max Unavailable
.spec.strategy.rollingUpdate.maxUnavailable
is an optional field that specifies the maximum number of Pods that can be unavailable during the update process. The value can be an absolute number (for example, 5) or a percentage of desired Pods (for example, 10%).
Change the .spec.strategy.rollingUpdate.maxUnavailable
field in the deployment
As-is
to-be
After the change, save the file, You should see the file is edited
Kubernetes Deployment rollback
Please deploy the following deployment for that exercise
For the nginx-deployment , please update image with nginx:1.16.1 version. After the rollout is successfully finished, please rollout undo the image with previous version
As a first step, please create the deployment in Kubernetes
Update the image with nginx:1.16.1
version
kubectl set image deployment nginx-deployment nginx=nginx:1.16.1
You can check whether the change is rolled out
kubectl rollout status deployment nginx-deployment
You can rollback to previous version via following command
kubectl rollout undo deployment.v1.apps nginx-deployment
Kubernetes Service
You can create a service with the following yaml
kubectl apply -f service-xx.yaml
Kubernetes Service (Cluster IP)
This following example creates a pod with 3 replica
kubectl run hello-world –replicas=3 –labels=”run=load-balancer-example”–-image=gcr.io/google-samples/node-hello:1.0 –port=8080
Expose the pod via Service. The following command creates a service without YAML
kubectl expose deployment hello-world — type=ClusterIP — name=my-service
The application can only be accessed within your cluster. In order to access your application, we need to use port-forward
kubectl port-forward service/my-service 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Check thee application from http://localhost:8080
Kubernetes Load Balancer
Please create a following deployment before starting exercice
You can run this deployment or find from internet
kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
Expose the deployment
kubectl expose deployment hello-world — type=LoadBalancer — name=my-service
kubectl get services my-service
Congrats ! You have finished the practise lesson
Certified Kubernetes Application Developer (CKAD) Test
3 tests with 57 hands-on exam questions in order to prepare Certified Kubernetes Application Developer (CKAD) exam
Go to the course with this link!