Prometheus Operator インストール

# 概要
 - k8sテスト環境構築<br>
<b>Prometheus Operator インストール</b>

# 構築目次
 - [全体目次] (https://qiita.com/prodigy413/items/89fcb8cfb71e80f541d8)

# 環境
 - Rancher: v2.6.3
 - kubernetes(Client): v1.22.4
 - kubernetes(Server): v1.22.4
 - kube-prometheus-stack Chart: v26.1.0
 - kube-prometheus-stack App: v0.53.1

# 前提条件
 - NGINX Ingress インストール済み<br>
[NGINX Ingress インストール] (https://qiita.com/prodigy413/items/b26bac84bbb4555ac8f0)
 - MetalLB インストール済み<br>
[MetalLB インストール] (https://qiita.com/prodigy413/items/3a24ff85819dc5eafe25)

# インストール
 - 作業場所: ClientPC
 - kube-prometheus-stack Page<br>
https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack
 - 構成component<br>
デフォルトで以下componentがインストールされる<br>
・**Prometheus**<br>
・**Alertmanager**<br>
・**Grafana**

### 設定ファイル作成
 - 各componentのingressを設定
 - Grafanaのパスワードを設定<br>
※PersistentVolumeのデフォルト設定は「emptyDir」

~~~yaml:config.yaml
alertmanager:
  ingress:
    enabled: true
    ingressClassName: nginx
    hosts:
      - alertmanager.test.local
prometheus:
  ingress:
    enabled: true
    ingressClassName: nginx
    hosts:
      - prometheus.test.local
grafana:
  adminPassword: xxxxxxx
  ingress:
    enabled: true
    ingressClassName: nginx
    hosts:
      - grafana.test.local
~~~

### prometheus operator インストール
~~~
## helmにインストール用repo追加とupdate ##
$ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
$ helm repo update

## 確認 ##
$ helm search repo prometheus-community/kube-prometheus-stack
NAME                                      	CHART VERSION	APP VERSION	DESCRIPTION                                       
prometheus-community/kube-prometheus-stack	26.1.0       	0.53.1     	kube-prometheus-stack collects Kubernetes manif...

## namespace(monitoring) 作成 ##
$ kubectl create ns monitoring

## 「prometheus-community/kube-prometheus-stack」を指定名前(prometheus)で指定namespace(monitoring)にインストール
## 上記で作成した設定ファイル(config.yaml)を指定
$ helm install prometheus -n monitoring -f config.yaml prometheus-community/kube-prometheus-stack

## 確認 ##
$ kubectl get all -n monitoring
NAME                                                         READY   STATUS    RESTARTS   AGE
pod/alertmanager-prometheus-kube-prometheus-alertmanager-0   2/2     Running   0          37s
pod/prometheus-grafana-9c84cf568-fxmnv                       3/3     Running   0          46s
pod/prometheus-kube-prometheus-operator-7c64864bb7-xnxj8     1/1     Running   0          46s
pod/prometheus-kube-state-metrics-676876fdcc-5b78j           1/1     Running   0          46s
pod/prometheus-prometheus-kube-prometheus-prometheus-0       2/2     Running   0          37s
pod/prometheus-prometheus-node-exporter-vw7tw                1/1     Running   0          46s
pod/prometheus-prometheus-node-exporter-w62kt                1/1     Running   0          46s
pod/prometheus-prometheus-node-exporter-wbkrq                1/1     Running   0          46s

NAME                                              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
service/alertmanager-operated                     ClusterIP   None            <none>        9093/TCP,9094/TCP,9094/UDP   38s
service/prometheus-grafana                        ClusterIP   10.43.109.80    <none>        80/TCP                       46s
service/prometheus-kube-prometheus-alertmanager   ClusterIP   10.43.158.119   <none>        9093/TCP                     46s
service/prometheus-kube-prometheus-operator       ClusterIP   10.43.131.158   <none>        443/TCP                      46s
service/prometheus-kube-prometheus-prometheus     ClusterIP   10.43.146.16    <none>        9090/TCP                     46s
service/prometheus-kube-state-metrics             ClusterIP   10.43.127.181   <none>        8080/TCP                     46s
service/prometheus-operated                       ClusterIP   None            <none>        9090/TCP                     37s
service/prometheus-prometheus-node-exporter       ClusterIP   10.43.240.65    <none>        9100/TCP                     46s

NAME                                                 DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
daemonset.apps/prometheus-prometheus-node-exporter   3         3         3       3            3           <none>          46s

NAME                                                  READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/prometheus-grafana                    1/1     1            1           46s
deployment.apps/prometheus-kube-prometheus-operator   1/1     1            1           46s
deployment.apps/prometheus-kube-state-metrics         1/1     1            1           46s

NAME                                                             DESIRED   CURRENT   READY   AGE
replicaset.apps/prometheus-grafana-9c84cf568                     1         1         1       46s
replicaset.apps/prometheus-kube-prometheus-operator-7c64864bb7   1         1         1       46s
replicaset.apps/prometheus-kube-state-metrics-676876fdcc         1         1         1       46s

NAME                                                                    READY   AGE
statefulset.apps/alertmanager-prometheus-kube-prometheus-alertmanager   1/1     38s
statefulset.apps/prometheus-prometheus-kube-prometheus-prometheus       1/1     37s
~~~

# アクセス確認
 - hosts 設定追加  
Ingress用IPに「prometheus.test.local, grafana.test.local, alertmanager.test.local」に追加

~~~
$ cat /etc/hosts
........
192.168.245.111 prometheus.test.local grafana.test.local alertmanager.test.local
........
~~~

※ブラウザを開いてアクセス確認

 - Prometheus
http://prometheus.test.local
![16-1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/561025/42886a95-9281-026e-b341-d78e753b09db.png)

 - Alertmanager
http://alertmanager.test.local
![17-1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/561025/cc957264-831a-630a-e7bf-48911964b800.png)

 - Grafana
http://grafana.test.local
ユーザー：Admin
パスワード：設定ファイルで設定したパスワード
![18-1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/561025/40d49275-e9b3-5dec-71f8-807111fd9ea0.png)
![19-1.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/561025/fa38f416-bc4c-fb59-74b1-aa5677caf387.png)