Kubernetes安装prometheus operator

317次阅读
没有评论

共计 5395 个字符,预计需要花费 14 分钟才能阅读完成。

无监控不运维,下面我们就来安装 prometheus operator 用来监控 k8s 的内部情况。
我用的是 Kubernetes v1.23.8 安装 prometheus operator 0.11.0 版本
版本如下(注意需要注意 prometheus operator 对应的版本兼容)
Kubernetes v1.23.8
prometheus operator 0.11.0
官方地址
https://github.com/prometheus-operator/kube-prometheus
下载好 prometheus operator 后解压
cd manifests/

修改持久化

首先你要创建一个存储类,这里因为有了一个监控了,就不贴出 yaml 了。
我的 storageClassName 名字叫 nfs-prometheus
增加一个 storage

[root@az-k8s-nginx-001]$vim prometheus-prometheus.yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  labels:
    app.kubernetes.io/component: prometheus
    app.kubernetes.io/instance: k8s
    app.kubernetes.io/name: prometheus
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 2.36.1
  name: k8s
  namespace: monitoring
spec:
  alerting:
    alertmanagers:
    - apiVersion: v2
      name: alertmanager-main
      namespace: monitoring
      port: web
  enableFeatures: []
  externalLabels: {}
  image: quay.io/prometheus/prometheus:v2.36.1
  nodeSelector:
    kubernetes.io/os: linux
  podMetadata:
    labels:
      app.kubernetes.io/component: prometheus
      app.kubernetes.io/instance: k8s
      app.kubernetes.io/name: prometheus
      app.kubernetes.io/part-of: kube-prometheus
      app.kubernetes.io/version: 2.36.1
  podMonitorNamespaceSelector: {}
  podMonitorSelector: {}
  probeNamespaceSelector: {}
  probeSelector: {}
  replicas: 2
  resources:
    requests:
      memory: 400Mi
  ruleNamespaceSelector: {}
  ruleSelector: {}
  securityContext:
    fsGroup: 2000
    runAsNonRoot: true
    runAsUser: 1000
  serviceAccountName: prometheus-k8s
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector: {}
  version: 2.36.1
  storage:
    volumeClaimTemplate:
      spec:
        storageClassName: nfs-prometheus
        resources:
          requests:
            storage: 10Gi

修改 grafana 访问方式为 nodeport, 你也可以使用官方的方式。

vim grafana-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: grafana
    app.kubernetes.io/name: grafana
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 8.5.5
  name: grafana
  namespace: monitoring
spec:
  type: NodePort
  ports:
  - name: http
    port: 3000
    targetPort: http
    nodePort: 31010
  selector:
    app.kubernetes.io/component: grafana
    app.kubernetes.io/name: grafana
    app.kubernetes.io/part-of: kube-prometheus

修改 prometheus 访问方式为 nodeport, 你也可以使用官方的方式。

vim prometheus-service.yaml

apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: prometheus
    app.kubernetes.io/instance: k8s
    app.kubernetes.io/name: prometheus
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 2.36.1
  name: prometheus-k8s
  namespace: monitoring
spec:
  type: NodePort
  ports:
  - name: web
    port: 9090
    targetPort: web
    nodePort: 31011
  - name: reloader-web
    port: 8080
    targetPort: reloader-web
  selector:
    app.kubernetes.io/component: prometheus
    app.kubernetes.io/instance: k8s
    app.kubernetes.io/name: prometheus
    app.kubernetes.io/part-of: kube-prometheus
  sessionAffinity: ClientIP

alertmanager 配置 nodeport 方式方便报警调试

vim alertmanager-service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/component: alert-router
    app.kubernetes.io/instance: main
    app.kubernetes.io/name: alertmanager
    app.kubernetes.io/part-of: kube-prometheus
    app.kubernetes.io/version: 0.24.0
  name: alertmanager-main
  namespace: monitoring
