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.

前提条件

以下が必要です。

  • イメージのプッシュ先となる、既存の DockerHub アカウント (または他の Docker レジストリ)

  • An existing kubernetes cluster or minikube test environment

アーティファクトのビルド

以下の例では、Node.js アプリをパッケージ化してデプロイします。Node.js プロジェクトを簡単にビルドするには、bitbucket-pipelines.yml を次のように構成します。 

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.

Docker イメージのビルドとプッシュ

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

最初に、リポジトリのルートに Dockerfile が必要です。以下を使用します。 

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

リポジトリで、左側のサイドバーにある [リポジトリ設定] を選択します。[Pipelines] セクションで [リポジトリ変数] を選択し、 DockerHub のユーザー名とパスワードを追加します。

以下に示すように、資格情報を bitbucket-pipelines.yml ファイルに追加します。

これらの変数は、デプロイ環境、リポジトリ、またはワークスペース レベルで定義できます。

bitbucket-pipelines.yml

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

ここでは APPLICATION_NAME の環境変数を使用していますが、独自のものを設定することも静的に定義することもできます。

アプリケーションの複数のインスタンスを実行するデプロイメントを Kubernetes で作成する

これは、Kubernetes クラスターを構成するために Pipelines 外で一度だけ行う必要がある手動プロセスです。Kubernetes サーバーの場所を kubectl に構成済みであることを前提としています。

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:

次のプレースホルダーを上記で使用した値に置き換える必要があります。 

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

要約

上記のすべてのステップを完了することで bitbucket-pipelines.yml ファイルが完成します。

bitbucket-pipelines.yml スニペットの例

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.

さらにヘルプが必要ですか?

アトラシアン コミュニティをご利用ください。