Deploy Super-Mario in Production Ready K8s Environment with Kops.

Deploy Super-Mario in Production Ready K8s Environment with Kops.

·

4 min read

Author: Ujwal Pachghare

In this tutorial, we will walk through the process of deploying a Mario game on Kubernetes (k8s) nodes. This includes creating the necessary manifests, applying them, and testing the deployment.

In the previous tutorial we have deployed Super-Mario game in docker with the help of multi container manager docker compose and inspection technique. if your interested you follow this tutorial “Deploy Super-Mario on Docker with Docker Compose by using Inspecting Technique” here.

In this tutorial the only prerequisite is to have fully setup a Kubernetes cluster with Kops. if you wanna learn how to setup highly available local Kubernetes cluster with Kops, you can follow this tutorial “Setting Up Local Kubernetes Cluster with Kops” here.

Step 1: Confirm Kubernetes Setup

In the previous tutorial we learn that ap-south-1c availability zone doesn`t support k8s deployment. so make the cluster in North Verginia region, because this region provide us 6 availability zones which helps to create highly available Kubernetes environment.

let’s ensure that all Kubernetes objects and services are up and running. Use the following command:

kubectl get all -A

Ensure your master node is connected to slave nodes:

kubectl get nodes -o wide

Step 2: Create Kubernetes Manifests

Next, we’ll create a new directory and namespace for storing our Kubernetes manifests:

mkdir mario && cd mario
kubectl create ns mario

Now, we’ll create several YAML files for our deployment:

Deployment Manifest

cat << EOF > mario-deploy.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mario-deploy
  labels:
    app: mario-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: mario-app
  template:
    metadata:
      labels:
        app: mario-app
    spec:
      containers:
      - name: mario-con
        image: uj5ghare/mario:80
        ports:
         - containerPort: 80
        volumeMounts:
          - name: mario-vol
            mountPath: /volume
      volumes:
        - name: mario-vol
          persistentVolumeClaim:
            claimName: mario-pvc

EOF

we will create Deployment Manifest for providing Auto Healing feature to our pods and application. Deployment will ensure that our pods are running and in healthy condition it will reduce the downtime of our application by 80–90 percent, in some cases it provide zero downtime.

Service Manifest

cat << EOF > mario-svc.yml

apiVersion: v1
kind: Service
metadata:
  name: mario-svc
spec:
  selector:
    app: mario-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

EOF

we will apply Service on deployment to expose our application outside the world. Service gave power to decide from where our application should be accessed. it could be inside the cluster only with ClusterIP, outside the nodes only by IP with NodePort, and expose to public with LoadBalancer DNS.

StorageClass Manifest

cat << EOF > mario-sc.yml

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: mario-sc
provisioner: kubernetes.io/aws-ebs
parameters:
  type: gp2
  fsType: ext4

EOF

StorageClass is work as shelfs for persistence volume claim. we can create different shelfs with deferent types of configurations. Ex: 8GB SSD and 8GB HDD

we just have to connect PVC to the StorageClass other things will be handled by Kubernetes. so, Kubernetes will create PVC for us according to our StorageClass configuration automatically.

Persistent Volume Manifest

cat << EOF > mario-pvc.yml 

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mario-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: my-storage-class
  resources:
    requests:
      storage: 4Gi

EOF

PV means persistent storage which helps prevent data loss. we can create PV also but according to our situation we don't need to create PV because we already have defined storage class so it will take care of the volume.

PVC gave power to apply persistent volume according to our needs. we can define how much persistent volume can be claimed by our application pods.

Horizontal Pod AutoScaller Manifest

cat << EOF > mario-hpa.yml 

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: mario-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: mario-deployment
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 50

EOF

The Horizontal Pod Autoscaler (HPA) plays a significant role in Kubernetes. It is responsible for the autoscaling of application pods.

Kubernetes does not offer a built-in HPA tool. To enable HPA in our cluster, we need to install a third-party tool called Metric Server, which will automatically scale our pods.

This tool helps reduce downtime and increase the durability and high availability of our application pods.

Install Metric-Server

kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.4.1/components.yaml

Step 3: Apply the Manifest

With our YAML files ready, we can apply the manifests. To apply all the manifests in Mario namespace run this command:

kubectl apply -f . -n mario

Step 4: Confirm all Objects are Up and Running

Let’s confirm that all objects and services are up and running:

kubectl get all -n mario

Step 5: Test the Deployment of Mario

To test our deployment, we’ll get the service for our Mario game:

kubectl get svc mario-svc -n mario

Copy the Load Balancer DNS (which is the External IP) and paste it into a browser to play the game!

If you wanna expose service with NodePort, so just change the Service type LoadBalancer to NodePort and then apply changes again.

make sure you have to copy public IP of slave nodes to test the deployment and then paste in on web browser. you can see this type of UI.

Step 6: Clean Up

Once you’re done, you can delete all manifests:

kubectl delete -f . -n mario

And finally, delete the cluster:

kops delete cluster --name=$KOPS_CLUSTER_NAME --state=$KOPS_STATE_STORE --yes

And that’s All! You’ve successfully deployed a Mario game on Kubernetes nodes.

Did you find this article valuable?

Support AutOps by becoming a sponsor. Any amount is appreciated!