基於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/gpgcss

安裝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源也換一下吧,省得一會還的重啓html

# 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安裝完畢!java

開啓網絡轉換

#echo 1 > /proc/sys/net/ipv4/ip_forward   #網絡重啓會後要從新開啓

用Dockerfile部署

zabbix-server部署階段

要用dockerzabbix-server,須要如下幾個組件mysql

組件名稱 做用
數據庫 MySQL或是PostgreSQL
Zabbix Java gateway Java管理擴展,能夠不添加
Zabbix server zabbix 服務端
zabbix-web 與MySQL服務器實例和Zabbix server實例關聯
zabbix-agent zabbix客戶端

MySQL數據庫

數據庫我選擇MySQL,仍是得寫Dockerfile,改一下時區,時間不對很麻煩,字符集也改一下吧linux

# 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,改一下時區和編碼。nginx

/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端口git

# 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 

編寫Dockerfilegithub

 /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容器web

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
sql

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

相關文章
相關標籤/搜索