通过Podmonitor 自动发现pod

时间:2023-02-19 13:20:48

1、查看podmonitor是否安装

[root@k8s-master podmonitor]# kubectl get crd|grep prometheus
prometheuses.monitoring.coreos.com 2022-06-08T10:05:26Z
prometheusrules.monitoring.coreos.com 2022-06-08T10:05:26Z

[root@k8s-master podmonitor]# kubectl get podmonitor -nmonitoring
NAME AGE

2、创建podminitor监控

[root@k8s-master podmonitor]# cat prometheus-podmonitor.yaml 
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
labels:
jmeter_mode: base
name: jmeter # 最终显示在promtheus中的job名称
namespace: monitoring
spec:
podMetricsEndpoints:
- interval: 15s
path: /metrics
targetPort: 9270
namespaceSelector:
matchNames:
- jmeter
selector:
matchLabels:
jmeter_mode: base
[root@k8s-master podmonitor]# kubectl apply -f prometheus-podmonitor.yaml
podmonitor.monitoring.coreos.com/jmeter unchanged
[root@k8s-master podmonitor]# kubectl get podmonitor -nmonitoring
NAME AGE
jmeter 3h46m

3、修改​kube-prometheus​/manifests/prometheus-clusterRole.yaml配置文件并应用如下:

[root@k8s-master prometheus]# cat kube-prometheus/manifests/prometheus-clusterRole.yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.36.0
name: prometheus-k8s
rules:
- apiGroups:
- ""
resources:
- nodes/metrics
- pods
- services
- endpoints
verbs:
- get
- list
- watch
- nonResourceURLs:
- /metrics
verbs:
- get
[root@k8s-master prometheus]# kubectl apply -f kube-prometheus/manifests/prometheus-clusterRole.yaml
clusterrole.rbac.authorization.k8s.io/prometheus-k8s changed

4、具体生产的配置。

1、修改​kube-prometheus​​/​​manifests​​/prometheus-clusterRole.yaml配置文件并应用

cat kube-prometheus/manifests/prometheus-clusterRole.yaml 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
labels:
app.kubernetes.io/component: prometheus
app.kubernetes.io/instance: k8s
app.kubernetes.io/name: prometheus
app.kubernetes.io/part-of: kube-prometheus
app.kubernetes.io/version: 2.36.0
name: prometheus-k8s
rules:
- apiGroups:
- ""
resources:
- nodes/metrics
- pods
- services
- endpoints
verbs:
- get
- list
- watch
- nonResourceURLs:
- /metrics
verbs:
- get
[root@k8s-master jmeter]# kubectl apply -f /home/prometheus/kube-prometheus/manifests/prometheus-clusterRole.yaml
Warning: resource clusterroles/prometheus-k8s is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
clusterrole.rbac.authorization.k8s.io/prometheus-k8s configured

2、创建podminitor监控

[root@k8s-master podmonitor]# cat prometheus-podmonitor.yaml 
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
labels:
jmeter_mode: base
name: jmeter # 最终显示在promtheus中的job名称
namespace: monitoring
spec:
podMetricsEndpoints:
- interval: 15s
path: /metrics
targetPort: 9270
namespaceSelector:
matchNames:
- jmeter
selector:
matchLabels:
jmeter_mode: base
[root@k8s-master podmonitor]# kubectl apply -f prometheus-podmonitor.yaml
podmonitor.monitoring.coreos.com/jmeter unchanged
[root@k8s-master podmonitor]# kubectl get podmonitor -nmonitoring
NAME AGE
jmeter 3h46m

3、创建jmeter-configmap

[root@k8s-master jmeter]# cat jmeter-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
name: jmeter-load-test
labels:
app: jmeter-load
data:
load_test: |
#!/bin/bash
# #Script created to invoke jmeter test script with the slave POD IP addresses
# #Script should be run like: ./load_test "path to the test script in jmx format"
/jmeter/apache-jmeter-*/bin/jmeter -n -t $1 -Jprometheus.port=9270 -Jprometheus.ip=$2 -Jprometheus.save.jvm=false
#
[root@k8s-master jmeter]# kubectl apply -f jmeter-configmap.yaml -njmeter
configmap/jmeter-load-test created

4、创建jmeter-deployment

[root@k8s-master jmeter]# cat jmeter.yaml 
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: jmeter-base
labels:
jmeter_mode: base
spec:
replicas: 1
selector:
matchLabels:
jmeter_mode: base
template:
metadata:
labels:
jmeter_mode: base
spec:
containers:
- name: jmbase
image: ccb/jmeter-base:5.4.3
imagePullPolicy: IfNotPresent
command: [ "/bin/bash", "-c", "--" ]
args: [ "while true; do sleep 30; done;" ]
volumeMounts:
- name: loadtest
mountPath: /load_test
subPath: "load_test"
ports:
- containerPort: 9270
volumes:
- name: loadtest
configMap:
name: jmeter-load-test

[root@k8s-master jmeter]# kubectl apply -f jmeter.yaml -njmeter
deployment.apps/jmeter-base created

5、查看相关的资源

[root@k8s-master jmeter]# kubectl get all -njmeter
NAME READY STATUS RESTARTS AGE
pod/jmeter-base-745944f478-wrksn 1/1 Running 0 13m
pod/jmeter-master-999999999-6ddbcb8d4d-w7pds 1/1 Running 0 9d

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/jmeter-base 1/1 1 1 13m
deployment.apps/jmeter-master-999999999 1/1 1 1 9d

NAME DESIRED CURRENT READY AGE
replicaset.apps/jmeter-base-745944f478 1 1 1 13m
replicaset.apps/jmeter-master-999999999-6ddbcb8d4d 1 1 1 9d

6、在Prometheus上进行验证

通过Podmonitor 自动发现pod