You are browsing a read-only backup copy of Wikitech. The live site can be found at wikitech.wikimedia.org

Kubernetes/Kubernetes Workshop/Build a service application on Kubernetes

From Wikitech-static
< Kubernetes‎ | Kubernetes Workshop
Revision as of 11:36, 13 June 2022 by imported>Mbaoma
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Overview

At the end of this module, you should be able to:

  • Run a web server application on Kubernetes (minikube).
  • Understand service networking on k8s.
  • Run replicas of an application.
  • Access and create logs on a pod

Run and Access Your Service Applications on a Web Browser

Kubernetes(k8s) has extensive support for running service applications such as a web server. k8s takes care of scaling and failover for your application and provides deployment patterns, automated restarts, load balancing, and dynamic scaling, among other services.

Build and Access the Application via the Internet (Dockerfile)

In this step, you will:

  • Set up an apache web server using a Dockerfile.
  • Build your web server on Docker's Ubuntu image.

Dockerfile

You can reference the previous module for a Dockerfile refresher.

  • Create an empty directory and a Dockerfile using your preferred text editor, e.g. vim:
FROM ubuntu
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update
RUN apt-get install -y apache2
RUN echo '<html><body>Hello World</body></html>' >/var/www/html/index.html
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf 
RUN service apache2 restart
EXPOSE 80
CMD ["apachectl","-DFOREGROUND"]
  • Build and run your image on a public port:
$ docker login

$ docker build -t <your-dockerhub-username>/<given-image-name>:<tag> .
---> b9818132e617
Successfully built b9818132e617
Successfully tagged <your-dockerhub-username>/<given-image-name>:<tag>

$ docker image ls

$ docker run -p 80:80 <image_id>

Note:

  • ENV DEBIAN_FRONTEND=noninteractive stops the prompt for location.
  • If you have previously started a container using port 80, you will get an error stating the port is already allocated. In that case, stop the running containers by:
$ docker ps
$ docker kill <id>
  • Lastly, access your web server by running:
$ curl http://localhost
<html><body>Hello World</body></html>

Step 1 - Working with Kubernetes Pods

You can now run the service on minikube following the steps listed in Module 1, and return to this module to expose the pod on a public port by creating a service.

  • Run the kubectl cluster and expose the pod on port 80:
$ kubectl run <pod_name> --image=<your_username>/<image_name>:<tag> --port=80
pod/<pod_name> created
$ kubectl expose pod <pod_name> --type=LoadBalancer --port=80
service/<pod_name> exposed
  • Get the cluster’s URL:
$ minikube service <pod_name> --url
http://192.168.49.2:31688 # This is sample output, yours WILL vary

$ curl <service_url>
<html><body>Hello World</body></html>

You can also fetch the above via a web browser.

Note:

  • To find out more information about the pods and the service, checkout the cheat sheet.
  • The --port=80 parameter in the command kubectl run <pod_name> is the port that the service in the container is running (apache by default listens on port 80). In contrast, in Module 1 the application did not listen on any port.
  • The --port=80 parameter in the command kubectl expose pod <pod_name> is the port that the kubernetes service will listen on. A Kubernetes service is a distinct concept from a pod and provides a way of exposing one or more pods to users.

Hands-on Demo: Creating replicas on minikube

To assure high availability, you can run more than one service instance. You have to use a new Kubernetes (k8s) object called a deployment to achieve this. In running deployments, you can determine the number of replicas you want.

Previously, you instructed k8s to run an image; now, you will create a deployment object for the image and define its parameters, including the number of replicas.

  • Create a deployment:
$ kubectl create deployment  <deployment_name> --image=<your_username>/<image_name>:<tag> 
deployment.apps/<deployment_name> created
  • List and describe your pods and deployments e.g. if you specify “vol2” as <deployment_name>
$ kubectl get pods
NAME                    READY   STATUS             RESTARTS         AGE
vol2-779c5f4b96-7vg8q   1/1     Running            0                4s
$ kubectl get deployments
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
vol2   1/1     1            1           105s
$ kubectl describe deployment <deployment_name>
  • Update replicas:
$ kubectl scale --current-replicas=1 --replicas=<replica_number> deployment <deployment_name>
deployment.apps/<deployment_name> scaled

Note: --current-replicas is a safeguard. The point is that the action will not be taken unless the actual number of replicas matches what --current-replicas specifies.

  • Expose the web servers deployment through a service:
$ kubectl expose deployment <deployment_name> --type=LoadBalancer --port=80
service/<deployment_name> exposed
  • View your deployment’s URL:
$ minikube service <deployment_name> --url
$ curl <service_url>
<html><body>Hello World</body></html>
  • You can confirm if all replicated pods received deployments by changing the contents of any page served on pod:
$ kubectl get pods
$ kubectl exec -it <chosen_pod_name> - /bin/bash
$ cd /var/www/html
echo "<html><body>Hello 2nd World</body></html>" > ./index.html
$ exit
minikube service <deployment_name> --url
$ curl <service_url>

Note: Run the curl command according to the number of replicas you created

  • Shut down minikube after you have practiced to your satisfaction:
$ minikube stop

Next Module

Module 3: Setting up Infrastructure as Code (IaC) in Kubernetes

Previous Module

Module 1: Set up a batch application on Kubernetes