There are many ways to host node js apps. A popular way is to host your app on Heroku. We can also deploy our app to Google cloud platform on container engine using Kubernetes. In this blog, we will learn how to deploy node js app on container engine with kubernetes. There are many resources on the web but we will use YAML files to create a deployment and to build docker image we will not use Google container registry (GCR) as it will cost us more for the deployment. To deploy we will start by creating an account on Google cloud and you can get a free tier of Google cloud platform worth 300$ for 12 months. After creating account create a project with any name of your choice. Enable Google cloud shell from an icon on right top.
We will also need a docker image of our app for deployment. To create a docker image first create an account on https://www.docker.com and create a repository with any name on it. Now, we will add docker file into our repository so that we can build docker image. Dockerfile will contain this code:
FROM node:boron # Create app directory RUN mkdir -p /usr/src/app WORKDIR /usr/src/app # Install app dependencies COPY package.json /usr/src/app RUN npm install # Bundle app source COPY . /usr/src/app EXPOSE 8080 CMD [ "npm", "start" ]
You can see an example of docker file in SUSI telegram repository. After pushing docker file to your repository we will now build docker image of the app. In Google cloud shell clone, your repository with git clone {your-repository-link here }and change your current directory to the cloned app. We will use –no-cache -t arguments as described above it will be better for building docker image and it will use fewer resources. Run these two commands to build docker image and pushing it to your docker hub.
docker build --no-cache -t {your-docker-username-here}/{repository-name-on-docker here} . docker push {your-docker-username-here}/{repository-name-on-docker here}
We have successfully created docker image for our app. Now we will deploy our app to container engine using this image. To deploy it we will use configuration files. Add a yaml folder in your repository and we will add four files into it now. In the first file, we will specify the namespace and name it as 00-namespace.yml It will contain following code:
apiVersion: v1 kind: Namespace metadata: name: web
In second file we will configure our namespace that we specified and name it as configmap.yml It will contain following code:
apiVersion: v1 metadata: name:{name-of-your-deployment-here} namespace: web kind: ConfigMap
In third file, we will define our deployment and name it as deployment.yml It will contain following code:
kind: Deployment apiVersion: apps/v1beta1 metadata: name: {name-of-your-deployment-here} namespace: web spec: replicas: 1 template: metadata: labels: app: {name-of-your-deployment-here} spec: containers: - name: {name-of-your-deployment-here} image: {your-docker-username-here}/{repository-name-on-docker here}:latest ports: - containerPort: 8080 protocol: TCP envFrom: - configMapRef: name: {name-of-your-deployment-here} restartPolicy: Always
In fourth file, we will define service for our deployment and name it as service.yml It will contain following code:
kind: Service apiVersion: v1 metadata: name: {name-of-your-deployment-here} namespace: web spec: ports: - port: 8080 protocol: TCP targetPort: 8080 selector: app: {name-of-your-deployment-here} type: LoadBalancer
After adding these files to repository we will now deploy our app to a cluster on container engine. Go to Google cloud shell and run the following commands:
gcloud config set compute/zone us-central1-b
This will set zone for our cluster.
gcloud container clusters create {name-your-cluster-here}
Now update in your repository with git pull as we have added new files to it. Run the following command to make your deployment and to see it:
kubectl create -R -f ./yamls
This will create our deployment.
kubectl get services --namespace=web
With above command, you will get external IP for your app and open that IP in your browser with the port. You will see your app.
kubectl get deployments --namespace=web
Run this command if available is 1 then it means your deployment is running the file.
You have successfully deployed your app to container engine using kubernetes.
Resources
Tutorial on Google cloud: https://cloud.google.com/container-engine/docs/tutorials/hello-node
Tutorial by Jatin Shridhar: https://www.sitepoint.com/kubernetes-deploy-node-js-docker-app/
You must log in to post a comment.