基于docker部署zabbix

基础环境

cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core)

docker安装

配置yum源

# vim /etc/yum.repos.d/docker-ce.repo
[docker-ce-stable]
name=Docker CE Stable - $basearch
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

安装docker-ce

# wget https://mirrors.aliyun.com/centos-vault/7.3.1611/extras/x86_64/Packages/container-selinux-2.9-4.el7.noarch.rpm
# yum localinstall container-selinux-2.9-4.el7.noarch.rpm -y
# yum install docker-ce -y

更换源

顺便把docker源也换一下吧,免得一会还的重启

# vim /etc/docker/daemon.json 
{
  "registry-mirrors": ["https://registry.docker-cn.com"]
}

启动docker,查看版本号

# systemctl start docker.service

# docker version
Client:
Version: 18.09.0
API version: 1.39
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:48:22 2018
OS/Arch: linux/amd64
Experimental: false

Server: Docker Engine - Community
Engine:
Version: 18.09.0
API version: 1.39 (minimum version 1.12)
Go version: go1.10.4
Git commit: 4d60db4
Built: Wed Nov 7 00:19:08 2018
OS/Arch: linux/amd64
Experimental: false

docker安装完毕!

开启网络转换

#echo 1 > /proc/sys/net/ipv4/ip_forward   #网络重启会后要重新开启

用Dockerfile部署

zabbix-server部署阶段

要用dockerzabbix-server,需要以下几个组件

组件名称作用
数据库 MySQL或是PostgreSQL
Zabbix Java gateway Java管理扩展,可以不添加
Zabbix server zabbix 服务端
zabbix-web 与MySQL服务器实例和Zabbix server实例关联
zabbix-agent zabbix客户端

MySQL数据库

数据库我选择MySQL,还是得写Dockerfile,改一下时区,时间不对很麻烦,字符集也改一下吧

# mkdir /data/docker/mysql -p
/data/docker/mysql]# vim Dockerfile
FROM mysql:5.7
ENV LANG en_US.utf8
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo Asia/Shanghai > /etc/timezone \

构建镜像

 #docker build -t debian-mysql5.7 .
 # docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
debian-mysql5.7     latest              3d3c7dc9608c        14 seconds ago      372MB
mysql               5.7                 563a026a1511        3 weeks ago         372MB

启动容器

# docker run --hostname mysql --name debian-mysql-5.7 -t \
 -e MYSQL_USER="zabbix" \
 -e MYSQL_DATABASE="zabbix" \
 -e MYSQL_PASSWORD="passwd" \
 -e MYSQL_ROOT_PASSWORD="passwd" \
 -v /data/mysql:/var/lib/mysql:rw \
 -d debian-mysql5.7
9a98a2ba0896a8a146d515764d2d8eef4bc32a9f0dbea445c360fab5668a5cfe
# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
9a98a2ba0896        debian-mysql5.7     "docker-entrypoint.s…

变量说明

变量 
MYSQL_USER 启动时要添加的用户
MYSQL_DATABASE 启动时要创建的数据库
MYSQL_PASSWORD 指定添加用的的密码
MYSQL_ROOT_PASSWORD root用户的密码

Zabbix Java gateway

这个看个人,如果不监控java应用服务器可以不安装这个,还是得写Dockerfile,改一下时区和编码。

/data/docker/java-gateway]# cat Dockerfile 
FROM zabbix/zabbix-java-gateway:latest
ENV LANG en_US.utf8
RUN apk add -U tzdata
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo Asia/Shanghai > /etc/timezone \
 /data/docker/java-gateway]# docker build -t zabbix-java-gateway .
 /data/docker/java-gateway]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED             SIZE
zabbix-java-gateway          latest              7948dffa9af0        35 seconds ago      80.1MB

启动容器

需要映射一下10052端口

# docker run --hostname zabbix-java-gateway \
 -name zabbix-java-gateway -t \
 -p 10052:10052 \
 -d zabbix-java-gateway 
