---
title: cf-for-k8s(CF4K8s)にminibrokerをインストール
tags: ["Cloud Foundry", "Kubernetes", "CF4K8s", "ytt"]
categories: ["Dev", "PaaS", "CloudFoundry", "CF4K8s"]
date: 2020-04-19T03:51:08Z
updated: 2020-04-22T17:41:47Z
---

"[cf-for-k8s(CF4K8s) 0.1.0をKind上にインストールする](/entries/519)"で構築した環境に[minibroker](https://github.com/kubernetes-sigs/minibroker)をインストールしてみます。

"[VMware Tanzu Application Service for Kubernetes (TAS4K8s) 0.1.0をKind上にインストールする](/entries/520)"で構築した環境でも同じようにインストールできます。

**目次**
<!-- toc -->

### Helmのインストール

[minibroker](https://github.com/kubernetes-sigs/minibroker)は[helm](https://helm.sh/)でパッケージングされています。パッケージを取得するために`helm` CLIをインストールします。

```
brew install helm
```

次のバージョンで確認しました。

```
$ helm version
version.BuildInfo{Version:"v3.1.2", GitCommit:"d878d4d45863e42fd5cff6743294a11d28a9abce", GitTreeState:"clean", GoVersion:"go1.13.8"}
```

### minibrokerのインストール

minibrokerをインストールするためにHelmのRepositoryを追加します。

```
helm repo add minibroker https://minibroker.blob.core.windows.net/charts
```

インストールは`heml install`では行わず、cf-for-k8s同様、`ytt`と`kapp`を使って行います。

> `helm install`でデプロイする場合の手順は[こちら](https://github.com/kubernetes-sigs/minibroker#usage-with-cloud-foundry)

minibrokerのmanifestファイルを`helm template`で取得し、変更したい項目を`ytt`のoverlayファイルとして作成し、`ytt`コマンドでmanifestファイルを結合し、`kapp`でデプロイします。

`helm template`ではインストール先の`namespace`を指定できないため、`namespace`を指定するためのoverlay fileを作成します。`minibroker-namespace.yml`に次の内容を記述してください。

```yaml
#@ load("@ytt:overlay", "overlay")

#@overlay/match by=overlay.all, expects="1+"
---
metadata:
  #@overlay/match missing_ok=True
  namespace: minibroker

---
apiVersion: v1
kind: Namespace
metadata:
  name: minibroker
```

`ytt`で結合されたmanifestファイルは次のコマンドで確認できます。

```
helm template minibroker minibroker/minibroker \
  -n minibroker \
  --set deployServiceCatalog=false \
  --set defaultNamespace=minibroker | \
  ytt --ignore-unknown-comments \
      -f  minibroker-namespace.yml \
      -f -
```

この内容を`kapp`でデプロイします。

```
kapp deploy -a minibroker -c -f <(helm template minibroker minibroker/minibroker \
  -n minibroker \
  --set deployServiceCatalog=false \
  --set defaultNamespace=minibroker | \
  ytt --ignore-unknown-comments \
      -f  minibroker-namespace.yml \
      -f -)
```

次のようなリソースが作成されます。

```
Target cluster 'https://127.0.0.1:32768' (nodes: kind-control-plane)

Changes

Namespace   Name                   Kind                Conds.  Age  Op      Wait to    Rs  Ri  
(cluster)   minibroker             ClusterRoleBinding  -       -    create  reconcile  -   -  
^           minibroker             Namespace           -       -    create  reconcile  -   -  
minibroker  minibroker             ServiceAccount      -       -    create  reconcile  -   -  
^           minibroker-minibroker  Deployment          -       -    create  reconcile  -   -  
^           minibroker-minibroker  Service             -       -    create  reconcile  -   -  

Op:      5 create, 0 delete, 0 update, 0 noop
Wait to: 5 reconcile, 0 delete, 0 noop

Continue? [yN]:
```

`y`を入力してインストールします。


### Service Brokerの登録

以降はCloud Foundry側の作業です。

まずはService Brokerを登録します。

```
cf create-service-broker minibroker user pass http://minibroker-minibroker.minibroker.svc.cluster.local
```

登録内容を確認します。

```
$ cf service-brokers
Getting service brokers as admin...

name         url
minibroker   http://minibroker-minibroker.minibroker.svc.cluster.local
```

次のServiceとPlanが利用可能になります。

```
 $ cf service-access
Getting service access as admin...
broker: minibroker
   service      plan               access   orgs
   mariadb      10-1-26            none     
   mariadb      10-1-28            none     
   mariadb      10-1-29            none     
   mariadb      10-1-30            none     
   mariadb      10-1-31            none     
   mariadb      10-1-32            none     
   mariadb      10-1-33            none     
   mariadb      10-1-34-debian-9   none     
   mariadb      10-1-34            none     
   mariadb      10-1-35            none     
   mariadb      10-1-36            none     
   mariadb      10-1-37            none     
   mariadb      10-1-38            none     
   mariadb      10-1-39            none     
   mariadb      10-1-40            none     
   mariadb      10-3-14            none     
   mariadb      10-3-15            none     
   mariadb      10-3-16            none     
   mariadb      10-3-17            none     
   mariadb      10-3-18            none     
   mariadb      10-3-20            none     
   mariadb      10-3-21            none     
   mariadb      10-3-22            none     
   mongodb      3-4-10             none     
   mongodb      3-4-6              none     
   mongodb      3-4-7              none     
   mongodb      3-4-9              none     
   mongodb      3-6-0              none     
   mongodb      3-6-1              none     
   mongodb      3-6-2              none     
   mongodb      3-6-4              none     
   mongodb      3-6-5              none     
   mongodb      3-6-5-debian-9     none     
   mongodb      3-6-6              none     
   mongodb      3-7-1              none     
   mongodb      3-7-3              none     
   mongodb      4-0-13             none     
   mongodb      4-0-1              none     
   mongodb      4-0-10             none     
   mongodb      4-0-11             none     
   mongodb      4-0-12             none     
   mongodb      4-0-14             none     
   mongodb      4-0-2              none     
   mongodb      4-0-3              none     
   mongodb      4-0-5              none     
   mongodb      4-0-6              none     
   mongodb      4-0-7              none     
   mongodb      4-0-8              none     
   mongodb      4-0-9              none     
   mongodb      4-2-2              none     
   mongodb      4-2-3              none     
   mongodb      4-2-4              none     
   mysql        5-7-14             none     
   mysql        5-7-27             none     
   mysql        5-7-28             none     
   postgresql   10-5-0             none     
   postgresql   10-6-0             none     
   postgresql   10-7-0             none     
   postgresql   10-8-0             none     
   postgresql   11-0-0             none     
   postgresql   11-3-0             none     
   postgresql   11-4-0             none     
   postgresql   11-5-0             none     
   postgresql   11-6-0             none     
   postgresql   11-7-0             none     
   postgresql   9-6-2              none     
   redis        3-2-9              none     
   redis        4-0-10             none     
   redis        4-0-10-debian-9    none     
   redis        4-0-11             none     
   redis        4-0-12             none     
   redis        4-0-13             none     
   redis        4-0-14             none     
   redis        4-0-2              none     
   redis        4-0-6              none     
   redis        4-0-7              none     
   redis        4-0-8              none     
   redis        4-0-9              none     
   redis        5-0-4              none     
   redis        5-0-5              none     
   redis        5-0-6              none     
   redis        5-0-7              none  
```

`mysql`, `postgresql`, `mongodb`を有効にします。

```
cf enable-service-access mysql
cf enable-service-access postgresql
cf enable-service-access mongodb
```

Marketplaceを確認します。

```
$ cf marketplace
Getting services from marketplace in org demo / space demo as admin...
OK

service      plans                                                                                                                                                                                                                      description                 broker
postgresql   10-5-0, 9-6-2, 11-5-0, 11-4-0, 10-7-0, 10-8-0, 10-6-0, 11-0-0, 11-7-0, 11-6-0, 11-3-0                                                                                                                                      Helm Chart for postgresql   minibroker
mysql        5-7-28, 5-7-27, 5-7-14                                                                                                                                                                                                     Helm Chart for mysql        minibroker
mongodb      3-6-6, 3-6-5, 4-0-11, 4-0-6, 4-0-1, 3-4-10, 3-4-6, 4-2-4, 4-0-14, 4-0-12, 3-7-1, 3-6-0, 3-4-7, 4-0-8, 4-0-7, 4-0-5, 4-0-10, 4-0-3, 3-6-5-debian-9, 3-6-4, 4-2-3, 4-2-2, 4-0-13, 4-0-9, 3-4-9, 3-7-3, 3-6-2, 3-6-1, 4-0-2   Helm Chart for mongodb      minibroker

TIP: Use 'cf marketplace -s SERVICE' to view descriptions of individual plans of a given service.
```

### サンプルアプリケーションのデプロイ

MySQLを使ったサンプルアプリケーションをデプロイしてみます。


雛形プロジェクトを作成します。

```
curl https://start.spring.io/starter.tgz \
       -d artifactId=hello-db \
       -d baseDir=hello-db \
       -d dependencies=data-rest,data-jpa,data-rest-hal,actuator,mysql \
       -d packageName=com.example \
       -d applicationName=HelloDbApplication | tar -xzvf -
```

ソースコードを作成します。

```java
cd hello-db
cat <<EOF > src/main/resources/application.properties
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
EOF
cat <<EOF > src/main/java/com/example/Message.java
package com.example;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Message {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;
    public String text;
}
EOF
cat <<EOF > src/main/java/com/example/MessageRepository.java
package com.example;

import org.springframework.data.repository.CrudRepository;

public interface MessageRepository extends CrudRepository<Message, Long> {
}
EOF
```

ビルドします。
```
./mvnw clean package -DskipTests
```

MySQLのサービスインスタンスを作成します。

```
cf create-service mysql 5-7-28 message-db -c '{"mysqlDatabase":"message", "mysqlUser":"message"}'
```

非同期でプロビジョニングされるので、`cf services`で`last operation`が`create succeeded`になるまで待ちます。

```
$ cf services
Getting services in org demo / space demo as admin...

name         service   plan     bound apps   last operation     broker       upgrade available
message-db   mysql     5-7-28                create succeeded   minibroker  
```

Cloud Foundryにアプリをデプロイするための`manifest.yml`を次の内容で作成します。

```yaml
applications:
- name: hello-db
  path: target/hello-db-0.0.1-SNAPSHOT.jar
  services:
  - message-db
  env:
    BP_AUTO_RECONFIGURATION: false
    SPRING_DATASOURCE_URL: jdbc:mysql://${vcap.services.message-db.credentials.host}:${vcap.services.message-db.credentials.port}/${vcap.services.message-db.credentials.database}
    SPRING_DATASOURCE_USERNAME: ${vcap.services.message-db.credentials.username}
    SPRING_DATASOURCE_PASSWORD: ${vcap.services.message-db.credentials.password}
```

デプロイします。

```
cf push
```

`cf app`でデプロイされたアプリを確認します。


```
$ cf app hello-db
Showing health and status for app hello-db in org demo / space demo as admin...

name:                hello-db
requested state:     started
isolation segment:   placeholder
routes:              hello-db.local.maki.lol
last uploaded:       Sun 19 Apr 04:26:23 JST 2020
stack:               
buildpacks:          

type:           web
instances:      1/1
memory usage:   1024M
     state     since                  cpu    memory    disk      details
#0   running   2020-04-18T19:26:30Z   0.0%   0 of 1G   0 of 1G  
```

動作確認します。

```
$ curl hello-db.local.maki.lol/messages 
{
  "_embedded" : {
    "messages" : [ ]
  },
  "_links" : {
    "self" : {
      "href" : "http://hello-db.local.maki.lol/messages"
    },
    "profile" : {
      "href" : "http://hello-db.local.maki.lol/profile/messages"
    }
  }
}                                                             

$ curl  -H 'Content-Type: application/json' -d '{"text":"Hello World!"}'  hello-db.local.maki.lol/messages
{
  "text" : "Hello World!",
  "_links" : {
    "self" : {
      "href" : "http://hello-db.local.maki.lol/messages/1"
    },
    "message" : {
      "href" : "http://hello-db.local.maki.lol/messages/1"
    }
  }
}

$ curl hello-db.local.maki.lol/messages                                                                   
{
  "_embedded" : {
    "messages" : [ {
      "text" : "Hello World!",
      "_links" : {
        "self" : {
          "href" : "http://hello-db.local.maki.lol/messages/1"
        },
        "message" : {
          "href" : "http://hello-db.local.maki.lol/messages/1"
        }
      }
    } ]
  },
  "_links" : {
    "self" : {
      "href" : "http://hello-db.local.maki.lol/messages"
    },
    "profile" : {
      "href" : "http://hello-db.local.maki.lol/profile/messages"
    }
  }
} 
```

ブラウザで[http://hello-db.local.maki.lol](http://hello-db.local.maki.lol)にアクセスするとHAL Browserが表示されます。

![image](https://user-images.githubusercontent.com/106908/79678982-63121700-823c-11ea-9bb4-df71ab56f5e0.png)


### minibrokerのアンインストール

```
cf delete-service hello-db
```
でService Instanceを削除した後、

```
kapp delete -a minibroker
```
でアンインストールできます。
先にminibrokerを削除してしまった場合は、

```
cf purge-service-instance hello-db
```
でCF側のデータを削除することができます。
