K8S高可用集群 之 Grafana部署与使用(基于NFS共享存储)

一、Grafana安装部署

参考文章:https://blog.51cto.com/u_12965094/2684970

备注:本次部署主要跟上面的文章有两个区别

  • 数据存储不再使用hostpath方式,使用了PV与PVC方式
  • 本次部署将grafana的数据库目录与配置文件目录挂载出来。

svc.yaml

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

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: grafana
  namespace: monitor
spec:
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      volumes:
      - name: storage
        persistentVolumeClaim:
          claimName: grafana-disk       #存放grafana数据盘
      - name: conf
        persistentVolumeClaim:
          claimName: grafana-conf
      nodeSelector:
        app: prometheus
      securityContext:         #解决容器读取文件夹权限
        runAsUser: 0
      containers:
      - name: grafana
        image: grafana/grafana:7.2.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000
          name: grafana
        env:
        - name: GF_SECURITY_ADMIN_USER
          value: admin     #帐号
        - name: GF_SECURITY_ADMIN_PASSWORD
          value: admin     #密码
        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: 150m
            memory: 512Mi
          requests:
            cpu: 150m
            memory: 512Mi
        volumeMounts:
        - mountPath: "/var/lib/grafana"
          name: storage
        - mountPath: "/usr/share/grafana/conf"
          name: conf

svc.yaml

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

ingress.yaml

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

备注:secretName:https-secret需提前创建好相关的secret证书

二、安装部署遇到的问题

1、grafana容器无法启动,提示如下:

K8S高可用集群  之 Grafana部署与使用(基于NFS共享存储)

原因:当容器启动时,grafana无法找到相关配置文件,当容器启动失败时,查看grafana-conf pvc磁盘时没有任何文件,表明容器启动时,配置文件被该PVC覆盖,清空了原有的配置文件

解决方法:单独用docker run 的方法,把该grafana的镜像启动,然后把里面的所有配置文件复制出来(/usr/share/grafana/conf/*),放到grafana-conf 的pvc目录下,再次启动容器时,启动服务成功。

上一篇:【对接抖店平台】 09测试版店铺开发商品发布流程深入刨析 Java版 工具型


下一篇:拼多多平台API接入文档