使用zabbix监控k8s
2020-05-18 本文已影响0人
阿乐_822e
- 集群环境:
[root@k8s-master0 ~]# kg node
NAME STATUS ROLES AGE VERSION
k8s-master0 Ready master 25d v1.17.0
k8s-master1 Ready master 25d v1.17.0
k8s-master2 Ready master 25d v1.17.0
k8s-node0 Ready <none> 25d v1.17.0
node1 Ready <none> 9d v1.17.0
说明:下文中用的一些命令别名,在https://www.jianshu.com/p/2f61a84bd739中有提到
- 部署MySQL
[root@k8s-master0 ~]# cd zabbix-setup/
[root@k8s-master0 ~]# kc create ns szatc-deploy
[root@k8s-master0 zabbix-setup]# cat mysql-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysql-user-pwd
namespace: szatc-deploy
data:
mysql-root-pwd: cGFzc3dvcmQ=
[root@k8s-master0 zabbix-setup]# cat mysql-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
namespace: szatc-deploy
data:
custom.cnf: |
[mysqld]
default_storage_engine=innodb
skip_external_locking
skip_host_cache
skip_name_resolve
default_authentication_plugin=mysql_native_password
[root@k8s-master0 zabbix-setup]# cat mysql-pv-pvc.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
namespace: szatc-deploy
labels:
type: nfs
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
nfs:
server: 172.20.10.175
path: "/data/mysql"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
namespace: szatc-deploy
spec:
#storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
[root@k8s-master0 zabbix-setup]# cat mysql-deployment.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: szatc-deploy
spec:
type: NodePort
ports:
- port: 3306
nodePort: 30006
protocol: TCP
targetPort: 3306
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: szatc-deploy
spec:
replicas: 1
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.7
name: mysql
imagePullPolicy: IfNotPresent
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-user-pwd
key: mysql-root-pwd
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-config
mountPath: /etc/mysql/conf.d/
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
- name: timezone
mountPath: /etc/localtime
volumes:
- name: mysql-config
configMap:
name: mysql-config
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
- 部署Zabbix后端
[root@k8s-master0 zabbix-setup]# cat zabbix-server-deploy.yaml
apiVersion: v1
kind: Service
metadata:
name: zabbix-server
namespace: szatc-deploy
spec:
type: NodePort
ports:
- port: 10051
nodePort: 30051
protocol: TCP
targetPort: 10051
selector:
app: zabbix-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zabbix-server
namespace: szatc-deploy
spec:
replicas: 1
selector:
matchLabels:
app: zabbix-server
template:
metadata:
labels:
app: zabbix-server
spec:
containers:
- name: zabbix-server
image: zabbix/zabbix-server-mysql
imagePullPolicy: IfNotPresent
ports:
- containerPort: 10051
name: server
protocol: TCP
readinessProbe:
tcpSocket:
port: server
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
tcpSocket:
port: server
initialDelaySeconds: 15
periodSeconds: 20
env:
- name: DB_SERVER_HOST
value: "mysql"
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-user-pwd
key: mysql-root-pwd
- name: MYSQL_USER
value: "zabbix"
- name: MYSQL_PASSWORD
value: "zabbix"
- name: MYSQL_DATABASE
value: "zabbix"
- name: ZBX_CACHESIZE
value: "1024M"
- name: ZBX_TRENDCACHESIZE
value: "1024M"
- name: ZBX_HISTORYCACHESIZE
value: "2048M"
- name: ZBX_HISTORYINDEXCACHESIZE
value: "1024M"
- name: ZBX_STARTTRAPPERS
value: "5"
- name: ZBX_STARTPREPROCESSORS
value: "10"
- name: ZBX_STARTDBSYNCERS
value: "10"
- name: DB_SERVER_PORT
value: "3306"
- name: zabbix-agent
image: zabbix/zabbix-agent
imagePullPolicy: IfNotPresent
ports:
- containerPort: 10050
name: zabbix-agent
env:
- name: ZBX_HOSTNAME
value: "Zabbix server"
- name: ZBX_SERVER_HOST
value: "127.0.0.1"
- name: ZBX_PASSIVE_ALLOW
value: "true"
- name: ZBX_STARTAGENTS
value: "3"
- name: ZBX_TIMEOUT
value: "10"
securityContext:
privileged: true
- 添加sa帐户
[root@k8s-master0 zabbix-setup]# cat zabbix-user-example.yml
apiVersion: v1
kind: ServiceAccount
metadata:
name: zabbix-user
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: zabbix-user
rules:
- apiGroups:
- ""
resources:
- pods
- services
- nodes
- namespaces
- apiservices
- componentstatuses
verbs:
- get
- list
- watch
- apiGroups:
- "apiregistration.k8s.io"
resources:
- apiservices
verbs:
- get
- list
- watch
- apiGroups:
- "apps"
resources:
- deployments
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: zabbix-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: zabbix-user
subjects:
- kind: ServiceAccount
name: zabbix-user
namespace: default
- 部署Zabbix前端
[root@k8s-master0 zabbix-setup]# cat zabbix-web.yaml
apiVersion: v1
kind: Service
metadata:
name: zabbix-web
namespace: szatc-deploy
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
nodePort: 30080
targetPort: 80
selector:
app: zabbix-web
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zabbix-web
namespace: szatc-deploy
spec:
replicas: 1
selector:
matchLabels:
app: zabbix-web
template:
metadata:
labels:
app: zabbix-web
spec:
containers:
- image: zabbix/zabbix-web-nginx-mysql
name: zabbix-web
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: web
protocol: TCP
env:
- name: DB_SERVER_HOST
value: "mysql"
- name: ZBX_SERVER_HOST
value: "zabbix-server"
- name: MYSQL_USER
value: "zabbix"
- name: MYSQL_PASSWORD
value: "zabbix"
- name: TZ
value: "Asia/Shanghai"
[root@k8s-master0 zabbix-setup]# kf .
-
在master01上安装Zabbix-agent
过程不赘述,安装完成后修改conf/Zabbix-agentd.conf文件中的server IP地址,并到web页面添加这台机器
26.jpg
- 导入并应用模板
参考https://github.com/sleepka/zabbix-kubernetes-monitoring页面,
在master01的Zabbix-agent安装目录下新建scripts文件夹,并新建k8s-stats.py脚本文件,再对其作一些修改。
- api_server = 'https://172.20.10.160:6443' # 这里改成自己的apiServer地址
- token 地址:
[root@k8s-master0 zabbix-setup]# kg secret
NAME TYPE DATA AGE
default-token-z7cft kubernetes.io/service-account-token 3 25d
zabbix-user-token-gpp4m kubernetes.io/service-account-token 3 6d5h
[root@k8s-master0 zabbix-setup]# kg secret zabbix-user-token-gpp4m -o yaml
# 复制下其中的token字段,再作下编码
[root@k8s-master0 zabbix-setup]# echo "ZXlK......==" |base64 -d
# 把编码后的字符作为k8s-stats.py中的token地址
# 还有一点要注意的是把Zabbix-agent安装的目录设为Zabbix用户所有,过程不赘述
# 复制k8s-zabbix-template.xml模板文件保存到本地,并导入到Zabbix中,再应用到master01机器 ...
-
总结:
1、有7大类自动发现规则
27.jpg
2、抓取的数据如下:
28.jpg
都是一些相对静态的数据,再考虑到无状态pod的漂移,感觉用 Zabbix来监控k8s还是有些鸡肋。