Docker版zabbix

1. docker-compose

       docker-compose :容器自带的编排工作,可以通过yaml编排文件,将容器要启动的命令写入文件,然后再利用docker-compose run file.yaml文件来直接启动,同时启动多个容器,方便应用服务的移植、共享。

2. 部署zabbix服务

       部署zabbix服务,然后利用docker-compose启动。

1> 上传压缩包,解压

[root@node ~]# cd /opt/
[root@node opt]# ls
rh  zabbix-agent.tar.gz  zabbix-server.tar.gz
[root@node opt]# tar xf zabbix-server.tar.gz
[root@node opt]# ls
cmp_mariadb  cmp_zabbix  rh  zabbix-agent.tar.gz  zabbix-server.tar.gz

2> 启动数据库

[root@node cmp_mariadb]# docker load -i  mariadb-latest.tar    #导入镜像
[root@node cmp_mariadb]# docker images
mariadb                       latest              901583bfdf5a        5 months ago        367MB

启用编排文件文件名必须为docker-compose.ymal
先查看文件内容
[root@node cmp_mariadb]# ls
docker-compose.yml  mariadb-latest.tar  mysqlconf  mysqllib  mysqllog
[root@node cmp_mariadb]# vim docker-compose.yml
mysql:
    image: mariadb:latest
    ports:
        - "3306:3306"                      #端口映射
    volumes:
        - ./mysqllib:/var/lib/mysql        #目录映射
        - ./mysqlconf:/etc/mysql
        - ./mysqllog:/var/log/mysql
    environment:                           #环境变量
        - MYSQL_USER=zabbix
        - MYSQL_DATABASE=zabbix
        - MYSQL_PASSWORD=zabbix
        - MYSQL_ROOT_PASSWORD=123321
        - LANG=en_US.utf8
        - TZ=Asia/Shanghai
    restart: always                       #一直重启,开机重启
[root@node cmp_mariadb]# docker-compose up -d  #启动编排文件,-d表示放在后台启动
Creating cmpmariadb_mysql_1 ... done
[root@node cmp_mariadb]# docker ps        #启动成功
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
a4bae322f4f6        mariadb:latest      "docker-entrypoint.s…"   4 minutes ago       Up 4 minutes        0.0.0.0:3306->3306/tcp   cmpmariadb_mysql_1

由docker-compose启动的容器无法用命令docker命令控制,必须用docker-compose 在docker-compose所在的目录下执行相关命令。

[root@node cmp_mariadb]# docker-compose ps
       Name                 Command          State          Ports       
-------------------------------------------------------------------------
cmpmariadb_mysql_1   docker-entrypoint.sh    Up      0.0.0.0:3306->3306/t

                     mysqld                          cp

3> 启动zabbix

[root@node opt]# \cd cmp_zabbix/
[root@node cmp_zabbix]# ls
docker-compose.yml  zabbixconfig    zabbix-server-mysql.tar
zabbix-agent.tar    zabbix-scripts  zabbix-web-nginx-mysql.tar

导入镜像
[root@node cmp_zabbix]# docker load -i zabbix-server-mysql.tar
[root@node cmp_zabbix]# docker load -i zabbix-agent.tar
[root@node cmp_zabbix]# docker load -i zabbix-web-nginx-mysql.tar

编排文件内容
[root@node cmp_zabbix]# vim docker-compose.yml
version: '3'
services:
  zabbix-server:
    container_name: zabbix-server
    image: zabbix/zabbix-server-mysql:latest
    restart: always
    network_mode: host
#    ports:
#      - "10051:10051"
    environment:
      - DB_SERVER_HOST=192.168.16.95
      - MYSQL_USER=zabbix
      - MYSQL_DATABASE=zabbix
      - MYSQL_PASSWORD=zabbix
      - DB_SERVER_ROOT_USER=root
      - DB_SERVER_ROOT_PASS=123321
      - TZ='Asia/Shanghai'
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./zabbix-scripts:/usr/lib/zabbix
      - ./zabbixconfig:/etc/zabbix
  zabbix-web-nginx:
    image: zabbix/zabbix-web-nginx-mysql:latest
