一、监控没有metrics接口的业务应用:以kafka为例
### --- 创建一个kafka-exporter的deployment
~~~ 创建kafka-exporter的yaml文件
[root@k8s-master01 kafka-exporter]# vim kafka-exporter-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
labels:
app: kafka-exporter
name: kafka-exporter
namespace: monitoring
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: kafka-exporter
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: kafka-exporter
spec:
containers:
- args:
- --kafka.server=kafka-0.kafka-headless.public-service:9092
env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: C.UTF-8
image: danielqsj/kafka-exporter:latest
imagePullPolicy: IfNotPresent
lifecycle: {}
name: kafka-exporter
ports:
- containerPort: 9308
name: web
protocol: TCP
resources:
limits:
cpu: 249m
memory: 318Mi
requests:
cpu: 10m
memory: 10Mi
securityContext:
allowPrivilegeEscalation: false
privileged: false
readOnlyRootFilesystem: false
runAsNonRoot: false
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /usr/share/zoneinfo/Asia/Shanghai
name: tz-config
- mountPath: /etc/localtime
name: tz-config
- mountPath: /etc/timezone
name: timezone
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
type: ""
name: tz-config
- hostPath:
path: /etc/timezone
type: ""
name: timezone
### --- 创建kafka-exporter
[root@k8s-master01 kafka-exporter]# kubectl create -f kafka-exporter-deployment.yaml
deployment.apps/kafka-exporter created
### --- 查看创建的kafka-exporter
[root@k8s-master01 kafka-exporter]# kubectl get po -n monitoring -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kafka-exporter-d47b9f76f-k92rm 1/1 Running 0 37s 172.27.14.227 k8s-node02 <none> <none>
二、创建kafka-exporter的service和endpoint
### --- 创建kafka-exporter的service配置文件
[root@k8s-master01 kafka-exporter]# vim kafka-exporter-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: kafka-exporter
name: kafka-exporter
namespace: monitoring
resourceVersion: "11300354"
spec:
ports:
- name: container-1-web-1
port: 9308
protocol: TCP
targetPort: 9308
selector:
app: kafka-exporter
sessionAffinity: None
type: ClusterIP
### --- 常见service和endpoint
[root@k8s-master01 kafka-exporter]# kubectl create -f kafka-exporter-svc.yaml
service/kafka-exporter created
### --- 查看创建的po,svc,ep资源详情
[root@k8s-master01 kafka-exporter]# kubectl get po,svc,ep -n monitoring -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/kafka-exporter-d47b9f76f-k92rm 1/1 Running 0 12m 172.27.14.227 k8s-node02 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kafka-exporter ClusterIP 10.110.47.163 <none> 9308/TCP 53s app=kafka-exporter
NAME ENDPOINTS AGE 3d15h
endpoints/kafka-exporter 172.27.14.227:9308 52s
三、验证kafka-exporter-svc是否可以获取数据
### --- 查看创建的svc
[root@k8s-master01 kafka-exporter]# kubectl get svc -n monitoring
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kafka-exporter ClusterIP 10.110.47.163 <none> 9308/TCP 7m51s
### --- curl一下svc的地址,查看是否可以获取数据
[root@k8s-master01 kafka-exporter]# curl 10.110.47.163:9308/metrics
# HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served.
# TYPE promhttp_metric_handler_requests_in_flight gauge
promhttp_metric_handler_requests_in_flight 1
# HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code.
# TYPE promhttp_metric_handler_requests_total counter
promhttp_metric_handler_requests_total{code="200"} 2
promhttp_metric_handler_requests_total{code="500"} 0
promhttp_metric_handler_requests_total{code="503"} 0
### --- 查看kafka-exporter的日志是否发生报错
~~~ 注:目前环境没有报错
[root@k8s-master01 kafka-exporter]# kubectl logs -f kafka-exporter-d47b9f76f-k92rm -n monitoring
time="2021-05-28T12:15:07+08:00" level=info msg="Refreshing client metadata" source="kafka_exporter.go:255"
time="2021-05-28T12:16:14+08:00" level=info msg="Refreshing client metadata" source="kafka_exporter.go:255"
四、创建kafka-exporter- ServiceMonitor
### --- 创建kafka-exporter- ServiceMonitor的yaml文件
[root@k8s-master01 kafka-exporter]# vim kafka-exporter-ServiceMonitor.yaml
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
k8s-app: kafka-exporter
name: kafka-exporter
namespace: monitoring
spec:
endpoints:
- interval: 30s
port: container-1-web-1
namespaceSelector:
matchNames:
- monitoring
selector:
matchLabels:
app: kafka-exporter
### --- 创建kafka-exporter- ServiceMonitor
[root@k8s-master01 kafka-exporter]# kubectl create -f kafka-exporter-ServiceMonitor.yaml -n monitoring
servicemonitor.monitoring.coreos.com/kafka-exporter created
### --- 查看创建的servicemonitor
[root@k8s-master01 kafka-exporter]# kubectl get servicemonitor -n monitoring
NAME AGE
kafka-exporter 34s