Red Hat OpenShift on IBM Cloud の無料Hands-onを試してみる - Lab2

# はじめに
以下の記事を参考に、Red Hat OpenShift on IBM Cloudを試してみたときのログです。
参考: [OpenShift on IBM Cloud をデモサイトから無料で試す方法](https://qiita.com/ayatokura/items/3cd9596d0243d940d143)

ここではLab2を実施してみます。
※Lab2の前提としてLab1を完了している必要があります。

## 関連記事
[Red Hat OpenShift on IBM Cloud の無料Hands-onを試してみる - Lab1](https://qiita.com/tomotagwork/items/22c09cb73d5614fb08a5)
[Red Hat OpenShift on IBM Cloud の無料Hands-onを試してみる - Lab2](https://qiita.com/tomotagwork/items/d3a86fe43cd6972a1401)
[Red Hat OpenShift on IBM Cloud の無料Hands-onを試してみる - Lab3](https://qiita.com/tomotagwork/items/f8bc20832933f685b8d7)

# Hands on 実施

## Lab2
このLabでは、Lab1を少し拡張し、OpenShiftの外のサービス(IBM Cloud上のCloudantDBサービス)を利用したアプリケーションの管理について体験できます。

Lab2で扱う環境の全体像です。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/e6f79bb8-1b61-950b-9dc0-82e5f8d02eb7.png)

以下からLab2のLaunch Labをクリックします。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/5006e290-ded3-fc88-ec83-2cd2d9f88d54.png)



### Exercise 1 / Install IBM Cloud Operator from Operator Hub

ここでは"オペレーター"を扱ってみます。"オペレーター"は運用関連の作業をコード化して運用を自動化するために用いられる仕組みです。また、Kubenetes APIを拡張して独自のリソースを扱えるようにCRD(Custom Resource Definition)という仕組みがあり、これを利用することで、Kubernetes(OpenShift)環境外で稼働するサービス(IBM Cloud上のサービスなど)も、Kubernetesの管理に統合して扱うことができるようです。

参考: 
 [Kubernetes - オペレーターパターン](https://kubernetes.io/ja/docs/concepts/extend-kubernetes/operator/)
 [Kubernetes - カスタムリソース](https://kubernetes.io/ja/docs/concepts/extend-kubernetes/api-extension/custom-resources/)
 [KubernetesのCRDまわりを整理する。](https://qiita.com/cvusk/items/773e222e0971a5391a51)

OpenShift Web Consoleにて、Operators - OperatorHub を選択し、Cloud ProviderからIBM Cloud Operatorを選択。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/4a804a8a-d0a5-7cb7-8777-842b2cebc6da.png)

Continue
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/93a2e5ce-1e49-3052-4dbf-5e87ac98d8d5.png)

Install
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/f98a64c5-1b83-99fd-b346-fdf07d87989d.png)

Subscribe
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/814c759a-b2ff-78de-2394-1af7843cbdbc.png)

しばらくすると、StatusがSucceededになります。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/bf8b84f6-a634-5b27-b4dc-d2292b660118.png)

Launch Labで開いたブラウザ画面の右側のTerminalから、ibmcloudにログインし、Accountを選択。

```
container-lab$ ibmcloud login --sso
API endpoint: https://cloud.ibm.com
Region: us-south

Get One Time Code from https://identity-1.us-south.iam.cloud.ibm.com/identity/passcode to proceed.
Open the URL in the default browser? [Y/n] > Y
One Time Code > 
Authenticating...
OK

Select an account:
1. Tomohiro Taguchi's Account (9xxxxxxxxxxxxxxxxxx0)
2. Ixxxxx <-> 1xxxx3
3. Ixxxx - xxxxx (4xxxxxxxxxxxxx3) <-> 423632
4. DTE Cloud Platform (aexxxxx1) <-> 20xxx24
Enter a number> 1
Targeted account Tomohiro Taguchi's Account (9xxxxxxxxxxxxxxxxxxx0)

                      
API endpoint:      https://cloud.ibm.com   
Region:            us-south   
User:              TOMOxxxxxx 
Account:           Tomohiro Taguchi's Account (9sssssssssssssssssssssssssssssss0)   
Resource group:    No resource group targeted, use 'ibmcloud target -g RESOURCE_GROUP'   
CF API endpoint:      
Org:                  
Space:                

New version 1.1.0 is available.
Release notes: https://github.com/IBM-Cloud/ibm-cloud-cli-release/releases/tag/v1.1.0
TIP: use 'ibmcloud config --check-version=false' to disable update check.

Do you want to update? [y/N] > N
```

Resource Groupを指定。

```
container-lab$ ibmcloud target --cf -g default
Targeted resource group default

Targeted Cloud Foundry (https://api.us-south.cf.cloud.ibm.com)

Targeted org TOMOxxxxxx

Targeted space dev_dallas


                      
API endpoint:      https://cloud.ibm.com   
Region:            us-south   
User:              TOMOxxxxxx   
Account:           Tomohiro Taguchi's Account (99fa8026b325659bd73faab4f9315210)   
Resource group:    default   
CF API endpoint:   https://api.us-south.cf.cloud.ibm.com (API version: 2.147.0)   
Org:               TOMOxxxxxx   
Space:             dev_dallas  
```

