Rancher v2.2.0-Alpha3 Multi-Tenant Prometheus Supportについて

Rancher2.2のAlpha版において、Multi-Tenant Pormetheus機能がサポートされたとのことで試してみました。
驚くほど簡単にPrometheus + Grafana環境を構築できます。

以下の構成で検証してみました。

![図2.png](https://qiita-image-store.s3.amazonaws.com/0/61606/5d771d95-7881-daba-30fc-27feee4b53c1.png)

Rancher Server Version:2.2.0-Alpha3

# 1.Rancher Serverの構築

## 1.Instance準備

|項目               |入力概要        |
|:----------------------------------|:---------------------------------------------|
|名前|rancher-server|
|リージョン|asia-northeast1(東京)|
|ゾーン|asia-northeast1-b|
|マシンタイプ|vCPUx1|
|ブートディスク|Ubuntu 16.04 LTS<br>ディスクサイズ80GB|
|ファイアウォール|HTTP トラフィックを許可する、HTTPS トラフィックを許可するの両方をチェックします。|

## 2.Dockerのインストール

```lang:コマンド
$ curl https://releases.rancher.com/install-docker/18.09.sh | sh
・
・（省略）
・
+ sudo -E sh -c docker version
Client:
 Version:           18.09.3
 API version:       1.39
 Go version:        go1.10.8
 Git commit:        774a1f4
 Built:             Thu Feb 28 06:40:58 2019
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          18.09.3
  API version:      1.39 (minimum version 1.12)
  Go version:       go1.10.8
  Git commit:       774a1f4
  Built:            Thu Feb 28 05:59:55 2019
  OS/Arch:          linux/amd64
  Experimental:     false

If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker iyutaka2018

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.

If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:

  sudo usermod -aG docker iyutaka2018

Remember that you will have to log out and back in for this to take effect!

WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.
```

## 3.Rancher2.2.0-Alpha3 Serverのインストール

```lang:コマンド
$ sudo docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher:v2.2.0-alpha3
Unable to find image 'rancher/rancher:v2.2.0-alpha3' locally
v2.2.0-alpha3: Pulling from rancher/rancher
32802c0cfa4d: Pull complete 
da1315cffa03: Pull complete 
fa83472a3562: Pull complete 
f85999a86bef: Pull complete 
a4daac5bee2d: Pull complete 
bf26f1964577: Pull complete 
a00bebfc6f0e: Pull complete 
3afff0aaa1d8: Pull complete 
8f3d31deda5a: Pull complete 
aa24579912ce: Pull complete 
6d77f6748e72: Pull complete 
d47b70b03591: Pull complete 
Digest: sha256:db7fe1ba357cfe5238634f2fb21085177b3ad897cec1aacacca1b3e2b22152a2
Status: Downloaded newer image for rancher/rancher:v2.2.0-alpha3
3a8894aa888fdb14d08c676b51d3a4011f5ee794f61eb90a6fe013e690e73057
```

## 4.ブラウザを起動して、RancherUIにアクセス

https://EXTERNAL-IP/

## 5.初回ログイン

### a.adminユーザのパスワード作成

任意のパスワードを入力
![screencapture-104-198-89-202-update-password-2018-12-08-16_03_50.png](https://qiita-image-store.s3.amazonaws.com/0/61606/db338f8c-ec92-03af-0455-152f531c2629.png)

### b.Rancher Server URLの登録

「Save URL」ボタンを押下
![screencapture-104-198-89-202-update-setting-2018-12-08-16_06_00.png](https://qiita-image-store.s3.amazonaws.com/0/61606/b4f50a64-b2b4-7aa1-c90b-3a8f92937ff4.png)

### c.ログイン完了

![screencapture-35-243-83-224-g-clusters-2018-12-18-02_19_01.png](https://qiita-image-store.s3.amazonaws.com/0/61606/c76c0012-ebc1-23d3-fbda-f6de6dcda0b3.png)

# 2.Single Kubernetes Clusterの構築

## 1.Instance準備

|項目               |入力概要        |
|:----------------------------------|:---------------------------------------------|
|名前|rancher-k8s-cluster|
|リージョン|asia-northeast1(東京)|
|ゾーン|asia-northeast1-b|
|マシンタイプ|vCPUx1|
|ブートディスク|Ubuntu 16.04 LTS<br>ディスクサイズ80GB|
|ファイアウォール|HTTP トラフィックを許可する、HTTPS トラフィックを許可するの両方をチェックします。|

## 2.rancher-k8s-clusterにDockerをインストール

```lang:コマンド
$ curl https://releases.rancher.com/install-docker/17.03.2.sh | sh
・
・（省略）
・
+ sudo -E sh -c docker version
Client:
 Version:      17.03.2-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   f5ec1e2
 Built:        Tue Jun 27 03:35:14 2017
 OS/Arch:      linux/amd64
Server:
 Version:      17.03.2-ce
 API version:  1.27 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   f5ec1e2
 Built:        Tue Jun 27 03:35:14 2017
 OS/Arch:      linux/amd64
 Experimental: false
If you would like to use Docker as a non-root user, you should now consider
adding your user to the "docker" group with something like:
  sudo usermod -aG docker iyutaka2018
Remember that you will have to log out and back in for this to take effect!
WARNING: Adding a user to the "docker" group will grant the ability to run
         containers which can be used to obtain root privileges on the
         docker host.
         Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
         for more information.
```

# 3.Kubernetesクラスタの構築

## 1.「Add Cluster」ボタンを押下

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/35447941-e4fd-1504-d657-8365ea8a8a07.png)

## 2.「Custom」を選択、「Cluster Name」に任意の名前を入力、「Next」ボタンを押下

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/4b176aab-af77-0c46-6c0d-7fda8efdd0b0.png)

## 3.「Node Role」の「etcd」と「Control Plane」にチェック、「Copy to Clipboard」ボタンを押下

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/5fb63e1d-b5ff-0b91-5f06-8f817e6e1c52.png)

## 4.コピーしたコマンドをrancher-k8s-clusterのコンソールで実行

```lang:コマンド
$ sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.2.0-alpha3 --server https://35.243.83.224 --token xx9ttl6ckrkzppk6msvx7jzslcc75m8m7pwbws7fjvbdls2g9l6nrn --ca-checksum 487497cfc43e87a717ca8c775c2b0e19677bf0a73b5091ee1572b8546defec75 --etcd --controlplane --worker
```

## 5.「Done」ボタンを押下

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/624c5019-57e4-bc69-030c-057e4b6b1a3d.png)


