LoginSignup
3
4

More than 3 years have passed since last update.

helm3を使用して超簡単にkubernetesにGrafanaをデプロイする方法がまるでtomato

Posted at

helmが2から3になり、tillerさんがクビになりました。
helmのとっつきにくい要因が結構tillerさんにあったと思うので、
これはとても良いupdateなのではないでしょうか。さようならtiller、いいやつだった!安らかに!! :wave:

そんなわけでhelm3にて、kubernetes環境での監視ツールとして人気のある
Grafanaをデプロイしてみたところ、簡単すぎて感動したので書いてみます。

前提条件

  • kubernetesのクラスタが構築されていること
  • helm3がインストールされていること

構築

bash
# repoにstableを追加
$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/
# 確認
$ helm repo ls
NAME        URL                                              
stable      https://kubernetes-charts.storage.googleapis.com/

# いちおupdateしておく
$ helm repo update

# change directoryしとく
$ cd <grafanaをfetchするディレクトリ>
# ローカルにfetchする
$ helm fetch stable/grafana

DLしたtarを解凍しまっす
するとvalues.yamlなるファイルが出てくるので、念のためデフォルトをコピーとして残しておきましょう

bash
$ cp values.yaml values_default.yaml

values.yamlの設定

values.yamlにもろもろの設定値が書かれているので、VSCodeとかのエディタで編集します。

主な変更点は以下の通りです(あくまで一例です)

service

values.yaml
service:
  type: NodePort
  selector:
    app: grafana
  port: 80
  targetPort: 3000
    # targetPort: 4181 To be used with a proxy extraContainer
  annotations: {}
  labels:
    app: grafana
  portName: grafana-port

今回はサブディレクトリでアクセスしたかったのでClusterIP -> NodePortに変更しています。
独立したURLでアクセスしたい場合はLoadBalancerを指定してください。
あとはselector,labels,portNameあたりの設定も追加しておきました。

ingress

values.yaml
ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/whitelist-source-range:  "xxx.xxx.xxx.xxx,xxx.xxx.xxx.xxx"

  labels: {}
  path: /
  hosts:
    - tomatotomatotomato.com
  ## Extra paths to prepend to every host configuration. This is useful when working with annotation based services.
  extraPaths: 
    - path: /grafana
      backend:
        serviceName: grafana
        servicePort: grafana-port
  tls:
    - secretName: tomato-ns/tomato-cert 
      hosts:
        - tomatotomatotomato.com

まずデフォルトではfalseになっているので、trueにします。
nginx.ingress.kubernetes.io/whitelist-source-rangeでIP制限のwhitelistを作ることが可能です。
サブディレクトリとして/grafanaを設定しつつ、メインとなるhostのSSL設定証明書の設定やらをすれば完成です。

persistence

values.yaml
persistence:
  type: pvc
  enabled: true
  storageClassName: default
  accessModes:
    - ReadWriteOnce
  size: 100Gi
  # annotations: {}
  finalizers:
    - kubernetes.io/pvc-protection
  # subPath: ""
  # existingClaim:

永続化Volumeの設定です。
これがないと、Podが再起動した際にGrafanaの設定が全て吹き飛びます。笑
つまり必須です。(デフォルトではfalseになってますが...)

ポイントとしてはstorageClassNameで、これはクラウドのプロバイダごとに異なります。
どのstorageClassNameが使用可能かは下記のコマンドで確認できます。
自分で定義を作成することも可能です。

bash
$ kubectl get storageclasses
NAME                PROVISIONER                AGE
default (default)   kubernetes.io/azure-disk   34h
managed-premium     kubernetes.io/azure-disk   34h

password

values.yaml
adminUser: admin
adminPassword: *****

adminのデフォルトパスワードです。
デフォルトだとstrongpasswordという強いんだか弱いんだかわからないパスワードになってます。
まあそれでもいいんですが、ここに書いてあることがわからないと一生ログインできないと思うので、一応書いておきました。

grafana.ini

values.yaml
grafana.ini
~
  auth.basic:
    enabled: true
  auth.anonymous:
    enabled: false
  auth.saml:
    enabled: true
  smtp:
    enabled: true
    host: "smtp.gmail.com:587"
    user: "alert@tomato.com"
    password: "Gmailの16桁のアプリケーションパスワード"
    from_address: "alert@tomato.com"
    from_name: "Grafana"
    skip_verify: false

デフォルト設定を残しつつ上記を追加します。
Gmailでの設定例です。Gmailの場合、アプリケーションパスワードなるものが必要なのですが、まあその辺はググっていただければ山ほど出てくると思うので割愛します:bow:

datasource

values.yaml
datasources: 
  datasources.yaml:
    apiVersion: 1
    datasources:
    - name: Prometheus
      type: prometheus
      url: http://pg-prometheus:9090
      access: proxy
      isDefault: true

Prometheusなどをお使いの場合はここでdatasourceの設定をすることができます。
が、helmでしか更新ができなくなってしまうので、後ほどUIから設定する方を推奨します。

インストォォオォール!!

これで材料が揃ったのでいよいよインストールです。
namespaceはお好みでどうぞ !

$ helm install -name grafana --namespace monitoring -f values.yaml stable/grafana

values.yamlを変更して更新したい場合は以下

helm upgrade -name grafana --namespace monitoring -f values.yaml  stable/grafana

⎈ Happy Helming!⎈

今回はサブディレクトリで設定しているので、以下のようなURLで簡単にアクセスできます。
例:https://tomatotomatotomato.com/grafana

tillerさんがお亡くなりになられたことで、一気に楽になった印象があります。helm3最高!
tomatoを切るより簡単でした!:tomato:

ではまた🙋‍♂️

さいごに

ZEROBILLBANKでは一緒に働く仲間を募集中です。
なんとかjsとか、ブロックチェーンとか、kubernetesとかでいろんなAPIを作るお仕事。
今のところエンジニアは5人くらい。スタートアップだけど、結構ホワイトで働きやすいです。

ZEROBILLBANK JAPAN Inc.

3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4