Practice Session 3: Working with Kubernetes
Make sure that you have already gone through Lab-02
The aim of this practice session is to get acquainted with multiple application service composition, orchestration, and deploying using Kubernetes respectively. You are going to deploy the Kubernetes cluster on the ETAIS private cloud. You will also learn to orchestrate and deploy application services using Kubernetes CLI known as kubectl. Kubernetes container deployment has several advantages such as high availability, scalability, and faster CI/CD service deployment in the DevOps ecosystem.
References
Referred documents and websites contain supportive information for the practice.
Manuals
Introduction to Kubernetes
Kubernetes is an open-source container orchestration platform that automates the biggest part of the manual processes involved in deploying, managing, and scaling containerized applications.
Kubernetes provides you with a platform for scheduling and running containers in clusters of physical or virtual machines. The Kubernetes architecture divides the cluster into components that work together to maintain the defined state of the cluster.
The Kubernetes cluster is a set of node machines for running container applications. The clusters can be understood as two parts: a management layer and computing machines or nodes. Each node has its own environment and can be either a physical or a virtual machine. Each node run pods consisting of containers.
Rancher
Rancher is an open-source software product to manage Kubernetes clusters. This includes not only managing existing clusters but building new clusters as well. Rancher is a good tool to use if you have a lot of clusters to manage, with users that are in multiple projects across clusters. This allows you to manage the users in one location and apply to all the projects. It also provides a “single pane of glass” for looking at clusters and configurations. It provides a secure, safe, and robust container environment, balanced with the need for developers to run code on their local desktops, as it would run in production.
Exercise 1. Installing Kubernetes cluster using Rancher.
In this task, you will configure the Kubernetes nodes using Rancher on CentOS 7.
Configuring Docker
- Create 3 virtual machines (image = Centos 7, flavor = m3.xsmall) - <student_name>-rancher, <student_name>-master and <student_name>-worker as carried out in Lab-01 and connect to the virtual machine remotely using git bash or any other SSH client.
- Make sure you have
default
,ssh
,ping
,web
andallow-all
security groups added. - Install docker
- Next steps have to be implemented on all three machines!
- NB! Extra modifications are needed:
- Create a directory in the virtual machine in the path:
sudo mkdir /etc/docker
- Create a file in the docker directory:
sudo vi /etc/docker/daemon.json
- Copy the following script:
- Create a directory in the virtual machine in the path:
{ "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2", "storage-opts": [ "overlay2.override_kernel_check=true" ], "default-address-pools": [{"base":"172.80.0.0/16","size":24}] }
- Install the latest version of Docker:
curl -fsSL https://get.docker.com/ | sh
- After installation is complete, run the Docker daemon:
sudo systemctl start docker
- Verify if Docker is running:
sudo systemctl status docker
- Verify if Docker is running:
- Enable Docker to start on boot:
sudo systemctl enable docker
- Install the latest version of Docker:
NB! To run docker commands with non-root privileges
- Create a docker group (If it's already created then ignore):
sudo groupadd docker
- Add a user to docker group:
sudo usermod -aG docker $USER
- Activate the changes:
newgrp docker
- Create a docker group (If it's already created then ignore):
- Check the installation by displaying docker version:
docker --version
Configuring Rancher node
NB! Following steps are to be implemented only on <student_name>-rancher machine!
- Enter this command to run the Rancher container:
sudo docker run -d --restart=unless-stopped -p 80:80 -p 443:443 --privileged rancher/rancher
- Go to the web browser on your host machine and hit
https://<SERVER_IP_Rancher_Node>
- Follow the wizard for initial setup.
Once it is done, you should be able to see the dashboard with local
as the cluster name.
Create a new cluster
- Go to the browser and access Rancher Dashboard.
- From the dashboard, click on
Create
. - Click on
Custom
. - Give Cluster Name as
cluster-1
and skip other fields. - Click on
Next
- Select etcd, Control Plane, Worker
- Copy the registration command in output.
- The command should look like this:
sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.6.0 --server https://172.17.90.86 --token bflxwjlsvsrtbnvp8nj5xq82xx2sr5npjsxm82mbtdcfkc9g65x5d8 --ca-checksum f9bc8c23fff67155023fde69026ec83a77f632657f1049fd6ca9ae5732cf59d3 --etcd --controlplane --worker
- Go to master node (<student_name>-master) terminal and execute the copied registration command
- At this point, you need to wait for few minutes...
- After this, you will see in the browser that
1 New node has registered
. - Now click on
Done
. - Repeat Step 6 to get the registration command for the worker node. For the worker node, you just need to select Worker.
Exercise 2. Preparing the Dockerfile on Rancher Node.
- Login to Docker
- Refer to Lab2 if you don't remember how to do that.
- Create a new directory:
mkdir lab3 && cd lab3
- Create the Dockerfile and fill it with the following:
FROM nginx:1.16-alpine COPY index.html /usr/share/nginx/html/index.html
- Create the
index.html
file with the following content:
<head> <title>Welcome to nginx!</title> <style> body { width: 80%; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <script> function myFunction() { var x = location.hostname; document.getElementById("demo").innerHTML= x; } </script> <button onclick="myFunction()">Get Hostname/IP</button> <p id="demo"></p> <pre> DevOps Course > Prac3 > You are now accessing the simple Nginx web app !!! </pre> <p>If you see this page, the nginx web server is successfully installed and working. Further configurationsupport please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
- Add your name to the
index.html
file so it could be clearly seen on a webpage later. - Build a docker image and push it to the Docker Hub. Provide it with a
nginx-alpine
tag.- Refer to Lab2 in case you don't remember how to do that.
Introduction to YAML
YAML, which stands for Yet Another Markup Language (or, in other sources, YAML Ain’t Markup Language), is a human-readable text-based format for specifying configuration-type information. It is much more convenient than writing all the configuration into the CLI for obvious reasons, and it also allows you to create more complex structures than you could do using the command line. YAML is a superset of JSON, which means that any valid JSON file is also a valid YAML file. Overall, YAML is very easy to understand and learn, so let's try it in practice. Here is the link where you can find additional information about YAML and how it is used in Kubernetes.
Exercise 3. Create a deployment.
- Navigate to the Rancher Dashboard.
- Go to the cluster you've created in previous steps.
- Click on Import YAML (top right corner).
!! You need to add your DOCKER_HUB_ACCOUNT_NAME/<Your_Docker_Image> at image object in the below yaml file.
- Paste the following code:
apiVersion: apps/v1 kind: Deployment metadata: name: mynginx-deployment labels: app: mynginx spec: replicas: 2 selector: matchLabels: app: mynginx template: metadata: labels: app: mynginx spec: containers: - name: mynginx image: <DOCKER_HUB_ACCOUNT_NAME>/<Your_docker_image>:<TAG_NAME> ports: - containerPort: 80
Now it is time to get acquainted with kubectl, the Kubernetes command-line tool, which allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.
- Open
kubectl shell
(top right corner).
- Check if deployments were created:
kubectl get deploy
- Check which pods were brought up:
kubectl describe deploy
- List the pods:
kubectl get pods
- Get the additional info about pods:
kubectl describe pods
Using the command from the last step, get an IP of pods and curl it from inside the VM. You should be able to see the webpage: curl <IP-address-of-a-pod>
.
Exercise 4. Exposing a deployment.
- Navigate to the Rancher Dashboard.
- Go to the cluster that we have created.
- Open kubectl shell.
- Expose the service to outside:
kubectl expose deployment mynginx-deployment --name=mynginx-app-service --type=LoadBalancer --port 8080 --target-port 80
- Get the
NodePort
from the output ofkubectl describe services mynginx-app-service
command.
Now visit <masternode_ip>:<obtained_NodePort
>.
The final output should look like this:
- Take a screenshot of a webpage where your name and IP address are clearly seen.
Exercise 5. Deploy a flask application using kubectl
Here you are going to use kubectl
and deploy your flask application using the docker image built during Practice session 2- Excerices 4.
- Open kubectl shell, then create YAML file that includes deployment and service objects using vi editor
vi flask.yaml
and modify the file according to your dockerhub image name.
apiVersion: apps/v1 kind: Deployment metadata: name: flask-deployment spec: selector: matchLabels: app: flask replicas: 1 template: metadata: labels: app: flask spec: containers: - name: flask image: your-image ports: - containerPort: 5000 hostPort: 5000 --- apiVersion: v1 kind: Service metadata: name: flask-service spec: ports: - port: 5000 targetPort: 5000 name: http selector: app: flask
- Now deploy it using
kubectl apply -f flask.yaml
- Check the deployment and services using
kubectl get deployment
, andkubectl get service
- Open the app using http://<Master_VM_IP>:5000
- Take a screenshot of a webpage where your name and IP address are clearly seen.
Deliverables
- Upload the screenshot taken wherever mentioned
- Pack the screenshots into a single zip file and upload them through the following submission form.
- Your instance must be terminated!
Deadline: 01 Oct 2021
3. Lab 3