文章目录
- 前言
- 一、思路
- 二、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: grafanaini
namespace: grafana
data:
grafana.ini: |
[server]
domain: daxinxindahaohao.cn
root_url: http://daxinxindahaohao.cn/grafana
serve_from_sub_path: true
[auth.anonymous]
enabled: true
org_role: Viewer
2. grafana.yaml
grafana启动服务的yaml文件
我这里使用的pvc的存储类型是rook-ceph,使用其他存储类型的修改为自己的就可以
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: grafana-pvc
namespace: grafana
spec:
storageClassName: rook-cephfs
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 50G
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: grafana
name: grafana
namespace: grafana
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
env:
- name: TZ
value: Asia/Shanghai
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
- name : grafanaini
mountPath: /etc/grafana/grafana.ini
subPath: grafana.ini
volumes:
- name: grafanaini
configMap:
name: grafanaini
- name: grafana-pv
persistentVolumeClaim:
claimName: grafana-pvc
---
apiVersion: v1
kind: Service
metadata:
name: grafana
namespace: grafana
spec:
ports:
- port: 3000
protocol: TCP
targetPort: http-grafana
selector:
app: grafana
sessionAffinity: None
type: 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: /grafana
pathType: Prefix # 前缀匹配
backend:
service:
name: grafana
port:
number: 3000
2. grafana-externalname.yaml
这里配置的是grafana的跨命名空间的service
kind: Service
apiVersion: v1
metadata:
name: grafana
namespace: daxinxindahaohao
spec:
type: ExternalName
externalName: grafana.grafana.svc.cluster.local
3. ingress-nginx-controller
端口是31247,下面会用到
四、 本机host文件准备
路径 C:\Windows\System32\drivers\etc\hosts
五、访问测试
通过浏览器进行访问测试
地址是:http://daxinxindahaohao.cn:31247/grafana