Red Hat CodeReady Containers 1.10セットアップとアプリケーションデプロイ

# 概要
MacOSにOpenShift CodeReady Containers 1.10をインストールし、テストアプリをデプロイしたので、その記録を残しておきます。

## 参考URL

CodeReady Containersの構築手順
https://code-ready.github.io/crc/



## 環境

今回用意した環境

| 項目 | スペック |
| :-- | :-- |
| マシン | MacBook Pro 13 （Catalina） |
| CPU | 8 core |
| メモリ | 16 GB |
| ストレージ容量 | 90 GB |

必要スペック

| 項目 | スペック |
| :-- | :-- |
| CPU | 4 core |
| メモリ | 9 GB |
| ストレージ容量 | 35 GB |

## OpenShift CodeReady Containersセットアップ手順

##### 1. 任意のフォルダにソースをダウンロードして、解凍する
```
curl -L https://mirror.openshift.com/pub/openshift-v4/clients/crc/latest/crc-macos-amd64.tar.xz -o crc-macos-amd64.tar.xz
```
```
xz -dc crc-macos-amd64.tar.xz | tar xfv -
```

ファイルを解凍すると、LICENSE、doc.pdf、crcができる。

```
codeready % xz -dc crc-macos-amd64.tar.xz | tar xfv -
x crc-macos-1.10.0-amd64/
x crc-macos-1.10.0-amd64/LICENSE
x crc-macos-1.10.0-amd64/doc.pdf
x crc-macos-1.10.0-amd64/crc
```

##### 2. 解凍したフォルダに移動して`crc setup`コマンドを実行する

```
cd crc-macos-1.10.0-amd64 
crc-macos-1.10.0-amd64 % ./crc setup
INFO Checking if oc binary is cached              
INFO Caching oc binary                            
INFO Checking if podman remote binary is cached   
INFO Checking if CRC bundle is cached in '$HOME/.crc' 
INFO Unpacking bundle from the CRC binary         
INFO Checking if running as non-root              
INFO Checking if HyperKit is installed            
INFO Setting up virtualization with HyperKit      
INFO Will use root access: change ownership of /Users/nakagawaeiji/.crc/bin/hyperkit 
Password:
INFO Will use root access: set suid for /Users/nakagawaeiji/.crc/bin/hyperkit 
INFO Checking if crc-driver-hyperkit is installed 
INFO Installing crc-machine-hyperkit              
INFO Will use root access: change ownership of /Users/nakagawaeiji/.crc/bin/crc-driver-hyperkit 
INFO Will use root access: set suid for /Users/nakagawaeiji/.crc/bin/crc-driver-hyperkit 
INFO Checking file permissions for /etc/resolver/testing 
INFO Setting file permissions for /etc/resolver/testing 
INFO Will use root access: create dir /etc/resolver 
INFO Will use root access: create file /etc/resolver/testing 
INFO Will use root access: change ownership of /etc/resolver/testing 
INFO Checking file permissions for /etc/hosts     
INFO Setting file permissions for /etc/hosts      
INFO Will use root access: change ownership of /etc/hosts 
Setup is complete, you can now run 'crc start' to start the OpenShift cluster
```

##### 3. 以下のRedHat Openshfit Clusterにアクセスして「Download pull secret」を押してPull Secretファイルをダウンロードしておく。（RedHatアカウント登録が必要）

https://cloud.redhat.com/openshift/install/crc/installer-provisioned

![Clustermanager.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/90447/c607dfdb-8df1-dc34-9a1d-cab65063aaea.png)


##### 4. `crc start -p <pull secret file>`でスタートする。今回はPull Secretファイルを`~/Download/pull-sercret.txt`としている。

```
crc-macos-1.10.0-amd64 % ./crc start -p ~/Downloads/pull-secret.txt 
INFO Checking if oc binary is cached              
INFO Checking if podman remote binary is cached   
INFO Checking if running as non-root              
INFO Checking if HyperKit is installed            
INFO Checking if crc-driver-hyperkit is installed 
INFO Checking file permissions for /etc/resolver/testing 
INFO Checking file permissions for /etc/hosts     
INFO Starting CodeReady Containers VM for OpenShift 4.4.3... 
INFO CodeReady Containers VM is running           
INFO Verifying validity of the cluster certificates ... 
INFO Network restart not needed                   
INFO Check internal and public DNS query ...      
INFO Check DNS query from host ...                
INFO Starting OpenShift kubelet service           
INFO Starting OpenShift cluster ... [waiting 3m]  
INFO                                              
INFO To access the cluster, first set up your environment by following 'crc oc-env' instructions 
INFO Then you can access it by running 'oc login -u developer -p developer https://api.crc.testing:6443' 
INFO To login as an admin, run 'oc login -u kubeadmin -p xxxxx-xxxxx-xxxxx-xxxxx https://api.crc.testing:6443' 
INFO                                              
INFO You can now run 'crc console' and use these credentials to access the OpenShift web console 
Started the OpenShift cluster
WARN The cluster might report a degraded or error state. This is expected since several operators have been disabled to lower the resource usage. For more information, please consult the documentation 
```

