Deploy to Kubernetes on GKE

Deploy to Kubernetes on GKE

ยท

2 min read

Tech used:

  • Node.js

  • Docker

  • Kubernetes

  • GKE(Google Kubernetes Engine)

  • GCR(Google Container Registry)

Steps

  • Create kubernetes cluster on GKE.

  • Setup Connection to created GKE cluster in with your local machine or cloud shell.

      gcloud container clusters get-credentials <CLUSTER_NAME> --zone <ZONE> --project <PROJECT_ID>
    
  • Create a simple nodejs/express application.

  • Write Dockerfile for the application

      FROM --platform=linux/amd64 node:14
      WORKDIR /usr/app
      COPY package.json .
      RUN npm install
      COPY . .
      EXPOSE 80
      CMD ["node","app.js"]
    
  • Build the Docker image

      docker build -t us.gcr.io/<PROJECT_ID>/imagename:tag .
    
  • Push docker image to GCR(Google Container Registry)

      docker push us.gcr.io/<PROJECT_ID>/imagename:tag
    
  • Test the application using docker.

      docker run -d -p 3000:80 us.gcr.io/<PROJECT_ID>/imagename:tag
    
  • Write kubernetes manifest file for deployment. deploy.yml

      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: nodecontainer
                image: us.gcr.io/<PROJECT_ID>/imagename:tag
                ports:
                  - containerPort: 80
    
  • Write kubernetes manifest file for service. service.yml

      kind: Service
      apiVersion: v1
      metadata:
        name: nodeapp-load-service
      spec:
        ports:
          - port: 80 
            targetPort: 80
        selector:
          type: backend
          app: nodeapp  
        type: LoadBalancer
    
  • Apply manifest file to create deployment.

      kubectl apply -f deploy.yml
    
  • Check status of the deployment.

      kubectl get deploy
    
  • Apply manifest file to create load balancer service.

      kubectl apply -f service.yml
    
  • Check status of service.

      kubectl get svc
    
  • Check the external IP of the service in the browser.

Did you find this article valuable?

Support KubeKode Blogs by becoming a sponsor. Any amount is appreciated!

ย