这里拿mysql举例
在harbor主机下载个mysql镜像然后上传到harbor仓库。
然后创建svc deployment
root@k8s-master1:/apps/mysql# cat svc-deployument-mysql.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
spec:
type: ClusterIP
selector:
apps: mysql
ports:
- port: 3306
protocol: TCP
targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
spec:
replicas: 1
selector:
matchLabels:
apps: mysql
template:
metadata:
labels:
apps: mysql
spec:
containers:
- name: mysql
image: images.guoguo.com/apps/mysql:5.7.23
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "mysql"
测试一下是否能登录
root@k8s-master1:/apps/mysql# kubectl get svc mysql-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-svc ClusterIP 10.104.104.251 <none> 3306/TCP 89s
root@k8s-master1:/apps/mysql# telnet 10.104.104.251 3306
Trying 10.104.104.251...
Connected to 10.104.104.251.
Escape character is '^]'.
J
5.7.23TkgY0,RzYuz'\./V;mysql_native_password
#telnet测试一下 是否通
#进入数据库 创建监控用户
root@k8s-master1:/apps/mysql# mysql -uroot -h 10.104.104.251 -p'mysql'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monor. Commands end wh ; or \g.
Your MySQL connection id is 3
Server version: 5.7.23 MySQL Communy Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or s affiliates.
Oracle is a registered trademark of Oracle Corporation and/or s
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user 'exporter@%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
##创建用户
mysql> grant PROCESS,REPLICATION CLIENT,SELECT on *.* to 'exporter@%';
Query OK, 0 rows affected (0.00 sec)
#授权
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#刷新权限
mysql>
创建用户授权
create user 'exporter@%' identified by '123456';
grant PROCESS,REPLICATION CLIENT,SELECT on *.* to 'exporter@%';
mysql监控的用户有了 现在需要一个mysql 的pxports
去prometheus官方下载
https://prometheus.io/download/
如果不是k8s 运行的那么在这下载就行
这就是个二进制文件,在运行mysql服务器上运行这个进程就行了
因为我们是k8s运行的mysql 我们需要下载镜像
docker pull prom/mysqld-exporter:latest
提前下载这个镜像 并在harbor仓库创建项目
docker tag prom/mysqld-exporter:latest images.guoguo.com/prom/mysqld-exporter:latest
docker push images.guoguo.com/prom/mysqld-exporter:latest
#改名上传到仓库
root@k8s-master1:/apps/mysql# cat mysql-export.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-exporter
namespace: monoring
spec:
replicas: 1
selector:
matchLabels:
k8s-app: mysql-exporter
template:
metadata:
labels:
k8s-app: mysql-exporter
spec:
containers:
- name: mysql-exporter
image: images.guoguo.com/prom/mysqld-exporter:latest
env:
- name: DATA_SOURCE_NAME #变量名
value: "exporter:123456@(mysql-svc.default:3306)/" #用户和密码 格式是固定的 mysql-svc是 service名称 .default 是default名称空间下的 service名称 这个mysql 就是service名称 上面不是创建了个mysql-svc的service嘛 3306 是端口号 如果上面创建在别的名称空间里面 那么就把default 改成对应的ns
imagePullPolicy: IfNotPresent
ports:
- containerPort: 9104 #自己定义端口号 后面要通过这个端口号访问 接口
---
apiVersion: v1
kind: Service
metadata:
name: mysql-exporter
namespace: monoring
labels:
k8s-app: mysql-exporter
spec:
type: ClusterIP
selector:
k8s-app: mysql-exporter
ports:
- name: api
port: 9104 #这个端口号和上面那个关联
protocol: TCP
上面是要运行一个mysql的rxporter
就是个deployment
相当于我们从网上下载的exporter 运行成pod 运行到k8s里面来
查看一下pod
root@k8s-master1:/apps/mysql# kubectl get pods -n monitoring
NAME READY STATUS RESTARTS AGE
alertmanager-main-0 2/2 Running 0 5h
alertmanager-main-1 2/2 Running 0 5h
alertmanager-main-2 2/2 Running 0 5h
blackbox-exporter-55c457d5fb-h2jn6 3/3 Running 0 5h
grafana-6dd5b5f65-cxc5f 1/1 Running 0 5h
kube-state-metrics-9bf64fb88-kfc8h 3/3 Running 0 5h
mysql-exporter-7f6dd9487d-f7b9l 1/1 Running 0 2m28s
#这个就是
node-exporter-745dt 2/2 Running 0 5h
node-exporter-95sxn 2/2 Running 0 5h
node-exporter-j7tqq 2/2 Running 0 5h
node-exporter-znxs4 2/2 Running 0 5h
prometheus-adapter-59df95d9f5-cgs28 1/1 Running 0 5h
prometheus-adapter-59df95d9f5-r89kq 1/1 Running 0 5h
prometheus-k8s-0 2/2 Running 1 4h45m
prometheus-k8s-1 2/2 Running 1 4h45m
prometheus-operator-7775c66ccf-7zq2t 2/2 Running 0 5h1m
查看一下svc
root@k8s-master1:/apps/mysql# kubectl get svc -n monitoring
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
alertmanager-main ClusterIP 10.100.115.250 <none> 9093/TCP 5h2m
alertmanager-operated ClusterIP None <none> 9093/TCP,9094/TCP,9094/UDP 5h2m
blackbox-exporter ClusterIP 10.107.81.48 <none> 9115/TCP,19115/TCP 5h2m
grafana NodePort 10.102.231.22 <none> 3000:31922/TCP 5h2m
kube-state-metrics ClusterIP None <none> 8443/TCP,9443/TCP 5h2m
mysql-exporter ClusterIP 10.101.27.78 <none> 9104/TCP 4m15s
#这个就是
node-exporter ClusterIP None <none> 9100/TCP 5h2m
prometheus-adapter ClusterIP 10.100.91.102 <none> 443/TCP 5h2m
prometheus-k8s NodePort 10.106.149.216 <none> 9090:30610/TCP 5h2m
prometheus-operated ClusterIP None <none> 9090/TCP 5h2m
prometheus-operator ClusterIP None <none> 8443/TCP 5h3m
root@k8s-master1:/apps/mysql# curl http://10.101.27.78:9104/metrics -k
#测试查看
如果有输出 就说明配置正确了
现在我有了exporter 再创建个 service Monitor 就可以了
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: mysql-exporter
namespace: monitoring
labels:
k8s-app: mysql-exporter
namespace: monitoring
spec:
jobLabel: k8s-app
endpoints:
- port: api
interval: 30s
scheme: http
selector: #最主要的就是这个 这个要关联上 service 刚才创建的mysql-exporter的svc
matchLabels: # 这里面没有tls conf 因为接口是http的不需要证书
k8s-app: mysql-exporter
namespaceSelector:
matchNames:
- monitoring
创建
查看一下
root@k8s-master1:/apps/mysql# kubectl get servicemonitors.monitoring.coreos.com -n monitoring
NAME AGE
alertmanager 5h20m
blackbox-exporter 5h20m
coredns 5h20m
etcd 5h3m
grafana 5h20m
kube-apiserver 5h20m
kube-controller-manager 5h20m
kube-scheduler 5h20m
kube-state-metrics 5h20m
kubelet 5h20m
mysql-exporter 4m21s
# 这里刚才创建的
node-exporter 5h20m
prometheus-adapter 5h20m
prometheus-k8s 5h20m
prometheus-operator 5h20m
然后到prometheus提供的 ui界面看下
就有了
然后去搜mysql
https://grafana.com/grafana/dashboards/?plcmt=footer
得到链接或者 id 去粘贴
就得到了
这就相当于把集群外或者集群内的 非云原生的服务监控起来了
这算是集群之内的
如果是集群之外 需要创建endpoiont连接svc 导入进来
最主要的方式是去找响应的exporter,云原生的不需要找exporter
云原生默认给提供了metrics接口
只需要通过把集群外的映射到集群内来
集群内的有service的 都不需要映射 直接创建service monitor就可以了
非云原生的 就需要下载exporter镜像 利用exporter镜像运行成pod 然后指定service 然后去创建service monitor 去指定创建的exporter service就可以了 就可以直接通过 service monitor去找对应的 exporter 去找对应的监控项里面的数据