ここまで来たらセットアップ完了。`oc console`でWebブラウザーが起動しOpenShiftコンソールが開く。ID/パスワードは、`crc start`の実行結果の`oc login -u <username> -p <password>`に記載されている。今回は、以下でログインできる。


| アカウント名 | パスワード | 備考 | 
|:-- |:-- | :-- |
| developer | developer | 開発用アカウント |
| kubeadmin | xxxxx-xxxxx-xxxxx-xxxxx（マスキングしました） | 管理者用アカウント |


また、コマンドラインからは`oc login -u developer -p developer https://api.crc.testing:6443`と`oc login -u kubeadmin -p xxxxx-xxxxx-xxxxx-xxxxx https://api.crc.testing:6443`でログインできる。

※ `oc`コマンドは`~/.crc/bin/`に配置されているため、デフォルトパスで使用したい場合は`/usr/local/bin/`などに移動しておく。

なお、後からでも`crc console --credentials`でID/パスワードを確認することが可能

```
crc-macos-1.10.0-amd64 % ./crc console --credentials
To login as a regular user, run 'oc login -u developer -p developer https://api.crc.testing:6443'.
To login as an admin, run 'oc login -u kubeadmin -p xxxxx-xxxxx-xxxxx-xxxxx https://api.crc.testing:6443'
```

コマンドラインでログインして`oc get node`、`oc get all`で情報確認してみる。

```
crc-macos-1.10.0-amd64 % oc login -u kubeadmin -p xxxxx-xxxxx-xxxxx-xxxxx https://api.crc.testing:6443
The server uses a certificate signed by an unknown authority.
You can bypass the certificate check, but any data you send to the server could be intercepted by others.
Use insecure connections? (y/n): y

Login successful.

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

Using project "default".
crc-macos-1.10.0-amd64 % oc get node
NAME                 STATUS   ROLES           AGE   VERSION
crc-dv9sm-master-0   Ready    master,worker   17d   v1.17.1
crc-macos-1.10.0-amd64 % oc get all
NAME                 TYPE           CLUSTER-IP   EXTERNAL-IP                            PORT(S)   AGE
service/kubernetes   ClusterIP      172.30.0.1   <none>                                 443/TCP   17d
service/openshift    ExternalName   <none>       kubernetes.default.svc.cluster.local   <none>    17d
```

##### 補足：Webコンソールでの操作は省略しますが。。
コンソールを起動したい場合は`crc console`でWebブラウザーが起動しログイン画面に切り替わる。ログインID/パスワードは、`crc console --credentials`で表示されるID/パスワードでログインできる。

```
crc-macos-1.10.0-amd64 % ./crc console
Opening the OpenShift Web Console in the default browser...
```

コンソールは起動しログインできるが、なぜかOverviewステータスはうまく表示されず。。。でも、他のコンポーネントは表示できているので良しとするか。。。。
![Overview.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/90447/5fc77f80-bb76-7fda-3556-7eab9192eba4.png)

---

# サンプルアプリデプロイ　その１

サンプルアプリデプロイはコマンドラインで実施します。Webコンソールから実施することも可能です。

## 参考URL
https://access.redhat.com/documentation/ja-jp/openshift_container_platform/4.1/html/applications/application-life-cycle-management#creating-new-applications


##### 1. CodeReadyにDeveloperアカウントでログイン

`oc login -u developer -p developer`

##### 2. 新規プロジェクトを作成。今回はsample-appプロジェクト。

`oc new-project sample-app`

```
sample-app % oc new-project sample-app
Now using project "sample-app" on server "https://api.crc.testing:6443".

You can add applications to this project with the 'new-app' command. For example, try:

    oc new-app django-psql-example

to build a new example application in Python. Or use kubectl to deploy a simple Kubernetes application:

    kubectl create deployment hello-node --image=gcr.io/hello-minikube-zero-install/hello-node
```

##### 3. rubyのhello-worldアプリをデプロイ

`oc new-app https://github.com/openshift/ruby-hello-world --name=myapp`

```
sample-app % oc new-app https://github.com/openshift/ruby-hello-world --name=myapp
--> Found container image 2e83f12 (13 days old) from Docker Hub for "centos/ruby-25-centos7"

    Ruby 2.5 
    -------- 
    Ruby 2.5 available as container is a base platform for building and running various Ruby 2.5 applications and frameworks. Ruby is the interpreted scripting language for quick and easy object-oriented programming. It has many features to process text files and to do system management tasks (as in Perl). It is simple, straight-forward, and extensible.

    Tags: builder, ruby, ruby25, rh-ruby25

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

--> Creating resources ...
    imagestream.image.openshift.io "ruby-25-centos7" created
    imagestream.image.openshift.io "myapp" created
    buildconfig.build.openshift.io "myapp" created
    deploymentconfig.apps.openshift.io "myapp" created
    service "myapp" created
--> Success
    Build scheduled, use 'oc logs -f bc/myapp' 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/myapp' 
    Run 'oc status' to view your app.
```

##### 4. `oc expose svc/myapp`で外部アクセス用のサービスを作成

```
sample-app % oc expose svc/myapp
route.route.openshift.io/myapp exposed
```

##### 5.`oc status`でステータスを確認

```
sample-app % oc status
In project sample-app on server https://api.crc.testing:6443

http://myapp-sample-app.apps-crc.testing to pod port 8080-tcp (svc/myapp)
  dc/myapp deploys istag/myapp:latest <-
    bc/myapp docker builds https://github.com/openshift/ruby-hello-world on istag/ruby-25-centos7:latest 
      build #1 running for about a minute - 57073c0: Merge pull request #85 from wewang58/master (Ben Parees <bparees@users.noreply.github.com>)
    deployment #1 waiting on image or update


2 infos identified, use 'oc status --suggest' to see details.

```

この時点では、デプロイメントステータスが`deployment #1 waiting on image or update`となっており、まだデプロイ途中である。よって、`http://myapp-sample-app.apps-crc.testing`にアクセスしてもエラー表示となる。

数分経過して、もう一度`oc status`で状況確認する。

```
sample-app % oc status
In project sample-app on server https://api.crc.testing:6443

http://myapp-sample-app.apps-crc.testing to pod port 8080-tcp (svc/myapp)
  dc/myapp deploys istag/myapp:latest <-
    bc/myapp docker builds https://github.com/openshift/ruby-hello-world on istag/ruby-25-centos7:latest 
    deployment #1 deployed 2 minutes ago - 1 pod


2 infos identified, use 'oc status --suggest' to see details.

```
今度は、デプロイメントステータスが`deployment #1 deployed 2 minutes ago - 1 pod`となっているのでアクセス可能になってる。

##### 6. この状態で`oc status`の結果にある`http://myapp-sample-app.apps-crc.testing`にアクセスするとWebページが表示される。

WebコンソールのRoutesの画面
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/90447/7b270072-7698-d46f-47fc-4c2d5c87fad1.png)
サンプルアプリ画面
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/90447/5dd8336f-9e62-d56d-9be1-8ef57bafe616.png)

（参考）`oc get all`の実行結果

```
sample-app % oc get all
NAME                 READY   STATUS      RESTARTS   AGE
pod/myapp-1-build    0/1     Completed   0          7m53s
pod/myapp-1-deploy   0/1     Completed   0          6m32s
pod/myapp-1-rmdks    1/1     Running     0          6m29s

NAME                            DESIRED   CURRENT   READY   AGE
replicationcontroller/myapp-1   1         1         1       6m32s

NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/myapp   ClusterIP   172.30.249.248   <none>        8080/TCP   7m55s

NAME                                       REVISION   DESIRED   CURRENT   TRIGGERED BY
deploymentconfig.apps.openshift.io/myapp   1          1         1         config,image(myapp:latest)

NAME                                   TYPE     FROM   LATEST
buildconfig.build.openshift.io/myapp   Docker   Git    1

NAME                               TYPE     FROM          STATUS     STARTED         DURATION
build.build.openshift.io/myapp-1   Docker   Git@57073c0   Complete   7 minutes ago   1m21s

NAME                                             IMAGE REPOSITORY                                                                     TAGS     UPDATED
imagestream.image.openshift.io/myapp             default-route-openshift-image-registry.apps-crc.testing/sample-app/myapp             latest   6 minutes ago
imagestream.image.openshift.io/ruby-25-centos7   default-route-openshift-image-registry.apps-crc.testing/sample-app/ruby-25-centos7   latest   7 minutes ago

NAME                             HOST/PORT                           PATH   SERVICES   PORT       TERMINATION   WILDCARD
route.route.openshift.io/myapp   myapp-sample-app.apps-crc.testing          myapp      8080-tcp                 None
```

---

# サンプルアプリデプロイ　その２

## 参考URL
https://access.redhat.com/documentation/ja-jp/openshift_container_platform/4.4/html/cli_tools/openshift-cli-oc


##### 1. CodeReadyにDeveloperアカウントでログイン

`oc login -u developer -p developer`

##### 2. 新規プロジェクトを作成。今回はsample-appプロジェクト。

`oc new-project sample-app2`

##### 3. CakePHPサンプルアプリをデプロイ

`oc new-app https://github.com/sclorg/cakephp-ex`

```
sample-app % oc new-app https://github.com/sclorg/cakephp-ex
--> Found image cb8f494 (2 weeks old) in image stream "openshift/php" under tag "7.3" for "php"

    Apache 2.4 with PHP 7.3 
    ----------------------- 
    PHP 7.3 available as container is a base platform for building and running various PHP 7.3 applications and frameworks. PHP is an HTML-embedded scripting language. PHP attempts to make it easy for developers to write dynamically generated web pages. PHP also offers built-in database integration for several commercial and non-commercial database management systems, so writing a database-enabled webpage with PHP is fairly simple. The most common use of PHP coding is probably as a replacement for CGI scripts.

    Tags: builder, php, php73, rh-php73

    * The source repository appears to match: php
    * A source build using source code from https://github.com/sclorg/cakephp-ex will be created
      * The resulting image will be pushed to image stream tag "cakephp-ex:latest"
      * Use 'oc start-build' to trigger a new build
    * This image will be deployed in deployment config "cakephp-ex"
    * Ports 8080/tcp, 8443/tcp will be load balanced by service "cakephp-ex"
      * Other containers can access this service through the hostname "cakephp-ex"

--> Creating resources ...
    imagestream.image.openshift.io "cakephp-ex" created
    buildconfig.build.openshift.io "cakephp-ex" created
    deploymentconfig.apps.openshift.io "cakephp-ex" created
    service "cakephp-ex" created
--> Success
    Build scheduled, use 'oc logs -f bc/cakephp-ex' 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/cakephp-ex' 
    Run 'oc status' to view your app.
```

##### 4. `oc expose svc/cakephp-ex`で外部アクセス用のサービスを作成

```
sample-app % oc expose svc/cakephp-ex
route.route.openshift.io/cakephp-ex exposed
```

##### 5. `oc status`でステータスを確認

デプロイメントステータスが`deployment #1 deployed 1 minutes ago - 1 pod`となっているのでアクセス可能になってる。

```
sample-app % oc status
In project sample-app2 on server https://api.crc.testing:6443

http://cakephp-ex-sample-app2.apps-crc.testing to pod port 8080-tcp (svc/cakephp-ex)
  dc/cakephp-ex deploys istag/cakephp-ex:latest <-
    bc/cakephp-ex source builds https://github.com/sclorg/cakephp-ex on openshift/php:7.3 
    deployment #1 deployed about a minute ago - 1 pod


2 infos identified, use 'oc status --suggest' to see details.
```

##### 6. この状態で`oc status`の結果にある`http://cakephp-ex-sample-app2.apps-crc.testing `にアクセスするとWebページが表示される。

WebコンソールのRoutes画面
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/90447/3048c6e4-5e9c-d3d5-abf0-dfbb6122688f.png)

アクセス画面
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/90447/0f613215-a44b-f023-3f11-f1ea2360ca87.png)


（参考）`oc get all`の実行結果

```
sample-app % oc get all
NAME                      READY   STATUS      RESTARTS   AGE
pod/cakephp-ex-1-build    0/1     Completed   0          6m4s
pod/cakephp-ex-1-deploy   0/1     Completed   0          4m14s
pod/cakephp-ex-1-v8drz    1/1     Running     0          4m11s

NAME                                 DESIRED   CURRENT   READY   AGE
replicationcontroller/cakephp-ex-1   1         1         1       4m15s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE
service/cakephp-ex   ClusterIP   172.30.30.244   <none>        8080/TCP,8443/TCP   6m5s

NAME                                            REVISION   DESIRED   CURRENT   TRIGGERED BY
deploymentconfig.apps.openshift.io/cakephp-ex   1          1         1         config,image(cakephp-ex:latest)

NAME                                        TYPE     FROM   LATEST
buildconfig.build.openshift.io/cakephp-ex   Source   Git    1

NAME                                    TYPE     FROM          STATUS     STARTED         DURATION
build.build.openshift.io/cakephp-ex-1   Source   Git@133cb8b   Complete   6 minutes ago   1m50s

NAME                                        IMAGE REPOSITORY                                                                 TAGS     UPDATED
imagestream.image.openshift.io/cakephp-ex   default-route-openshift-image-registry.apps-crc.testing/sample-app2/cakephp-ex   latest   4 minutes ago

NAME                                  HOST/PORT                                 PATH   SERVICES     PORT       TERMINATION   WILDCARD
route.route.openshift.io/cakephp-ex   cakephp-ex-sample-app2.apps-crc.testing          cakephp-ex   8080-tcp                 None
```

### 所感
OpenShift4と比較してCodeReadyのほうは意外に簡単にデプロイできました。OpenShift4の構築は事前にDNSなどのNW環境構築が必要ですが、CodeReadyではそれが不要になっています。すぐに操作するには良いのですが、OpenShiftのアーキテクチャを理解するにはちょっと物足りない感じでした。

以上