prometheus operatorは長い間保存します.

19934 ワード

クビー-prometheusは長期保存を使用しています.
prometheus operator配置の参考:https://blog.csdn.net/networken/article/details/85620793 耐久保存プロファイルの参照:https://github.com/coreos/prometheus-operator/blob/master/Documentation/user-guides/storage.md
prometheusはデフォルトではempty-dirを使用してデータを保存します.pod移行またはモニタデータを再起動すると失われます.以下はnfsとlocal pvを例にして、prometheusデータを恒久化します.
nfsメモリを使う
nfs serverおよびダイナミックなストレージの参考:https://blog.csdn.net/networken/article/details/86697018
以下はクビ-prometheusを展開する前に、公式manifeesの下のプロメザス-prometheus.yamlの内容を修正して、最後にstorageの配置を追加すればいいです.storge Class Nameを指定するには、前もって準備しておく必要があります.
# cat manifests/prometheus-prometheus.yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  labels:
    prometheus: k8s
  name: k8s
  namespace: monitoring
spec:
  alerting:
    alertmanagers:
    - name: alertmanager-main
      namespace: monitoring
      port: web
  image: willdockerhub/prometheus:v2.15.2
  nodeSelector:
    kubernetes.io/os: linux
  podMonitorNamespaceSelector: {}
  podMonitorSelector: {}
  replicas: 2
  resources:
    requests:
      memory: 400Mi
  ruleSelector:
    matchLabels:
      prometheus: k8s
      role: alert-rules
  securityContext:
    fsGroup: 2000
    runAsNonRoot: true
    runAsUser: 1000
  serviceAccountName: prometheus-k8s
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector: {}
  version: v2.15.2
  storage:
    volumeClaimTemplate:
      spec:
        storageClassName: nfs-client
        resources:
          requests:
            storage: 40Gi
修正後は公式の配置によってprometheusを展開すればいいです.配置が完了したら作成されたpvとpvcを見ることができます.
バインディングのpvとpvcを確認します.
[root@master01 ~]# kubectl -n monitoring get pvc
NAME                                 STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
prometheus-k8s-db-prometheus-k8s-0   Bound    pvc-a8b091d5-3929-4a29-b89f-fe47022aee55   40Gi       RWO            nfs-client     9h
prometheus-k8s-db-prometheus-k8s-1   Bound    pvc-1276a939-7d5d-43b9-ade9-ae0ef4108ecc   40Gi       RWO            nfs-client     9h

[root@master01 ~]# kubectl -n monitoring get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                           STORAGECLASS   REASON   AGE
pvc-1276a939-7d5d-43b9-ade9-ae0ef4108ecc   40Gi       RWO            Delete           Bound    monitoring/prometheus-k8s-db-prometheus-k8s-1   nfs-client              9h
pvc-a8b091d5-3929-4a29-b89f-fe47022aee55   40Gi       RWO            Delete           Bound    monitoring/prometheus-k8s-db-prometheus-k8s-0   nfs-client              9h
[root@master01 ~]#
pvcを削除した後、pvを保留します.pv回収戦略を変更する必要があります.Retainです.
kubectl patch pv <your-pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
local pvを使う
storge class yamlファイルを作成します.
cat > promethues-sc.yaml <kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
EOF
storge classを作成します
kubectl apply -f prometheus-sc.yaml
プロメザスの構成を定義して、同様にmaifees/prometheus-prometheus.yamlファイルを修正して、それから正常な流れによってkube-prometheusを配置すればいいです.
# cat manifests/prometheus-prometheus.yaml
apiVersion: monitoring.coreos.com/v1
kind: Prometheus
metadata:
  labels:
    prometheus: k8s
  name: k8s
  namespace: monitoring
spec:
  alerting:
    alertmanagers:
    - name: alertmanager-main
      namespace: monitoring
      port: web
  image: willdockerhub/prometheus:v2.15.2
  nodeSelector:
    kubernetes.io/os: linux
  podMonitorNamespaceSelector: {}
  podMonitorSelector: {}
  replicas: 2
  resources:
    requests:
      memory: 400Mi
  ruleSelector:
    matchLabels:
      prometheus: k8s
      role: alert-rules
  securityContext:
    fsGroup: 2000
    runAsNonRoot: true
    runAsUser: 1000
  serviceAccountName: prometheus-k8s
  serviceMonitorNamespaceSelector: {}
  serviceMonitorSelector: {}
  version: v2.15.2
  storage:
    volumeClaimTemplate:
      spec:
        selector:
          matchLabels:
            app: prometheus
        storageClassName: local-storage
        resources:
          requests:
            storage: 20Gi
 
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: prom-local-pv-0
  labels:
    app: prometheus  
spec:
  capacity:
    storage: 20Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /data/prometheus/data
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node01                
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: prom-local-pv-1
  labels:
    app: prometheus  
spec:
  capacity:
    storage: 20Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /data/prometheus/data
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node02
手動でpvで指定されたノードにマウントディレクトリを作成します.
mkdir -p /data/prometheus/data
配置が完了したら、作成したpvcとpvを確認します.
[root@master01 ~]# kubectl -n monitoring get pvc
NAME                                 STATUS   VOLUME            CAPACITY   ACCESS MODES   STORAGECLASS    AGE
prometheus-k8s-db-prometheus-k8s-0   Bound    prom-local-pv-1   20Gi       RWO            local-storage   42m
prometheus-k8s-db-prometheus-k8s-1   Bound    prom-local-pv-0   20Gi       RWO            local-storage   42m

[root@master01 ~]# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM                                           STORAGECLASS    REASON   AGE
prom-local-pv-0                            20Gi       RWO            Retain           Bound      monitoring/prometheus-k8s-db-prometheus-k8s-1   local-storage            42m
prom-local-pv-1                            20Gi       RWO            Retain           Bound      monitoring/prometheus-k8s-db-prometheus-k8s-0   local-storage            42m