基于腾讯云 Lighthouse 自建 Grafana 和 Prometheus 服务
链接:https://cloud.tencent.com/developer/article/2351514
前言
Grafana
Prometheus
准备Kubernetes集群
k3s
**。创建kubernetes部署配置文件
grafpro.yaml
,可根据实际修改部分参数,或使用后文的脚本自动修改。kind: Deployment
apiVersion: apps/v1
metadata:
name: &name grafpro
labels:
app: *name
spec:
selector:
matchLabels:
app: *name
template:
metadata:
labels:
app: *name
spec:
containers:
- name: grafana
image: grafana/grafana
securityContext:
runAsUser: 0
ports:
- containerPort: 3000
volumeMounts:
- name: *name
subPath: grafana
mountPath: /var/lib/grafana
- name: prometheus
image: prom/prometheus
securityContext:
runAsUser: 0
ports:
- containerPort: 9090
volumeMounts:
- name: *name
subPath: etc
mountPath: /etc/prometheus
- name: *name
subPath: prometheus
mountPath: /prometheus
volumes:
- name: *name
hostPath:
path: /srv/grafpro
type: DirectoryOrCreate
---
kind: Service
apiVersion: v1
metadata:
name: &name grafpro
labels:
app: *name
spec:
selector:
app: *name
ports:
- name: grafana
port: 3000
targetPort: 3000
- name: prometheus
port: 9090
targetPort: 9090
---
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: &name grafpro
annotations:
traefik.ingress.kubernetes.io/router.entrypoints: web,websecure
spec:
rules:
- host: grafana.example.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: *name
port:
name: grafana
- host: prometheus.example.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: *name
port:
name: prometheus
tls:
- secretName: default
grafpro.sh
,请注意修改GRAFPRO_STORAGE
、GRAFPRO_DOMAIN
和PROMETHEUS_DOMAIN
变量的值。export GRAFPRO_STORAGE=${GRAFPRO_STORAGE:-"/srv/grafpro"}
export GRAFANA_DOMAIN=${GRAFPRO_DOMAIN:-"grafana.example.org"}
export PROMETHEUS_DOMAIN=${PROMETHEUS_DOMAIN:-"grafana.example.org"}
mkdir -p $GRAFPRO_STORAGE/etc
cat <<EOF >$GRAFPRO_STORAGE/etc/prometheus.yml
global:
scrape_interval: 5s
scrape_timeout: 5s
evaluation_interval: 15s
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- 127.0.0.1:9090
EOF
cat grafpro.yaml \
| sed "s#/srv/grafpro#$GRAFPRO_STORAGE#g" \
| sed "s#grafana.example.org#$GRAFANA_DOMAIN#g" \
| sed "s#prometheus.example.org#$PROMETHEUS_DOMAIN#g" \
| kubectl apply -f -
grafpro.yaml
和grafpro.sh
文件上传到k8s主服务器上的同一目录内。注意检查grafpro.sh
文件的换行是否为LF
,否脚本可能无法执行。部署 Grafana 和 Prometheus
chmod +x grafpro.sh
./grafpro.sh
测试和验证
Grafana
http://grafana.example.org
,使用用户名admin
和密码admin
登录,此时会提示修改一个新的密码,修改后将自动转入控制台页面。http://grafana.example.org/connections/datasources/edit/bc3e3d0a-090a-469f-8c25-f93818410784
,在Prometheus server URL
输入框内填入http://127.0.0.1:9090
,点击Save & Test
后保存。http://grafana.example.org/connections/datasources/edit/bc3e3d0a-090a-469f-8c25-f93818410784/dashboards
,导入Prometheus 2.0 Stats
仪表盘。Prometheus
http://prometheus.example.org
,无需登录即可使用查询面板。生产环境建议至少添加Basic Auth
认证,防止数据泄露。END
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604215
Linux技术交流群:2636170
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章