---
title: BOSHを使ってConcourse CIをAzureにインストール
tags: ["BOSH", "Concourse CI", "Azure"]
categories: ["Dev", "CI", "ConcourseCI"]
date: 2016-04-28T14:22:01Z
updated: 2016-11-12T17:44:36Z
---

[前記事](https://blog.ik.am/entries/383)で、BOSHを使ってAWSにConcourse CIをインストールしましたが、BOSHを使えばIaaS非依存(ただしCloud Provider Interfaceの対応が必要)でインストールが可能になるので、今度はAzureにインストールしてみます。

基本的には[ドキュメント](https://bosh.io/docs/init-azure.html)の通り。


* https://azure.microsoft.com/en-us/pricing/free-trial/
* https://github.com/cloudfoundry-incubator/bosh-azure-cpi-release/blob/master/docs/get-started/create-service-principal.md または https://blog.ik.am/entries/374

をみて、

* `TENANTID`
* `CLIENTID`
* `CLIENTSECRET`

を用意してください。

### BOSH Directorのインストール

テンプレートが用意されているので簡単です。

https://azure.microsoft.com/en-us/documentation/templates/bosh-setup/
から"Deploy to Azure"をクリック


![image](https://qiita-image-store.s3.amazonaws.com/0/1852/563b3f57-c0d5-0ad6-39f1-ed823e530269.png)



必要な情報を入力。SSHの公開鍵は普段使っているやつでOK。`ADMINUSERNAME`に`admin`という名前は使えないので注意してください。

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/e3e8fc07-bfe0-9f2d-d293-436517421305.png)

"Resource group"は新規作成(ここでは`bosh`という名前にしました)。
"Resource group location"は`Japan East`を選択。
"Legal terms"で"Purchase"をクリック。

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/fd9fad06-7b6b-f501-e9de-01d11e649c49.png)

"Create"をクリック。1時間くらい待ちます。

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/4e7edd5a-4aac-aa9e-a9a3-698161307140.png)

`bosh/initdevbox`が完了すれば準備完了です。

devboxというBOSHクライアントとして使うVMと、devboxから`bosh-init`で作られたBOSH DirectorのVMができます。`Public IP address`を確認してください。

devboxにログインしましょう。VM名はテンプレートで指定した名前になっています(上の例だと`bosh`)。

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/cac78305-239d-341a-291b-02e7170de290.png)


テンプレートで指定した公開鍵に対応する秘密鍵を使ってSSHでログインします。ユーザー名もテンプレートで指定したものです。

``` console
$ ssh bosh@13.78.52.18
```

BOSH Directorにログイン済みの状態になっています。

``` console
bosh@bosh:~$ bosh target
Current target is https://10.0.0.4:25555 (bosh)
```

これでBOSH Directorのインストールが不要です。

ただし、インストールされているBOSHのバージョンには注意してください。
ホームディレクトリに`bosh.yml`があるので、`less ~/bosh.yml`で以下のバージョンをチェックしてください。

``` yaml
---
name: bosh

releases:
- name: bosh
  url: https://bosh.io/d/github.com/cloudfoundry/bosh?v=257.3
  sha1: e4442afcc64123e11f2b33cc2be799a0b59207d0
- name: bosh-azure-cpi
  url: https://bosh.io/d/github.com/cloudfoundry-incubator/bosh-azure-cpi-release?v=17
  sha1: b265730fc26ae8dc07958de61c4b7f8c0187d59a
```

### Azureの情報をBOSH Directorに設定

AWSのときと同様にAzureのインフラ情報を`cloud.yml`に設定します。以下のyamlを完全コピーでOKのはずです。

