问题:k8s搭建的grafana容器只要一重启,配置的dashboard,添加的用户等就会重置,从而得出,grafana没有数据持久化。
解决:
1.先在集群建立持久花存储pvc
然后挂在到容器/var目录
volumeMounts:
- mountPath: /var
name: grafana-storage
volumes:
- name: grafana-storage
persistentVolumeClaim:
claimName: grafana
网上资料好多都是这么做的,然并卵,并没什么作用,重启依然失去配置。
2.参考好多资料,发现需要配套mysql数据考实现grafana数据持久化。
mysql安装可通过helm search mysql使用helm安装,mysql要挂持久化存储,这里就不多介绍了。
下面会详细介绍grafana怎么配置mysql数据库。
首先要先在mysql里新建grafana的数据库,创建 参考mysql sql语法
其次:
第一步:通过k8s集群查出旧有grafana容器所在的节点,找到那个容器,把grafana.ini导出到服务器上。
docker cp ad7fb5f9ef38:/etc/grafana/grafana.ini /lyf/
第二步:编辑/lyf/grafana.ini文件,修改database选项
[database]
\# You can configure the database connection by specifying type, host, name, user and password
\# as seperate properties or as on string using the url propertie.
\# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql
host = mysql.default:3306
name = grafana
user = root
\# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = root.123
修改完配置文件,保存。然后执行下面的命令创建configmap
创建configmap grafana-etc
kubectl create configmap "grafana-etc" --from-file=/lyf/grafana.ini --namespace=kube-system
第三步:创建grafana deployment
[root@k8s01 prometheus]# cat grafana-deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "15"
labels:
k8s-app: grafana
task: monitoring
name: monitoring-grafana
namespace: kube-system
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: grafana
task: monitoring
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
k8s-app: grafana
task: monitoring
spec:
containers:
- env:
- name: INFLUXDB_HOST
value: monitoring-influxdb.kube-system
- name: GF_SERVER_HTTP_PORT
value: "3000"
- name: GF_AUTH_BASIC_ENABLED
value: "false"
- name: GF_AUTH_ANONYMOUS_ENABLED
value: "false"
- name: GF_AUTH_ANONYMOUS_ORG_ROLE
value: Admin
- name: GF_SERVER_ROOT_URL
value: /
image: registry.gcloud.srcb.com/registry.cn-hangzhou.aliyuncs.com/acs/grafana:5.0.4-gpu-monitoring
imagePullPolicy: IfNotPresent
name: grafana
ports:
- containerPort: 3000
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/ssl/certs
name: ca-certificates
readOnly: true
- mountPath: /var
name: grafana-storage
- mountPath: /etc/grafana/
name: grafana-etc-volume
dnsPolicy: ClusterFirst
nodeSelector:
role: master
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /etc/ssl/certs
type: ""
name: ca-certificates
- name: grafana-storage
persistentVolumeClaim:
claimName: grafana
- configMap:
defaultMode: 420
items:
- key: grafana.ini
path: grafana.ini
name: grafana-etc
name: grafana-etc-volume
小结:加mysql配置的grafana容器重启配置不会丢失。
参考:https://blog.csdn.net/wenwst/article/details/76624019