Deploy Apache Web Server on Kubernetes Cluster

This is part one of a series of lab tasks from KodeKloud for Kubernetes. Master blog listing all parts can be seen here. Make sure to follow setup instructions for minikube on your PC.

This blog lists solutions to a set of exercises from KodeKloud's training on level-3 Kubernetes/DevOps. Understanding of containerisation/Docker is a prerequisite to get Kubernetes.

Create a Namespace with a deployment and service

There is an application that needs to be deployed on Kubernetes cluster under Apache web server. The Nautilus application development team has asked the DevOps team to deploy it. We need to develop a template as per requirements mentioned below:

Create a namespace named as httpd-namespace-xfusion.

$ kubectl create namespace httpd-namespace-xfusion
# Verify
$ kubectl get namespaces # Or just 'ns'
NAME                      STATUS   AGE
default                   Active   158m
httpd-namespace-xfusion   Active   8s
kube-node-lease           Active   158m
kube-public               Active   158m
kube-system               Active   158m

Create a deployment named as httpd-deployment-xfusion under newly created namespace. For the deployment use httpd image with latest tag only and remember to mention the tag i.e httpd:latest, and make sure replica counts are 2.

Deployment specification in YAML, short descriptions are in comment:

apiVersion: apps/v1 # This changes, depends on Kind on next line
kind: Deployment # Important!
metadata:
  name: httpd-deployment-xfusion
  namespace: httpd-namespace-xfusion # Important!
  labels:
    app: httpd_app
spec:
  replicas: 2 # Number of instance of this object
  selector:
    matchLabels:
      app: httpd_app
  template:
    metadata:
      labels:
        app: httpd_app
    spec:
      containers:
        - name: httpd-container
          image: httpd:latest # Container image to tun
          ports:
            - containerPort: 80

Apply/create deployment:

$ kubectl apply -f deployment.yaml
# Verify pods
$ kubectl get pods --namespace=httpd-namespace-xfusion
NAME                                        READY   STATUS    RESTARTS   AGE
httpd-deployment-xfusion-5d8ddc848d-dfbfc   1/1     Running   0          14s
httpd-deployment-xfusion-5d8ddc848d-jctwb   1/1     Running   0          14s

Create a service named as httpd-service-xfusion under same namespace to expose the deployment, nodePort should be 30004.

Service YAML spec:

apiVersion: v1
kind: Service # Important!
metadata:
  name: httpd-service-xfusion
  namespace: httpd-namespace-xfusion
spec:
  type: NodePort
  selector:
    app: httpd_app
  ports:
    - port: 80
      targetPort: 80 # Port where requests land up!
      nodePort: 30004 # Port where service is accessed.

Create/apply service specification defined above:

$ kubectl apply -f service.yaml
# Verify service
$ kubectl get service --namespace=httpd-namespace-xfusion
NAME                    TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
httpd-service-xfusion   NodePort   10.100.213.255   <none>        80:30004/TCP   9s

Get the URL to service tunnel:

$ minikube service httpd-service-xfusion --url --namespace=httpd-namespace-xfusion
http://192.168.49.2:30004

Verify that the URL listed above will returns the default page from httpd running on the container.

Finally clean up. Delete all components created:

$ kubectl delete service httpd-service-xfusion --namespace=httpd-namespace-xfusion
service "httpd-service-xfusion" deleted
$ kubectl delete deployment httpd-deployment-xfusion --namespace=httpd-namespace-xfusion
deployment.apps "httpd-deployment-xfusion" deleted
$ kubectl delete namespace httpd-namespace-xfusion
namespace "httpd-namespace-xfusion" deleted