5e2434e7a4c6f3f947e6408ff5a90097e802c6a36282c36ce7e4cd5819474f7a
# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                      NAMES
5e2434e7a4c6        zabbix-java-gateway   "docker-entrypoint.sh"   9 seconds ago       Up 7 seconds        0.0.0.0:10052->10052/tcp   zabbix-java-gateway
9a98a2ba0896        debian-mysql5.7       "docker-entrypoint.s…"   33 minutes ago      Up 33 minutes       3306/tcp, 33060/tcp        debian-mysql-5.7

Zabbix server 

编写Dockerfile

 /data/docker/zabbix-server]# cat Dockerfile 
FROM zabbix/zabbix-server-mysql:latest
ENV LANG en_US.utf8
RUN apk add -U tzdata
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo Asia/Shanghai > /etc/timezone \
/data/docker/zabbix-server]# docker build -t zabbix-server .
/data/docker/zabbix-server]# docker images
REPOSITORY                   TAG                 IMAGE ID            CREATED              SIZE
zabbix-server                latest              531aa3116f61        About a minute ago   62.3MB

需要映射10051端口,指定数据库服务器,连接MySQLzabbix-java-gateway容器

10051:10051 \
--hostname zabbix-server \
-e DB_SERVER_HOST="debian-mysql-5.7" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="passwd" \
-e MYSQL_ROOT_PASSWORD="passwd" \
-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
--link debian-mysql-5.7:mysql \
--link zabbix-java-gateway:zabbix-java-gateway \
-d zabbix-server
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a45ef352cc68 zabbix-server "docker-entrypoint.sh" 9 minutes ago Up 8 minutes 0.0.0.0:10051->10051/tcp zabbix-server

zabbix-web

编写Dockerfile

/data/docker/zabbix-web]# cat Dockerfile 
FROM zabbix/zabbix-web-nginx-mysql:latest
ENV LANG en_US.utf8
RUN apk add -U tzdata
RUN ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo Asia/Shanghai > /etc/timezone \
 /data/docker/zabbix-web]# docker build -t zabbix-web .
 /data/docker/zabbix-web]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED              SIZE
zabbix-web                      latest              6ff51e2f0545        About a minute ago   164MB

启动容器

 需要映射80端口到宿主机,连接数据库,连接zabbix-server容器

# docker run --name zabbix-web -t \
 -p 80:80 \
 --hostname zabbix-web \
 -e PHP_TZ="Asia/Shanghai" \
 -e DB_SERVER_HOST="debian-mysql-5.7" \
 -e MYSQL_DATABASE="zabbix" \
 -e MYSQL_USER="zabbix" \
 -e MYSQL_PASSWORD="passwd" \
 -e MYSQL_ROOT_PASSWORD="passwd" \
 --link debian-mysql-5.7:mysql \
 --link zabbix-server:zabbix-server \
 -d zabbix-web
f0c33f227992ce220730da9454df2f354565ed0b3d0813513a87797c2b752702
# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                         NAMES
f0c33f227992        zabbix-web            "docker-entrypoint.sh"   13 seconds ago      Up 10 seconds       0.0.0.0:80->80/tcp, 443/tcp   zabbix-web

以上方式安装的是最新版本,因为在指定镜像的时候没有指定版本号,默认的就是laster,也就是最新版本,建议zabbix-agent版本要和服务端一致。

看下安装的容器

# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS                         NAMES
b4b2524953b4        zabbix-web            "docker-entrypoint.sh"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 443/tcp   zabbix-web
d5a45e58b69d        zabbix-server         "docker-entrypoint.sh"   2 hours ago         Up 2 hours          0.0.0.0:10051->10051/tcp      zabbix-server
cd16d62b98c4        zabbix-java-gateway   "docker-entrypoint.sh"   2 hours ago         Up 2 hours          0.0.0.0:10052->10052/tcp      zabbix-java-gateway
6a4b9ce12398        debian-mysql5.7       "docker-entrypoint.s…"   2 hours ago         Up 2 hours          3306
/tcp, 33060/tcp debian-mysql-5.

