Skip to main content

Command Palette

Search for a command to run...

Deploying a microservice using Kubernetes

Microservice deployment using kubernetes and docker

Published
4 min read
Deploying a microservice using Kubernetes

Introduction

This blog assumes you have a basic understanding of Docker and Kubernetes. If you want to read more about Kubernetes, i would recommend this very good article

Kubernetes is a container orchestration platform, what this means is that Kubernetes is responsible for maintaining the entire lifecycle of a container, this includes spinning up new containers and shutting down containers when needed.

In this blog, we're going to deploy a simple microservice in kubernetes. So let's get started. All the code from this blog is available here.

Microservice Docker Image

First of all we're going to need a microservice which we can use for this demo. I've created such a service and pushed it to dockerhub. It's available as a docker image here. We have exposed this service on port 5000 and the endpoint is /check. Before we start with kubernetes deployment let's just run this docker image locally and verify this. We'll start a container and port forward it using port 5000 locally by running this command in the terminal(make sure you have docker running)

docker run -d -p 5000:5000 sandyjswl/hello-world:v1.0

you should see an output like this

Screenshot from 2021-11-08 12-15-34.png

So now we have the container up and running. Let's hit the endpoint and see the response.

curl http://localhost:5000/check

you should see the following result

Screenshot from 2021-11-08 12-16-12.png

So now we have verified that the docker image is working correctly. Let's move onto the next part which is creating a kubernetes deployment.

Kubernetes Deployment

Before we start with the kubernetes part,we need to setup a kubernetes cluster locally. We can do this using minikube. Follow this till the second step to setup minikube and start it. Next, to interact with minikube we need the kubernetes command like tool kubectl, follow this to install kubectl on your machine.

So now we're going to create a Kubernetes deployment for our service. To do this we're going to create a deployment manifest. Copy this code block and paste it in a file called hello-world-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
  labels:
    app: hello-world
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: sandyjswl/hello-world:v1.0
        ports:
        - containerPort: 5000

In this code block we're creating a Deployment and naming it hello-world-deployment and providing it a label called hello-world, this label will be used in the service (which we will create later) to reference this deployment. We're also setting replicas to 3, so this will create 3 pods. And finally we're using the sandyjswl/hello-world image to create this containers. This is the same docker image we used above. Once we've saved the file, let's run the following command from the same folder where the hello-world-deployment.yaml is located to create the deployment.

kubectl apply -f hello-world-deployment.yaml

Once this is done, let's check the pods using this command

kubectl get pods

you should see something like this

Screenshot from 2021-11-08 12-37-46.png

Kubernetes Service

Next, to connect to the pods we'll create a service of type LoadBalancer this will help us interact with the pods and hit the endpoint. Let's copy this code block and paste it in a file called hello-world-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: hello-world-service
spec:
  selector:
    app: hello-world
  ports:
    - port: 5000
      targetPort: 5000
  type: LoadBalancer

Here we're creating a Service called hello-world-service and in the selector we're using hello-world, this is the same label which we have given our deployment. In the ports we're using the targetPort as 5000 and the service type is of LoadBalancer. Let's create this service

kubectl apply -f hello-world-service.yaml

Once this is done, we can check the service created using the following command

kubectl get service

Screenshot from 2021-11-08 12-45-23.png

So now that we have deployed the application. Let's try to access it. To do this we need the service url because the service is how we communicate with the pods.

minikube service hello-world-service --url

This command with give ur a url something like http://192.168.49.2:31635, then we can just curl the url and see if it returns Hello World! as response.

curl http://192.168.49.2:31635/check

That's all for this blog. See you next time!