您现在的位置是: 网站首页 >Shell Shell
【zabbix、docker】Docker部署Zabbix5.0
admin2021年8月4日 22:29 【Docker | Linux | Zabbix 】 1529人已围观
# 【zabbix、docker】Docker部署Zabbix5.0 参考文档Docker部署:https://www.zabbix.com/documentation/5.0/zh/manual/installation/containers ## 部署脚本(全过程) ```bash #!/bin/bash # 脚本功能: install zabbix docker # 脚本环境:Debian10 # 1、安装Docker # 2、拉取镜像 # 3、启动服务 # 启动MySQL:docker-zabbix-mysql # 启动Zabbix Server:docker-zabbix-server # 启动Zabbix Web:docker-zabbix-nginx # 启动Zabbix Agent:docker-zabbix-agent # 4、脚本修正Web页面中文字体乱码 # 5、脚本配置Web页面选择中文、时区、Agent IP # 6、其他文件预准备到 /opt/docker 目录 # Docker安装包:docker-ce_18.06.3~ce~3-0~debian_amd64.deb # 微软雅黑字体:msyh.ttf ZABBIX_SERVER_TAG=alpine-latest ZABBIX_WEB_TAG=alpine-latest ZABBIX_AGENT_TAG=alpine-latest MYSQL_DOCKER_TAG=5.7 MYSQL_ROOT_PSWD="admin@password" MYSQL_ZABBIX_USER="zabbix" MYSQL_ZABBIX_PSWD="zabbix" WORK_PATH=/opt/docker mkdir -p $WORK_PATH cd $WORK_PATH function installDocker(){ docker info if [ $? -eq 1 ]; then if [ ! -f docker-ce_18.06.3~ce~3-0~debian_amd64.deb ];then wget https://download.docker.com/linux/debian/dists/buster/pool/stable/amd64/docker-ce_18.06.3~ce~3-0~debian_amd64.deb fi apt-get install -y libltdl7 dpkg -i docker-ce_18.06.3~ce~3-0~debian_amd64.deb tee /etc/docker/daemon.json << EOF { "registry-mirrors": [ "https://dockerhub.azk8s.cn", "https://hub-mirror.c.163.com", "https://docker.mirrors.ustc.edu.cn" ] } EOF systemctl daemon-reload systemctl restart docker fi } function pullImages(){ echo echo echo "===================> 拉取镜像" docker pull mysql:$MYSQL_DOCKER_TAG # MySQL 数据库支持的 Zabbix server - zabbix/zabbix-server-mysql:https://hub.docker.com/r/zabbix/zabbix-server-mysql/ docker pull zabbix/zabbix-server-mysql:$ZABBIX_SERVER_TAG # 基于 Nginx Web 服务器以及支持 MySQL 数据库的 Zabbix web 接口 - zabbix/zabbix-web-nginx-mysql:https://hub.docker.com/r/zabbix/zabbix-web-nginx-mysql/ docker pull zabbix/zabbix-web-nginx-mysql:$ZABBIX_WEB_TAG # Zabbix 代理部署在监控目标上,以主动监控本地资源和应用程序(硬盘、内存、处理器统计信息等)。 docker pull zabbix/zabbix-agent:$ZABBIX_AGENT_TAG } function runZabbixDocker(){ echo echo echo "===================> 创建专用于Zabbix组件容器的网络" docker network create --subnet 172.20.0.0/16 --ip-range 172.20.240.0/20 zabbix-net # 官方示例,每个容器启动添加了:-e MYSQL_ROOT_PASSWORD="$MYSQL_ROOT_PSWD" \ echo echo echo "===================> 启动 mysql" # -p 33006:3306 如果不外网访问,不用映射端口,在zabbix-net网络中,直接可以通过3306访问 docker ps | grep docker-zabbix-mysql if [ $? -eq 1 ]; then docker run --name docker-zabbix-mysql -t \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="$MYSQL_ZABBIX_USER" \ -e MYSQL_PASSWORD="$MYSQL_ZABBIX_PSWD" \ -e MYSQL_ROOT_PASSWORD="$MYSQL_ROOT_PSWD" \ -v /data/docker/zabbix/mysql/logs:/var/log/mysql \ -v /data/docker/zabbix/mysql/data:/var/lib/mysql \ --network=zabbix-net \ -p 33006:3306 \ --restart=always \ -d mysql:$MYSQL_DOCKER_TAG \ --character-set-server=utf8 \ --collation-server=utf8_bin \ --default-authentication-plugin=mysql_native_password fi echo echo echo "===================> 启动 Zabbix Server(基于mysql数据库)" # Zabbix服务器实例向主机公开10051 / TCP端口(Zabbix trapper) --link docker-zabbix-mysql:mysql \ docker ps | grep docker-zabbix-server if [ $? -eq 1 ]; then docker run --name docker-zabbix-server -t \ -e DB_SERVER_HOST="docker-zabbix-mysql" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="$MYSQL_ZABBIX_USER" \ -e MYSQL_PASSWORD="$MYSQL_ZABBIX_PSWD" \ -e MYSQL_ROOT_PASSWORD="$MYSQL_ROOT_PSWD" \ -e DB_SERVER_PORT="3306" \ -v /etc/localtime:/etc/localtime \ -v /etc/timezone:/etc/timezone \ -v /data/docker/zabbix/alertscripts:/usr/lib/zabbix/alertscripts \ -v /data/docker/zabbix/externalscripts:/usr/lib/zabbix/externalscripts \ --network=zabbix-net \ -p 10051:10051 \ --restart unless-stopped \ -d zabbix/zabbix-server-mysql:$ZABBIX_SERVER_TAG docker logs docker-zabbix-server sleep 3 # 镜像中安装curl工具 docker exec -it --user root docker-zabbix-server apk add curl fi echo echo echo "===================> 启动Zabbix Web界面(基于mysql数据库)" # 并将实例与创建的MySQL服务器和Zabbix server实例链接,Zabbix Web界面实例向主机公开80 / TCP端口(HTTP) docker ps | grep docker-zabbix-nginx if [ $? -eq 1 ]; then docker run --name docker-zabbix-nginx -t \ -e ZBX_SERVER_HOST="docker-zabbix-server" \ -e DB_SERVER_HOST="docker-zabbix-mysql" \ -e MYSQL_DATABASE="zabbix" \ -e MYSQL_USER="$MYSQL_ZABBIX_USER" \ -e MYSQL_PASSWORD="$MYSQL_ZABBIX_PSWD" \ -e DB_SERVER_PORT=3306 \ -e TZ="Asia/Shanghai" \ --network=zabbix-net \ -p 81:8080 \ --restart unless-stopped \ -d zabbix/zabbix-web-nginx-mysql:$ZABBIX_WEB_TAG docker logs docker-zabbix-nginx fi echo echo echo "===================> 启动Zabbix Agent" docker ps | grep docker-zabbix-agent if [ $? -eq 1 ]; then docker run --name docker-zabbix-agent -t \ -e ZBX_HOSTNAME="docker-zabbix-agent" \ -e ZBX_SERVER_HOST="docker-zabbix-server" \ --network=zabbix-net \ --restart unless-stopped \ -d zabbix/zabbix-agent:$ZABBIX_AGENT_TAG docker logs docker-zabbix-agent fi } # 容器内字体乱码修正 function repairGarbled(){ if [ ! -f "$WORK_PATH/msyh.ttf" ];then echo "指定路径下需要放置 $WORK_PATH/msyh.ttf 微软雅黑字体文件,需手动上传替换" return 1 fi # 进 C:\Windows\Fonts 找到想使用的字体,例如这儿的 C:\Windows\Fonts\微软雅黑 docker cp $WORK_PATH/msyh.ttf docker-zabbix-nginx:/usr/share/zabbix/assets/fonts/ # 需要以root的身份进入容器 # docker exec -it --user root docker-zabbix-nginx sh # 方法一,修改字体调用: # cat /usr/share/zabbix/include/defines.inc.php | grep DejaVuSans # define('ZBX_GRAPH_FONT_NAME', 'DejaVuSans'); // font file name # define('ZBX_FONT_NAME', 'DejaVuSans'); # sed -i "s/DejaVuSans/msyh/" /usr/share/zabbix/include/defines.inc.php # cat /usr/share/zabbix/include/defines.inc.php | grep msyh # define('ZBX_GRAPH_FONT_NAME', 'msyh'); // font file name # define('ZBX_FONT_NAME', 'msyh'); # 方法二,修改字体链接 # cd /usr/share/zabbix/assets/fonts/ # 备份默认字库文件 # mv DejaVuSans.ttf DejaVuSans.ttf.bak # 创建字体软连接 # ln -s msyh.ttf DejaVuSans.ttf echo echo echo "===================> 容器内Zabbix Web 字体乱码修正完成" # 直接进容器执行脚本 docker exec -it --user root docker-zabbix-nginx sh -c "cd /usr/share/zabbix/assets/fonts/ && if [ ! -f 'DejaVuSans.ttf.bak' ]; then mv DejaVuSans.ttf DejaVuSans.ttf.bak; fi && ln -s msyh.ttf DejaVuSans.ttf" } # 配置Zabbix中文、时区、Agent IP function configZabbixWeb(){ echo "===================> 等待其他容器启动完成,1分钟" sleep 1m echo echo echo "===================> 在Docker中的mysql指定zabbix个性化设置" AGENT_IP=`docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker-zabbix-agent` echo $AGENT_IP docker exec -it docker-zabbix-mysql mysql -uzabbix -pzabbix -e \ "select default_lang from zabbix.config; \ select default_timezone from zabbix.config; \ SELECT hostid FROM zabbix.hosts WHERE host='Zabbix server'; \ SELECT hostid,main,ip FROM zabbix.interface WHERE ip='127.0.0.1'; \ UPDATE zabbix.config SET default_lang='zh_CN' WHERE configid='1'; \ UPDATE zabbix.config SET default_timezone='Asia/Shanghai' WHERE configid='1'; \ UPDATE zabbix.interface SET main=1,ip='$AGENT_IP' WHERE interfaceid='1' AND ip='127.0.0.1';" echo echo echo "===================> 修改完成后结果" docker exec -it docker-zabbix-mysql mysql -uzabbix -pzabbix -e \ "select default_lang from zabbix.config; \ select default_timezone from zabbix.config; \ SELECT hostid,main,ip FROM zabbix.interface WHERE ip='$AGENT_IP';" } removeAllZabbix(){ docker stop docker-zabbix-agent && docker rm docker-zabbix-agent docker stop docker-zabbix-server && docker rm docker-zabbix-server docker stop docker-zabbix-nginx && docker rm docker-zabbix-nginx docker stop docker-zabbix-mysql && docker rm docker-zabbix-mysql # 完全清除mysql数据 # rm -rf /data/docker/zabbix/mysql/ } # 删除已运行的容器 # removeAllZabbix # 安装Docker installDocker # 拉取镜像 pullImages # 启动Zabbix服务 runZabbixDocker # 修复中文乱码问题 repairGarbled # 配置Zabbix页面 configZabbixWeb echo "配置完成,登录帐密:Admin:zabbix" ``` ## 过程问题记录 ### 处理Zabbix agent is not available(脚本已处理) 出现`Zabbix agent is not available (for 3m)`告警 ![BLOG_20210804_223100_58](/media/blog/images/2021/08/BLOG_20210804_223100_58.png "博客图集BLOG_20210804_223100_58.png") ![BLOG_20210804_223053_24](/media/blog/images/2021/08/BLOG_20210804_223053_24.png "博客图集BLOG_20210804_223053_24.png") 是因为容器部署,IP没设置正确 - 查看容器IP方法1: ```bash docker inspect docker-zabbix-agent | grep IPAddress # "SecondaryIPAddresses": null, # "IPAddress": "", # "IPAddress": "172.20.240.4", ``` 将容器实际的IP设置进去即可。 ![BLOG_20210804_223043_49](/media/blog/images/2021/08/BLOG_20210804_223043_49.png "博客图集BLOG_20210804_223043_49.png") - 查看容器IP方法2: ![BLOG_20210804_223036_74](/media/blog/images/2021/08/BLOG_20210804_223036_74.png "博客图集BLOG_20210804_223036_74.png") ```bash docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' docker-zabbix-agent # 172.20.240.4 ``` ### 修改MySQL个性化Zabbix设置(脚本已处理) ```bash docker exec -it mysql-server mysql -uzabbix -pzabbix mysql> select default_lang from zabbix.config; # +--------------+ # | default_lang | # +--------------+ # | en_GB | # +--------------+ # 1 row in set (0.00 sec) mysql> select default_timezone from zabbix.config; # +------------------+ # | default_timezone | # +------------------+ # | system | # +------------------+ # 1 row in set (0.00 sec) # 修改页面中文 mysql> UPDATE zabbix.config SET default_lang='zh_CN' WHERE configid='1'; # 修改时区 mysql> UPDATE zabbix.config SET default_timezone='Asia/Shanghai' WHERE configid='1'; # 修改Agent IP mysql> SELECT hostid FROM zabbix.hosts WHERE host='Zabbix server'; mysql> SELECT hostid,main,ip FROM zabbix.interface WHERE ip='127.0.0.1'; mysql> UPDATE zabbix.interface SET main=1,ip='172.20.240.4' WHERE interfaceid='1' AND ip='127.0.0.1'; ``` # 客户端Zabbix Agent安装 ## Debian9 https://www.zabbix.com/cn/download?zabbix=5.0&os_distribution=debian&os_version=9_stretch&db=mysql ```bash # 安装zabbix源(Debian9) wget https://repo.zabbix.com/zabbix/5.0/debian/pool/main/z/zabbix-release/zabbix-release_5.0-1+stretch_all.deb # Debian1-:https://repo.zabbix.com/zabbix/5.0/debian/pool/main/z/zabbix-release/zabbix-release_5.0-1+buster_all.deb # Ubuntu18.05:https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+bionic_all.deb # Ubuntu20.04:https://repo.zabbix.com/zabbix/5.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_5.0-1+focal_all.deb dpkg -i zabbix-release_5.0-1+stretch_all.deb apt update # 安装zabbix apt install zabbix-agent -y # 安装配置文件修改工具 apt install crudini -y crudini --get /etc/zabbix/zabbix_agentd.conf "" # pidfile # logfile # logfilesize # server # serveractive # hostname # include # zabbix server IP crudini --set /etc/zabbix/zabbix_agentd.conf "" server 192.168.96.20 # # zabbix server IP crudini --set /etc/zabbix/zabbix_agentd.conf "" serveractive 192.168.96.20 # 必需与server端agent配置的hostname保持一致 crudini --set /etc/zabbix/zabbix_agentd.conf "" hostname "PXE Image Server99" # 启动zabbix agent进程,并设置开机启动 systemctl restart zabbix-agent systemctl enable zabbix-agent ```
很赞哦! (4)
相关文章
文章交流
- emoji