## 6.「rancher-k8s-cluster」を選択

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/5e871c3f-23b5-6031-d635-5708ac2b60e5.png)


## 7.上部メニュー「Tools」-「Monitoring」を選択

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/827af918-b621-5c2f-ef16-9b49c588fdba.png)

## 8.Prometheusアイコンを選択、「Save」ボタンを押下

この作業だけで自動的にPrometheus + Grafana環境が構築されます。

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/55100df1-0183-78d3-bf2c-cdb07ad89cea.png)

## 9.画面が切り替わりPrometheus + Grafanaが有効になったことを確認

![screencapture-35-243-83-224-c-c-77jq6-monitoring-2018-12-18-02_55_55.png](https://qiita-image-store.s3.amazonaws.com/0/61606/1a155304-04ee-b16e-ada9-5f0bcfb5bacc.png)

## 10.アコーディオンメニューを展開

それぞれのメトリクスからグラフを確認できます。デフォルトでは、右上プルダウンメニューが「1hour」となっているので「5minute」に変更するとグラフが出てきます。

![screencapture-35-243-83-224-c-c-77jq6-monitoring-2018-12-18-03_00_15.png](https://qiita-image-store.s3.amazonaws.com/0/61606/7a42ea60-fa59-7a59-0ca4-1d4240606897.png)

カーソルをグラフに合わせると詳細が表示されます

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/9b4b5aff-18c0-ff5b-026d-b3a784f8c0f9.png)


## 11.GrafanaアイコンをクリックするとGrafanaダッシュボードで確認可能

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/952d8c74-d1be-a586-d073-712c8cb0ebc6.png)

