文章目录

  • 监控不携带metrics接口的服务
  • 准备测试环境
  • 1、部署expertor
  • 1)准备目录
  • 2)创建服务
  • 3)部署服务
  • 4)创建Service
  • 5)部署Service
  • 6)查看部署
  • 7)访问测试
  • 2、部署EndPrints
  • 3、部署Service
  • 4、创建ServiceMonitor
  • 1)编写配置清单
  • 2)部署查看
  • 3)测试
  • 4)加入grafana,做大屏展示
  • 监控Redis
  • 1.部署测试环境
  • 1)编写部署清单
  • 2)编写Service清单
  • 3)部署
  • 2.配置部署expertor
  • 1)编写部署清单
  • 2)编写svc清单
  • 3)部署
  • 3.部署EndPrints
  • 4.部署Service
  • 5.创建ServiceMonitor
  • 1)编写配置清单
  • 2)部署
  • 6.grafana展示![在这里插入图片描述](https://s2.51cto.com/images/blog/202311/09083512_654c29401f12438986.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=)
  • 监控Mysql
  • 1.造环境
  • 1)安装mariadb
  • 2)授权monitor用户
  • 2.安装数据库exporter
  • 1)编写部署清单
  • 2)编写Service清单
  • 3.部署服务
  • 监控exporter
  • 1.造环境
  • 1)下载部署exporter
  • 1)安装mariadb
  • 2)授权monitor用户
  • 2.配置maridb
  • 3.启动mysqld_exporter


监控不携带metrics接口的服务

普罗米修斯的两种监控服务:

1.携带metrics接口的服务

可以通过metrics接口来获取服务的监控项、监控信息

2.不携带metrics接口的服务

通过EndPoint造一个metrict接口

PS:metrics(接口)

监控流程:

1、部署expertor,从而创建一个metrics接口

2、部署EndPrints,链接expertor暴露出来的metrics接口

3、部署Service,基于ServiceMonitor使用

4、创建ServiceMonitor,注入promethues

5、测试服务

6、加入grafana,做大屏展示

准备测试环境

[root@k8s-master1 ~]# cat /etc/nginx/conf.d/status.conf 
server {
	listen 8081;
	location / {
	    stub_status;
	}
	    
}

tongredis prometheus监控方案 prometheus监控接口_nginx

1、部署expertor

  • 部署expertor,从而创建一个metrics接口
1)准备目录
[root@k8s-master1 ~]# mkdir exporter-nginx
[root@k8s-master1 ~]# cd exporter-nginx/
[root@k8s-master1 exporter-nginx]# vim exporter-nginx-deployment.yaml
2)创建服务
  • 创建一个服务,获取并格式化metrics接口数据
cat > exporter-nginx-deployment.yaml <<EOF
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-prometheus-exporter
spec:
  selector:
    matchLabels:
      app: nginx-prometheus-exporter
      deploy: nginx-exporter
  template:
    metadata:
      labels:
        app: nginx-prometheus-exporter
        deploy: nginx-exporter
    spec:
      containers:
        - name: nginx-exporter
          image: nginx/nginx-prometheus-exporter:0.9.0
          imagePullPolicy: IfNotPresent
          command:
            - "nginx-prometheus-exporter"
            - "-nginx.scrape-uri=http://192.168.12.11/status"
EOF
3)部署服务
[root@k8s-master1 exporter-nginx]# kubectl apply -f exporter-nginx-deployment.yaml 
deployment.apps/name-prometheus-exporter created
4)创建Service
  • 给予prometheus集群获取格式化好了的metrics接口服务
cat > exporter-service.yaml EOF
kind: Service
apiVersion: v1
metadata:
  name: nginx-prometheus-exporter
spec:
  ports:
    - port: 9113
      targetPort: 9113
      name: nginx-prometheus-exporter
      protocol: TCP
  selector:
    app: nginx-prometheus-exporter
    deploy: nginx-exporter
EOF
5)部署Service
[root@k8s-master1 exporter-nginx]# kubectl apply -f exporter-service.yaml
6)查看部署
[root@k8s-master1 exporter-nginx]# kubectl get svc,pod
NAME                  TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     			     AGE
service/nginx-prometheus-exporter   ClusterIP   10.109.114.223   <none>     9113/TCP         3m19s

