LoginSignup
1
0

More than 5 years have passed since last update.

Azure Service Broker の使い方 (3)

Posted at

Azure Service Broker の使い方 (3)

今回から Azure Service Broker の使い方を探っていこう。
サンプルが入っているので最初にクローンしておこう。

Instance のインストール

試しにサンプルの、postgres をインストールしてみる。

$ kubectl create -f contrib/k8s/examples/postgresql-instance.yaml 
serviceinstance "my-postgresql-instance" created
invincible:azure-service-broker ushio$ kubectl get serviceinstances
NAME                     AGE
my-postgresql-instance   2m

これでしばらく待つと、demo というリソースグループの中に、マネージドの PostgreSQL のリソースが作成される。

ここにスクリーンショット

$ kubectl get serviceinstance my-postgresql-instance -o yaml

このコマンドを叩くと、状態が出てくる。status:conditions: をみると、現在の状態がわかる。しばらく待つと、Success になった。

apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceInstance
metadata:
  creationTimestamp: 2017-11-19T12:07:54Z
  finalizers:
  - kubernetes-incubator/service-catalog
  generation: 1
  name: my-postgresql-instance
  namespace: default
  resourceVersion: "336"
  selfLink: /apis/servicecatalog.k8s.io/v1beta1/namespaces/default/serviceinstances/my-postgresql-instance
  uid: 4614210e-cd22-11e7-a0dd-0a580af40104
spec:
  clusterServiceClassExternalName: azure-postgresqldb
  clusterServiceClassRef:
    name: b43b4bba-5741-4d98-a10b-17dc5cee0175
  clusterServicePlanExternalName: basic50
  clusterServicePlanRef:
    name: b2ed210f-6a10-4593-a6c4-964e6b6fad62
  externalID: a2972880-181e-4bbf-8a2e-a92d0d165d58
  parameters:
    extensions:
    - uuid-ossp
    - postgis
    location: eastus
    resourceGroup: demo
  updateRequests: 0
status:
  asyncOpInProgress: false
  conditions:
  - lastTransitionTime: 2017-11-19T12:16:25Z
    message: The instance was provisioned successfully
    reason: ProvisionedSuccessfully
    status: "True"
    type: Ready
  deprovisionStatus: Required
  externalProperties:
    clusterServicePlanExternalID: b2ed210f-6a10-4593-a6c4-964e6b6fad62
    clusterServicePlanExternalName: basic50
    parameterChecksum: 99ba190fa88b80d79fc9d601c0fcb13e84bc2e72c38c1230e232a5eab95165a4
    parameters:
      extensions:
      - uuid-ossp
      - postgis
      location: eastus
      resourceGroup: demo
  orphanMitigationInProgress: false
  reconciledGeneration: 1

ちなみに、適用した yaml の中身はこんな感じ。Kubernetes の標準ではないServiceinstance という種別でデプロイされている。

postgresql-instance.yaml

apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceInstance
metadata:
  name: my-postgresql-instance
  namespace: default
spec:
  clusterServiceClassExternalName: azure-postgresqldb
  clusterServicePlanExternalName: basic50
  parameters:
    location: eastus
    resourceGroup: demo
    extensions:
    - uuid-ossp
    - postgis

Binding の実施

インスタンスができたら、Binding を実施しよう。

$ kubectl create -f contrib/k8s/examples/postgresql-binding.yaml
servicebinding "my-postgresql-binding" created

すぐ終わるとは限らないので確認。 動いているっぽい。

$ kubectl get servicebinding my-postgresql-binding -o yaml
apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceBinding
metadata:
  creationTimestamp: 2017-11-19T12:20:44Z
  finalizers:
  - kubernetes-incubator/service-catalog
  generation: 1
  name: my-postgresql-binding
  namespace: default
  resourceVersion: "340"
  selfLink: /apis/servicecatalog.k8s.io/v1beta1/namespaces/default/servicebindings/my-postgresql-binding
  uid: 10ee3219-cd24-11e7-a0dd-0a580af40104
spec:
  externalID: 7386578e-1b93-4d1d-8fc6-85e1cef77cdb
  instanceRef:
    name: my-postgresql-instance
  secretName: my-postgresql-secret
status:
  asyncOpInProgress: false
  conditions:
  - lastTransitionTime: 2017-11-19T12:20:48Z
    message: Injected bind result
    reason: InjectedBindResult
    status: "True"
    type: Ready
  externalProperties: {}
  orphanMitigationInProgress: false
  reconciledGeneration: 1
  unbindStatus: Required

バインディングのyaml をみると、ServicveBinding という種別のリソースを作っている。面白いのが、my-postgresql-secret というのができているところ。

postgresql-binding.yaml

apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceBinding
metadata:
  name: my-postgresql-binding
  namespace: default