restart: always
network_mode: bridge
    ports:
      - "8080:80"
      - "8443:443"
    environment:
      - DB_SERVER_HOST=192.168.16.95
      - MYSQL_DATABASE=zabbix
      - MYSQL_USER=zabbix
      - MYSQL_PASSWORD=zabbix
      - MYSQL_ROOT_PASSWORD=123321
      - ZBX_SERVER_HOST=192.168.16.95
      - PHP_TZ="Asia/Shanghai"
      - TZ='Asia/Shanghai'
  zabbix-agent:
    image: zabbix/zabbix-agent:latest
    environment:
      - ZBX_HOSTNAME=Zabbix server
  - ZBX_SERVER_HOST=127.0.0.1
      - ZBX_SERVER_PORT=10051
      - TZ='Asia/Shanghai'
    network_mode: host
#    ports:
#      - "10050:10050"
    restart: always
    privileged: true
#    links:
#      - zabbix-server:zabbixhost

启动
[root@node cmp_zabbix]# docker-compose up -d
Creating cmpzabbix_zabbix-web-nginx_1 ... done
Creating cmpzabbix_zabbix-agent_1     ... done
Creating cmpzabbix_zabbix-agent_1     ...
[root@node cmp_zabbix]# docker-compose  ps
        Name                 Command          State          Ports      
-------------------------------------------------------------------------
cmpzabbix_zabbix-      docker-entrypoint.sh   Up                        
agent_1                                                                 
cmpzabbix_zabbix-      docker-entrypoint.sh   Up      0.0.0.0:8443->443/t
web-nginx_1                                           cp, 0.0.0.0:8080->8
                                                      0/tcp             
zabbix-server          docker-entrypoint.sh   Up  

容器启动后,在web端查看

若无法进入页面,修改文件/etc/sysctl.conf,加入net.ipv4.ip_forward=1

[root@node cmp_zabbix]# vim /etc/sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward=1

用户/密码:Admin/zabbix

4> 监控其他三个节点

将zabbi-agent导入到三个节点

[root@node1 ~]# cd /opt
[root@node1 opt]# ls
rh  zabbix-agent.tar.gz
[root@node1 opt]# tar xf zabbix-agent.tar.gz 
[root@node1 opt]# ls
cmp_zabbix_agent  rh  zabbix-agent.tar  zabbix-agent.tar.gz

导入zabbix镜像
[root@node1 opt]# docker load -i zabbix-agent.tar

进入编排文件所在目录
[root@node1 opt]# cd cmp_zabbix_agent/
[root@node1 cmp_zabbix_agent]# ls
docker-compose.yml
文件内容
[root@node1 cmp_zabbix_agent]# vim docker-compose.yml
version: '3'
services:
  zabbix-agent:
    image: zabbix/zabbix-agent:latest
    container_name: zabbix-agent
    environment:
      - ZBX_HOSTNAME=node2
      - ZBX_SERVER_HOST=192.168.16.95
      - ZBX_SERVER_PORT=10051
      - TZ='Asia/Shanghai'
    network_mode: bridge
    ports:
      - "10050:10050"
    restart: always
privileged: true
 
启动
[root@node1 cmp_zabbix_agent]# ls
docker-compose.yml
[root@node1 cmp_zabbix_agent]# docker-compose up -d
Creating zabbix-agent ... done
[root@node1 cmp_zabbix_agent]# docker-compose ps
    Name             Command          State            Ports         
----------------------------------------------------------------------
zabbix-agent   docker-entrypoint.sh   Up      0.0.0.0:10050->10050/tcp

监控节点已添加,在web界面添加主机进zabbix监控。

 

 

posted @ 2019-07-15 22:46  Ajunyu  阅读(625)  评论(0编辑  收藏  举报