· raspberrypi · 8 min read
ラズパイのk3sにPrometheusとGrafanaを入れる
k3sパフォーマンス監視のためにPrometheusとGrafanaを入れる

概要
各ノードの負荷を監視するためにデータ収集の仕組みとしてPrometheusを、可視化ツールとしてGrafanaを使用してみます。 kube-prometheus-stack という、ひとまとめになった丁度良さそうなパッケージがあるのでこれを使います。
Helmインストール
Helmを入れていないのでインストールします。Helmはk8s (kubernetes)のためのパッケージマネージャー。
Helm のインストールは以下の公式サイトを参考にします。 https://helm.sh/ja/docs/intro/install/
インストールは以下のコマンドで。
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh
Longhornを入れる
Prometheus、Grafanaをk3sで動作させるのですが、データは永続化ストレージに配置しないといけません。というのも、何もしないとコンテナのライフサイクルに依存してしまうためコンテナが終了するとデータが喪失されてしまいます。
k3sはデフォルトでLocal Path Provisionerという機構が付属しています。設定不要で使用できるローカルストレージ機構ですが、podに対応するノードのストレージを使用するものですので、複数ノードある今回、メリットを活かせません。※軽量であることを重視するならこれでいいのですが https://docs.k3s.io/ja/storage
そこで、Longhornを使用します。 kubernetes用の軽量な分散ストレージシステムです。 https://github.com/longhorn/longhorn https://www.publickey1.jp/blog/17/longhornrancher_labs.html
インストール
前提としてOpen-iSCSIが必要なのでインストールします。 複数ノードがある場合、各ノードにインストールが必要です。
sudo apt install open-iscsi
以下のコマンドでHelmリポジトリの追加・更新を実施します。
helm repo add longhorn https://charts.longhorn.io
helm repo update
うまくいくと、こんな感じ。
pi@rp51:~/work/k3s $ helm repo add longhorn https://charts.longhorn.io
"longhorn" has been added to your repositories
pi@rp51:~/work/k3s $ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "longhorn" chart repository
...Successfully got an update from the "prometheus-community" chart repository
Update Complete. ⎈Happy Helming!⎈
pi@rp51:~/work/k3s $
インストールは以下のコマンドを実行します。 注意点としては、インストールの前に、環境変数KUBECONFIGを設定してやる必要があります(これがないとエラーになる)。
pi@rp51:~/work/k3s $ export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
pi@rp51:~/work/k3s $ helm install longhorn longhorn/longhorn --namespace longhorn-system --create-namespace
問題なくインストールできると、以下のようになると思います。
pi@rp51:~/work/k3s $ export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
pi@rp51:~/work/k3s $ helm install longhorn longhorn/longhorn --namespace longhorn-system --create-namespace
NAME: longhorn
LAST DEPLOYED: Mon May 5 17:08:21 2025
NAMESPACE: longhorn-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Longhorn is now installed on the cluster!
Please wait a few minutes for other Longhorn components such as CSI deployments, Engine Images, and Instance Managers to be initialized.
Visit our documentation at https://longhorn.io/docs/
pi@rp51:~/work/k3s $
podの状態を見てみましょう。
名前空間longhorn-systemに配置しています。おそらく問題なくできているでしょう。
pi@rp51:~ $ kubectl -n longhorn-system get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
longhorn-driver-deployer-7f95558b85-qmdsr 0/1 Init:0/1 0 8s 10.42.1.31 rp52 <none> <none>
longhorn-manager-kffvh 2/2 Running 0 8s 10.42.2.37 rp53 <none> <none>
longhorn-manager-knmcj 1/2 Running 0 8s 10.42.0.45 rp51 <none> <none>
longhorn-manager-vtbtc 1/2 Running 0 8s 10.42.1.30 rp52 <none> <none>
longhorn-ui-7ff79dfb4-lqsz9 1/1 Running 0 8s 10.42.1.32 rp52 <none> <none>
longhorn-ui-7ff79dfb4-lwddd 1/1 Running 0 8s 10.42.2.38 rp53 <none> <none>
service一覧は以下の通り。longhorn-frontendはWeb UI画面のサービスです。
pi@rp51:~ $ kubectl get svc -n longhorn-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
longhorn-admission-webhook ClusterIP 10.43.88.255 <none> 9502/TCP 42s
longhorn-backend ClusterIP 10.43.16.114 <none> 9500/TCP 42s
longhorn-conversion-webhook ClusterIP 10.43.105.241 <none> 9501/TCP 42s
longhorn-frontend ClusterIP 10.43.4.112 <none> 80/TCP 42s
longhorn-recovery-backend ClusterIP 10.43.173.218 <none> 9503/TCP 42s
永続ボリューム作成のためにPVC(PersistentVolumeClaim)を作成します。 ファイル名は、pvc-prometheus.yamlとし、内容は以下のようにします。
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: prometheus-pvc
namespace: monitoring
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: longhorn
名前空間を作成してから、monitoring名前空間に上記のPVCを適用します。
kubectl create namespace monitoring
kubectl apply -f pvc-prometheus.yaml
できました。storageclassとしてlonghornとしました。
pi@rp51:~/work/k3s $ kubectl get pvc -n monitoring
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
prometheus-pvc Bound pvc-2aca62f7-69e1-4e04-804a-0261eec11e4d 5Gi RWO longhorn <unset>
12s
kube-prometheus-stack導入
PrometheusとGrafanaが一緒になったHelmチャートです。 kube-prometheus-stack https://github.com/prometheus-community/helm-charts/tree/main/charts/kube-prometheus-stack
Helmのリポジトリを追加します。
helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update
こんな感じにメッセージが出れば、問題なくできていると思います。
pi@rp51:~/work/k3s $ helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
"prometheus-community" has been added to your repositories
pi@rp51:~/work/k3s $ helm repo update
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "prometheus-community" chart repository
Update Complete. ⎈Happy Helming!⎈
インストールする内容をカスタマイズしないといけないので、いったん現在の設定を書き出します。LonghornのStorageClassNameを指定する必要があります。
helm inspect values prometheus-community/kube-prometheus-stack > prometheus_values.yaml
出力ファイルの抜粋ですが、storageClassNameとstorageのみ修正します。
alertmanagerSpec:
(略)
storage:
volumeClaimTemplate:
spec:
storageClassName: longhorn
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
selector: {}
(略)
prometheusSpec:
(略)
storageSpec:
volumeClaimTemplate:
spec:
storageClassName: longhorn
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
selector: {}
(略)
thanosRulerSpec:
storage:
volumeClaimTemplate:
spec:
storageClassName: longhorn
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 1Gi
selector: {}
インストールします。 namespaceはmonitoringとします。
helm install prometheus -f prometheus_values.yaml prometheus-community/kube-prometheus-stack --namespace monitoring
こんな感じで出力されれば成功。
pi@rp51:~/work/k3s $ helm install prometheus -f prometheus_values.yaml prometheus-community/kube-prometheus-stack --namespace monitoring
W0505 23:43:15.934518 57621 warnings.go:70] unknown field "spec.storage.volumeClaimTemplate.selector"
W0505 23:43:15.964734 57621 warnings.go:70] unknown field "spec.storage.volumeClaimTemplate.selector"
NAME: prometheus
LAST DEPLOYED: Mon May 5 23:42:59 2025
NAMESPACE: monitoring
STATUS: deployed
REVISION: 1
NOTES:
kube-prometheus-stack has been installed. Check its status by running:
kubectl --namespace monitoring get pods -l "release=prometheus"
Get Grafana 'admin' user password by running:
kubectl --namespace monitoring get secrets prometheus-grafana -o jsonpath="{.data.admin-password}" | base64 -d ; echo
Access Grafana local instance:
export POD_NAME=$(kubectl --namespace monitoring get pod -l "app.kubernetes.io/name=grafana,app.kubernetes.io/instance=prometheus" -oname)
kubectl --namespace monitoring port-forward $POD_NAME 3000
Visit https://github.com/prometheus-operator/kube-prometheus for instructions on how to create & configure Alertmanager and Prometheus instances using the Operator.
pi@rp51:~/work/k3s $
ローカルでk3sを動かしているなら以下のコマンドを実行してブラウザからWeb UIにアクセスできるのですが、今回は外部からラズパイにアクセスします。
kubectl port-forward svc/grafana 3000:3000 -n monitoring
外部公開する場合、外部公開のためのポートを指定する必要があります。この場合、serviceを作りなおさないといけないようで、まず、以下のyamlを作成します。ファイル名はprometheus-grafana-svc.yamlとしておきます。
apiVersion: v1
kind: Service
metadata:
name: prometheus-grafana
namespace: monitoring
spec:
type: NodePort
selector:
app.kubernetes.io/instance: prometheus
app.kubernetes.io/name: grafana
ports:
- port: 3000
targetPort: 3000
nodePort: 32000
いったんprometheus-grafanaを削除します。
kubectl delete svc prometheus-grafana -n monitoring
ちなみにサービス名は以下のコマンドで確認できます。
kubectl get svc -n monitoring
適用します。
kubectl apply -f prometheus-grafana-svc.yaml
これでアクセスできるようになります。
Grafanaのパスワードは”prom-operator”です。http://<rasp piのIPアドレス>:32000
しかし、、、ラズパイのアクティブクーラーが思ったよりなりっぱなし。