Deploy to 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. 

All code snippets are also available in the example repository.

Prerequisites

You'll need to have:

  • An existing DockerHub account (or other docker registry) to push the image to

  • An existing kubernetes cluster or minikube test environment

Build your artifact

In this example we package and deploy a Node.js app. To easily build the Node.js project you can configure your bitbucket-pipelines.yml to look like this: 

bitbucket-pipelines.yml

1 2 3 4 5 6 7 pipelines: default: - step: script: # Modify the commands below to build your repository. # build and test the Node app - npm install - npm test

You can check your bitbucket-pipelines.yml file with our online validator.

Build and push a Docker image

Next, 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. In this case we'll be pushing to DockerHub

First we need a Dockerfile in the root of our repository. We'll use the following: 

Dockerfile

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 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" ]

Next, we want to build and push the Docker image to a private repository in the DockerHub registry. This means we have to also configure our DockerHub credentials as variables in Bitbucket Pipelines.

Go to your repository settings in Bitbucket and navigate to Pipelines  >  Repository variables, then add your DockerHub username and password.

Now, add those credentials to the bitbucket-pipelines.yml file, as shown below.

bitbucket-pipelines.yml

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 pipelines: default: - step: name: Test script: - npm install - npm test - step: name: Build script: - export IMAGE_NAME=$DOCKER_HUB_USERNAME/$APPLICATION_NAME:$BITBUCKET_COMMIT # build the Docker image (this will use the Dockerfile in the root of the repo) - docker build -t $IMAGE_NAME . # authenticate with the Docker Hub registry - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD # push the new Docker image to the Docker registry - docker push $IMAGE_NAME services: - docker

We use an environment variable for APPLICATION_NAME, but you can configure your own or even statically define it.

Create a deployment to run multiple instances of your application in Kubernetes

This is a one time manual process performed outside of Pipelines to configure your Kubernetes cluster and assumes that you have already configured kubectl to tell it where your Kubernetes server is. See the 'Configure kubectl' section below for an example.

To practice continuous deployment using Bitbucket Pipelines we must first configure a deployment to run our application in Kubernetes. Create a deployment by running the following command using the kubectl command line interface: 

Note that you need to replace the following placeholders with the values you used above: 

  • <my.dockerhub.username>  

  • <my.app>

1 kubectl run <my.app> --labels="app=<my.app>" --image=<my.dockerhub.username>/<my.app>:latest --replicas=2 --port=8080

Your application is now running in Kubernetes but it won't be accessible externally until it's exposed as a Kubernetes service. This is not required in order to update the application using Pipelines however and so it's outside the scope of this example.

Summary

All the steps above result in the following complete bitbucket-pipelines.yml file.

bitbucket-pipelines.yml snippet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 pipelines: default: - step: name: Test script: - npm install - npm test - step: name: Build script: - export IMAGE_NAME=$DOCKER_HUB_USERNAME/$APPLICATION_NAME:$BITBUCKET_COMMIT # build the Docker image (this will use the Dockerfile in the root of the repo) - docker build -t $IMAGE_NAME . # authenticate with the Docker Hub registry - docker login --username $DOCKER_HUB_USERNAME --password $DOCKER_HUB_PASSWORD # push the new Docker image to the Docker registry - docker push $IMAGE_NAME services: - docker - step: name: Deploy deployment: production script: - sed -i "s|{{image}}|$DOCKER_HUB_USERNAME/$APPLICATION_NAME:$BITBUCKET_COMMIT|g" deployment.yml - pipe: atlassian/kubectl-run:1.1.2 variables: KUBE_CONFIG: $KUBE_CONFIG KUBECTL_COMMAND: 'apply' RESOURCE_PATH: 'deployment.yml'

Remember, you can check your bitbucket-pipelines.yml file with our online validator.

Additional Help