Kubernetes安装grafana数据持久化

管理员

 

一、搭建前期准备条件

1、在k8s集群创建一个 prometheus 命名空间,做好服务隔离,确保不会影响到其它服务

二、数据持久化环境搭建

1、安装NFS,查看是否已安装NFS

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# rpm -qa | grep nfs

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# rpm -qa | grep rpcbind

安装NFS

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# yum -y install nfs-utils rpcbind

创建NFS数据存储目录

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# mkdir /nfs/kubernetes/prometheus

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# chmod 666 mkdir /nfs/kubernetes/prometheus

配置访问

[root@iZwz913wwcdk2r7whthcrsZ prometheus]#  echo "/home/kvm  *(rw,no_root_squash,sync)" >>/etc/exports

刷新配置生效

[root@iZwz913wwcdk2r7whthcrsZ prometheus]#  exportfs -r

启动NFS

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# systemctl start rpcbind

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# systemctl enable rpcbind

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# systemctl enable nfs

查看RPC服务注册状态

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# rpcinfo -p localhost

创建挂载目录

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# mkdir /testnfs

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# showmount -e 挂载机器IP地址

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# mount -t nfs 挂载机器IP地址:/nfs/kubernetes/prometheus/prometheus

[root@iZwz913wwcdk2r7whthcrsZ prometheus]# df -h

三、k8s搭建grafana

1、创建grafana目录用来存放YAML配置

[root@iZwz913wwcdk2r7whthcrsZ grafana]# vim grafana_deployment.yaml

apiVersion: apps/v1

kind: Deployment

metadata:

  name: grafana

  namespace: prometheus

  labels:

    app: grafana

    k8s-app: grafana

spec:

  selector:

    matchLabels:

      k8s-app: grafana

      app: grafana

  revisionHistoryLimit: 10

  template:

    metadata:

      labels:

        app: grafana

        k8s-app: grafana

    spec:

      containers:

      - name: grafana

        image: grafana/grafana:7.5.2

        imagePullPolicy: IfNotPresent

        ports:

        - containerPort: 3000

          name: grafana

        env:

        - name: GF_SECURITY_ADMIN_USER

          value: admin

        - name: GF_SECURITY_ADMIN_PASSWORD

          value: bwgfs127127

        readinessProbe:

          failureThreshold: 10

          httpGet:

            path: /api/health

            port: 3000

            scheme: HTTP

          initialDelaySeconds: 60

          periodSeconds: 10

          successThreshold: 1

          timeoutSeconds: 30

        livenessProbe:

          failureThreshold: 3

          httpGet:

            path: /api/health

            port: 3000

            scheme: HTTP

          periodSeconds: 10

          successThreshold: 1

          timeoutSeconds: 1

        resources:

          limits:

            cpu: 8000m

            memory: 16384Mi

          requests:

            cpu: 4000m

            memory: 8192Mi

        volumeMounts:

        - mountPath: /var/lib/grafana

          subPath: grafana

          name: storage

      securityContext:

        fsGroup: 472

        runAsUser: 0

      volumes:

      - name: storage

        persistentVolumeClaim:

          claimName: grafana

2、创建grafana目录用来存放YAML配置

[root@iZwz913wwcdk2r7whthcrsZ grafana]# vim grafana_volume.yaml

apiVersion: v1

kind: PersistentVolume

metadata:

  name: grafana

spec:

  capacity:

    storage: 100Gi

  accessModes:

  - ReadWriteOnce

  persistentVolumeReclaimPolicy: Recycle

  nfs:

    server: 10.11.33.57                                                #NFS地址

    path: /nfs/kubernetes/prometheus                          #NFS地址目录

---

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: grafana

  namespace: prometheus

spec:

  accessModes:

  - ReadWriteOnce

  resources:

    requests:

      storage: 100Gi

3、创建grafana目录用来存放YAML配置

[root@iZwz913wwcdk2r7whthcrsZ grafana]# vim grafana_svc.yaml

apiVersion: v1

kind: Service

metadata:

  name: grafana

  namespace: prometheus

  labels:

    app: grafana

spec:

  type: NodePort

  ports:

    - port: 3000

  selector:

    app: grafana

4、创建grafana目录用来存放YAML配置

[root@iZwz913wwcdk2r7whthcrsZ grafana]# vim grafana_job.yaml

apiVersion: batch/v1

kind: Job

metadata:

  name: grafana-chown

  namespace: prometheus

spec:

  template:

    spec:

      restartPolicy: Never

      containers:

      - name: grafana-chown

        command: ["chown", "-R", "472:0", "/var/lib/grafana"]

        image: busybox

        imagePullPolicy: IfNotPresent

        volumeMounts:

        - name: storage

          subPath: grafana

          mountPath: /var/lib/grafana

      volumes:

      - name: storage

        persistentVolumeClaim:

          claimName: grafana

5、启动grafana服务

[root@iZwz913wwcdk2r7whthcrsZ grafana]# kubectl create -f grafana_volume.yaml

persistentvolume/grafana created

persistentvolumeclaim/grafana created

[root@iZwz913wwcdk2r7whthcrsZ grafana]# kubectl create -f grafana_job.yaml

job.batch/grafana-chown created

[root@iZwz913wwcdk2r7whthcrsZ grafana]# kubectl apply -f grafana_deployment.yaml

deployment.apps/grafana created

[root@iZwz913wwcdk2r7whthcrsZ grafana]# kubectl create -f grafana_svc.yaml

查看状态

[root@iZwz913wwcdk2r7whthcrsZ grafana]# kubectl get pod,svc -n prometheus |grep grafana