Minishiftで遊んでみよう！手軽にkubernetesを体験するの巻

# はじめに
無料でOpenshiftを試せるminishiftで遊んでみました。という内容です。

## 環境
macOS Catalina (10.15.4)
Minishift (1.34.3)
[Minishift 最新versionを確認](https://github.com/minishift/minishift/releases)

Minishiftでは、OpenShiftクラスターがプロビジョニングされている仮想マシンを起動するためにハイパーバイザーが必要なので、ハイパーバイザーがシステムにインストールされ、有効になっている必要があります

### インストール方法
プラットフォームごとにインストール手順がいつくかあります
・ macOS → hyperkit
・ Linux → KVM
・ Windows → Hyper-V
**・ All Platforms → VirtualBox**

今回は、VirtualBoxを利用していこうと思います。
理由としては、この手順でのインストール方法で検証されている方が多く、過去にも試したことがある為です

#### VirtualBoxのインストール
こちらのコマンドを実施します

```
brew cask install minishift
```

#### minishiftのインストール
こちらのコマンドを実施します

```
brew cask install minishift
```

### VirtualBoxを利用して、minishiftを起動します
引数で--vm-driver=virtualboxを指定することで、virtualboxでの起動が可能になります

```
minishift start --vm-driver=virtualbox
```

実行ログ

```
-- Starting profile 'minishift'
-- Check if deprecated options are used ... OK
-- Checking if https://github.com is reachable ... OK
-- Checking if requested OpenShift version 'v3.11.0' is valid ... OK
-- Checking if requested OpenShift version 'v3.11.0' is supported ... OK
-- Checking if requested hypervisor 'virtualbox' is supported on this platform ... OK
-- Checking if VirtualBox is installed ... OK
-- Checking the ISO URL ... OK
-- Downloading OpenShift binary 'oc' version 'v3.11.0'
 53.27 MiB / 53.27 MiB [============================================] 100.00% 0s-- Downloading OpenShift v3.11.0 checksums ... OK
-- Checking if provided oc flags are supported ... OK
-- Starting the OpenShift cluster using 'virtualbox' hypervisor ...
-- Minishift VM will be configured with ...
   Memory:    4 GB
   vCPUs :    2
   Disk size: 20 GB

   Downloading ISO 'https://github.com/minishift/minishift-centos-iso/releases/download/v1.16.0/minishift-centos7.iso'
 370.00 MiB / 370.00 MiB [==========================================] 100.00% 0s
-- Starting Minishift VM .................................. OK
-- Checking for IP address ... OK
-- Checking for nameservers ... OK
-- Checking if external host is reachable from the Minishift VM ...
   Pinging 8.8.8.8 ... OK
-- Checking HTTP connectivity from the VM ...
   Retrieving http://minishift.io/index.html ... OK
-- Checking if persistent storage volume is mounted ... OK
-- Checking available disk space ... 1% used OK
-- Writing current configuration for static assignment of IP address ... OK
   Importing 'openshift/origin-control-plane:v3.11.0' . CACHE MISS
   Importing 'openshift/origin-docker-registry:v3.11.0' . CACHE MISS
   Importing 'openshift/origin-haproxy-router:v3.11.0' . CACHE MISS
-- OpenShift cluster will be configured with ...
   Version: v3.11.0
-- Pulling the OpenShift Container Image ............. OK
-- Copying oc binary from the OpenShift container image to VM ... OK
-- Starting OpenShift cluster ............................................................
Getting a Docker client ...
Checking if image openshift/origin-control-plane:v3.11.0 is available ...
Pulling image openshift/origin-cli:v3.11.0
E1022 15:53:08.320557    2643 helper.go:173] Reading docker config from /home/docker/.docker/config.json failed: open /home/docker/.docker/config.json: no such file or directory, will attempt to pull image docker.io/openshift/origin-cli:v3.11.0 anonymously
Image pull complete
Pulling image openshift/origin-node:v3.11.0
E1022 15:53:13.117920    2643 helper.go:173] Reading docker config from /home/docker/.docker/config.json failed: open /home/docker/.docker/config.json: no such file or directory, will attempt to pull image docker.io/openshift/origin-node:v3.11.0 anonymously
Pulled 5/6 layers, 88% complete
Pulled 6/6 layers, 100% complete
Extracting
Image pull complete
Checking type of volume mount ...
Determining server IP ...
Using public hostname IP 192.168.99.100 as the host IP
Checking if OpenShift is already running ...
Checking for supported Docker version (=>1.22) ...
Checking if insecured registry is configured properly in Docker ...
Checking if required ports are available ...
Checking if OpenShift client is configured properly ...
Checking if image openshift/origin-control-plane:v3.11.0 is available ...
Starting OpenShift using openshift/origin-control-plane:v3.11.0 ...
I1022 15:53:26.585128    2643 config.go:40] Running "create-master-config"
I1022 15:53:28.548205    2643 config.go:46] Running "create-node-config"
I1022 15:53:29.414864    2643 flags.go:30] Running "create-kubelet-flags"
I1022 15:53:29.756520    2643 run_kubelet.go:49] Running "start-kubelet"
I1022 15:53:29.943541    2643 run_self_hosted.go:181] Waiting for the kube-apiserver to be ready ...
I1022 15:54:13.959145    2643 interface.go:26] Installing "kube-proxy" ...
I1022 15:54:13.959163    2643 interface.go:26] Installing "kube-dns" ...
I1022 15:54:13.959169    2643 interface.go:26] Installing "openshift-service-cert-signer-operator" ...
I1022 15:54:13.959185    2643 interface.go:26] Installing "openshift-apiserver" ...
I1022 15:54:13.959211    2643 apply_template.go:81] Installing "openshift-apiserver"
I1022 15:54:13.960047    2643 apply_template.go:81] Installing "kube-proxy"
I1022 15:54:13.961077    2643 apply_template.go:81] Installing "kube-dns"
I1022 15:54:13.961225    2643 apply_template.go:81] Installing "openshift-service-cert-signer-operator"
I1022 15:54:17.965245    2643 interface.go:41] Finished installing "kube-proxy" "kube-dns" "openshift-service-cert-signer-operator" "openshift-apiserver"
I1022 15:56:45.986497    2643 run_self_hosted.go:242] openshift-apiserver available
I1022 15:56:45.987410    2643 interface.go:26] Installing "openshift-controller-manager" ...
I1022 15:56:45.987432    2643 apply_template.go:81] Installing "openshift-controller-manager"
I1022 15:56:49.025267    2643 interface.go:41] Finished installing "openshift-controller-manager"
Adding default OAuthClient redirect URIs ...
Adding registry ...
Adding persistent-volumes ...
Adding web-console ...
Adding centos-imagestreams ...
Adding router ...
Adding sample-templates ...
I1022 15:56:49.051658    2643 interface.go:26] Installing "openshift-image-registry" ...
I1022 15:56:49.051668    2643 interface.go:26] Installing "persistent-volumes" ...
I1022 15:56:49.051675    2643 interface.go:26] Installing "openshift-web-console-operator" ...
I1022 15:56:49.051681    2643 interface.go:26] Installing "centos-imagestreams" ...
I1022 15:56:49.051685    2643 interface.go:26] Installing "openshift-router" ...
I1022 15:56:49.051689    2643 interface.go:26] Installing "sample-templates" ...
I1022 15:56:49.051733    2643 interface.go:26] Installing "sample-templates/jenkins pipeline ephemeral" ...
I1022 15:56:49.051739    2643 interface.go:26] Installing "sample-templates/mongodb" ...
I1022 15:56:49.051743    2643 interface.go:26] Installing "sample-templates/mysql" ...
I1022 15:56:49.051746    2643 interface.go:26] Installing "sample-templates/postgresql" ...
I1022 15:56:49.051751    2643 interface.go:26] Installing "sample-templates/cakephp quickstart" ...
I1022 15:56:49.051755    2643 interface.go:26] Installing "sample-templates/rails quickstart" ...
I1022 15:56:49.051759    2643 interface.go:26] Installing "sample-templates/sample pipeline" ...
I1022 15:56:49.051763    2643 interface.go:26] Installing "sample-templates/mariadb" ...
I1022 15:56:49.051767    2643 interface.go:26] Installing "sample-templates/dancer quickstart" ...
I1022 15:56:49.051771    2643 interface.go:26] Installing "sample-templates/django quickstart" ...
I1022 15:56:49.051775    2643 interface.go:26] Installing "sample-templates/nodejs quickstart" ...
I1022 15:56:49.051814    2643 apply_list.go:67] Installing "sample-templates/nodejs quickstart"
I1022 15:56:49.052594    2643 apply_template.go:81] Installing "openshift-web-console-operator"
I1022 15:56:49.052748    2643 apply_list.go:67] Installing "centos-imagestreams"
I1022 15:56:49.053126    2643 apply_list.go:67] Installing "sample-templates/jenkins pipeline ephemeral"
I1022 15:56:49.053216    2643 apply_list.go:67] Installing "sample-templates/mongodb"
I1022 15:56:49.053280    2643 apply_list.go:67] Installing "sample-templates/mysql"
I1022 15:56:49.053344    2643 apply_list.go:67] Installing "sample-templates/postgresql"
I1022 15:56:49.053467    2643 apply_list.go:67] Installing "sample-templates/cakephp quickstart"
I1022 15:56:49.053545    2643 apply_list.go:67] Installing "sample-templates/rails quickstart"
I1022 15:56:49.053610    2643 apply_list.go:67] Installing "sample-templates/sample pipeline"
I1022 15:56:49.053671    2643 apply_list.go:67] Installing "sample-templates/mariadb"
I1022 15:56:49.053731    2643 apply_list.go:67] Installing "sample-templates/dancer quickstart"
I1022 15:56:49.053791    2643 apply_list.go:67] Installing "sample-templates/django quickstart"
I1022 15:57:02.946920    2643 interface.go:41] Finished installing "sample-templates/jenkins pipeline ephemeral" "sample-templates/mongodb" "sample-templates/mysql" "sample-templates/postgresql" "sample-templates/cakephp quickstart" "sample-templates/rails quickstart" "sample-templates/sample pipeline" "sample-templates/mariadb" "sample-templates/dancer quickstart" "sample-templates/django quickstart" "sample-templates/nodejs quickstart"
I1022 15:57:32.386272    2643 interface.go:41] Finished installing "openshift-image-registry" "persistent-volumes" "openshift-web-console-operator" "centos-imagestreams" "openshift-router" "sample-templates"
Login to server ...
Creating initial project "myproject" ...
Server Information ...
OpenShift server started.

The server is accessible via web console at:
    https://192.168.99.100:8443/console

You are logged in as:
    User:     developer
    Password: <any value>

To login as administrator:
    oc login -u system:admin
```

これでminishiftが起動しました

### ログインしてみる
まず、注目していただきたいのが先ほどのログです
最後の方を見てみますと、下記のように書いてあります

```
The server is accessible via web console at:
    https://192.168.99.100:8443/console

You are logged in as:
    User:     developer
    Password: <any value>

To login as administrator:
    oc login -u system:admin
```

では、早速ブラウザより確認してみます。
[Username][Password]の入力を求められるので、まずはdeveloperでログインしてみます。
![スクリーンショット 2020-10-23 1.03.48.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/563635/8e9297e5-75f0-1d50-d177-fbb686cd7c54.png)
いい感じですね。ブラウザ画面からプロジェクトの作成もできるのですが、せっかくなので、cliから(**ocコマンドにより**)操作してみたいと思います。


### ocコマンドでのログイン
[参考基: minishift Quickstart](https://docs.okd.io/3.11/minishift/getting-started/quickstart.html)

まずは、ocコマンドにパスを通します。

```
eval $(minishift oc-env)
```

admin userでログインを行なっておきます

```
❯ oc login -u system:admin
Logged into "https://192.168.99.100:8443" as "system:admin" using existing credentials.

You have access to the following projects and can switch between them with 'oc project <projectname>':

    default
    kube-dns
    kube-proxy
    kube-public
    kube-system
  * myproject
    openshift
    openshift-apiserver
    openshift-controller-manager
    openshift-core-operators
    openshift-infra
    openshift-node
    openshift-service-cert-signer
    openshift-web-console

Using project "myproject".
```

### ocコマンドからサンプルのNode.jsアプリケーションをデプロイする

#### 1.Node.jsサンプルアプリを作成する

```
❯ oc new-app https://github.com/sclorg/nodejs-ex -l name=myapp
--> Found image 93de123 (2 years old) in image stream "openshift/nodejs" under tag "10" for "nodejs"

    Node.js 10.12.0
    ---------------
    Node.js  available as docker container is a base platform for building and running various Node.js  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, nodejs-10.12.0

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

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

#### 2.アプリがビルド、デプロイされるまで、ビルドログを追跡する

```
❯ oc logs -f bc/nodejs-ex
Cloning "https://github.com/sclorg/nodejs-ex" ...
	Commit:	7b9f57949786059a3fab03b8493279c945770fb0 (Merge pull request #249 from multi-arch/master)
	Author:	Honza Horak <hhorak@redhat.com>
	Date:	Wed Sep 23 10:52:52 2020 +0200
Using 172.30.1.1:5000/openshift/nodejs@sha256:3cc041334eef8d5853078a0190e46a2998a70ad98320db512968f1de0561705e as the s2i builder image
---> Installing application source
---> Building your Node application from source
Current git config
url.https://github.com.insteadof=git@github.com:
url.https://.insteadof=ssh://
url.https://github.com.insteadof=ssh://git@github.com
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
remote.origin.url=https://github.com/sclorg/nodejs-ex
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master
branch.master.remote=origin
branch.master.merge=refs/heads/master
---> Installing dependencies
---> Using 'npm install -s --only=production'
added 121 packages from 342 contributors and audited 121 packages in 4.254s
found 10 vulnerabilities (3 low, 2 moderate, 4 high, 1 critical)
run `npm audit fix` to fix them, or `npm audit` for details
---> Pruning the development dependencies
npm info it worked if it ends with ok
npm info using npm@6.4.1
npm info using node@v10.12.0
npm timing stage:loadCurrentTree Completed in 238ms
npm timing stage:loadIdealTree:cloneCurrentTree Completed in 2ms
npm timing stage:loadIdealTree:loadShrinkwrap Completed in 110ms
npm timing stage:loadIdealTree:loadAllDepsIntoIdealTree Completed in 55ms
npm timing stage:loadIdealTree Completed in 198ms
npm timing stage:generateActionsToTake Completed in 14ms
npm timing audit compress Completed in 11ms
npm info audit Submitting payload of 7102bytes
npm timing stage:executeActions Completed in 200ms
npm timing stage:rollbackFailedOptional Completed in 1ms
npm timing stage:runTopLevelLifecycles Completed in 694ms
npm timing audit submit Completed in 509ms
npm http fetch POST 200 https://registry.npmjs.org/-/npm/v1/security/audits/quick 374ms
npm timing audit body Completed in 3ms
audited 121 packages in 1.023s
found 10 vulnerabilities (3 low, 2 moderate, 4 high, 1 critical)
run `npm audit fix` to fix them, or `npm audit` for details
npm timing npm Completed in 1337ms
npm info ok
---> Cleaning up npm cache
---> Fix permissions on app-root
Pushing image 172.30.1.1:5000/myproject/nodejs-ex:latest ...
Pushed 0/7 layers, 10% complete
Pushed 1/7 layers, 14% complete
Push successful
```

#### 3.作成されたリソースを確認してみる

```
❯ oc status
In project My Project (myproject) on server https://192.168.99.100:8443

svc/nodejs-ex - 172.30.239.166:8080
  dc/nodejs-ex deploys istag/nodejs-ex:latest <-
    bc/nodejs-ex source builds https://github.com/sclorg/nodejs-ex on openshift/nodejs:10
    deployment #1 deployed about a minute ago - 1 pod


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

#### 4.サービスを公開する

```
❯ oc expose svc/nodejs-ex
route.route.openshift.io/nodejs-ex exposed
```
#### 5.アプリケーションにアクセスする

```
❯ minishift openshift service nodejs-ex --in-browser
Opening the route/NodePort http://nodejs-ex-myproject.192.168.99.100.nip.io in the default browser...
```
表示されました
![スクリーンショット 2020-10-23 1.19.23.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/563635/e99f2fed-4801-91fb-76e1-a634ce501c6c.png)

#### 6.port等を確認

```
❯ oc get route
NAME        HOST/PORT                                   PATH      SERVICES    PORT       TERMINATION   WILDCARD
nodejs-ex   nodejs-ex-myproject.192.168.99.100.nip.io             nodejs-ex   8080-tcp                 None
```

#### 7.minishiftを停止します

```
❯ minishift stop
Stopping the OpenShift cluster...
Cluster stopped.
```

### おわり
openShift楽しいですね！
良いkubernetes lifeを！


# 参考
[公式ドキュメント](https://docs.okd.io/3.11/minishift/index.html)