๐ŸงŠkube cadvisor with external prometheus

Pre Kubernetes Cluster at Master Node:

  • install cadvisor

kubectl apply -k https://github.com/google/cadvisor/deploy/kubernetes/base
  • assing env pod name

pod=`kubectl -n cadvisor get pod -l app=cadvisor -o jsonpath="{.items[0].metadata.name}"`
  • port-forward cadvisor

kubectl -n cadvisor port-forward pod/"${pod}" 8080:8080 --address='0.0.0.0' &

-----------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------

  • ฤฐlk adฤฑm service account oluลŸturun.

apiVersion: v1
kind: ServiceAccount
metadata:
  name: testsa
  namespace: default

kubectl apply -f service-account-testsa.yml

  • Service account'u bir clusterrolebinding oluลŸturup, clusterrole'e atayarak, yetki tanฤฑmlamฤฑลŸ olalฤฑm.

kubectl create clusterrolebinding cluster_role_binding_name --clusterrole=cluster-admin --serviceaccount=namespace:service_account_name

Example

kubectl create clusterrolebinding cluster_role_binding_testsa --clusterrole=cluster-admin --serviceaccount=default:testsa
  • Service account'ฤฑna ait, token ve ca.crt dosyalarฤฑnฤฑ almak iรงin, service account'ฤฑ bir pod'a tanฤฑmlayalฤฑm.

apiVersion: v1
kind: Pod
metadata:
  name: testpod
  namespace: default
spec:
  serviceAccountName: testsa
  containers:
  - name: testcontainer
    image: nginx
    ports:
    - containerPort: 80
  • OluลŸturduฤŸumuz Pod'a shell'den baฤŸlanฤฑp, iglili dizine gidelim.

kubectl exec -it testpod -- bash

Dizine geรงelim.

cd /var/run/secrets/kubernetes.io/serviceaccount/

Token ve ca.crt dosyalarฤฑnฤฑn iรงeriklerini alalฤฑm.

-----------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------

Prometheus Sunucuya Geรงelim;

  • ca.crt dosyasฤฑnฤฑ /tmp altฤฑna kopyalayalฤฑm.

  • Prometheus yapฤฑlandฤฑrma dosyasฤฑnฤฑ gรผncelleyelim.

  - job_name: 'kubernetes-cadvisor'
    scheme: https
    tls_config:
      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tg'
    relabel_configs:
      - separator: ;
        regex: __meta_kubernetes_node_label_(.+)
        replacement: $1
        action: labelmap
      - separator: ;
        regex: (.*)
        target_label: __address__
        replacement: '10.90.0.170:6443' # <URL to you k8s API>
        action: replace
      - source_labels: [__meta_kubernetes_node_name]
        separator: ;
        regex: (.+)
        target_label: __metrics_path__
        replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor
        action: replace

token, ca.crt, api server tanฤฑmlarฤฑ yapฤฑldฤฑ.

  • Prometheus servisi restart edelim.

systemctl restart prometheus

Last updated