Deploying an Application to Kubernetes on GCP - DevOps Project

Deploying an Application to Kubernetes on GCP - DevOps Project

Deploying a Node Application to GKE.

In this Blog Post, I will be showing you how you can deploy your Node application on Google Cloud using GKE (Google Kubernetes Engine) and GCR (Google Container Registry).

Pre-Requisites

  1. Docker

  2. Google Cloud SDK and CLI

  3. Basic Knowledge of YAML, GCP, Docker and Kubernetes.

  4. VSCode with GCP, Docker and Kubernetes extensions installed.

  5. NodeJS must be installed in your local system.

Tech Stacks Used

  1. NodeJs

  2. Kubernetes

  3. Docker

  4. GCP

Steps 🪜

1. Create a Kubernetes Cluster

We will create a standard cluster with my-first-cluster option in the Kubernetes Navigation Panel.

My First Cluster

2. Build the app

While the cluster is building, we will create our Node js application

  1. Make a new directory, you can name it nodeapp and open VSCode here.

  2. Initialize npm in the terminal.

    npm init -y

  3. Install Express

    npm i express

  4. Create a new file app.js

     const express = require("express")
     const app = express();
     app.get("/", (req, res)=>{
         res.send("Server is up and running")
     })
    
     app.listen(80, ()=>{
         console.log("Server is up")
     })
    
  5. Run the JS file

    node app.js

  6. Check if the server is running on localhost or not.

3. Authorizing into GCP

  1. Login into GCP using the command

    gcloud auth login

  2. Enable Docker

    gcloud auth configure-docker

4. Building the Dockerfile and Pushing it to GCP

  • Create a new file and Name it Dockerfile {With No Extensions}
FROM --platform=arm64 node:14

WORKDIR /usr/app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 80

CMD ["node", "app.js"]
  • Build the Image

    docker build -t us.gcr.io/<PROJECT_ID>/nodeapp:v1 .

    Replace the <PROJECT_ID> with your ID that you can find by clicking on it.

  • Push the image to GCP

    docker push us.gcr.io/<PROJECT_ID>/nodeapp:v1

5. Creating the Configuration Files

  • Make a new directory named K8s and cd into it.

  • Write Kubernetes manifest file deployment.yml for deployment.

apiVersion: apps/v1

kind: Deployment
metadata:
  name: nodeappdeployment
  labels:
    type: backend
    app: nodeapp
spec:
  replicas: 1
  selector:
    matchLabels:
      type: backend
      app: nodeapp
  template:
    metadata:
      name: nodeapppod
      labels:
        type: backend
        app: nodeapp
    spec:
      containers:
        - name: nodeappcontainer
          image: us.gcr.io/macro-spider-376408/nodeapp:v1
          ports:
            - containerPort: 80
  • Write Kubernetes manifest file service.yml for service.
apiVersion: v1
kind: Service
metadata:
  name: nodeapp-load-balancer-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 80
  selector:
    type: backend
    app: nodeapp

6. Connecting the Cluster to the image and applying the manifest files.

  1. Apply manifest file to create deployment

    kubectl apply -f deploy.yml

  2. Check the status of the deployment.

    kubectl get deploy

  3. Apply manifest file to create load balancer service.

    kubectl apply -f service.yml

  4. Check the status of service.

    kubectl get svc

  5. Copy the external IP and paste it into your browser, You should see the same things you did on the localhost at the start of this tutorial.

Source Code🧑‍💻

If you want to check my repository out, you can do so by clicking on the following link. Also, Star the Repository while you are at it🙃.

Repository - All the Source Code

Conclusion😊

That was it, You just created a docker image of a Node App and deployed it to the Google Kubernetes Engine. BE PROUD OF YOURSELF !!!

If you found this Project interesting, then please like this blog and Star my Repository on GitHub.

Special thanks to Tushar Rajpoot on YouTube.

Did you find this article valuable?

Support Varchasv Hoon by becoming a sponsor. Any amount is appreciated!