``` yaml
azs:
- name: z1 # dummy
- name: z2 # dummy

vm_types:
- name: web
  cloud_properties:
    instance_type: Standard_D1 # 安くしたい場合はBasic_A1
- name: database
  cloud_properties:
    instance_type: Standard_D1 # 安くしたい場合はBasic_A1
- name: worker
  cloud_properties:
    instance_type: Standard_D1 # 安くしたい場合はBasic_A1
- name: compilation
  cloud_properties:
    instance_type: Standard_D1

disk_types:
- name: database
  disk_size: 50_000

networks:
- name: web
  type: vip
  cloud_properties:
    tcp_endpoints:
    - "80:80"
    - "443:443"
    - "8080:8080"
    - "22:22"
- name: public
  type: manual
  subnets:
  - range: 10.0.16.0/20
    dns: [168.63.129.16]
    gateway: 10.0.16.1
    azs: [z1]
    reserved: [10.0.16.2-10.0.16.10]
    cloud_properties:
      virtual_network_name: boshvnet-crp
      subnet_name: CloudFoundry
- name: private # dummy
  type: manual
  subnets:
  - range: 10.0.16.0/20
    dns: [168.63.129.16]
    gateway: 10.0.16.1
    azs: [z1]
    reserved: [10.0.16.2-10.0.16.10]
    cloud_properties:
      virtual_network_name: boshvnet-crp
      subnet_name: CloudFoundry

compilation:
  workers: 8
  reuse_compilation_vms: true
  az: z1
  vm_type: compilation
  network: public
```

テンプレートから環境を作ると`CloudFoundry`という名前のサブネットができています。Cloud Foundryをインストールするわけではありませんが、今回はこのサブネットを使いました。

`public`と`private`というネットワークの設定を作りましたが、同じ内容です。`private`は作る必要がないですが、[前記事](https://blog.ik.am/entries/383)で作成したConcourse CIのマニフェストファイルをそのまま使うため、ダミーのネットワークとして作りました。

`bosh update cloud-config`でこのIaaS情報を適用します。

``` console
$ bosh update cloud-config cloud.yml
```

### Concourse CIのマニフェストファイル作成

テンプレートから作った場合、

* `bosh-bosh`
* `bosh-cf`
* `bosh-devbox`

というPublic IP addressができています。`bosh-devbox`はアサイン済みなので、`bosh-cf`のIPアドレスをConcourse CIのWeb用に使います。
(prefixの`bosh`は多分resource groupに指定したもの。もしくはVM名・・・)
残ったIPアドレスは使わないので捨ててもOKです。

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/476c0f02-1346-e996-6d5a-fe7ccf70e1a6.png)


`concourse.yml`にConcourse CIのマニフェストを定義します。

``` yaml
---
name: concourse

director_uuid: <%= `bosh status --uuid` %>

releases:
- name: concourse
  version: latest
- name: garden-runc
  version: latest

stemcells:
- alias: trusty
  os: ubuntu-trusty
  version: latest

instance_groups:
- name: web
  instances: 1
  vm_type: web
  stemcell: trusty
  azs: [z1]
  networks:
  - name: public
    default: [dns, gateway]
  - name: web
    static_ips: [13.78.58.110] # Should be changed!!
  jobs:
  - name: atc
    release: concourse
    properties:
      # replace with your CI's externally reachable URL, e.g. https://ci.foo.com
      external_url: http://13.78.58.110:8080

      # replace with username/password, or configure GitHub auth
      basic_auth_username: concourse
      basic_auth_password: password

      postgresql_database: &atc_db atc

      # Allow viewing of pipelines as an anonymous user if the job is configured with `public: true`
      publicly_viewable: true
  - name: tsa
    release: concourse
    properties: {}

- name: db
  instances: 1
  vm_type: database
  stemcell: trusty
  persistent_disk_type: database
  azs: [z1]
  networks: [{name: private}]
  jobs:
  - name: postgresql
    release: concourse
    properties:
      databases:
      - name: *atc_db
        # make up a role and password
        role: REPLACE_ME
        password: REPLACE_ME

- name: worker
  instances: 1
  vm_type: worker
  stemcell: trusty
  azs: [z1]
  networks: [{name: private}]
  jobs:
  - name: groundcrew
    release: concourse
    properties: {}
  - name: baggageclaim
    release: concourse
    properties: {}
  - name: garden
    release: garden-runc
    properties:
      garden:
        listen_network: tcp
        listen_address: 0.0.0.0:7777

update:
  canaries: 1
  max_in_flight: 1
  serial: false
  canary_watch_time: 1000-60000
  update_watch_time: 1000-60000
```