設定確認。

```
container-lab$ ibmcloud target

                      
API endpoint:      https://cloud.ibm.com   
Region:            us-south   
User:              TOMOxxxxx   
Account:           Tomohiro Taguchi's Account (99fa8026b325659bd73faab4f9315210)   
Resource group:    default   
CF API endpoint:   https://api.us-south.cf.cloud.ibm.com (API version: 2.147.0)   
Org:               TOMOxxxxx  
Space:             dev_dallas   
````

OpenShift Clusterへのログイン

```
container-lab$ oc login --server https://c100-e.us-south.containers.cloud.ibm.com:32416 -u apikey -p 80d5c862c51a1464caedd0a477b1e0b3
Login successful.

You have access to 58 projects, the list has been suppressed. You can list all projects with 'oc projects'

Using project "example-health".
```

Cluster Projectをdefaultに設定

```
container-lab$ oc project default
Now using project "default" on server "https://c100-e.us-south.containers.cloud.ibm.com:32416".
```

API tokenを生成するためのスクリプト実行
実行している中身はこちら => [operator.sh](https:\/\/raw.githubusercontent.com/IBM/cloud-operators/master/hack/config-operator.sh)

```
container-lab$ curl -sL https:\/\/raw.githubusercontent.com/IBM/cloud-operators/master/hack/config-operator.sh | bash
*** Generating new APIKey
Please preserve the API key! It cannot be retrieved after it's created.
secret/secret-ibm-cloud-operator created
configmap/config-ibm-cloud-operator created
```

configmapの設定内容確認。

```
container-lab$ oc get configmap/config-ibm-cloud-operator -o yaml -n default
apiVersion: v1
data:
  org: TOMOxxx
  region: us-south
  resourcegroup: default
  resourcegroupid: 2fe3cdca4103477ca944feabd7e76076
  space: dev_dallas
  user: TOMOxxx
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"org":"TOMOxxx","region":"us-south","resourcegroup":"default","resourcegroupid":"2fxxxxxx076","space":"dev_dallas","user":"TOMOxxx"},"kind":"ConfigMap","metadata":{"annotations":{},"labels":{"app.kubernetes.io/name":"ibmcloud-operator"},"name":"config-ibm-cloud-operator","namespace":"default"}}
  creationTimestamp: "2020-05-25T06:59:45Z"
  labels:
    app.kubernetes.io/name: ibmcloud-operator
  name: config-ibm-cloud-operator
  namespace: default
  resourceVersion: "1186645"
  selfLink: /api/v1/namespaces/default/configmaps/config-ibm-cloud-operator
  uid: 3559764e-3d2e-44b1-8b36-795643056255
container-lab$ 
```

secretの設定内容確認

```
container-lab$ oc get secret/secret-ibm-cloud-operator -o yaml -n default
apiVersion: v1
data:
  api-key: M3xxxxxxxxxxx=
  region: dxxxxx=
kind: Secret
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"api-key":"M3xxxxxxxxs=","region":"dxxxxxxx="},"kind":"Secret","metadata":{"annotations":{},"labels":{"app.kubernetes.io/name":"ibmcloud-operator","seed.ibm.com/ibmcloud-token":"apikey"},"name":"secret-ibm-cloud-operator","namespace":"default"},"type":"Opaque"}
  creationTimestamp: "2020-05-25T06:59:45Z"
  labels:
    app.kubernetes.io/name: ibmcloud-operator
    seed.ibm.com/ibmcloud-token: apikey
  name: secret-ibm-cloud-operator
  namespace: default
  resourceVersion: "1186642"
  selfLink: /api/v1/namespaces/default/secrets/secret-ibm-cloud-operator
  uid: 6xxxxx0
type: Opaque
```

### Exercise 2 / Create a Cloudant service

Openshift Web Consoleにて、Operators - Installed Operators からProject: example-healthを選択してIBM Cloud Operatorをクリック。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/7b7a99df-d6c9-b553-c5c8-ad202205ee24.png)

ServiceのCreate Instanceをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/edf55c6a-64e8-800d-ac8f-45b038a307fc.png)

以下のYAMLに置き換えます。

```yaml
apiVersion: ibmcloud.ibm.com/v1alpha1
kind: Service
metadata:
  name: cloudant-service
spec:
  plan: lite
  serviceClass: cloudantnosqldb
```

Createをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/cc09c166-0646-14b3-e0ce-a822ac29a2d3.png)

しばらく待って、cloudant-serviceをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/b19686dc-4e91-b128-f783-32cec486341b.png)

Onlineになっていることを確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/ed9bc9ad-2dd4-c692-9bc1-d4061fcc1bd7.png)

さらに、IBM Cloudにログインして、cloudant serviceが作成されていることを確認します。
IBM Cloudのダッシュボードから、Servicesを選択。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/63b1c1ca-430c-bdfc-7576-e295d8ac452c.png)

cloudant-serviceがアクティブになっていることが確認できました。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/05920e09-3883-0fa1-d1e4-21731e4160aa.png)

Openshit web consoleに戻って、BindingのCreate Instanceをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/585f893f-0711-a0cd-29bc-b9bdc08848b4.png)

以下のYAMLに置き換えます。

```yaml
apiVersion: ibmcloud.ibm.com/v1alpha1
kind: Binding
metadata:
  name: cloudant-binding