spec:
  type: NodePort
  ports:
  - name: web
    port: 9093
    targetPort: web
    nodePort: 31012
  - name: reloader-web
    port: 8080
    targetPort: reloader-web
  selector:
    app.kubernetes.io/component: alert-router
    app.kubernetes.io/instance: main
    app.kubernetes.io/name: alertmanager
    app.kubernetes.io/part-of: kube-prometheus
  sessionAffinity: ClientIP

替换镜像地址(由于国内无法访问国外的源需要替换)

cd ./kube-prometheus/manifests/
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' prometheus-prometheus.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' alertmanager-alertmanager.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' blackboxExporter-deployment.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' nodeExporter-daemonset.yaml
sed -i 's/quay.io/quay.mirrors.ustc.edu.cn/g' prometheusOperator-deployment.yaml

这里需要特别注意,使用的是自己私有镜像

sed -i 's/k8s.gcr.io\/prometheus-adapter/registry.cn-hangzhou.aliyuncs.com\/yx-k8s/g' prometheusAdapter-deployment.yaml
sed -i 's/k8s.gcr.io\/kube-state-metrics/registry.cn-hangzhou.aliyuncs.com\/yx-k8s/g' kubeStateMetrics-deployment.yaml
sed -i 's/quay.io\/brancz/registry.cn-hangzhou.aliyuncs.com\/yx-k8s/g' kubeStateMetrics-deployment.yaml

更改客户端监控的 9100 地址为 9200(原因是我本地使用了一个 9100 的端口避免冲突)

sed -i s/9100/9200/g *

执行安装命令

kubectl apply --server-side -f manifests/setup
kubectl apply -f manifests/

然后检查

until kubectl get servicemonitors --all-namespaces ; do date; sleep 1; echo ""; done
NAMESPACE    NAME                      AGE
monitoring   alertmanager-main         37m
monitoring   blackbox-exporter         37m
monitoring   coredns                   37m
monitoring   grafana                   37m
monitoring   kube-apiserver            37m
monitoring   kube-controller-manager   37m
monitoring   kube-scheduler            37m
monitoring   kube-state-metrics        37m
monitoring   kubelet                   37m
monitoring   node-exporter             37m
monitoring   prometheus-adapter        37m
monitoring   prometheus-k8s            37m
monitoring   prometheus-operator       37m

检查 pod 状态如图
2022-08-04T08:07:41.png

访问 prometheus 地址 ip 加端口我的是 ip:31011 效果如下
Kubernetes 安装 prometheus operator
访问 Grafana 地址 ip 加端口我的是 ip:31010 效果如下
2022-08-04T08:12:56.png

由于无法下载国外镜像我的方法是使用 docker save 和 docker load - i 命令使用文件

先到能下载的机器上 pull 下来

docker pull willdockerhub/prometheus-adapter:v0.9.1
docker pull bitnami/kube-rbac-proxy:0.12.0

然后保持为文件

docker save willdockerhub/prometheus-adapter:v0.9.1 > prometheus-adapter.tar
docker save docker.io/bitnami/kube-state-metrics:2.5.0 > kube-state-metrics.tar

加载到机器中去

docker load -i kube-state-metrics.tar
docker load -i prometheus-adapter.tar 

docker tag  bitnami/kube-state-metrics:2.5.0 registry.cn-hangzhou.aliyuncs.com/yx-k8s/kube-state-metrics:v2.5.0 
docker tag willdockerhub/prometheus-adapter:v0.9.1 registry.cn-hangzhou.aliyuncs.com/yx-k8s/prometheus-adapter:v0.9.1

镜像推送

docker push registry.cn-hangzhou.aliyuncs.com/yx-k8s/kube-state-metrics:v2.5.0
docker push registry.cn-hangzhou.aliyuncs.com/yx-k8s/prometheus-adapter:v0.9.1

然后替换成自己的地址国内就可以使用了。
到此完成。

正文完
 0
yx
版权声明:本站原创文章,由 yx 于2022-08-04发表,共计5395字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码