Kubernetes deploying Zabbix

Zabbix Introduction [1]

ZabbixIt was developed by Alexei Vladishev of a network monitoring and management system, based on Server-Client architecture. It may be used to monitor a variety of network services, network servers, and other state machines.

ZabbixUse MySQL, PostgreSQL, SQLite, Oracle or IBM DB2 data store. Server-side C-based, Web-based PHP front-end is produced. Zabbix can be used to monitor a variety of ways. Simple Check may only need to install Client-side, may be based on various agreements made to monitor whether the SMTP or HTTP. The various states of the client UNIX, Windows, after installation Zabbix Agent, may monitor CPU load, network usage, capacity of a hard disk and the like. And even if Agent is not installed in the monitoring target, Zabbix can be via SNMP, TCP, ICMP checks and monitors the use of the target IPMI, SSH, telnet. Further, Zabbix comprising XMPP other Item warning function.

Zabbix features and functions [2]

  • Simple installation and configuration
  • Visual web management interface
  • Free Open Source
  • Support Chinese
  • Automatic discovery
  • Distributed Monitoring
  • Live Plot

surroundings

  • Kubernetes version 1.15.6
  • Zabbix version 3.4.7 (image, based on the official modifications, specific description will hereinafter)
  • Mariadb version 10.3.5

Zabbix Dockerfile 修改

zabbix-server-mysql: Dockerfile official basis to modify, add python支持, for 支持python通知脚本环境; time zone changed to 上海时区;

FROM zabbix/zabbix-server-mysql:alpine-3.4.7

