kube-state-metrics 部署

背景

kube-prometheus 部署的 kube-state-metrics 因为带有https加密 无法正常监控 重新部署 kube-state-metrics 解决

https://github.com/kubernetes/kube-state-metrics.git github 页面查看符合k8s集群的版本

方法1 进行部署 (推荐)

# 获取github代码
git clone https://github.com/kubernetes/kube-state-metrics.git   
# git clone https://gitee.com/peichengchen/kube-state-metrics.git   网络不通就拉码云

# 切换对应k8s可以使用的分支
cd kube-state-metrics/examples/standard
# git branch -a 
git checkout -b release-2.0  remotes/origin/release-2.0

# 替换命名空间和镜像地址
grep "kube-system" -rl . |xargs sed -i "s#kube-system#monitoring#g"
grep "k8s.gcr.io" -rl . |xargs sed -i "s#k8s.gcr.io/kube-state-metrics/kube-state-metrics:v2.0.0#bitnami/kube-state-metrics:2.0.0#g"

# 进行重新删除部署
kubectl delete -f .
kubectl apply -f .

ServiceMonitor

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
#  annotations:
#    kubectl.kubernetes.io/last-applied-configuration: |
#      {"apiVersion":"monitoring.coreos.com/v1","kind":"ServiceMonitor","metadata":{"annotations":{},"labels":{"app.kubernetes.io/component":"exporter","app.kubernetes.io/name":"kube-state-metrics","app.kubernetes.io/part-of":"kube-prometheus","app.kubernetes.io/version":"2.3.0"},"name":"kube-state-metrics","namespace":"monitoring"},"spec":{"endpoints":[{"bearerTokenFile":"/var/run/secrets/kubernetes.io/serviceaccount/token","honorLabels":true,"interval":"30s","port":"https-main","relabelings":[{"action":"labeldrop","regex":"(pod|service|endpoint|namespace)"}],"scheme":"https","scrapeTimeout":"30s","tlsConfig":{"insecureSkipVerify":true}},{"bearerTokenFile":"/var/run/secrets/kubernetes.io/serviceaccount/token","interval":"30s","port":"https-self","scheme":"https","tlsConfig":{"insecureSkipVerify":true}}],"jobLabel":"app.kubernetes.io/name","selector":{"matchLabels":{"app.kubernetes.io/component":"exporter","app.kubernetes.io/name":"kube-state-metrics","app.kubernetes.io/part-of":"kube-prometheus"}}}}
  generation: 5
  labels:
    app.kubernetes.io/component: exporter
    app.kubernetes.io/name: kube-state-metrics
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 2.3.0
  name: kube-state-metrics
  namespace: monitoring
spec:
  endpoints:
#  - bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
#    honorLabels: true
#    interval: 30s
#    port: http-metrics
#    relabelings:
#    - action: labeldrop
#      regex: (pod|service|endpoint|namespace)
#    scheme: http
#    scrapeTimeout: 30s
#    tlsConfig:
#      insecureSkipVerify: true
  - bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
    interval: 30s
    port: http-metrics
    scheme: http
    tlsConfig:
      insecureSkipVerify: true
  jobLabel: app.kubernetes.io/name
  selector:
    matchLabels:
      app.kubernetes.io/component: exporter
      app.kubernetes.io/name: kube-state-metrics
      app.kubernetes.io/version: 2.3.0
kubectl get servicemonitor -n monitoring
kubectl edit servicemonitor -n monitoring kube-state-metrics 

方法2 进行部署


tee kube-state-metrics.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: kube-state-metrics
    app.kubernetes.io/version: 2.0.0
  name: kube-state-metrics
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app.kubernetes.io/name: kube-state-metrics
  template:
    metadata:
      labels:
        app.kubernetes.io/name: kube-state-metrics
        app.kubernetes.io/version: 2.0.0
    spec:
      containers:
      - image: bitnami/kube-state-metrics:2.0.0
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
          initialDelaySeconds: 5
          timeoutSeconds: 5
        name: kube-state-metrics
        ports:
        - containerPort: 8080
          name: http-metrics
        - containerPort: 8081
          name: telemetry
        readinessProbe:
          httpGet:
            path: /
            port: 8081
          initialDelaySeconds: 5
          timeoutSeconds: 5
        securityContext:
          runAsUser: 65534
      nodeSelector:
        kubernetes.io/os: linux
      serviceAccountName: prometheus
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/name: kube-state-metrics
    app.kubernetes.io/version: 2.0.0
  name: kube-state-metrics
  namespace: kube-system
  annotations:
    # 注意: 此处需要进行添加到annotations(注释)便于prometheus进行自动发现。
    prometheus.io/scrape: 'true'
spec:
  clusterIP: None
  ports:
  - name: http-metrics
    port: 8080
    targetPort: http-metrics
  - name: telemetry
    port: 8081
    targetPort: telemetry
  selector:
    app.kubernetes.io/name: kube-state-metrics
EOF

# - 创建 monitor 名称空间
# kubectl create ns monitor

sed -i "s#kube-system#monitoring#g" kube-state-metrics.yaml


  kubectl create sa prometheus --namespace monitoring
  kubectl create clusterrolebinding prometheus --clusterrole cluster-admin --serviceaccount=monitor:prometheus 
  
# - 部署 kube-state-metrics
kubectl delete -f kube-state-metrics.yaml
kubectl apply -f kube-state-metrics.yaml
  # deployment.apps/kube-state-metrics created
  # service/kube-state-metrics created

参考 https://www.bilibili.com/read/cv13419284/

posted @ 2022-09-20 21:18  鸣昊  阅读(1077)  评论(0编辑  收藏  举报