: https://github.com/prometheus/prometheus是CNCF毕业的项目,是非常流行监控程序,Prometheus属于是典型的有状态应用,而其有包含了一些自身特有的运维管理和配置管理方式。而这些都无法通过Kubernetes原生提供的应用管理概念实现自动化。CoreOS率先引入了Operator的概念,并且首先推出了针对在Kubernetes下运行和管理Etcd的Etcd Operator。并随后推出了Prometheus Operator。用ConfigMap管理Prometheus配置文件。
https://github.com/prometheus-operator/kube-prometheus
而https://github.com/iKubernetes/k8s-prom里面包含了一套完整的配置文件。下面我们开始搭建k8s的监控环境:
1,创建ns
% kubectl apply -f namespace.yaml
namespace/prom created
复制
2,创建exporter
% kubectl apply -f node_exporter/
daemonset.apps/prometheus-node-exporter created
service/prometheus-node-exporter created
复制
3,启动prometheus
% kubectl apply -f prometheus/
configmap/prometheus-config created
deployment.apps/prometheus-server created
Warning: rbac.authorization.k8s.io/v1beta1 ClusterRole is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRole
clusterrole.rbac.authorization.k8s.io/prometheus created
serviceaccount/prometheus created
Warning: rbac.authorization.k8s.io/v1beta1 ClusterRoleBinding is deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 ClusterRoleBinding
clusterrolebinding.rbac.authorization.k8s.io/prometheus created
service/prometheus created
复制
4,创建k8s的matrix
% kubectl apply -f kube-state-metrics/
deployment.apps/kube-state-metrics created
serviceaccount/kube-state-metrics created
clusterrole.rbac.authorization.k8s.io/kube-state-metrics created
clusterrolebinding.rbac.authorization.k8s.io/kube-state-metrics created
service/kube-state-metrics created
复制
访问k8s的资源需要k8s的ssl认证
A,生成serving.key
(umask 077; openssl genrsa -out serving.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
.........................................................+++++
..................+++++
e is 65537 (0x010001)
复制
B,生成serving.csr
%openssl req -new -key serving.key -out serving.csr -subj "/CN=serving"
复制
C,生成serving.crt
%openssl x509 -req -in serving.csr -CA ~/Library/Group\ Containers/group.com.docker/pki/apiserver.crt -CAkey ~/Library/Group\ Containers/group.com.docker/pki/apiserver.key -CAcreateserial -out serving.crt -days 3650
复制
5,创建secret
% kubectl create secret generic cm-adapter-serving-certs --from-file=serving.crt=./serving.crt --from-file=serving.key -n prom
secret/cm-adapter-serving-certs created
复制
6,由于k8s的指标并不是直接符合protheus标准需要adapter
$kubectl apply -f k8s-prometheus-adapter
复制
7,安装完prometheus后安装grafana
% kubectl apply -f grafana.yaml
deployment.apps/monitoring-grafana created
service/monitoring-grafana created
复制
对应的yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: monitoring-grafana
namespace: prom #修改名称空间
spec:
replicas: 1
selector:
matchLabels:
task: monitoring
k8s-app: grafana
template:
metadata:
labels:
task: monitoring
k8s-app: grafana
spec:
containers:
- name: grafana
image: angelnu/heapster-grafana:v5.0.4
ports:
- containerPort: 3000
protocol: TCP
volumeMounts:
- mountPath: etc/ssl/certs
name: ca-certificates
readOnly: true
- mountPath: var
name: grafana-storage
env: #这里使用的是原先的heapster的grafana的配置文件,需要注释掉这个环境变量
#- name: INFLUXDB_HOST
# value: monitoring-influxdb
- name: GF_SERVER_HTTP_PORT
value: "3000"
# The following env variables are required to make Grafana accessible via
# the kubernetes api-server proxy. On production clusters, we recommend
# removing these env variables, setup auth for grafana, and expose the grafana
# service using a LoadBalancer or a public IP.
- name: GF_AUTH_BASIC_ENABLED
value: "false"
- name: GF_AUTH_ANONYMOUS_ENABLED
value: "true"
- name: GF_AUTH_ANONYMOUS_ORG_ROLE
value: Admin
- name: GF_SERVER_ROOT_URL
# If you're only using the API Server proxy, set this value instead:
# value: api/v1/namespaces/kube-system/services/monitoring-grafana/proxy
value: /
volumes:
- name: ca-certificates
hostPath:
path: /etc/ssl/certs
- name: grafana-storage
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
# For use as a Cluster add-on (https://github.com/kubernetes/kubernetes/tree/master/cluster/addons)
# If you are NOT using this as an addon, you should comment out this line.
kubernetes.io/cluster-service: 'true'
kubernetes.io/name: monitoring-grafana
name: monitoring-grafana
namespace: prom
spec:
# In a production setup, we recommend accessing Grafana through an external Loadbalancer
# or through a public IP.
# type: LoadBalancer
# You could also use NodePort to expose the service at a randomly-generated port
type: NodePort
ports:
- port: 80
targetPort: 3000
selector:
k8s-app: grafana
复制
至此我们的基本环境搭建起来了
% kubectl -n prom get svc monitoring-grafana
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
monitoring-grafana NodePort 10.105.98.11 <none> 80:32649/TCP 8h
复制
看下grafana界面
http://127.0.0.1:32649/?orgId=1
复制
看下prometheus界面
% kubectl get svc -n prom
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
custom-metrics-apiserver ClusterIP 10.96.172.58 <none> 443/TCP 8h
kube-state-metrics ClusterIP 10.109.86.171 <none> 8080/TCP 17h
monitoring-grafana NodePort 10.105.98.11 <none> 80:32649/TCP 17h
prometheus NodePort 10.103.148.186 <none> 9090:30090/TCP 17h
prometheus-node-exporter ClusterIP None <none> 9100/TCP 17h
复制
http://127.0.0.1:30090/graph
复制
配置相关环境:
1,配置protheus的数据源:
2,在grafana里面查看数据
文章转载自golang算法架构leetcode技术php,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
【专家观点】罗敏:从理论到真实SQL,感受DeepSeek如何做性能优化
墨天轮编辑部
1232次阅读
2025-03-06 16:45:38
【专家有话说第五期】在不同年龄段,DBA应该怎样规划自己的职业发展?
墨天轮编辑部
1214次阅读
2025-03-13 11:40:53
2025年2月国产数据库大事记
墨天轮编辑部
948次阅读
2025-03-05 12:27:34
2025年2月国产数据库中标情况一览:GoldenDB 3500+万!达梦近千万!
通讯员
839次阅读
2025-03-06 11:40:20
2月“墨力原创作者计划”获奖名单公布
墨天轮编辑部
429次阅读
2025-03-13 14:38:19
AI的优化能力,取决于你问问题的能力!
潇湘秦
403次阅读
2025-03-11 11:18:22
优炫数据库成功应用于国家电投集团青海海南州新能源电厂!
优炫软件
334次阅读
2025-03-21 10:34:08
达梦数据与法本信息签署战略合作协议
达梦数据
278次阅读
2025-03-06 09:26:57
国产化+性能王炸!这套国产方案让 3.5T 数据 5 小时“无感搬家”
YMatrix
263次阅读
2025-03-13 09:51:26
IBM收购数据库厂商DataStax:瞄准向量和AI搜索
深度数据云
256次阅读
2025-02-28 12:04:04