CentOS7にOpenShift4(Red Hat CodeReady Containers)をインストールする

# はじめに
Dockerでいろいろ試してみてダメだったので、勉強用の中古PCをGetしました。
VMwareで仮想マシン立てて[OpenShift4(Red Hat CodeReady Containers)](https://try.openshift.com/)を試してみたいと思います。

まずはRed Hat Developer アカウントを作成する必要があります。

OpenShift 4のページの**GET STARTED**から**Infrastructure Provider**で**LapTop**を選択します。

![01.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/40801df2-4c3a-89ba-6431-49d0d547e0ad.png)

OpenShift4(Red Hat CodeReady Containers)のインストーラーのURLをメモメモ
そしてOpenShift4(Red Hat CodeReady Containers)スタート時に使うPull Secretをコピーしておきます。(下側のCopy Pull Secretより)
![02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/889bac1a-f251-7998-5304-e13847b95dfb.png)


# 環境
- Windows10 Pro
- VMware Workstation 15 Player
    - CentOS7-1908 with GNOME Desktop

## Prerequisites

OSに関する要求事項。ハードルがかなり上がってます。

> CodeReady Containers requires the following system resources in order to run OpenShift:
> - 4 virtual CPUs (vCPUs)
> - 8 GB of memory
> - 35 GB of storage space

# 下準備
VMWare Workstation 15 PlayerにCentOS7を立てます。
利用したのは[CentOS-7-x86_64-Minimal-1908.iso](https://wiki.centos.org/Download)のイメージ
これにGNOME Desktopを入れました。
とりあえずvCPU:4, Memory: 8GB, HDD:50GBとしています。

# OpenShift4(Red Hat CodeReady Containers)のインストール

OpenShift4(Red Hat CodeReady Containers)を実行するユーザは一般ユーザです。
ただし、セットアップを行う際にsudoを行いますので、/etc/sudoersを修正しsudoできるようにします。

[1.3. Installing CodeReady Containers](https://code-ready.github.io/crc/#installing-codeready-containers_gsg)に従いインストールしていきます。


## まずは要求されているソフトウェアのインストール確認

[1.4. Required software packages](https://code-ready.github.io/crc/#required-software-packages_gsg)より、`NetworkManager`が必要だそうです。
インストールされているか確認します。

```console
[root@centos7 ~]# yum install NetworkManager
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: ftp.riken.jp
base                                                                                                                                                                  | 3.6 kB  00:00:00     
extras                                                                                                                                                                | 2.9 kB  00:00:00     
updates                                                                                                                                                               | 2.9 kB  00:00:00     
updates/7/x86_64/primary_db                                                     76% [=====================================================-                ]  0.0 B/s | 1.4 MB  --:--:-- ETA updates/7/x86_64/primary_db                                                                                                                                           | 1.9 MB  00:00:00     
パッケージ 1:NetworkManager-1.18.0-5.el7_7.1.x86_64 はインストール済みか最新バージョンです
何もしません
```

特に記載はありませんでしたが、`libvirtd`の設定も行っておきました。

```console
[root@centos7 ~]# usermod -aG libvirt tak
[root@centos7 ~]# exit
ログアウト
[tak@centos7 ~]$ newgrp libvirt
```


下準備が終わったのでいよいよセットアップ開始です。
[Install on Laptop: Red Hat CodeReady Containers](https://cloud.redhat.com/openshift/install/crc/installer-provisioned)からLinux: Download (Libvirt)のURLをコピーし`curl`でダウンロードします。

```console
[tak@centos7 ~]$ curl -sS https://mirror.openshift.com/pub/openshift-v4/clients/crc/latest/crc-linux-amd64.tar.xz -o /tmp/crc-linux-amd64.tar.xz
[tak@centos7 ~]$ cd /tmp
[tak@centos7 tmp]$ ls crc*
crc-linux-amd64.tar.xz    
```

ダウンロードできたら解凍しバイナリー(`crc`)をパスの通った場所に移動します。

```console
[tak@centos7 tmp]$ xz  -dc /tmp/crc-linux-amd64.tar.xz | tar xfv -
crc-linux-1.0.0-rc.0-amd64/
crc-linux-1.0.0-rc.0-amd64/LICENSE
crc-linux-1.0.0-rc.0-amd64/doc.pdf
crc-linux-1.0.0-rc.0-amd64/crc
[tak@centos7 tmp]$ su -
パスワード:
最終ログイン: 2019/10/05 (土) 09:20:45 JST日時 pts/1
[root@centos7 ~]# mv /tmp/crc-linux-1.0.0-rc.0-amd64/crc /usr/local/bin
[root@centos7 ~]# ll /usr/local/bin/
合計 2304424
-rwxr-xr-x. 1 tak libvirt 2359726874  9月 29 20:56 crc
[root@centos7 ~]# exit
ログアウト
[tak@centos7 tmp]$ cd ~
[tak@centos7 ~]$
```

## OpenShift4(Red Hat CodeReady Containers)の初期セットアップ

初期セットアップはsudoさえできれば簡単で、`crc setup`するだけです。
セットアップメッセージを確認すると`INFO Adding user to libvirt group`と書いてあるので
事前準備で行った`libvirtd`の設定は不要だったかもしれません。

```console
[tak@centos7 ~]$ crc setup
INFO Checking if running as non-root              
INFO Caching oc binary                            
INFO Setting up virtualization                    
INFO Setting up KVM                               
INFO Installing libvirt service and dependencies  
INFO Will use root access: install virtualization related packages 
[sudo] tak のパスワード:
INFO Adding user to libvirt group                 
INFO Enabling libvirt                             
INFO Starting libvirt service                     
INFO Checking if a supported libvirt version is installed 
INFO Installing crc-driver-libvirt                
INFO Removing older system-wide crc-driver-libvirt 
INFO Setting up libvirt 'crc' network             
INFO Starting libvirt 'crc' network               
INFO Checking if NetworkManager is installed      
INFO Checking if NetworkManager service is running 
INFO Writing Network Manager config for crc       
INFO Will use root access: write NetworkManager config in /etc/NetworkManager/conf.d/crc-nm-dnsmasq.conf 
INFO Will use root access: execute systemctl daemon-reload command 
INFO Will use root access: execute systemctl stop/start command 
INFO Writing dnsmasq config for crc               
INFO Will use root access: write dnsmasq configuration in /etc/NetworkManager/dnsmasq.d/crc.conf 
INFO Will use root access: execute systemctl daemon-reload command 
INFO Will use root access: execute systemctl stop/start command 
INFO Unpacking bundle from the CRC binary         
Setup is complete, you can now run 'crc start' to start a CodeReady Containers instance
```

## OpenShift4(Red Hat CodeReady Containers)の実行

ではOpenShift4(Red Hat CodeReady Containers)を起動します。
`crc start`と入力します。
途中で`? Image pull secret [? for help] `が出たら最初にメモっていたPull Secretを入力します。
(下側のCopy Pull Secretより)
![02.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/889bac1a-f251-7998-5304-e13847b95dfb.png)


```console
[tak@centos7 ~]$ crc start
INFO Checking if running as non-root              
INFO Checking if oc binary is cached              
INFO Checking if Virtualization is enabled        
INFO Checking if KVM is enabled                   
INFO Checking if libvirt is installed             
INFO Checking if user is part of libvirt group    
INFO Checking if libvirt is enabled               
INFO Checking if libvirt daemon is running        
INFO Checking if a supported libvirt version is installed 
INFO Checking if crc-driver-libvirt is installed  
INFO Checking if libvirt 'crc' network is available 
INFO Checking if libvirt 'crc' network is active  
INFO Checking if NetworkManager is installed      
INFO Checking if NetworkManager service is running 
INFO Checking if /etc/NetworkManager/conf.d/crc-nm-dnsmasq.conf exists 
INFO Checking if /etc/NetworkManager/dnsmasq.d/crc.conf exists 
? Image pull secret [? for help] 

INFO Extracting bundle: crc_libvirt_4.2.0-0.nightly-2019-09-26-192831.crcbundle ... 
INFO Creating CodeReady Containers VM for OpenShift 4.2.0-0.nightly-2019-09-26-192831... 
```

ここでTeraTermが落ちてしまいました。嫌な予感が…
一度を停止し再度ホスト側のターミナルで実行してみます。

```console
[tak@centos7 ~]$ crc stop
Stopping CodeReady Containers instance... this may take a few minutes
CodeReady Containers instance stopped
```

```console
[tak@centos7 ~]$ crc start
INFO Checking if running as non-root              
INFO Checking if oc binary is cached              
INFO Checking if Virtualization is enabled        
INFO Checking if KVM is enabled                   
INFO Checking if libvirt is installed             
INFO Checking if user is part of libvirt group    
INFO Checking if libvirt is enabled               
INFO Checking if libvirt daemon is running        
INFO Checking if a supported libvirt version is installed 
INFO Checking if crc-driver-libvirt is installed  
INFO Checking if libvirt 'crc' network is available 
INFO Checking if libvirt 'crc' network is active  
INFO Checking if NetworkManager is installed      
INFO Checking if NetworkManager service is running 
INFO Checking if /etc/NetworkManager/conf.d/crc-nm-dnsmasq.conf exists 
INFO Checking if /etc/NetworkManager/dnsmasq.d/crc.conf exists 
INFO Starting CodeReady Containers VM for OpenShift 4.2.0-0.nightly-2019-09-26-192831... 
ERRO Error occurred: Failed to connect to the crc VM with SSH 
```
マジですか…

# 気を取り直して再セットアップ

VMイメージを一度削除し再度インストールしてみました。
今度はTeraTerm経由ではなく、CentOS7のコンソールで実行してます。
なお、今回は`libvirtd`の設定を事前に行っていません。

## OpenShift4(Red Hat CodeReady Containers)のセットアップ(`crc setup`)

```console
[tak@centos7 tmp]$ crc setup
INFO Checking if running as non-root              
INFO Caching oc binary                            
INFO Setting up virtualization                    
INFO Setting up KVM                               
INFO Installing libvirt service and dependencies  
INFO Will use root access: install virtualization related packages 
INFO Adding user to libvirt group                 
INFO Will use root access: add user to libvirt group 
INFO Enabling libvirt                             
INFO Starting libvirt service                     
INFO Checking if a supported libvirt version is installed 
INFO Installing crc-driver-libvirt                
INFO Removing older system-wide crc-driver-libvirt 
INFO Setting up libvirt 'crc' network             
INFO Starting libvirt 'crc' network               
INFO Checking if NetworkManager is installed      
INFO Checking if NetworkManager service is running 
INFO Writing Network Manager config for crc       
INFO Will use root access: write NetworkManager config in /etc/NetworkManager/conf.d/crc-nm-dnsmasq.conf 
INFO Will use root access: execute systemctl daemon-reload command 
INFO Will use root access: execute systemctl stop/start command 
INFO Writing dnsmasq config for crc               
INFO Will use root access: write dnsmasq configuration in /etc/NetworkManager/dnsmasq.d/crc.conf 
INFO Will use root access: execute systemctl daemon-reload command 
INFO Will use root access: execute systemctl stop/start command 
INFO Unpacking bundle from the CRC binary         
Setup is complete, you can now run 'crc start' to start a CodeReady Containers instance
```

## OpenShift4(Red Hat CodeReady Containers)の起動(`crc start`)

```console
[tak@centos7 tmp]$ crc start
INFO Checking if running as non-root              
INFO Checking if oc binary is cached              
INFO Checking if Virtualization is enabled        
INFO Checking if KVM is enabled                   
INFO Checking if libvirt is installed             
INFO Checking if user is part of libvirt group    
INFO Checking if libvirt is enabled               
INFO Checking if libvirt daemon is running        
INFO Checking if a supported libvirt version is installed 
INFO Checking if crc-driver-libvirt is installed  
INFO Checking if libvirt 'crc' network is available 
INFO Checking if libvirt 'crc' network is active  
INFO Checking if NetworkManager is installed      
INFO Checking if NetworkManager service is running 
INFO Checking if /etc/NetworkManager/conf.d/crc-nm-dnsmasq.conf exists 
INFO Checking if /etc/NetworkManager/dnsmasq.d/crc.conf exists 
? Image pull secret [? for help] 

INFO Extracting bundle: crc_libvirt_4.2.0-0.nightly-2019-09-26-192831.crcbundle ... 
INFO Creating CodeReady Containers VM for OpenShift 4.2.0-0.nightly-2019-09-26-192831... 
INFO Verifying validity of the cluster certificates ... 
INFO Check internal and public dns query ...      
INFO Copying kubeconfig file to instance dir ...  
INFO Adding user's pull secret and cluster ID ... 
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, username is 'kubeadmin' and password is *****-*****-*****-***** 
INFO                                              
INFO You can now run 'crc console' and use these credentials to access the OpenShift web console 
CodeReady Containers instance is running
[tak@centos7 tmp]$ 
```

今度はうまくいきました！

## OpenShift4(Red Hat CodeReady Containers)のコンソールへアクセス(`crc console`)

ではOpenShift4(Red Hat CodeReady Containers)のコンソールとご対面しましょうか♪

```console
[tak@centos7 tmp]$ crc console
Opening the OpenShift Web Console in the default browser...
This tool has been deprecated, use 'gio open' instead.
See 'gio help open' for more info.

```

![03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/7719722b-54b0-b026-d2cb-8562198c7a05.png)

Log in with...
`kube:admin`を選択してみます。

![04.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/5bc09dd6-3613-b617-1e82-8c897da82378.png)

とりあえずdeveloper？

![05.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/2852a138-5679-e193-f257-ea80964b7531.png)

![06.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/0029bbc5-6ba8-49e4-38c0-871f0cf94b86.png)

おやおや。。。

じゃあ、
Log in with...
`htpasswd_provider`を選択してみます。

![07.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/d8415999-afec-473e-69cc-85a1e92f39ad.png)

![06.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/0029bbc5-6ba8-49e4-38c0-871f0cf94b86.png)

どうやらユーザ作る必要がありそうですね（汗
と思ったら、`crc start`の出力に書いてありました（笑）

## OpenShift4(Red Hat CodeReady Containers)のコンソールへログイン

```console
[tak@centos7 tmp]$ crc console
Opening the OpenShift Web Console in the default browser...
This tool has been deprecated, use 'gio open' instead.
See 'gio help open' for more info.

```

![03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/7719722b-54b0-b026-d2cb-8562198c7a05.png)

Log in with...
`htpasswd_provider`を選択します。

`developer/develoer`でログイン

![07-5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/6ba4d40b-f8bc-f88d-8794-981d69172329.png)

![08.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/36462afb-0d99-69e6-7275-6716c3a35174.png)

ようやくご対面♪
大幅にUIも変わっておりこれは大変かも（笑）

![09.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/324255/0e241bad-4c7d-d460-ed4b-b3ea64045d35.png)