spec:
  instanceRef:
    name: my-postgresql-instance
  secretName: my-postgresql-secret

Secret の中身はこのような感じ。

$ kubectl get servicebinding my-postgresql-binding -o yaml
apiVersion: servicecatalog.k8s.io/v1beta1
kind: ServiceBinding
metadata:
  creationTimestamp: 2017-11-19T12:20:44Z
  finalizers:
  - kubernetes-incubator/service-catalog
  generation: 1
  name: my-postgresql-binding
  namespace: default
  resourceVersion: "340"
  selfLink: /apis/servicecatalog.k8s.io/v1beta1/namespaces/default/servicebindings/my-postgresql-binding
  uid: 10ee3219-cd24-11e7-a0dd-0a580af40104
spec:
  externalID: 7386578e-1b93-4d1d-8fc6-85e1cef77cdb
  instanceRef:
    name: my-postgresql-instance
  secretName: my-postgresql-secret
status:
  asyncOpInProgress: false
  conditions:
  - lastTransitionTime: 2017-11-19T12:20:48Z
    message: Injected bind result
    reason: InjectedBindResult
    status: "True"
    type: Ready
  externalProperties: {}
  orphanMitigationInProgress: false
  reconciledGeneration: 1
  unbindStatus: Required
invincible:azure-service-broker ushio$ kubectl get secret my-postgresql-secret -o yaml
apiVersion: v1
data:
  database: ZnI1ejlwNHMzNA==
  host: MWFlNzEzY2UtNDdhZC00N2YwLWEwZTctOWVmZWVhYzgwMWUyLnBvc3RncmVzLmRhdGFiYXNlLmF6dXJlLmNvbQ==
  password: SUtEd3dhbjZscVlDbzRSbQ==
  port: NTQzMg==
  username: YWJkMmNqNDVzMkAxYWU3MTNjZS00N2FkLTQ3ZjAtYTBlNy05ZWZlZWFjODAxZTI=
kind: Secret
metadata:
  creationTimestamp: 2017-11-19T12:20:47Z
  name: my-postgresql-secret
  namespace: default
  ownerReferences:
  - apiVersion: servicecatalog.k8s.io/v1beta1
    blockOwnerDeletion: true
    controller: true
    kind: ServiceBinding
    name: my-postgresql-binding
    uid: 10ee3219-cd24-11e7-a0dd-0a580af40104
  resourceVersion: "119949"
  selfLink: /api/v1/namespaces/default/secrets/my-postgresql-secret
  uid: 12fd2879-cd24-11e7-a22d-000d3a5183f5
type: Opaque

Pod からサービスを利用する

さて、Pod からサービスが利用できるか調べてみる。
Secret があれば、環境変数、もしくは、ボリュームマウントでサービスへの情報が取れる。

Kubernetes Secrets の紹介 – データベースのパスワードやその他秘密情報をどこに保存するか?

例えばこんなyaml を作って試してみる。

nginx-secret.yaml

apiVersion: apps/v1beta1 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /tmp
          name : my-postgresql-secret
          readOnly: true
      volumes:
      - name: my-postgresql-secret
        secret:
          secretName: my-postgresql-secret

デプロイする。

$ kubectl create -f nginx-secret.yaml 

出来たPod に入って、値を調べる。うむ。ちゃんと出来ている。これでフロントのアプリを作れば完璧なはず。

$ kubectl exec nginx-deployment-2278088344-4f26h -it /bin/bash
root@nginx-deployment-2278088344-4f26h:/# cd /tmp                                                                                    
root@nginx-deployment-2278088344-4f26h:/tmp# ls
database  host  password  port  username
root@nginx-deployment-2278088344-4f26h:/tmp# cat database 
fr5z9p4s34
root@nginx-deployment-2278088344-4f26h:/tmp# cat host 
1ae713ce-47ad-47f0-a0e7-9efeeac801e2.postgres.database.azure.com
root@nginx-deployment-2278088344-4f26h:/tmp# 

unbinding

Binding を削除すると、Secret も削除される。

$ kubectl delete servicebinding my-postgresql-binding
servicebinding "my-postgresql-binding" deleted
$ kubectl get secret my-postgresql-secret
Error from server (NotFound): secrets "my-postgresql-secret" not found

Deprovisioning

ServiceInstance を削除すると Azure 側のリソースも削除される。

$ kubectl delete serviceinstance my-postgresql-instance
serviceinstance "my-postgresql-instance" deleted
$ kubectl get serviceinstance my-postgresql-instance -o yaml
Error from server (NotFound): serviceinstances.servicecatalog.k8s.io "my-postgresql-instance" not found

これは便利そうだわ。次はサンプルアプリを作ってみる。せっかくなので、Cosmos あたりでやってみよう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0