K8S高可用集群 之 Prometheus搭建(NFS共享存储)

一、网络环境描述

K8S高可用集群 之 Prometheus搭建(NFS共享存储)

二、相关yaml文件

1、prometheus-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: monitor

2、prometheus-pvc.yaml

apiVersion: v1
metadata:
  name: prometheus-disk        #pvc声明卷名称
  namespace: monitor         #pvc与pod必须在相同的命名空间
spec:
  accessModes:
    - ReadWriteOnce      #读写模式,现在为单点读写
  storageClassName: managed-nfs-storage     #为当前K8S集群的storageclass名称,即上面创建的storageclass
  resources:
    requests:
      storage: 100Gi      #卷大小

3、prometheus-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: monitor
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    scrape_configs:
    - job_name: 'prometheus'
      static_configs:
      - targets: ['localhost:9090']

4、prometheus-rbac.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: prometheus
  namespace: monitor
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: prometheus
rules:
- apiGroups:
  - ""
  resources:
  - nodes
  - services
  - endpoints
  - pods
  - nodes/proxy
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - "extensions"
  resources:
    - ingresses
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - configmaps
  - nodes/metrics
  verbs:
  - get
- nonResourceURLs:
  - /metrics
  verbs:
  - get
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: prometheus
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: prometheus
subjects:
- kind: ServiceAccount
  name: prometheus
  namespace: monitor

5、prometheus-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: prometheus
  namespace: monitor
  labels:
    app: prometheus
spec:
  selector:
    matchLabels:
      app: prometheus
  template:
    metadata:
      labels:
        app: prometheus
    spec:
      serviceAccountName: prometheus
      nodeSelector:
        app: prometheus
      initContainers:
      - name: "change-permission-of-directory"
        image: busybox
        command: ["/bin/sh"]
        args: ["-c", "chown -R 65534:65534 /prometheus"]
        securityContext:
          privileged: true
        volumeMounts:
        - mountPath: "/etc/prometheus"
          name: config-volume
        - mountPath: "/prometheus"
          name: data
      containers:
      - image: prom/prometheus:v2.19.2
        name: prometheus
        args:
        - "--config.file=/etc/prometheus/prometheus.yml"
        - "--storage.tsdb.path=/prometheus"  # 指定tsdb数据路径
        - "--web.enable-lifecycle"  # 支持热更新,直接执行localhost:9090/-/reload立即生效
        - "--web.console.libraries=/usr/share/prometheus/console_libraries"
        - "--web.console.templates=/usr/share/prometheus/consoles"
        ports:
        - containerPort: 9090
          name: http
        volumeMounts:
        - mountPath: "/etc/prometheus"
          name: config-volume
        - mountPath: "/prometheus"
          name: data
        resources:
          requests:
            cpu: 100m
            memory: 512Mi
          limits:
            cpu: 100m
            memory: 512Mi
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: prometheus-disk    #修改绑定的地方
      - configMap:
          name: prometheus-config
        name: config-volume

6、prometheus-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: prometheus
  namespace: monitor
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 9090
  selector:
    app: prometheus              #此处需与deployment的标签选择器一致
  type: ClusterIP

7、创建monitor的加密证书

kubectl -n monitor create secret tls https-secret --key cedarhd.com.key --cert cedarhd.com.crt

8、prometheus-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: prometheus-ingress
  namespace: monitor
spec:
  rules:
  - host: prometheus.cedarhd.com
    http:
      paths:
      - path: /
        backend:
          serviceName: prometheus
          servicePort: 80
  tls:
    - hosts:
      - prometheus.cedarhd.com
      secretName: https-secret

按顺序完成上面所有yaml的资源创建

K8S高可用集群 之 Prometheus搭建(NFS共享存储)

三、效果验证

此时将prometheus.cedarhd.com域名解释到k8s-proxy-1负载均衡地址上,访问地址:
K8S高可用集群 之 Prometheus搭建(NFS共享存储)

备注:关于后续的prometheus的监控使用,可参考本博客的prometheus系列

上一篇:PYTHON实现玫瑰花!


下一篇:JSON解析与创建