1、系统环境
节点名称 IP地址 K8S版本
master01 192.168.68.20 v1.20.15
master02 192.168.68.21 v1.20.15
node01 192.168.68.22 v1.20.15
node02 192.168.68.23 v1.20.15
建议将所有的prometheus yaml文件存在一块
mkdir /opt/prometheus -p && cd /opt/prometheus
2、搭建nfs
因为pod会运行在k8s的所有节点,所以我们需要用到nfs共享存储
1.首先要准备nfs的服务器,这里为了简单,直接是master节点做nfs服务器
# 在nfs上安装nfs服务apt install nfs-kernel-server -y# 检查nfs-server是否已经启动:systemctl status nfs-server#启动nfssystemctl restart nfs-kernel-server.service#查看nfs支持的版本cat /proc/fs/nfsd/versions# 创建一个共享目录mkdir -p /data/prometheus# 将共享目录以读写权限暴露给192.168.68.0/24网段中的所有主机vim /etc/exports/data/prometheus 192.168.68.0/24(rw,no_root_squash,no_subtree_check)# 启动nfs服务exportfs -arvsystemctl restart nfs-server# 使用showmount -e查看是否可以看到共享目录:showmount -e 192.168.68.24
2.接下来,要在的每个node节点上都安装下nfs,这样的目的是为了node节点可以驱动nfs设备
# 在Ubuntu20.04中安装客户端:nfs-commonapt install nfs-common -y# 创建一个目录mkdir -p /data/prometheus#在其它机器上测试挂载mount -t nfs 192.168.68.24:/data/prometheus /data/prometheus# 取消挂载umount /data/prometheus
3.创建命名空间
创建vim prometheus-namespace.yml
apiVersion: v1kind: Namespacemetadata:name: prometheuslabels:name: prometheus
kubectl apply -f prometheus-namespace.yml
4、创建pv & pvc
vim prometheus-pv-pvc.yml
apiVersion: v1kind: PersistentVolumemetadata:name: prometheus-dataspec:capacity:storage: 10GiaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: Recyclenfs:server: 192.168.68.24path: /data/prometheus---apiVersion: v1kind: PersistentVolumeClaimmetadata:name: prometheus-datanamespace: prometheusspec:accessModes:- ReadWriteOnceresources:requests:storage: 10Gi
这里通过一个简单的NFS作为存储后端创建一个pv & pvc
kubectl apply -f prometheus-pv-pvc.yml
查看pv,pvc
kubectl get pv -o wide
kubectl get pvc -n prometheus -o wide
这里稍微提示一下,我们创建的pv和pvc大小都是10g,只是测试存储为10g。线上可以修改为200或者更多,一般prometheus数据保留15-30天就可以,如果数据量过大建议使用TSBD分布式存储
5、创建RBAC
我们这里还需要创建rbac认证,因为prometheus需要访问k8s集群内部的资源
apiVersion: v1kind: ServiceAccountmetadata:name: prometheusnamespace: prometheus---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:name: prometheusrules:- apiGroups:- ""resources:- nodes- nodes/metrics- services- endpoints- podsverbs:- get- list- watch- apiGroups:- ""resources:- configmapsverbs:- get- nonResourceURLs:- "/metrics"verbs:- get---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: prometheusroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: prometheussubjects:- kind: ServiceAccountname: prometheusnamespace: prometheus
cat >>prometheus-rbac.yaml <<EOFapiVersion: v1kind: ServiceAccountmetadata:name: prometheusnamespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata:name: prometheusrules:- apiGroups:- ""resources:- nodes- services- endpoints- pods- nodes/proxyverbs:- get- list- watch- apiGroups:- ""resources:- configmaps- nodes/metricsverbs:- get- nonResourceURLs:- /metricsverbs:- get---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:name: prometheusroleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: prometheussubjects:- kind: ServiceAccountname: prometheusnamespace: kube-systemEOF
由于我们要获取的资源,在每一个namespace下面都有可能存在,所以我们这里使用的是ClusterRole的资源对象,nonResourceURLs是用来对非资源型metrics进行操作的权限声明
创建rbac文件:kubectl apply -f prometheus-rbac.yaml
6、创建deployment文件
cat >>prometheus-deploy.yaml <<EOFapiVersion: apps/v1kind: Deploymentmetadata:name: prometheusnamespace: kube-systemlabels:app: prometheusspec:template:metadata:labels:app: prometheusspec:serviceAccountName: prometheuscontainers:- image: prom/prometheus:v2.35.0name: prometheuscommand:- "/bin/prometheus"args:- "--config.file=/etc/prometheus/prometheus.yml"- "--storage.tsdb.path=/prometheus"- "--storage.tsdb.retention=30d"- "--web.enable-admin-api" # 控制对admin HTTP API的访问,其中包括删除时间序列等功能- "--web.enable-lifecycle" # 支持热更新,直接执行localhost:9090/-/reload立即生效ports:- containerPort: 9090protocol: TCPname: httpvolumeMounts:- mountPath: "/prometheus"subPath: prometheusname: data- mountPath: "/etc/prometheus"name: config-volumeresources:requests:cpu: 100mmemory: 512Milimits:cpu: 100mmemory: 512MisecurityContext:runAsUser: 0volumes:- name: datapersistentVolumeClaim:claimName: prometheus- configMap:name: prometheus-configname: config-volumeEOF
cat >>prometheus-deploy.yaml <
kind: Deployment
metadata:
name: prometheus
namespace: prometheus
labels:
app: prometheus
spec:
template:
metadata:
labels:
app: prometheus
spec:
serviceAccountName: prometheus
containers:
- image: prom/prometheus:v2.35.0
name: prometheus
command:
- “/bin/prometheus”
args:
- “—config.file=/etc/prometheus/prometheus.yml”
- “—storage.tsdb.path=/prometheus”
- “—storage.tsdb.retention=30d”
- “—web.enable-admin-api” # 控制对admin HTTP API的访问,其中包括删除时间序列等功能
- “—web.enable-lifecycle” # 支持热更新,直接执行localhost:9090/-/reload立即生效
ports:
- containerPort: 9090
protocol: TCP
name: http
volumeMounts:
- mountPath: “/prometheus”
subPath: prometheus
name: data
- mountPath: “/etc/prometheus”
name: config-volume
resources:
requests:
cpu: 100m
memory: 512Mi
limits:
cpu: 100m
memory: 512Mi
securityContext:
runAsUser: 0
volumes:
- name: data
persistentVolumeClaim:
claimName: prometheus
- configMap:
name: prometheus-config
name: config-volume
EOF
这里稍微讲解一下配置参数
我们在启动程序的时候,除了指定prometheus.yaml(configmap)以外,还通过storage.tsdb.path指定了TSDB数据的存储路径、通过storage.tsdb.rentention设置了保留多长时间的数据,还有下面的web.enable-admin-api参数可以用来开启对admin api的访问权限,参数web.enable-lifecyle用来开启支持热更新,有了这个参数之后,prometheus.yaml(configmap)文件只要更新了,通过执行localhost:9090/-/reload就会立即生效
我们添加了一行securityContext,,其中runAsUser设置为0,这是因为prometheus运行过程中使用的用户是nobody,如果不配置可能会出现权限问题
创建deployment文件:kubectl apply -f prometheus-deploy.yaml
1
6、创建configmap
#生成配置文件
cat >> prometheus.configmap.yaml <
kind: ConfigMap
metadata:
name: prometheus-config
namespace: prometheus
data:
prometheus.yml: |
global:
scrape_interval: 15s
scrape_timeout: 15s
scrape_configs:
- job_name: ‘prometheus’
static_configs:
- targets: [‘localhost:9090’]
EOF
上面包含了3个模块global、rule_files和scrape_configs
其中global模块控制Prometheus Server的全局配置
scrape_interval:表示prometheus抓取指标数据的频率,默认是15s,我们可以覆盖这个值
evaluation_interval:用来控制评估规则的频率,prometheus使用规则产生新的时间序列数据或者产生警报
rule_files模块制定了规则所在的位置,prometheus可以根据这个配置加载规则,用于生产新的时间序列数据或者报警信息,当前我们没有配置任何规则,后期会添加
scrape_configs用于控制prometheus监控哪些资源。由于prometheus通过http的方式来暴露它本身的监控数据,prometheus也能够监控本身的健康情况。在默认的配置有一个单独的job,叫做prometheus,它采集prometheus服务本身的时间序列数据。这个job包含了一个单独的、静态配置的目标;监听localhost上的9090端口。
prometheus默认会通过目标的/metrics路径采集metrics。所以,默认的job通过URL:http://localhost:9090/metrics采集metrics。收集到时间序列包含prometheus服务本身的状态和性能。如果我们还有其他的资源需要监控,可以直接配置在该模块下即可
创建configmap文件: kubectl create -f prometheus.configmap.yaml
1
现在我们prometheus服务状态是已经正常了,但是我们在浏览器是无法访问prometheus的 webui服务。那么我们还需要创建一个service
7、创建service nodeport
cat >>prometeheus-svc.yaml <
kind: Service
metadata:
name: prometheus
namespace: kube-system
labels:
app: prometheus
spec:
selector:
app: prometheus
type: NodePort
ports:
- name: web
port: 9090
targetPort: http
EOF
为了方便测试,我这里使用的是NodePort,我们也可以创建一个Ingress对象使用域名访问
8、创建ingress
cat >>prometheus.ingress.yaml <
kind: Ingress
metadata:
name: prometheus-ingress
namespace: kube-system
spec:
rules:
- host: www.prometheus.com
http:
paths:
- path: /
backend:
serviceName: prometheus
servicePort: 9090
EOF
修改hosts文件
windows系统在C:/windows/drivers/etc
9、测试
默认prometheus会监控自己
我们查看一下数据,是否收集到数据
比如我们这里就选择scrape_duration_seconds这个指标,然后点击Execute,如果这个时候没有查询到任何数据,我们可以切换到Graph这个 tab 下面重新选择下时间,选择到当前的时间点,重新执行,就可以看到类似于下面的图表数据了:
