【Helm】helmfileでPrometheusとkube-state-metricsを試す

スポンサーリンク

はじめに

helmfileを使ってPrometheusとkube-state-metricsをローカルに構築し、いろいろ試せる環境を作っていきます。

helmfileについては下記で解説しています。

【Helm】helmfile使ってみる
はじめにHelmfileについてざっくり解説し、シンプルな例で実際に使ってみたいと思います。Helmについては下記で解説しています。HelmfileとはHelmfileとは、Helmチャートを宣言的にデプロイするツールです。Helmfile...

用意するファイルとディレクトリ構成

用意するファイルとディレクトリ構成は下記の通りになります。

.
├── helmfile.yaml
└── values.yaml

helmfile.yamlの作成

helmfile.yamlを作成します。

利用するチャートは下記のprometheus-community/prometheusを使います。

helm-charts/charts/prometheus at main · prometheus-community/helm-charts
PrometheuscommunityHelmcharts.Contributetoprometheus-community/helm-chartsdevelopmentbycreatinganaccountonGitHub.
releases:
  - name: prometheus
    namespace: default
    chart: prometheus-community/prometheus
    values:
      - ./values.yaml

values.yamlの作成

values.yamlを作成します。

不要なコンポーネントはデプロイせず、kube-state-metricsのみをデプロイするように設定します。

alertmanager:
  enabled: false
kube-state-metrics:
  enabled: true
prometheus-pushgateway:
  enabled: false
prometheus-node-exporter:
  enabled: false

helmfileの適用

Prometheusのチャートがあるリポジトリを追加します。

helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
helm repo update

helmfileでチャートのインストールをします。

helmfile apply

デプロイされたものを確認します。

❯ helm list
NAME            NAMESPACE       REVISION        UPDATED                                 STATUS          CHART                   APP VERSION
prometheus      default         2               2022-06-25 09:29:55.647289 +0900 JST    deployed        prometheus-15.10.1      2.34.0

❯ kubectl get pod,svc
NAME                                                 READY   STATUS    RESTARTS   AGE
pod/prometheus-kube-state-metrics-5c9f756cf8-kv6cm   1/1     Running   0          3h34m
pod/prometheus-server-7d57c9697f-nxcgx               2/2     Running   0          3h34m

NAME                                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
service/prometheus-kube-state-metrics   ClusterIP   10.108.204.201   <none>        8080/TCP   3h34m
service/prometheus-server               ClusterIP   10.98.9.132      <none>        80/TCP     3h34m

Prometheusからkube-state-metricsを確認

ローカルからprometheus-serverにアクセスできるようにポートフォワードします。

kubectl port-forward svc/prometheus-server 9090:80

localhost:9090からPrometheusにアクセスできるようになります。

また、kube-state-metricsを確認するために、kube-state-metricsもポートフォワードします。

kubectl port-forward svc/prometheus-kube-state-metrics 8080:8080

curlコマンドやブラウザからlocalhost:8080/metricsにアクセスすると、メトリクスの一覧が確認できます。

❯ curl localhost:8080/metrics
# HELP kube_certificatesigningrequest_annotations Kubernetes annotations converted to Prometheus labels.
# TYPE kube_certificatesigningrequest_annotations gauge
# HELP kube_certificatesigningrequest_labels Kubernetes labels converted to Prometheus labels.
# TYPE kube_certificatesigningrequest_labels gauge
# HELP kube_certificatesigningrequest_created Unix creation timestamp
# TYPE kube_certificatesigningrequest_created gauge
# HELP kube_certificatesigningrequest_condition The number of each certificatesigningrequest condition
# TYPE kube_certificatesigningrequest_condition gauge
# HELP kube_certificatesigningrequest_cert_length Length of the issued cert
# TYPE kube_certificatesigningrequest_cert_length gauge
# HELP kube_configmap_annotations Kubernetes annotations converted to Prometheus labels.
# TYPE kube_configmap_annotations gauge
kube_configmap_annotations{namespace="default",configmap="kube-root-ca.crt"} 1
kube_configmap_annotations{namespace="kube-node-lease",configmap="kube-root-ca.crt"} 1
kube_configmap_annotations{namespace="kube-system",configmap="extension-apiserver-authentication"} 1
kube_configmap_annotations{namespace="kube-system",configmap="kubeadm-config"} 1
kube_configmap_annotations{namespace="kube-system",configmap="kubelet-config"} 1
kube_configmap_annotations{namespace="default",configmap="prometheus-server"} 1
kube_configmap_annotations{namespace="kube-public",configmap="cluster-info"} 1
kube_configmap_annotations{namespace="kube-public",configmap="kube-root-ca.crt"} 1

...

Prometheusからいくつかメトリクスを確認してみます。

kube_deployment_spec_replicas: Deploymentのレプリカ数

kube_node_status_condition: ノードの状態

kube_pod_container_status_restarts_total: Podのリスタート回数

片付け

使い終わった場合は下記で削除します。

helmfile delete

参考

タイトルとURLをコピーしました