Leverage bitbucket pipeline to deploy Node.js in Kubernetes

This guide will help you configure Bitbucket Pipelines to automatically deploy a containerized application to Kubernetes. We’ll create a deployment in Kubernetes to run multiple instances of our application, then package a new version of our Node.js app in a new version of a Docker image and push this image to DockerHub. Finally, we’ll update our deployment using Pipelines to release the new Docker image without a service outage.

1: Need to push your code into the bitbucket repository.
2: bitbucket-pipeline.yml
3: Dockerfile
4: Create CI/CD pipeline by using the yml file.

To build a node.js application we need to execute the below command

  • npm install
  • npm run build

The first step to do the same thing in bitbucket we need put our source code in bitbucket and create a bitbucket-pipelines.yml YAML file as below

image: node:8options:
docker: true
pipelines:
# specify the branch name
branches:
development:
- step:
caches:
- node
script:
- npm install create-react-app
- npm install --save
# Treating warnings as errors because process.env.CI = true. Most CI servers set it automatically.
- unset CI
- npm run build
# If you are using private container registry
- docker login REGISTRY_SERVER -u REGISTRY_USERNAME -p REGISTRY_PASSWORD
# tag docker image
- docker build -t tagname
# Push docker image into container registry
- docker push tagname
artifacts:
- build

we package our Node.js app as a Docker image. To do this, we have to enable Docker for our repository, build the Docker image, and then push it to a registry.

FROM node:8.11.1#maintain by 
MAINTAINER xyz #maintainer name
# 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
# start app
CMD ["sh"]

Now CI part is ready let’s complete the second phase where we will integrate Kubernetes with bitbucket and do the deployment

By using the below steps we can configure Kubernetes with bitbucket

- kubectl config set-cluster <my.cluster.name> --server=<my.kubernetes.host> --certificate-authority=/path/to/ca.pem- kubectl config set-credentials <my.user> --username=$KUBERNETES_USERNAME --password=$KUBERNETES_PASSWORD- kubectl config set-context <my.context> --cluster=<my.cluster.name> --user=<my.user>- kubectl config use-context <my.context>

For deployment, we can use below

kubectl set image deployment/<my.app> <my.app>=<my.dockerhub.username>/<my.app>:$BITBUCKET_COMMIT

DevOps Engineer with 10+ years of experience in the IT Industry. In-depth experience in building highly complex, scalable, secure and distributed systems.