docker部署zabbix ansile批量部署自动发现
1. docker部署zabbix
1.1 上传并解压zabbix镜像包
[root@localhost ~]# tar xzf zabbix_image.tar.gz [root@localhost ~]# cd zabbix/[root@localhost zabbix]# lszabbix-agent.tar zabbix-server.tar zabbix-web.tar
1.2 导入zabbix镜像
[root@localhost zabbix]# docker load -i zabbix-agent.tar [root@localhost zabbix]# docker load -i zabbix-server.tar [root@localhost zabbix]# docker load -i zabbix-web.tar
1.3 查看导入的zabbix镜像
[root@localhost ~]# docker images |grep zabbix zabbix/zabbix-web-nginx-mysql latest 116855eaede2 2 weeks ago 164MBzabbix/zabbix-server-mysql latest 8ca9fbc31571 2 weeks ago 67.7MBzabbix/zabbix-agent latest 9084e5d9908f 2 weeks ago 16.4MB
1.4 mysql镜像
上传导入
[root@localhost ~]# tar xzf mysql5.7_utf8.tar.gz [root@localhost ~]# docker load -i mysql5.7_utf8.tar
1.5 编写compose文件
[root@localhost zabbix]# vim docker-compose.yml [root@localhost zabbix]# cat docker-compose.yml version: "3"services: mysql: image: mysql:5.7 environment: MYSQL_ROOT_PASSWORD: 123 MYSQL_DATABASE: zabbix MYSQL_USER: zabbix MYSQL_PASSWORD: 123 zabbix-server: image: zabbix/zabbix-server-mysql ports: - "10051:10051" environment: DB_SERVER_HOST: mysql MYSQL_DATABASE: zabbix MYSQL_USER: zabbix MYSQL_PASSWORD: 123 depends_on: - mysql zabbix-web: image: zabbix/zabbix-web-nginx-mysql ports: - "80:8080" environment: DB_SERVER_HOST: mysql MYSQL_DATABASE: zabbix MYSQL_USER: zabbix MYSQL_PASSWORD: 123 PHP_TZ: Asia/Shanghai ZBX_SERVER_HOST: zabbix-server depends_on: - zabbix-server - mysql
1.6 基于compose文件运行容器
1.6.1 运行
[root@localhost zabbix]# docker-compose up -dCreating network "zabbix_default" with the default driverCreating zabbix_mysql_1 ... doneCreating zabbix_zabbix-server_1 ... doneCreating zabbix_zabbix-web_1 ... done
1.6.2 查看
[root@localhost zabbix]# docker-compose ps Name Command State Ports ------------------------------------------------------------------------------------------------zabbix_mysql_1 docker-entrypoint.sh mysqld Up 3306/tcp, 33060/tcp zabbix_zabbix-server_1 /sbin/tini -- /usr/bin/doc ... Up 0.0.0.0:10051->10051/tcp zabbix_zabbix-web_1 docker-entrypoint.sh Up 0.0.0.0:80->8080/tcp, 8443/tcp[root@localhost zabbix]#
1.7 浏览器验证
1.7.1 登录
用户名:Admin
密码: zabbix
1.7.2 成功访问zabbix主页
1.7.3 设置中文
1.7.4 配置自动发现
1.7.4.1 添加自动发现规则
1.7.4.2 定义发现规则
1.7.4.3 配置动作
1.7.4.4 查看自动发现
2.ansible批量部署
2.1 安装ansible
[root@localhost ~]# yum search ansible[root@localhost ~]# yum -y install centos-release-ansible-29[root@localhost ~]# yum -y insatll ansible
2.2 配置免密登录
2.2.1 生成秘钥对
[root@localhost ~]# ssh-keygen Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'.Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa.Your public key has been saved in /root/.ssh/id_rsa.pub.The key fingerprint is:SHA256:4VR DrKshFAcKdb3Ax/ d46b4wQZsMGhCp6EKXbqGjA root@localhost.localdomainThe key's randomart image is: ---[RSA 2048]---- | ooo . .. || oo. o.o* || o o ..==. . ||ooo o = * ||E.o o . Soo . ||o. . . ... . ||.. . .. ||.. .o.. ||. . o | ----[SHA256]-----
2.2.2 复制 公钥到远程主机
[root@localhost ~]# ssh-copy-id root@192.168.153.184[root@localhost ~]# ssh-copy-id root@192.168.153.185
2.3 配置主机清单
[root@localhost ~]# vim /etc/ansible/hosts [webservers]192.168.153.184192.168.153.185
2.4 验证主机的连通性
[root@localhost ~]# ansible all -m ping192.168.153.185 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}192.168.153.184 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong"}[root@localhost ~]#
2.5 编写playbook文件
[root@localhost agent]# vim agent.yml ---- hosts: webservers tasks: - name: "推送zabbix源" copy: src=zabbix_Aliyun.repo dest=/etc/yum.repos.d/ - name: "安装zabbix-agent" yum: name=zabbix-agent state=latest - name: "修改agent配置文件" script: agent.sh - name: "启动agent" systemd: name=zabbix-agent state=started enabled=yes - name: "关闭防火墙" systemd: name=firewalld state=stopped - name: "关闭selinux" shell: "setenforce 0"[root@localhost agent]#
2.6 依据playbook文件执行批量部署
[root@localhost agent]# ansible-playbook agent.yml PLAY [webservers] ***************************************************************************************************************TASK [Gathering Facts] **********************************************************************************************************ok: [192.168.153.185]ok: [192.168.153.184]TASK [推送zabbix源] ****************************************************************************************************************changed: [192.168.153.185]changed: [192.168.153.184]TASK [安装zabbix-agent] ***********************************************************************************************************changed: [192.168.153.184]changed: [192.168.153.185]TASK [修改agent配置文件] **************************************************************************************************************changed: [192.168.153.184]changed: [192.168.153.185]TASK [启动agent] ******************************************************************************************************************changed: [192.168.153.185]changed: [192.168.153.184]TASK [关闭防火墙] ********************************************************************************************************************changed: [192.168.153.184]changed: [192.168.153.185]TASK [关闭selinux] ****************************************************************************************************************changed: [192.168.153.185]changed: [192.168.153.184]PLAY RECAP **********************************************************************************************************************192.168.153.184 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 192.168.153.185 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.7 连接zabbix-server测试
[root@localhost agent]# cd ../zabbix/[root@localhost zabbix]# docker-compose exec zabbix-server bashbash-5.0$ zabbix_get -s 192.168.153.184 -k agent.ping1bash-5.0$ zabbix_get -s 192.168.153.185 -k agent.ping1bash-5.0$
返回值为1则配置成功
赞 (0)