IPアドレス以外は、AWSのときと同じです。

(AWS版を執筆時点ではWebインスタンスへPublic IPアドレスを設定するのをWebから行っていましたが、今回はBOSHから設定しました。AWS版も後で直す)

### Concourse CIをデプロイ

まずはベースVMイメージであるStemcellとConcourse CIで必要となるリリースをBOSHにアップロードします。
リリースは[http://bosh.io](http://bosh.io)から確認できます。

Stemcellのアップロードは少し時間がかかります。

``` console
bosh upload stemcell https://bosh.io/d/stemcells/bosh-azure-hyperv-ubuntu-trusty-go_agent?v=3263.8
bosh upload release http://bosh.io/d/github.com/concourse/concourse?v=2.4.0
bosh upload release http://bosh.io/d/github.com/cloudfoundry/garden-runc-release?v=1.0.2
```

マニフェストを設定して、デプロイ。

``` console
bosh deployment concourse.yml
bosh deploy
```

完了を待ってください。

完了すると`db`、`web`、`worker`3つのVMができていることがわかります。

``` console
$ bosh vms
Acting as user 'admin' on 'bosh'
Deployment `concourse'

Director task 17

Task 17 done

+-------------------------------------------------+---------+----+----------+--------------+
| VM                                              | State   | AZ | VM Type  | IPs          |
+-------------------------------------------------+---------+----+----------+--------------+
| db/0 (0fedc3a7-77d1-4b58-b999-9bfe33142b55)     | running | z1 | database | 10.0.16.14   |
| web/0 (d87cc7b6-839b-455a-814e-41ed1ad75dc0)    | running | z1 | web      | 10.0.16.11   |
|                                                 |         |    |          | 13.78.58.110 |
| worker/0 (a8bc5d86-16c4-4b55-9b4b-d4b6a65359e3) | running | z1 | worker   | 10.0.16.15   |
+-------------------------------------------------+---------+----+----------+--------------+

VMs total: 3
```

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/ea06ca90-0102-2c25-2692-b139d0f31e20.png)


ちなみに、

``` console
Error 400007: `worker/0 (...)' is not running after update. Review logs for failed jobs: ...
```
というエラーが出たら、もう一回deployすれば大丈夫になったりします。

最後に、VMにデフォルトで設定されている`nsg-bosh`というセキュリティグループに対して`8080`番でのアクセスを許可します。

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/888353d3-3d50-0c24-6d29-9958df556bd2.png)

(セキュリティグループの設定はマニフェストで明示したほうがいいかも。)

これで完了です。

ブラウザPublic IP addressの8080番にアクセスしてください。

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/4013c950-3d4f-f16d-b7ae-b793fea33238.png)


`fly`コマンドでログイン

``` console
$ fly -t azr login -c http://13.78.58.110:8080/
username: concourse
password: password
target saved
```

Hello Worldなパイプラインをセット & アンポーズ

``` console
$ fly -t azr sp -p first -c first.yml
$ fly -t azr up -p first
```

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/afe132c4-ff83-951a-378d-7598018f60c2.png)

ジョブをクリックして、

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/041759ea-a778-4d41-93bc-88d570ec6f9a.png)

(+)ボタンをクリックしてジョブを実行


![image](https://qiita-image-store.s3.amazonaws.com/0/1852/c602ea61-3223-f3c6-86fb-452267243914.png)


できた。AWSのVPCとは違って、NATがなくてもVM内部からインターネットアクセスできるようなので、設定が少し楽でした。


---

AWSのときと同じようにAzureでもConcourse CIをインストールできました。
BOSHのおかげです。

また、いままで作ってきたパイプラインをそのままここで作ったConcourse CI上で即実行できるのがうれしいです。
Concourse CIは本当に良いCI/CDツールだと思います。

次はGCPでも試してみたい。
https://github.com/cloudfoundry-incubator/bosh-google-cpi-release
