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.

要件

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
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
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 environment variables in Bitbucket Pipelines.

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

Now, add those credentials to the bitbucket-pipelines.yml file, as shown below. Note that you need to replace the following placeholders with your own details: 

  • <my.dockerhub.username> 
  • <my.app>  (the name of your Docker image)
bitbucket-pipelines.yml
# enable Docker for your repository
options:
  docker: true
 
pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          # build and test the Node app
          - npm install
          - npm test
          - export IMAGE_NAME=<my.dockerhub.username>/<my-app>:$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

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 practise 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>
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.

Install deploy tools

Our deployment script requires kubectl to be installed in our Pipelines build container:

bitbucket-pipelines.yml snippet
- curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
- chmod +x ./kubectl
- sudo mv ./kubectl /usr/local/bin/kubectl

Configure kubectl

This example uses basic auth to authenticate requests to the Kubernetes server. Stronger forms of authentication such as client certificates or bearer tokens are recommended for production systems.

Before you can update the deployment, you must configure kubectl to tell it where your Kubernetes server is. Note that you need to replace the following placeholders with your own details: 

    • <my.cluster.name> (the name you want to associate with your Kubernetes server)
    • <my.kubernetes.host> (the hostname and port of your Kubernetes server, for example: https://mydomain.com:8443)
    • <my.user> (the name you want to associate with a configured user)
    • <my.context> (the name of the context used to reference your server as a particular user)

You must also create a user in Kubernetes and set their credentials in the $KUBERNETES_USERNAME and $KUBERNETES_PASSWORD Pipelines secured variables.

bitbucket-pipelines.yml snippet
- kubectl config set-cluster <my.cluster.name> --server=<my.kubernetes.host>
- 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>

Update the deployment

Finally, to have Pipelines update your application on every build we add a kubectl command to our bitbucket-pipelines.yml file to update our Kubernetes deployment with the new Docker image.

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

  • <my.dockerhub.username>  
  • <my.app>
bitbucket-pipelines.yml snippet
- kubectl set image deployment/<my.app> <my.app>=<my.dockerhub.username>/<my.app>:$BITBUCKET_COMMIT

概要

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

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

  • <my.dockerhub.username>  
  • <my.app>
bitbucket-pipelines.yml
# enable Docker for your repository
options:
  docker: true
 
pipelines:
  default:
    - step:
        script: # Modify the commands below to build your repository.
          # build and test the Node app
          - npm install
          - npm test
          - export IMAGE_NAME=<my.dockerhub.username>/<my.app>:$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
          # Download the necessary tools to deploy to kubernetes
          - curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
          - chmod +x ./kubectl
          - sudo mv ./kubectl /usr/local/bin/kubectl
          # Configure kubectl
          - kubectl config set-cluster <my.cluster.name> --server=<my.kubernetes.host>
          - 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>
          # Update the deployment to use the new Docker image
          - kubectl set image deployment/<my.app> <my.app>=<my.dockerhub.username>/<my.app>:$BITBUCKET_COMMIT

 

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

最終更新日 2017 年 8 月 2 日

この翻訳に満足しましたか?

はい
いいえ
この記事についてのフィードバックを送信する

お探しの情報が見つかりませんか?

コミュニティへの質問

Powered by Confluence and Scroll Viewport.