NAME                                             READY   STATUS    RESTARTS   AGE
pod/nginx-prometheus-exporter-7fdfcf6d7b-szt82   1/1     Running   0          6m10s
7)访问测试
[root@k8s-master1 exporter-nginx]# curl 10.109.114.223:9113/metrics
# HELP nginx_connections_accepted Accepted client connections
# TYPE nginx_connections_accepted counter
nginx_connections_accepted 4
# HELP nginx_connections_active Active client connections
# TYPE nginx_connections_active gauge
nginx_connections_active 1
# HELP nginx_connections_handled Handled client connections
# TYPE nginx_connections_handled counter

2、部署EndPrints

  • 部署EndPrints,链接expertor暴露出来的metrics接口
# 参考上述Service,此处无需重复创建。

3、部署Service

  • 基于ServiceMonitor使用
# 参考上述Service,此处无需重复创建。

4、创建ServiceMonitor

  • 创建ServiceMonitor,注入promethues
1)编写配置清单
cat > service-monitor.yaml <<EOF
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
  labels:
    app: nginx-prometheus-exporter-servicemonitor
    deploy: nginx-exporter-servicemonitor
  name: nginx-monitor
  namespace: monitoring
spec:
  endpoints:
    - interval: 3s
      port: nginx-prometheus-exporter
  selector:
    matchLabels:
      app: nginx-prometheus-exporter
      deploy: nginx-exporter
  namespaceSelector:
    matchNames:
      - "default"
EOF
2)部署查看
[root@k8s-master1 exporter-nginx]# kubectl apply -f service-monitor.yaml
[root@k8s-master1 exporter-nginx]# kubectl get servicemonitor -n monitoring 
NAME                      AGE
nginx-monitor             25s
3)测试

tongredis prometheus监控方案 prometheus监控接口_redis_02

4)加入grafana,做大屏展示

略~

监控Redis

1.部署测试环境

1)编写部署清单
cat > redis-deploy.yaml <EOF
kind: Deployment
apiVersion: apps/v1
metadata:
  name: redis
spec:
  selector:
    matchLabels:
      k8s: redis-exporter
  template:
    metadata:
      labels:
        k8s: redis-exporter
    spec:
      containers:
        - name: redis
          imagePullPolicy: IfNotPresent
          image: redis
EOF
2)编写Service清单
cat > redis-svc.yaml <<EOF
kind: Service
apiVersion: v1
metadata:
  name: redis-svc
  labels:
    k8s: redis-exporter
spec:
  ports:
    - port: 6379
      targetPort: 6379
      name: redis
  selector:
    k8s: redis-exporter
EOF
3)部署
[root@k8s-master1 exporter-redis]# kubectl apply -f redis-deploy.yaml
[root@k8s-master1 exporter-redis]# kubectl apply -f redis-svc.yaml

2.配置部署expertor

  • 配置部署expertor,从而创建一个metrics接口
1)编写部署清单
cat > redis-exporter-deploy.yaml <<EOF
kind: Deployment
apiVersion: apps/v1
metadata:
  name: redis-exporter-server
spec:
  selector:
    matchLabels:
      k8s: redis-exporter-server
  template:
    metadata:
      annotations:
        prometheus.io/scrape: "true"
        prometheus.io/port: "9121"
      labels:
        k8s: redis-exporter-server
    spec:
      containers:
        - name: redis
          image: oliver006/redis_exporter
          args:
            - "--redis.addr=redis://redis-svc.default.svc.cluster.local:6379"
          ports:
            - containerPort: 9121
EOF
2)编写svc清单
cat > redis-exporter-svc.yaml <<EOF
kind: Service
apiVersion: v1
metadata:
  name: redis-exporter-server
  labels:
    k8s: redis-exporter-server-svc
spec:
  ports:
    - port: 9121
      targetPort: 9121
      name: redis-exporter
  selector:
    k8s: redis-exporter-server
EOF
3)部署
[root@k8s-master1 exporter-redis]# kubectl apply -f redis-exporter-deploy.yaml
[root@k8s-master1 exporter-redis]# kubectl apply -f redis-exporter-svc.yaml

3.部署EndPrints

  • 部署EndPrints,链接expertor暴露出来的metrics接口
# 参考上述Service,此处无需重复创建。

4.部署Service

  • 部署Service,基于ServiceMonitor使用
# 参考上述Service,此处无需重复创建。

5.创建ServiceMonitor

  • 创建ServiceMonitor,注入promethues
1)编写配置清单
cat > redis-monitor.yaml <<EOF
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
  labels:
    app: redis-prometheus-exporter-servicemonitor
    deploy: redis-exporter-servicemonitor
  name: redis-monitor
  namespace: monitoring
