部署MySQL5.7
[root@harbor monitoring]# cat mysql.yaml
apiVersion: v1
kind: Namespace
metadata:
name: test
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: test
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
command: ["sh","-c","ulimit -n 1048576 && exec /entrypoint.sh mysqld"] # rocky9 k8s bug
resources:
requests:
cpu: 0.5
memory: 1Gi
limits:
cpu: 1
memory: 2Gi
env:
- name: MYSQL_ROOT_PASSWORD
value: root@mysql
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: test
spec:
type: ClusterIP
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql
[root@harbor monitoring]# kubectl get pod -n test -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-74cbf66797-zjfzg 1/1 Running 0 25s 10.243.58.231 k8s-node02 <none> <none>
[root@harbor monitoring]# kubectl get svc -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.98.117.58 <none> 3306/TCP 33s
k8s节点测试
[root@k8s-master01 ~]# dig -t a mysql.test.svc.cluster.local @10.96.0.10
; <<>> DiG 9.16.23-RH <<>> -t a mysql.test.svc.cluster.local @10.96.0.10
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 15651
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 5457e5a32fb90fcb (echoed)
;; QUESTION SECTION:
;mysql.test.svc.cluster.local. IN A
;; ANSWER SECTION:
mysql.test.svc.cluster.local. 5 IN A 10.98.117.58
;; Query time: 0 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Sat Aug 10 05:27:17 EDT 2024
;; MSG SIZE rcvd: 113
连接信息
host: mysql.test.svc.cluster.local
port: 3306
user: root
passwd: root@mysql
部署mysql-exporter
[root@harbor ~]# helm repo add prometheus-community https://prometheus-community.github.io/helm-charts
[root@harbor ~]# helm repo update
[root@harbor ~]# helm search repo prometheus-mysql-exporter
NAME CHART VERSION APP VERSION DESCRIPTION
prometheus-community/prometheus-mysql-exporter 2.6.1 v0.15.1 A Helm chart for prometheus mysql exporter with...
[root@harbor monitoring]# helm pull prometheus-community/prometheus-mysql-exporter
[root@harbor monitoring]# tar xf prometheus-mysql-exporter-2.6.1.tgz
[root@harbor monitoring]# cd prometheus-mysql-exporter
# 32行
serviceMonitor:
# enabled should be set to true to enable prometheus-operator discovery of this service
enabled: true # 开启
# interval is the interval at which metrics should be scraped
interval: 30s
# scrapeTimeout is the timeout after which the scrape is ended
scrapeTimeout: 10s
# namespace: monitoring
# namespaceSelector: []
# additionalLabels is the set of additional labels to add to the ServiceMonitor
additionalLabels: {}
jobLabel: ""
targetLabels: []
podTargetLabels: []
metricRelabelings: []
# Set relabel_configs as per https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config
relabelings: []
# Enable multi target scraping.
multipleTarget:
enabled: false
targets: []
# 168行
mysql:
db: ""
host: "mysql.test.svc.cluster.local" # 修改svc
# config my.cnf https://dev.mysql.com/doc/c-api/8.0/en/mysql-options.html
additionalConfig:
# - connect-timeout=5
# - debug
pass: "root@mysql" # 修改
port: 3306
protocol: ""
user: "root" # 修改
[root@harbor prometheus-mysql-exporter]# helm install prometheus-mysql-exporter ./ -f values.yaml -n monitoring
[root@harbor prometheus-mysql-exporter]# kubectl get pod -n monitoring -owide | grep mysql
prometheus-mysql-exporter-58fbff49b8-h2rcg 1/1 Running 0 12s 10.243.85.203 k8s-node01 <none> <none>
等待30秒
grafana 模板ID: 7362