Skip to content

Latest commit

 

History

History
186 lines (127 loc) · 5.32 KB

2.-prod-deployment.md

File metadata and controls

186 lines (127 loc) · 5.32 KB

Initial setup

This is a step-by-step guide to deploy this (Kusama API) service in a minikube cluster. Internally, at LocalCoinSwap, we have deployed to AWS EKS with Fargate. There's a separate documentation for it. This document is meant for more general use.

Minikube kubernetes cluster setup

Although some familiarity with Kubernetes would help understand and make sense of the commands we run, this guide assumes minimal pre-requisites.

Install command-line utilities

Install minikube

Linux:

curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube
sudo install minikube /usr/local/bin

macOS:

brew cask install minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && chmod +x minikube
sudo mv minikube /usr/local/bin

Build docker image of the service for production

We'll use Docker Hub to host our kusama-api image. Let's initialize a few env variables for that.

export CONTAINER_REPO=ivarojha # Update this with your dockerhub username
export IMAGE_NAME=kusama-api
export COMMIT_HASH=$(git rev-parse --short HEAD)
export CLUSTER_NAME=kusamacluster

Build the prod image

CONTAINER_REPO=$CONTAINER_REPO IMAGE_NAME=$IMAGE_NAME COMMIT_HASH=$COMMIT_HASH docker-compose -f docker-compose.prod.yaml build

Push to the built image to docker hub

docker push $CONTAINER_REPO/$IMAGE_NAME:$COMMIT_HASH

Create minikube cluster

minikube start -p $CLUSTER_NAME --extra-config=apiserver.enable-swagger-ui=true --alsologtostderr

CLUSTER_NAME deliberately doesn't contain - because that's not supported. Be patient, this command will take some time.

You can open the cluster dashboard for visual overview. Run this in a new terminal tab.

minikube -p $CLUSTER_NAME dashboard

Create a deployment in cluster

Check the file kubernetes/kusama-api-deployment.yaml in the repo. It is parameterised but there's a strightforward way to pass env variables to it, so we make use of envsubst package.

envsubst < kubernetes/kusama-api-deployment.yaml | kubectl apply -f -
Example output
[ravi@ojha-thinkpad:~/_code_/lcs/kusama-api]$ envsubst < kubernetes/kusama-api-deployment.yaml | kubectl apply -f -
deployment.apps/kusama-api-deployment created

If everything went well, you should see two new pods in the cluster. Check the dashboard or use kubectl get pods in terminal.

Scaling horizontally

Let's say we want to increase the number of pods running in the cluster. We can easily scale by running:

kubectl scale --replicas=3 deployment kusama-api-deployment

Here kusama-api-deployment is the name of our deployment. It is defined in kubernetes/kusama-api-deployment.yaml file.

Attach a load balancer

Depending on the vendor and configuration, there maybe different ways to attach the load balancer. In minikube, we can create a load balancer service with following config. Internally, we have deployed on AWS Kubernetes Cluster and we used Ingress controller to route the traffic inside cluster.

apiVersion: v1
kind: Service
metadata:
  name: lb
  labels:
    name: lb
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    name: kusama-api
  type: LoadBalancer

For now, let's create a LoadBalancer service.

kubectl apply -f kubernetes/minikube-kusama-api-service.yaml
Example output
[ravi@ojha-thinkpad:~/_code_/lcs/kusama-api]$ kubectl apply -f kubernetes/minikube-kusama-api-service.yaml
service/lb created

Accessing API endpoint

First, get the cluster IP.

[roy@miller-thinkpad:~/_code_/lcs/kusama-api]$ minikube -p $CLUSTER_NAME ip
172.17.0.3

Then, get the port exposed by loadbalancer in your local by kubectl get services

[ravi@ojha-thinkpad:~/_code_/lcs/kusama-api]$ kubectl get services
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1       <none>        443/TCP        67m
lb           LoadBalancer   10.98.178.131   <pending>     80:31072/TCP   25s

Try to access the endpoint http://172.17.0.3:31072/HeartBeat

[roy@miller-thinkpad:~/_code_/lcs/kusama-api|master *]$ curl "http://172.17.0.3:31072/HeartBeat"
true

That's it! We have successfully deployed within a minikube cluster on local. This is just a one-time setup. Follow along for how to make future builds and deployments.

Deploying new image to production as pods

Building for production

export CONTAINER_REPO=localcoinswap # Update this with your dockerhub username
export IMAGE_NAME=kusama-api
export COMMIT_HASH=$(git rev-parse --short HEAD)
CONTAINER_REPO=$CONTAINER_REPO IMAGE_NAME=$IMAGE_NAME COMMIT_HASH=$COMMIT_HASH docker-compose -f docker-compose.prod.yaml build

Push the image to container repo

docker push $CONTAINER_REPO/$IMAGE_NAME:$COMMIT_HASH

Initiate a rolling deployment

kubectl edit deployments.apps/kusama-api-deployment

It'll open up default editor. Look for the container repo and image name. Change the image tag and save & exit. It will start rolling deployment without causing any outage.