IK.AM

@making's tech note


cf-for-k8s(CF4K8s)にminibrokerをインストール

🗃 {Dev/PaaS/CloudFoundry/CF4K8s}
🏷 Cloud Foundry 🏷 Kubernetes 🏷 CF4K8s 🏷 ytt 
🗓 Updated at 2020-04-22T17:41:47Z  🗓 Created at 2020-04-19T03:51:08Z   🌎 English Page

"cf-for-k8s(CF4K8s) 0.1.0をKind上にインストールする"で構築した環境にminibrokerをインストールしてみます。

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

目次

Helmのインストール

minibrokerhelmでパッケージングされています。パッケージを取得するために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同様、yttkappを使って行います。

helm installでデプロイする場合の手順はこちら

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

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

#@ 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 -

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

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 serviceslast operationcreate 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を次の内容で作成します。

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にアクセスするとHAL Browserが表示されます。

image

minibrokerのアンインストール

cf delete-service hello-db

でService Instanceを削除した後、

kapp delete -a minibroker

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

cf purge-service-instance hello-db

でCF側のデータを削除することができます。


✒️️ Edit  ⏰ History  🗑 Delete