spec:
  serviceName: cloudant-service
```

Createをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/2efd9286-e146-cc2b-60c5-c50e939c67ae.png)

しばらく待ってcloudant-bindingをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/74970e08-a7ea-3c05-514d-7641e9ddc364.png)

Onlineになっていることを確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/29e391bb-8663-31ae-74cc-bbe9f8439d2a.png)

Resourceタブからcloudant-bindingをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/fc677d74-8025-eef5-66ee-5e797e13f87e.png)

CloudantDBへのアクセス情報を確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/21dfca34-046b-35b1-e98c-bdb250507bc9.png)


### Exercise 3 / Deploy new application back-end

Terminalにて、プロジェクトをexample-healthにセットします。

```
container-lab$  oc project example-health
Now using project "example-health" on server "https://c100-e.us-south.containers.cloud.ibm.com:32416".
```

S2Iメソッドを使って、バックエンドのNode.jsアプリをデプロイします。

```
container-lab$ oc new-app --name=patient-db centos/nodejs-10-centos7~https://github.com/svennam92/nodejs-patientdb-cloudant
--> Found container image d138d6e (2 weeks old) from Docker Hub for "centos/nodejs-10-centos7"

    Node.js 10 
    ---------- 
    Node.js 10 available as container is a base platform for building and running various Node.js 10 applications and frameworks. Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

    Tags: builder, nodejs, nodejs10

    * An image stream tag will be created as "nodejs-10-centos7:latest" that will track the source image
    * A source build using source code from https://github.com/svennam92/nodejs-patientdb-cloudant will be created
      * The resulting image will be pushed to image stream tag "patient-db:latest"
      * Every time "nodejs-10-centos7:latest" changes a new build will be triggered
    * This image will be deployed in deployment config "patient-db"
    * Port 8080/tcp will be load balanced by service "patient-db"
      * Other containers can access this service through the hostname "patient-db"

--> Creating resources ...
    imagestream.image.openshift.io "nodejs-10-centos7" created
    imagestream.image.openshift.io "patient-db" created
    buildconfig.build.openshift.io "patient-db" created
    deploymentconfig.apps.openshift.io "patient-db" created
    service "patient-db" created
--> Success
    Build scheduled, use 'oc logs -f bc/patient-db' to track its progress.
    Application is not exposed. You can expose services to the outside world by executing one or more of the commands below:
     'oc expose svc/patient-db' 
    Run 'oc status' to view your app.
```

Opneshift web consoleにて、Developer perspective - Topologyからpatient-dbをクリックして右側のResourcesタブにてCrashLoopBackOffというステータスになっていることを確認します(CloudantDBとの接続ができていないのでエラーになっている状態)。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/5a8f2c44-73b3-e306-2538-cfbb5f10deb3.png)

patient-db をクリックします。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/fecbeec6-d507-ddb1-fba1-901ff69d659d.png)

Environment タブからAdd from Config Map or Secreteをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/033a36af-3775-c51b-6ac1-a6ed82b0f9e4.png)

Name: CLOUDANT_URL, resource: cloudant-binding, key: urlを指定してSaveをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/a1312dd3-07e8-1a4b-02f9-15abf825ae9f.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/617eaa26-475c-a889-5ef7-76145bac22f6.png)


再度patient-dbのステータスを確認し、Runningになっていることを確認
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/96d4f4ed-cd7e-9990-1996-fb2921a5e0d1.png)


### Exercise 4 / Configure Application to Use Cloudant

Node.jsのOpen URLのアイコンをクリックして、フロントエンドアプリにアクセスします。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/4087daf6-5634-3301-436c-6ddaf8fc5736.png)

settingsをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/a6903405-2900-923d-c5bd-e5efcb2feba2.png)

URLに`http://patient-db:8080/`を指定して、nodeをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/fd2ee797-e4a5-c739-cd9f-892e7c3e33ef.png)

opall/opallでログイン
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/dffee7b1-5569-968d-64f7-adb762cd873a.png)

これは、CloudantDBに登録されているユーザーID/Passwordでログインしていることになります。Lab1では人にの名前でログインできていましたが、ここではCloudantDBを使うように変更されているので、任意の名前でのログインはできなくなっています(CloudantDBに登録されていないユーザーでのログインは不可)。

CloudantDBの中身を確認してみます。

IBM Cloudにログインして、cloudant-serviceを選択します。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/5ff67e40-b0af-3883-9b31-8c662c604a28.png)

Launch Dashboardをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/3a7793b6-16f5-c57f-de7f-5635417a2e5e.png)

patientsをクリック
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/3162376d-0ac7-7fde-c0ca-326aab6d64f9.png)

JSONタブをクリックして、データの中身を確認。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/132331/d7a33c0a-8656-31c6-0830-50669ed71841.png)