多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ## 创建grafana ```yaml cat<<'EOF' | kubectl apply -f - apiVersion: apps/v1 kind: Deployment metadata:   name: grafana   namespace: kube-system spec:   selector:     matchLabels:       app: grafana   template:     metadata:       labels:         app: grafana     spec: securityContext: fsGroup: 472 supplementalGroups: - 0       nodeSelector:         kubernetes.io/node: monitor # 初始化容器解决外挂数据目录权限不足的问题 initContainers: - name: grafana-prefix image: jiaxzeng/client:v1.1 imagePullPolicy: IfNotPresent command: - "chown" - "-R" - "472." - "/var/lib/grafana" volumeMounts: - mountPath: /var/lib/grafana name: storage       containers:       - name: grafana         image: grafana/grafana:9.5.18         imagePullPolicy: IfNotPresent         ports:         - containerPort: 3000           name: grafana         env: # 所有的grafana配置都可以修改 # 请参考 https://grafana.com/docs/grafana/latest/administration/configuration/#override-configuration-with-environment-variables # 下面两项配置账号密码 - name: GF_SECURITY_ADMIN_USER value: admin - name: GF_SECURITY_ADMIN_PASSWORD value: admin321 # 下面两项配置上下文的 - name: GF_SERVER_ROOT_URL value: "%(protocol)s://%(domain)s:%(http_port)s/grafana" - name: GF_SERVER_SERVE_FROM_SUB_PATH value: "true"         readinessProbe:           httpGet:             path: /api/health             port: 3000             scheme: HTTP           initialDelaySeconds: 10           periodSeconds: 5           successThreshold: 1 failureThreshold: 10         livenessProbe:           httpGet:             path: /api/health             port: 3000             scheme: HTTP           initialDelaySeconds: 10           periodSeconds: 5           successThreshold: 1 failureThreshold: 10 resources: requests: cpu: 250m memory: 750Mi         volumeMounts:         - mountPath: /var/lib/grafana           name: storage       volumes:       - name: storage         hostPath:           path: /data/grafana/ EOF ``` ## 创建service ```yaml cat<<'EOF' | kubectl apply -f - apiVersion: v1 kind: Service metadata:   name: grafana   namespace: kube-system spec:   type: ClusterIP   ports:   - name: grafana     port: 3000     targetPort: grafana     protocol: TCP   selector:     app: grafana EOF ``` ## 创建ingress ```yaml # kubernetes < 1.19 cat<<'EOF' | kubectl apply -f - apiVersion: extensions/v1beta1 kind: Ingress metadata: name: grafana-ingress namespace: kube-system spec: ingressClassName: nginx rules: - host: www.ecloud.com http: paths: - path: /grafana backend: serviceName: grafana servicePort: 3000 EOF # kubernetes >= 1.19 cat<<'EOF' | kubectl apply -f - apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: grafana-ingress namespace: kube-system spec: ingressClassName: nginx rules: - host: www.ecloud.com http: paths: - pathType: Prefix path: /grafana backend: service: name: grafana port: name: grafana EOF ``` ## 部署granafa ```shell kubectl apply -f glafana-deploy.yaml kubectl apply -f glafana-ingress.yaml  ``` ## 验证 在本地电脑做域名映射(hosts文件)自行配置。配置之后,可以通过浏览器输入 `http://www.ecloud.com/grafana` 进行访问。 ![grafana界面](https://img.kancloud.cn/bf/e6/bfe63c53ce8b5e828398dd846eb7a651_1920x981.png) ## 配置数据源 Ⅰ 找到数据源 ![](https://img.kancloud.cn/8e/29/8e295c3e287015bba6c859c1e8216b10_1920x868.png) Ⅱ 添加数据源 ![](https://img.kancloud.cn/2f/83/2f83711439cc25b7ef4cccdfebe412bd_1622x501.png) Ⅲ 选择 Prometheus 数据源 ![](https://img.kancloud.cn/f1/b0/f1b0ff50a377f006d98a1b8924eabd9e_1639x779.png) Ⅳ 配置数据源 ![](https://img.kancloud.cn/97/21/9721d7b0d8029c68af094b8e5953dc0b_1639x779.png) >[info] 注意:如果promtheus有添加上下文的话,URL也需要添加上对应的上下文 > 截图使用地址:http://prometheus.kube-system.svc:9090/prometheus Ⅴ 测试数据源可用性 ![](https://img.kancloud.cn/cb/84/cb8461d4efb19b4973b31c7af0653958_1920x865.png)