文章目录
- 前言
- 一、思路
- 二、grafana准备
- 1. grafana-configmap.yaml
- 2. grafana.yaml
- 三、ingress准备
- 1. ingress.yaml
- 2. grafana-externalname.yaml
- 3. ingress-nginx-controller
- 四、 本机host文件准备
- 五、访问测试
前言
在k8s集群中,使用ingress服务转发grafana的页面。
一、思路
ingress方面增加路由规则和跨命名空间的service即可
grafana增加configmap挂载grafana.ini文件,增加匿名登陆配置和root_url配置
二、grafana准备
1. grafana-configmap.yaml
grafana.ini文件的yaml文件
apiVersion: v1
kind: Namespace
metadata:name: grafana
---
apiVersion: v1
kind: ConfigMap
metadata:name: grafanaininamespace: grafana
data:grafana.ini: |[server]domain: daxinxindahaohao.cnroot_url: http://daxinxindahaohao.cn/grafanaserve_from_sub_path: true[auth.anonymous]enabled: trueorg_role: Viewer
2. grafana.yaml
grafana启动服务的yaml文件
我这里使用的pvc的存储类型是rook-ceph,使用其他存储类型的修改为自己的就可以
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: grafana-pvcnamespace: grafana
spec:storageClassName: rook-cephfsaccessModes:- ReadWriteOnceresources:requests:storage: 50G
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: grafananame: grafananamespace: grafana
spec:selector:matchLabels:app: grafanatemplate:metadata:labels:app: grafanaspec:securityContext:fsGroup: 472supplementalGroups:- 0containers:- name: grafanaimage: grafana/grafana:latestimagePullPolicy: IfNotPresentenv:- name: TZvalue: Asia/Shanghaiports:- containerPort: 3000name: http-grafanaprotocol: TCPreadinessProbe:failureThreshold: 3httpGet:path: /robots.txtport: 3000scheme: HTTPinitialDelaySeconds: 10periodSeconds: 30successThreshold: 1timeoutSeconds: 2livenessProbe:failureThreshold: 3initialDelaySeconds: 30periodSeconds: 10successThreshold: 1tcpSocket:port: 3000timeoutSeconds: 1resources:requests:cpu: 250mmemory: 750MivolumeMounts:- mountPath: /var/lib/grafananame: grafana-pv- name : grafanainimountPath: /etc/grafana/grafana.inisubPath: grafana.inivolumes:- name: grafanainiconfigMap:name: grafanaini- name: grafana-pvpersistentVolumeClaim:claimName: grafana-pvc
---
apiVersion: v1
kind: Service
metadata:name: grafananamespace: grafana
spec:ports:- port: 3000protocol: TCPtargetPort: http-grafanaselector:app: grafanasessionAffinity: Nonetype: ClusterIP
三、ingress准备
1. ingress.yaml
apiVersion: v1
kind: Namespace
metadata:name: daxinxindahaohao
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: daxinxindahaohao-ingress #ingress名称namespace: daxinxindahaohao #这里写ingress服务运行所在的命名空间名称annotations:kubernetes.io/ingress.class: "nginx"
spec:rules:- host: daxinxindahaohao.cn #设置域名http:paths:- path: /grafanapathType: Prefix # 前缀匹配backend:service:name: grafanaport:number: 3000
2. grafana-externalname.yaml
这里配置的是grafana的跨命名空间的service
kind: Service
apiVersion: v1
metadata:name: grafananamespace: daxinxindahaohao
spec:type: ExternalNameexternalName: grafana.grafana.svc.cluster.local
3. ingress-nginx-controller
端口是31247,下面会用到
四、 本机host文件准备
路径 C:\Windows\System32\drivers\etc\hosts
五、访问测试
通过浏览器进行访问测试
地址是:http://daxinxindahaohao.cn:31247/grafana