RUN cp /etc/apk/repositories /etc/apk/repositories.bak \
  && echo "http://mirrors.aliyun.com/alpine/v3.4/main/" > /etc/apk/repositories \
  && apk add --update python python-dev py-pip build-base \
  && apk add -U tzdata \
  && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  && pip install requests configparser \
  && touch /tmp/zabbix_dingding.log \
  && chown zabbix:zabbix /tmp/zabbix_dingding.log \
  && rm -rf /var/cache/apk/*

WORKDIR /var/lib/zabbix

EXPOSE 10051/TCP

VOLUME ["/usr/lib/zabbix/alertscripts", "/usr/lib/zabbix/externalscripts", "/var/lib/zabbix/enc", "/var/lib/zabbix/mibs", "/var/lib/zabbix/modules"]
VOLUME ["/var/lib/zabbix/snmptraps", "/var/lib/zabbix/ssh_keys", "/var/lib/zabbix/ssl/certs", "/var/lib/zabbix/ssl/keys", "/var/lib/zabbix/ssl/ssl_ca"]

ENTRYPOINT ["docker-entrypoint.sh"]

zabbix-web-nginx-mysql: Dockerfile official basis to modify, add Chinese font to solve view web监控时中文乱码; time zone changed to 上海时区;

msyh.ttf Font, can be obtained from the following has to lay a mirror.

FROM zabbix/zabbix-web-nginx-mysql:alpine-3.4.7

COPY msyh.ttf /usr/share/fonts/ttf-dejavu/DejaVuSans.ttf

RUN cp /etc/apk/repositories /etc/apk/repositories.bak \
  && echo "http://mirrors.aliyun.com/alpine/v3.4/main/" > /etc/apk/repositories \
  && apk add -U tzdata \
  && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  && rm -rf /var/cache/apk/*

EXPOSE 80/TCP 443/TCP
WORKDIR /usr/share/zabbix
VOLUME ["/etc/ssl/nginx"]

ENTRYPOINT ["docker-entrypoint.sh"]

Zabbix K8S deployment

First deployment Mariadb

PS:NFS 提供存储

$ vim mariadb-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mariadb-pv
  namespace: kube-system
spec:
  capacity:
    storage: 100Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /nfs-data/mariadb_db_data
    server: 192.16.3.6

$ vim mariadb-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mariadb-pvc
  namespace: kube-system
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Gi

$ vim mariadb-deploy.yaml

apiVersion: v1
kind: Service
metadata:
  name: mariadb-server
  namespace: kube-system
  labels:
    name: mariadb-server
spec:
  ports:
  - port: 3306
    targetPort: 3306
    protocol: TCP
  selector:
    name: mariadb-server

---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mariadb-server
  namespace: kube-system
  labels:
    name: mariadb-server
spec:
  replicas: 1
  revisionHistoryLimit: 3
  strategy:
    rollingUpdate:
      maxSurge: 30%
      maxUnavailable: 30%
  template:
    metadata:
      labels:
        name: mariadb-server
    spec:
      volumes:
        - name: mariadb-storage
          persistentVolumeClaim:
            claimName: mariadb-pvc
      hostname: mariadb-server
      containers:
      - name: mariadb-server
        image: yangpeng2468/mariadb:10.3.5
        resources:
         limits:
           cpu: 400m
           memory: 1024Mi
         requests:
           cpu: 100m
           memory: 100Mi
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "Password"
        volumeMounts:
          - name: mariadb-storage
            mountPath: /var/lib/mysql

# 部署 Mariadb

$ kubectl apply -f mariadb-pv.yaml
$ kubectl apply -f mariadb-pvc.yaml
$ kubectl apply -f mariadb-deploy.yaml

Configmap notice nailed script deployment

$ vim zabbix-dingding-conf-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: zabbix-dingding-conf
  namespace: kube-system
data:
  dingding.conf: |
    [config]
    #此文件注意权限
    log=/tmp/zabbix_dingding.log
    webhook=https://oapi.dingtalk.com/robot/send?access_token=${钉钉机器人token}

$ vim zabbix-dingding-script-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: zabbix-dingding-script
  namespace: kube-system
data:
  zabbix_dingding.py: |
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import requests
    import json
    import sys
    import time
    import configparser

    Headers = {'Content-Type': 'application/json'}
    Time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    config = configparser.ConfigParser()
    config.read('/usr/lib/zabbix/externalscripts/dingding.conf')
    # config.read('/etc/zabbix/dingding.conf')

    log_file = config.get('config', 'log')
    api_url = config.get('config', 'webhook')


    def log(info):
        #注意权限,否则写不进去日志
        with open(log_file, 'a ') as infile:
            infile.write(info)

    def msg(text,user):
        json_text = {
         "msgtype": "text",
            "text": {
                "content": text
            },
            "at": {
                "atMobiles": [
                    user
                ],
                "isAtAll": False
            }
        }

        r = requests.post(api_url, data=json.dumps(json_text), headers=Headers).json()
        code = r["errcode"]
        if code == 0:
            log(Time   ":消息发送成功 返回码:"   str(code)   "\n")
        else:
            log(Time   ":消息发送失败 返回码:"   str(code)   "\n")
            exit(3)

    if __name__ == '__main__':
        text = sys.argv[3]
        user = sys.argv[1]
        msg(text, user)

# 部署

$ kubectl apply -f zabbix-dingding-conf-configmap.yaml zabbix-dingding-script-configmap.yaml

Zabbix-server deployment

$ vim zabbix-server-deploy.yaml

apiVersion: v1
kind: Service
metadata:
  name: zabbix-server
  namespace: kube-system
  labels:
    app: zabbix-server
spec:
  type: NodePort
  ports:
  - port: 10051
    targetPort: 10051
    nodePort: 30017
    protocol: TCP
  selector:
    app: zabbix-server

---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: zabbix-server
  namespace: kube-system
  labels:
    app: zabbix-server
spec:
  replicas: 1
  revisionHistoryLimit: 3
  strategy:
    rollingUpdate:
      maxSurge: 30%
      maxUnavailable: 30%
  template:
    metadata:
      labels:
        app: zabbix-server
    spec:
      hostname: zabbix-server
      volumes:
        - name: zabbix-dingding-script
          configMap:
            name: zabbix-dingding-script
            defaultMode: 0775
        - name: zabbix-dingding-conf
          configMap:
            name: zabbix-dingding-conf
            defaultMode: 0664
      containers:
      - name: zabbix-server
        image: yangpeng2468/zabbix-server-mysql:3.4.7
        imagePullPolicy: IfNotPresent
        resources:
         limits:
           cpu: 400m
           memory: 1024Mi
         requests:
           cpu: 100m
           memory: 100Mi
        ports:
        - containerPort: 10051
        env:
        - name: DB_SERVER_HOST
          value: "mariadb-server"
        - name: MYSQL_USER
          value: "zabbix"
        - name: MYSQL_PASSWORD
          value: "zabbix"
        - name: MYSQL_DATABASE
          value: "zabbix"
        - name: ZBX_CACHESIZE
          value: "1024M"
        - name: TZ
          value: "Asia/Shanghai"
        volumeMounts:
          - name: zabbix-dingding-script
            mountPath: /usr/lib/zabbix/alertscripts
          - name: zabbix-dingding-conf
            mountPath: /usr/lib/zabbix/externalscripts

# 部署

$ kubectl apply -f zabbix-server-deploy.yaml

Zabbix-web deployment

$ vim zabbix-web-deploy.yaml

apiVersion: v1
kind: Service
metadata:
  name: zabbix-web
  namespace: kube-system
  labels:
    app: zabbix-web
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: zabbix-web

---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: zabbix-web
  namespace: kube-system
  labels:
    app: zabbix-web
spec:
  replicas: 1
  revisionHistoryLimit: 3
  strategy:
    rollingUpdate:
      maxSurge: 30%
      maxUnavailable: 30%
  template:
    metadata:
      labels:
        app: zabbix-web
    spec:
      hostname: zabbix-web
      containers:
      - name: zabbix-web
        image: yangpeng2468/zabbix-web-nginx-mysql:3.4.7
        imagePullPolicy: IfNotPresent
        resources:
         limits:
           cpu: 300m
           memory: 600Mi
         requests:
           cpu: 100m
           memory: 100Mi
        ports:
        - containerPort: 80
        env:
        - name: DB_SERVER_HOST
          value: "mariadb-server"
        - name: ZBX_SERVER_HOST
          value: "zabbix-server"
        - name: MYSQL_USER
          value: "zabbix"
        - name: MYSQL_PASSWORD
          value: "zabbix"
        - name: TZ
          value: "Asia/Shanghai"
        - name: PHP_TZ
          value: "Asia/Shanghai"

# 部署

$ kubectl apply -f zabbix-web-deploy.yaml

Deployment zabbix-agent

zabbix-agent not go into detail here, if Docker or k8s deployment, you can use the official mirror zabbix/zabbix-agent:alpine-3.4.7. The official also directly download the installation package, deployed on the host, where according to their actual need to deploy client.

Zabbix Dashboard

After the successful deployment of the above, according to their actual environment, set the external network access k8s cluster entrance, Zabbix Dashboard display as follows:

Reference links

  • [1]https://zh.wikipedia.org/wiki/Zabbix
  • [2]https://yq.aliyun.com/articles/525926

This article from the YP station released!

Published 31 original articles · won praise 11 · views 1403

Guess you like

Origin blog.csdn.net/qq_24794401/article/details/104182586