Deploy javascript game using minikube in Kubernetes & docker

· Tech

This post is about creating and deploying a javascript game using minikube which is like a mini version of Kubernetes & Docker. You will learn:

Required:

VirtualBox

Minikube

kubectl

docker

More information can be found here.

All the code to be mentioned in this post can be found here.

First create the html file which has the game. Call it index.html

<!DOCTYPE html>

<html>

<head>

<title>Reaction Timer</title>

<style type=”text/css”>

body{

font-family: sans-serif;

}

#shape{

width: 200px;

height: 200px;

background-color: red;

display: none;

position: relative;

}

.bold{

font-weight: bold;

}

</style>

</head>

<body>

<h1>Test your reactions!</h1>

<p>Click on the boxes and squares as quickly as you can!</p>

<p class=”bold”>Your time: <span id=”timeTaken”></p>

<div id=”shape”></div>

<script type=”text/javascript”>

var start = new Date().getTime();

function getRandomColor() {

var letters = ‘0123456789ABCDEF’;

var color = ‘#’;

for (var i = 0; i < 6; i++ ) {

color += letters[Math.floor(Math.random() * 16)];

}

return color;

}

function makeShapeAppear(){

var top = Math.random()*400;

var left = Math.random()*400;

var width = Math.random()*200 + 100;

if(Math.random() > 0.5){

document.getElementById(“shape”).style.borderRadius = “50%”;

} else {

document.getElementById(“shape”).style.borderRadius = “0”;

}

document.getElementById(“shape”).style.width= width + “px”;

document.getElementById(“shape”).style.height= width + “px”;

document.getElementById(“shape”).style.top= top + “px”;

document.getElementById(“shape”).style.left= left + “px”;

document.getElementById(“shape”).style.display = “block”;

document.getElementById(“shape”).style.backgroundColor= getRandomColor();

start = new Date().getTime();

}

function appearAfterDelay(){

setTimeout(makeShapeAppear, Math.random()*2000);

}

setTimeout(makeShapeAppear, Math.random()*2500);

appearAfterDelay();

document.getElementById(“shape”).onclick = function(){

document.getElementById(“shape”).style.display = “none”;

var end = new Date().getTime();

var timeTaken = (end-start)/1000;

document.getElementById(“timeTaken”).innerHTML=timeTaken + “s” ;

appearAfterDelay();

}

</script>

</body>

</html>

Next step is to create a docker image for this and push it to docker hub registry so that it is accessible.

docker login

docker build -t "game:v0" .

docker images

docker tag game:v0 emailtovamos/game-repo

docker push emailtovamos/game-repo

Then minikube cluster needs to be started.

minikube start

Then we need to ensure kubectl has its context set as minikube.

kubectl config use-context minikube

Next we write the pod definition in a yaml file.

apiVersion: v1 kind: Pod metadata: name: game labels: mylabelname: game spec: containers: – name: game image: emailtovamos/game-repo:latest

This is followed by running the pod.

kubectl apply -f pod.yaml

Now this pod can’t be accessed by us directly. So we need to create a service that can act as a network endpoint for the pod.

apiVersion: v1 kind: Service metadata: name: game-service spec: selector: mylabelname: game ports: – name: http port: 80 nodePort: 30100 type: NodePort

Once the service yaml file is run, we can access the game!

kubectl apply -f service.yaml

minikube ip

Open <minikubeip>:30100

All the files are available here: https://github.com/emailtovamos/deploy-game

Interested in learning more on Kubernetes through building an end-to-end application? Click here to take my FREE course on Kubernetes. Don’t worry if you don’t know it already, this is for beginners!