Spin up your first Kubernetes Cluster in under 30 mins.
Published: 2020-05-22 08:19:36

[Kubernetes is an open-source system for automating deployment, scaling, and managing containerized applications.](https://kubernetes.io/)

It provides us with a platform to orchestrate different microservices or systems while deploying. It serves a load of features to manage our resources efficiently. To containerise an application, we need a base image which can be used while creating an instance of the container.

**Docker Hub** is the centralised container image registry where we can host our images. These images can then be used for development.

###Scope of the Article:
Here, we will look at the following things:
1. Package application in a Docker container image.
2. Host the image on Docker Hub.
3. Set up a Kubernetes Cluster locally using **MiniKube**.
4. Deploy the image on a locally running Kubernetes cluster.

For the purpose of this article, let's quickly create a sample application in Node.js
Create a directory for the application where all the files will reside.

`mkdir hello-world `

Now, we will create `package.json` file to describe the app and its dependencies.

```
// package.json
{
    "name": "docker_sample_app",
    "version": "1.0.0",
    "description": "Node.js on Docker",
    "author": "akanksha priyadarshini",
    "main": "server.js",
    "scripts": {
        "start": "node server.js"
    },
   "dependencies": {
        "express": "^4.16.1"
   }
}
```
Run `npm install` to install all the dependencies for our app.

Create a `server.js` file that will define our application.

```
// server.js
'use strict';
const express = require('express');
// Constants
const PORT = 8080;
const HOST = '0.0.0.0';
// App
const app = express();
app.get('/', (req, res) => {
    res.send('Hello world');
});
app.listen(PORT, HOST);
console.log(`App is running on http://${HOST}:${PORT}`);
```

Now, let's create a file called **Dockerfile**.
[A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.](https://docs.docker.com/engine/reference/builder/)

``` touch Dockerfile ```

In this file, first we will define from what we want to build the image from. Here, we will use node version 10 available from the Docker Hub.

``` FROM node:10 ```

Next, we will specify the directory where our application code will reside inside the image. If the directory doesn't exist it will be created.

``` WORKDIR /home/node/app ```

Copy package.json and package-lock.json and install the dependencies.

```
COPY package*.json ./
RUN npm install
```

To bundle the app's source code inside the docker image, we can use COPY command.

``` COPY . . ```

Since our app binds to port 8080. We will expose this port to be mapped by docker.

``` EXPOSE 8080 ```

Lastly, specify the command to run the application.

``` CMD [ "node", "server.js" ] ```

Final Dockerfile:
```
// Dockerfile
FROM node:10
WORKDIR /home/node/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
```

Let's create .dockerignore file.
```
// .dockerignore
node_modules
npm-debug.log
```

Before we move further, head on to [Docker Hub](https://hub.docker.com/), create an account if you don't already have one.

Let's create a repository named `hello-world` here to keep our application's images to be used and shared.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2xwqubvzxcrcuqgyunor.png)

Build the docker image.
```
// sample app directory where Dockerfile is present
cd hello-world
docker build -t <your username>/hello-world .
```

The `-t` flag is used to tag the image so that it's easier to find.
Run the command `docker images` to see the list of images. It should contain the image we built above.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/v6b50ahgpotuj9qg9z32.png)

Run the image locally and verify that everything is working fine.
```
docker run -p 8000:8080 -d <your username>/hello-world
```

`-p` flag redirects a public port to a private port inside the container.
`docker ps` will give the list of running containers images.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/29q4ybihbrsdb2k32tvf.png)

In this example docker mapped the `8080` port inside of the container to the port `8000` on our machine.

Test:
``` curl -i localhost:8000 ```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3gp6kl0vfmfwtm2sgfzp.png)

We have successfully built and tested our docker image.

Let's push the locally created image to the repo we created on **Docker Hub** using the following commands.

```
// docker tag <local-image>:<tagname> <new-repo>:<tagname>
docker tag <your-username>/hello-world:latest <your-username>/hello-world:latest
// docker push <new-repo>:<tagname>
docker push <your-username>/hello-world:latest
```

We can check our repo on **Docker Hub** to verify the container image upload. In the tags section, we can see the recently pushed tag.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b3fbjnf58v6opkr139c1.png)

We can use these images for development and sharing.

Now, we will look at how to set up a Kubernetes cluster locally and deploy our application's docker image.

[Kubernetes coordinates a highly available cluster of computers that are connected to work as a single unit.](https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-intro/#kubernetes-clusters)

We will use Minikube to run Kubernetes cluster locally. You can check out how to install Minikube [here](https://kubernetes.io/docs/tasks/tools/install-minikube/).

Start minikube and create a cluster.
``` minikube start ```

You can check out more info about starting the cluster [here](https://kubernetes.io/docs/setup/learning-environment/minikube/#starting-a-cluster).

To interact with the cluster, we need a command-line tool called **kubectl** . Install and set up **kubectl** from [here](https://kubernetes.io/docs/tasks/tools/install-kubectl/).

[The Kubernetes command-line tool, kubectl, allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.](https://kubernetes.io/docs/user-guide/kubectl/)

Create a Kubernetes Deployment and use our application's docker image with it.

```
kubectl create deployment hello-kubernetes --image=<your-username>/hello-world
```

Output:

```
deployment.apps/hello-kubernetes created
```

To access the hello-kubernetes Deployment, we will expose it as a Service.

```
kubectl expose deployment hello-kubernetes --type=NodePort --port=8080
```

Output:

```
service/hello-kubernetes exposed
```

The pod `hello-kubernetes` is now launched. It takes a few seconds for the pod to get to the running state.

[A Pod (as in a pod of whales or pea pod) is a group of one or more containers (such as Docker containers), with shared storage/network, and a specification for how to run the containers.](https://kubernetes.io/docs/concepts/workloads/pods/pod/)

You can run the following command to get the list of pods.

``` kubectl get pods ```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/14ki6s4eebu7k1dolsgx.png)
Get the url of the exposed service.

``` minikube service hello-kubernetes --url ```

Now, you can curl to the url and verify the deployment.
And we are done!

![Alt text of image](https://media.giphy.com/media/aQYR1p8saOQla/giphy.gif)

But you don't have to stop here. Continue to learn and test out different features supported by Kubernetes 😊


Originally Published on my [website](https://akanksha.io/post/spin-up-your-first-kubernetes-cluster-in-under-30-mins).