spec:
  endpoints:
    - interval: 3s
      port: redis-exporter
  selector:
    matchLabels:
      k8s: redis-exporter-server-svc
  namespaceSelector:
    matchNames:
      - "default"
EOF
2)部署
[root@k8s-master1 exporter-redis]# kubectl apply -f redis-monitor.yaml

6.grafana展示

tongredis prometheus监控方案 prometheus监控接口_redis_03

监控Mysql

1.造环境

1)安装mariadb
[root@k8s-master1]# yum install mariadb\* -y
[root@k8s-master1]# systemctl enable --now mariadb
2)授权monitor用户
[root@k8s-master1]# mysql
MariaDB [(none)]> grant select,replication client,process ON *.* to 'mysql_monitor'@'%' identified by 'oldboy@123';
Query OK, 0 rows affected (0.00 sec)

PS:授权ip为localhost,因为不是prometheus服务器来直接找mariadb 获取数据,而是prometheus服务器找mysql_exporter,mysql_exporter 再找mariadb,所以这个localhost是指的mysql_exporter的IP

2.安装数据库exporter

1)编写部署清单
cat > mysql-deploy.yaml <<EOF
kind: Deployment
apiVersion: apps/v1
metadata:
  name: mysql-exporter-server
spec:
  selector:
    matchLabels:
      k8s: mysql-exporter-server
  template:
    metadata:
      labels:
        k8s: mysql-exporter-server
    spec:
      containers:
        - name: mysql
          image: prom/mysqld-exporter
          env:
            - name: DATA_SOURCE_NAME
              value: "mysql_monitor:oldboy@123@(mysql.default.svc.cluster.local:3306)"
          ports:
            - containerPort: 9104
EOF
2)编写Service清单
cat > mysql-svc.yaml <<EOF
kind: Service
apiVersion: v1
metadata:
  name: mysql-exporter-server
  labels:
    k8s: mysql-exporter-server-svc
spec:
  ports:
    - port: 9104
      targetPort: 9104
      name: mysql-exporter-port
      protocol: TCP
  selector:
    k8s: mysql-exporter-server
---
kind: ServiceMonitor
apiVersion: monitoring.coreos.com/v1
metadata:
  labels:
    app: mysql-prometheus-exporter-servicemonitor
    deploy: mysql-exporter-servicemonitor
  name: mysql-exporter-monitor
  namespace: monitoring
spec:
  endpoints:
    - interval: 3s
      port: mysql-exporter-port
  selector:
    matchLabels:
      k8s: mysql-exporter-server-svc
  namespaceSelector:
    matchNames:
      - "default"
EOF

3.部署服务

[root@k8s-master1 exporter-mysql]# kubectl apply -f mysql-deploy.yaml
[root@k8s-master1 exporter-mysql]# kubectl apply -f mysql-svc.yaml

监控exporter

  • 监控一个外部的exporter

1.造环境

1)下载部署exporter
#下载
[root@k8s-master1]# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

# 解压
[root@k8s-master1]# tar -xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /usr/local/

#查看
[root@k8s-master1]# ll /usr/localmysqld_exporter-0.12.1.linux-amd64
总用量 14484
-rw-r--r-- 1 3434 3434    11325 7月  29 2019 LICENSE
-rwxr-xr-x 1 3434 3434 14813452 7月  29 2019 mysqld_exporter
-rw-r--r-- 1 3434 3434       65 7月  29 2019 NOTICE
1)安装mariadb
[root@k8s-master1]# yum install mariadb\* -y
[root@k8s-master1]# systemctl enable --now mariadb
2)授权monitor用户
[root@k8s-master1]# mysql
MariaDB [(none)]> grant select,replication client,process ON *.* to 'mysql_monitor'@'%' identified by 'oldboy@123';
Query OK, 0 rows affected (0.00 sec)

PS:授权ip为localhost,因为不是prometheus服务器来直接找mariadb 获取数据,而是prometheus服务器找mysql_exporter,mysql_exporter 再找mariadb,所以这个localhost是指的mysql_exporter的IP

2.配置maridb

  • 创建一个mariadb配置文件,写上连接的用户名与密码(和上面的授权的用户名 和密码要对应)
[root@k8s-master1]# vim /usr/local/mysqld_exporter-0.12.1/.my.cnf
[client]
user=mysql_monitor
password=123

3.启动mysqld_exporter

[root@k8s-master1]# nohup /usr/local/mysqld_exporter-0.12.1/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter-0.12.1/.my.cnf &

PS:确认端口是否为 9104