Nodes
![screencapture-35-243-83-224-k8s-clusters-c-77jq6-api-v1-namespaces-cattle-prometheus-services-http-access-grafana-80-proxy-d-FnxG5TYiz-nodes-2018-12-18-03_08_16.png](https://qiita-image-store.s3.amazonaws.com/0/61606/0a621a45-a27c-f91d-932a-b94acaf24da1.png)

Cluster
![screencapture-35-243-83-224-k8s-clusters-c-77jq6-api-v1-namespaces-cattle-prometheus-services-http-access-grafana-80-proxy-d-icjpCppik-cluster-2018-12-18-03_08_37.png](https://qiita-image-store.s3.amazonaws.com/0/61606/462c6e14-afec-a375-17a9-122d85fb0dbe.png)


## 12.「Launch kubectl」ボタンを押下

![image.png](https://qiita-image-store.s3.amazonaws.com/0/61606/2e6f6556-7f55-df8d-6818-4d64539ee4e4.png)

## 13.「kubectl get pods --all-namespaces」を実行して、状況を確認

```lang:コマンド
# Run kubectl commands inside here
# e.g. kubectl get all
> kubectl get pods --all-namespaces
NAMESPACE           NAME                                                      READY     STATUS      RESTARTS   AGE
cattle-prometheus   exporter-kube-state-cluster-monitoring-779b96cf75-gg272   1/1       Running     0          20m
cattle-prometheus   exporter-node-cluster-monitoring-ljhch                    1/1       Running     0          20m
cattle-prometheus   grafana-cluster-monitoring-5b4bc74756-hx7mx               3/3       Running     0          20m
cattle-prometheus   prometheus-cluster-monitoring-0                           4/4       Running     1          20m
cattle-prometheus   prometheus-cluster-monitoring-nginx-5ff785dd4d-d6l8l      1/1       Running     3          20m
cattle-prometheus   prometheus-operator-system-monitor-77df6db6df-gdmmz       1/1       Running     0          20m
cattle-system       cattle-cluster-agent-77498f79c-5w78s                      1/1       Running     0          30m
cattle-system       cattle-node-agent-s8gnz                                   1/1       Running     0          30m
ingress-nginx       default-http-backend-5bdd9fdd69-2ntmw                     1/1       Running     0          30m
ingress-nginx       nginx-ingress-controller-xdwnk                            1/1       Running     0          30m
kube-system         canal-wzw4r                                               3/3       Running     0          30m
kube-system         kube-dns-autoscaler-689f6f9756-zqn6f                      1/1       Running     0          30m
kube-system         kube-dns-ddddcfcc8-xbpwn                                  3/3       Running     0          30m
kube-system         metrics-server-5444cf6dfc-g7xvp                           1/1       Running     0          30m
kube-system         rke-ingress-controller-deploy-job-bshtx                   0/1       Completed   0          30m
kube-system         rke-kubedns-addon-deploy-job-k9vxg                        0/1       Completed   0          30m
kube-system         rke-metrics-addon-deploy-job-hmr7k                        0/1       Completed   0          30m
kube-system         rke-network-plugin-deploy-job-tjqhn                       0/1       Completed   0          31m
```

![screencapture-35-243-83-224-c-c-77jq6-monitoring-2018-12-18-03_15_51.png](https://qiita-image-store.s3.amazonaws.com/0/61606/aa0e1ad0-47b2-d3fd-d4b1-78b0c9eb2aac.png)


# 4.Documents

- [How to Get Started with Rancher’s New Multi-Tenant Prometheus Support and Alpha Release Process](https://rancher.com/blog/2018/2018-12-12-how-to-multi-tenant-prometheus-and-alpha-process/)