访问:http://宿主机IP:80

zabbix-agent

安装(用官方yum源)

#rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
#yum -y install zabbix-agent-4.0.1
#vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.130.6
UnsafeUserParameters=1
Include=/etc/zabbix/zabbix_agentd.d/*.conf
# systemctl start zabbix-agent

注意:如果要对宿主机进行监控,请编辑宿主机的zabbix_agentd.conf,将Server地址,默认127.0.0.1,修改为docker-server容器地址,否则不通;如果是对其他主机的监控则使用docker-zabbix-server宿主机的IP。

查看容器Zabbix-server的地址

# docker exec -it zabbix-server ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
23: eth0@if24: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

使用docker-compose

docker-compose去做,该镜像版本为4.0.1

安装docker-compose

# curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose
# docker-compose --version
docker-compose version 1.23.1, build b02f1306

编写zabbix.yml

这个已经是写好的,拿过去直接用吧,image不要动,其他的看自己情况自行调整吧

# mkdir /docker-compose
# cd /docker-compose/
/docker-compose]# vim zabbix.yml version: '3' services: mysql: image: swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-mysql environment: MYSQL_USER: zabbix MYSQL_DATABASE: zabbix MYSQL_PASSWORD: zabbix MYSQL_ROOT_PASSWORD: Sowhat? volumes: - /data/mysql/zabbix:/var/lib/mysql ports: - 3306:3306 restart: always networks: - zabbix zabbix-java-gateway: image: swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-java-gateway ports: - 10052:10052 restart: always networks: - zabbix zabbix-server: image: swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-server environment: DB_SERVER_HOST: mysql MYSQL_DATABASE: zabbix MYSQL_USER: zabbix MYSQL_PASSWORD: zabbix MYSQL_ROOT_PASSWORD: Sowhat? links: - mysql ports: - 10051:10051 depends_on: - mysql restart: always networks: - zabbix zabbix-web: image: swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-web environment: PHP_TZ: Asia/Shanghai DB_SERVER_HOST: mysql MYSQL_DATABASE: zabbix MYSQL_USER: zabbix MYSQL_PASSWORD: zabbix MYSQL_ROOT_PASSWORD: Sowhat? links: - mysql ports: - 80:80 depends_on: - zabbix-server - mysql restart: always networks: - zabbix networks: zabbix: driver: bridge

启动容器组

/docker-compose]# docker-compose -f zabbix.yml up -d --build
Creating network "dockercompose_zabbix" with driver "bridge"
Pulling zabbix-java-gateway (swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-java-gateway:latest)...
latest: Pulling from rj-bai/zabbix-java-gateway
c67f3896b22c: Pull complete
28bd08666da4: Pull complete
21b8ee686afa: Pull complete
824932b5f2bc: Pull complete
8684d0f43c47: Pull complete
e29aa4d7f4bf: Pull complete
b908f38442f8: Pull complete
2384821aec7a: Pull complete
Digest: sha256:fc95fd77ab38768b0d35101b19622c73d1aa7f0fdda0e4c82699f15c4a1dfa8c
Status: Downloaded newer image for swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-java-gateway:latest
Pulling mysql (swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-mysql:latest)...
latest: Pulling from rj-bai/zabbix-mysql
a5a6f2f73cd8: Pull complete
936836019e67: Pull complete
283fa4c95fb4: Pull complete
1f212fb371f9: Pull complete
e2ae0d063e89: Pull complete
5ed0ae805b65: Pull complete
0283dc49ef4e: Pull complete
a7905d9fbbea: Pull complete
cd2a65837235: Pull complete
5f906b8da5fe: Pull complete
e81e51815567: Pull complete
423a0fdeb46c: Pull complete
Digest: sha256:74a62f652e6cc5ba4ac8ec28e55227c781b5b1bf5d8533597e4200353f2c00ed
Status: Downloaded newer image for swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-mysql:latest
Pulling zabbix-server (swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-server:latest)...
latest: Pulling from rj-bai/zabbix-server
d6a5679aa3cf: Pull complete
7a046b910a3c: Pull complete
497545b1664f: Pull complete
58ac09628c71: Pull complete
9e045e3ec3a3: Pull complete
432e63b42dc5: Pull complete
Digest: sha256:69d854834e344bb512d8f70cbeaf2226db5db5b19c926545fc598e04e0d1156c
Status: Downloaded newer image for swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-server:latest
Pulling zabbix-web (swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-web:latest)...
latest: Pulling from rj-bai/zabbix-web
c67f3896b22c: Already exists
90884e24b31c: Pull complete
c9ddded2780e: Pull complete
1fb53af6cb01: Pull complete
fbefe3595fbc: Pull complete
6b4acce82f87: Pull complete
a4bf8619ae81: Pull complete
604f32616982: Pull complete
1bf0ca328b88: Pull complete
913cf2a0cff6: Pull complete
571ab96e2d5b: Pull complete
e6e318768910: Pull complete
ad8b7746cfb4: Pull complete
76a428e15158: Pull complete
Digest: sha256:6435ee08428960c7f8385e783b776c15eb2f5bcc4fc4671fb40eabd95da204d8
Status: Downloaded newer image for swr.cn-north-1.myhuaweicloud.com/rj-bai/zabbix-web:late
Creating dockercompose_zabbix-java-gateway_1 ... done
Creating dockercompose_mysql_1 ... 
Creating dockercompose_zabbix-server_1 ... done
Creating dockercompose_zabbix-server_1 ... 
Creating dockercompose_zabbix-web_1 ... done
/docker-compose]# docker-compose -f docker-compose.yml ps

Name                                Command                State                     Ports 
------------------------------------------------------------------------------------------------------
zabbix_mysql_1                      docker-entrypoint.sh mysqld        Up            0.0.0.0:3306->3306/tcp, 33060/tcp
zabbix_zabbix-java-gateway_1        docker-entrypoint.sh               Up            0.0.0.0:10052->10052/tcp 
zabbix_zabbix-server_1              docker-entrypoint.sh               Up            0.0.0.0:10051->10051/tcp 
zabbix_zabbix-web_1                 docker-entrypoint.sh               Up            443/tcp, 0.0.0.0:80->80/tcp
ps:容器启动完毕!

安装zabbix-agent

#rpm -ivh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
#yum -y install zabbix-agent-4.0.1
#docker exec -it zabbix_zabbix-server_1  ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
12: eth0@if13: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP 
    link/ether 02:42:ac:12:00:04 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.4/16 brd 172.18.255.255 scope global eth0
       valid_lft forever preferred_lft forever
# vim /etc/zabbix/zabbix_agentd.conf
Server=172.18.0.4
UnsafeUserParameters=1
Include=/etc/zabbix/zabbix_agentd.d/*.conf
# systemctl start zabbix-agent.service

访问:http://宿主机IP:80

网页端操作

直接编辑名为zabbix server的主机,Agent interfaces改为宿主机内网IP,然后update,我顺便又手动加了个主机进来,监控别的主机server地址直接写zabbix server宿主机即可。

邮件报警

以nginx down 为例前面博客有具体操作,这就不再赘述。

邮件设置

点开AdministrationMedia typesEmail编辑,以使qq邮箱为例,163邮箱收不到。

添加收件人

接下来去Configurationactions,选择Triggers,添加触发条件,可以有多个,我添加的是时间段和触发器。

在Acrion里添加动作—>operations

测试

手动停掉nginx

 

^<>^

部署完成,有什么错误望指出,谢谢!

参考: https://blog.rj-bai.com/post/144.html

 

posted @ 2018-12-28 13:52  琼兔  阅读(4160)  评论(0编辑  收藏  举报