k8s以Deployment方式部署zabbix。当前最新版本:5.2.4
namespace
mkdir -p /home/k8s/zabbix/{mysql,zabbix-server,zabbix-web}
cd /home/k8s/zabbix
vim namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: monitoring
pv
- 部署nfs:
选择一节点,
mkdir -p /data/mysql
yum -y install nfs-utils rpcbind
echo '/data/mysql 192.168.30.0/24(rw,sync,no_root_squash)' > /etc/exports
chmod -R 755 /data/mysql
exportfs -arv
systemctl enable rpcbind && systemctl start rpcbind
systemctl enable nfs && systemctl start nfs
nfs部署完毕。对于需要使用nfs的node节点,都要安装nfs:
yum -y install nfs-utils
- 创建pv:
cd /home/k8s/zabbix
vim nfs-mysql-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
labels:
pvname: nfs-mysql-pv
spec:
capacity:
storage: 20Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.30.129
path: /data/mysql
mysql
vim mysql/mysql.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: monitoring
labels:
app: mysql
spec:
selector:
app: mysql
ports:
- name: mysql
port: 3306
protocol: TCP
targetPort: 3306
clusterIP: None
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
namespace: monitoring
spec:
serviceName: mysql
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7.28
imagePullPolicy: IfNotPresent
args:
- "--character-set-server=utf8"
- "--collation-server=utf8_bin"
- "--default-authentication-plugin=mysql_native_password"
env:
- name: MYSQL_DATABASE
value: "zabbix"
- name: MYSQL_USER
value: "zabbix"
- name: MYSQL_PASSWORD
value: "zabbix"
- name: MYSQL_ROOT_PASSWORD
value: "zabbix"
ports:
- containerPort: 3306
name: mysql
protocol: TCP
resources:
requests:
cpu: 1000m
memory: 1000Mi
limits:
cpu: 2000m
memory: 2000Mi
volumeMounts:
- name: timezone
mountPath: /etc/localtime
- name: data
mountPath: /var/lib/mysql
terminationGracePeriodSeconds: 20
volumes:
- name: timezone
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
volumeClaimTemplates:
- metadata:
name: data
namespace: monitoring
spec:
selector:
matchLabels:
pvname: nfs-mysql-pv
accessModes: [ "ReadWriteMany" ]
resources:
requests:
storage: 20Gi
zabbix-server
vim zabbix-server/zabbix-server.yaml
apiVersion: v1
kind: Service
metadata:
name: zabbix-server
namespace: monitoring
labels:
app: zabbix-server
spec:
selector:
app: zabbix-server
ports:
- name: zabbix-server
port: 10051
nodePort: 30051
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zabbix-server
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app: zabbix-server
template:
metadata:
labels:
app: zabbix-server
spec:
containers:
- name: zabbix-server
image: zabbix/zabbix-server-mysql:latest
imagePullPolicy: IfNotPresent
env:
- name: DB_SERVER_HOST
value: mysql
- name: DB_SERVER_PORT
value: "3306"
- name: MYSQL_DATABASE
value: zabbix
- name: MYSQL_USER
value: zabbix
- name: MYSQL_PASSWORD
value: zabbix
- name: MYSQL_ROOT_PASSWORD
value: zabbix
- name: ZBX_CACHESIZE
value: "512M"
- name: ZBX_HISTORYCACHESIZE
value: "128M"
- name: ZBX_HISTORYINDEXCACHESIZE
value: "128M"
- name: ZBX_TRENDCACHESIZE
value: "128M"
- name: ZBX_VALUECACHESIZE
value: "256M"
- name: ZBX_TIMEOUT
value: "30"
ports:
- containerPort: 10051
name: zabbix-server
protocol: TCP
resources:
requests:
cpu: 1000m
memory: 1000Mi
limits:
cpu: 1000m
memory: 1000Mi
- name: zabbix-agent
image: zabbix/zabbix-agent:latest
imagePullPolicy: IfNotPresent
env:
- name: ZBX_HOSTNAME
value: "Zabbix server"
- name: ZBX_SERVER_HOST
value: "127.0.0.1"
- name: ZBX_STARTAGENTS
value: "3"
- name: ZBX_UNSAFEUSERPARAMETERS
value: "1"
- name: ZBX_TIMEOUT
value: "10"
ports:
- containerPort: 10050
name: zabbix-agent
protocol: TCP
resources:
requests:
cpu: 200m
memory: 200Mi
limits:
cpu: 200m
memory: 200Mi
nodeSelector: #固定zabbix server ip
node-role.kubernetes.io/master: ""
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
zabbix-web
vim zabbix-web/zabbix-web.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: zabbix-web
namespace: monitoring
spec:
rules:
- host: zabbix.lzxlinux.cn
http:
paths:
- path: /
backend:
serviceName: zabbix-web
servicePort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: zabbix-web
namespace: monitoring
labels:
app: zabbix-web
spec:
selector:
app: zabbix-web
ports:
- name: web
port: 8080
protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: zabbix-web
namespace: monitoring
spec:
replicas: 1
selector:
matchLabels:
app: zabbix-web
template:
metadata:
labels:
app: zabbix-web
spec:
containers:
- name: zabbix-web
image: zabbix/zabbix-web-nginx-mysql:latest
imagePullPolicy: IfNotPresent
env:
- name: DB_SERVER_HOST
value: mysql
- name: MYSQL_DATABASE
value: zabbix
- name: MYSQL_USER
value: zabbix
- name: MYSQL_PASSWORD
value: zabbix
- name: MYSQL_ROOT_PASSWORD
value: zabbix
- name: ZBX_SERVER_HOST
value: zabbix-server
- name: PHP_TZ
value: "Asia/Shanghai"
ports:
- containerPort: 8080
name: web
protocol: TCP
resources:
requests:
cpu: 500m
memory: 500Mi
limits:
cpu: 500m
memory: 500Mi
部署
tree .
.
├── mysql
│ └── mysql.yaml
├── namespace.yaml
├── nfs-mysql-pv.yaml
├── zabbix-server
│ └── zabbix-server.yaml
└── zabbix-web
└── zabbix-web.yaml
3 directories, 5 files
kubectl apply -f namespace.yaml
kubectl apply -f nfs-mysql-pv.yaml
kubectl apply -f mysql/
kubectl apply -f zabbix-server/
kubectl apply -f zabbix-web/
kubectl get all -n monitoring
NAME READY STATUS RESTARTS AGE
pod/mysql-0 1/1 Running 0 5m13s
pod/zabbix-server-98fdf455b-kjfpg 2/2 Running 0 4m2s
pod/zabbix-web-7c5485fcb9-mxhsm 1/1 Running 0 104s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql ClusterIP None <none> 3306/TCP 5m13s
service/zabbix-server NodePort 10.109.117.167 <none> 10051:30051/TCP 4m2s
service/zabbix-web ClusterIP 10.106.252.238 <none> 8080/TCP 104s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/zabbix-server 1/1 1 1 4m2s
deployment.apps/zabbix-web 1/1 1 1 104s
NAME DESIRED CURRENT READY AGE
replicaset.apps/zabbix-server-98fdf455b 1 1 1 4m2s
replicaset.apps/zabbix-web-7c5485fcb9 1 1 1 104s
NAME READY AGE
statefulset.apps/mysql 1/1 5m13s
添加hosts:zabbix.lzxlinux.cn
,账号/密码:Admin
/zabbix
。
Zabbix server 信息,
zabbix-agent
zabbix agent 节点启动docker,
mkdir -p /data/zabbix-agent && chmod -R 755 /data/zabbix-agent
cat > /data/zabbix-agent/zabbix_agentd.conf << EOF
LogType=console
Server=192.168.30.128 #k8s master ip,部署时删除注释
StartAgents=3
ServerActive=192.168.30.128:30051
Hostname=192.168.30.129 #k8s node ip,部署时删除注释
User=zabbix
UnsafeUserParameters=1
LoadModulePath=/var/lib/zabbix/modules/
EOF
docker run -d --name zabbix-agent \
-v /data/zabbix-agent:/etc/zabbix \
-p 10050:10050 zabbix/zabbix-agent:latest
查看日志报错:no active checks on server [192.168.30.128:30051]: host [192.168.30.129] not found
解决:zabbix web 界面添加host,hostname 为 192.168.30.129。
Zabbix agent 信息,
k8s部署zabbix完成。已存放至个人github:kubernetes