> 参考Grafana官方部署文档
>
> https://grafana.com/docs/grafana/latest/setup-grafana/installation/kubernetes/

### 创建命名空间

```bash
kubectl create ns monitor
```

### 创建PVC

```yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: grafana-pvc
  namespace: monitor
spec:
  storageClassName: nfs-client
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

```

### 创建ConfigMap

> 参考 Grafana官方文档
>
> https://grafana.com/docs/grafana/latest/setup-grafana/installation/kubernetes/#increasing-log-levels-to-debug-mode

- 准备grafana.ini 配置文件
- 可以先不挂载grafana配置文件部署，然后去pod里边查看需要的配置内容在新建ini文件

```bash
kubectl create configmap grafana-config --from-file=grafana.ini --namespace=monitor
```



### 创建Deployment

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: grafana
  name: grafana
  namespace: monitor
spec:
  selector:
    matchLabels:
      app: grafana
  template:
    metadata:
      labels:
        app: grafana
    spec:
      securityContext:
        fsGroup: 472
        supplementalGroups:
          - 0
      containers:
        - name: grafana
          image: grafana/grafana:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 3000
              name: http-grafana
              protocol: TCP
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /robots.txt
              port: 3000
              scheme: HTTP
            initialDelaySeconds: 10
            periodSeconds: 30
            successThreshold: 1
            timeoutSeconds: 2
          livenessProbe:
            failureThreshold: 3
            initialDelaySeconds: 30
            periodSeconds: 10
            successThreshold: 1
            tcpSocket:
              port: 3000
            timeoutSeconds: 1
          resources:
            requests:
              cpu: 250m
              memory: 750Mi
          volumeMounts:
            - mountPath: /var/lib/grafana
              name: grafana-pv
            - mountPath: /etc/grafana # 没有配置ini文件时可以注释此部分
              name: grafana-config
      volumes:
        - name: grafana-pv
          persistentVolumeClaim:
            claimName: grafana-pvc
        - name: grafana-config # 没有配置ini文件时可以注释此部分
          configMap:
            name: grafana-config

```

### 创建Service

- ClusterIP 模式，也可以NodePort或LoadBalancer模式，我用ingress，所以使用的ClusterIP模式

```yaml
apiVersion: v1
kind: Service
metadata:
  name: grafana
  namespace: monitor
spec:
  ports:
    - port: 3000
      protocol: TCP
      targetPort: http-grafana
  selector:
    app: grafana
  sessionAffinity: None
  type: ClusterIP

```

### 创建Ingress基于trafik

```yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: kubepi-traefik-ingress
  namespace: monitor
  annotations:
    traefik.ingress.kubernetes.io/router.entrypoints: web
spec:
  ingressClassName: "traefik"
  rules:
    - host: home.heysq.com
      http:
        paths:
          - backend:
              service:
                name: grafana
                port:
                  number: 3000
            path: /grafana
            pathType: Prefix

```

