Docker:容器间互联的应用zabbix监控项目 [十]

时间:2022-07-03 08:34:52

一、docker容器间的互联

1、创建两个容器

[root@luoahong ~]# docker run -d --name luoahong httpd:latest
8f771f043391e77d18fb26d1af0f9c9a6e9fdd1e76ae05c41c265db1f6780067
[root@luoahong ~]# docker run -it --link luoahong:web01 centos:6.8
Unable to find image 'centos:6.8' locally
6.8: Pulling from library/centos
b268bb2f98b2: Pull complete
Digest: sha256:21567ed29d2ae26d89709006f881d6e2b14e5a6144239e707d922a0dd7b8cd6e
Status: Downloaded newer image for centos:6.8

2、容器互访(之前是通过如下方式)

1、查询容器ip

[root@luoahong ~]# docker inspect luoahong

2、通过ip互联

[root@124528a51f1c /]# ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:05
inet addr:172.17.0.5 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:14 errors:0 dropped:0 overruns:0 frame:0
TX packets:8 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1229 (1.2 KiB) TX bytes:660 (660.0 b) lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) [root@124528a51f1c /]# curl -I 172.17.0.4
HTTP/1.1 200 OK
Date: Thu, 17 Jan 2019 07:08:53 GMT
Server: Apache/2.4.37 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

3、原理推导

1、curl luoahong

[root@124528a51f1c /]# curl -I luoahong
HTTP/1.1 200 OK
Date: Thu, 17 Jan 2019 07:10:07 GMT
Server: Apache/2.4.37 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

2、curl web01(别名)

[root@124528a51f1c /]# curl -I web01
HTTP/1.1 200 OK
Date: Thu, 17 Jan 2019 07:10:20 GMT
Server: Apache/2.4.37 (Unix)
Last-Modified: Mon, 11 Jun 2007 18:53:14 GMT
ETag: "2d-432a5e4a73a80"
Accept-Ranges: bytes
Content-Length: 45
Content-Type: text/html

3、原来是因为这里

Docker:容器间互联的应用zabbix监控项目 [十]

[root@124528a51f1c /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.4 web01 8f771f043391 luoahong
172.17.0.5 124528a51f1c

4、小结

1、先启动的容器无法连接后启动的容器

2、但是后启动的容器可以连接新启动的容器

3、容器间互联的方法:--link  单方向的

二、docker容器间互联的应用

1、zabbix-server容器创建

[root@luoahong ~]# docker run --name mysql-server -t \
> -e MYSQL_DATABASE="zabbix" \
> -e MYSQL_USER="zabbix" \
> -e MYSQL_PASSWORD="zabbix_pwd" \
> -e MYSQL_ROOT_PASSWORD="root_pwd" \
> -d mysql:5.7 \
> --character-set-server=utf8 --collation-server=utf8_bin
MYSQL_PASSWORD="zabbix_pwd" \
-e MYSQL_ROOT_PASSWORD="root_pwd" \
-e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
--link mysql-server:mysql \
--link zabbix-java-gateway:zabbix-java-gateway \
-p 10051:10051 \
-d zabbix/zabbix-server-mysql:latest docker run --name zabbix-web-nginx-mysql -t \
-e DB_SERVER_HOST="mysql-server" \
-e MYSQL_DATABASE="zabbix" \
-e MYSQL_USER="zabbix" \
-e MYSQL_PASSWORD="zabbix_pwd" \
-e MYSQL_ROOT_PASSWORD="root_pwd" \
--link mysql-server:mysql \
--link zabbix-server-mysql:zabbix-server \
-p 80:80 \
-d zabbix/zabbix-web-nginx-mysql:latestUnable to find image 'mysql:5.7' locally
5.7: Pulling from library/mysql
177e7ef0df69: Already exists
cac25352c4c8: Pull complete
8585afabb40a: Pull complete
1e4af4996053: Pull complete
c326522894da: Pull complete
9020d6b6b171: Pull complete
55eb37ec6e5f: Pull complete
1a9d2f77e0e7: Pull complete
d7e648ad64aa: Pull complete
4120d828ea6b: Pull complete
3b39dc5451af: Pull complete
Digest: sha256:bf17a7109057494c45fba5aab7fc805ca00ac1eef638dfdd42b38d5a7190c9bb
Status: Downloaded newer image for mysql:5.7
1c3951a744dfba3f0f5651295069c2480e18dcf32185e28de5e04ebee9370dd3
[root@luoahong ~]#
[root@luoahong ~]# docker run --name zabbix-java-gateway -t \
> -d zabbix/zabbix-java-gateway:latest
Unable to find image 'zabbix/zabbix-java-gateway:latest' locally
latest: Pulling from zabbix/zabbix-java-gateway
c67f3896b22c: Pull complete
8d77a75362f3: Downloading
936305c240da: Download complete
142f69f7e033: Download complete
5461b2656c90: Download complete
3aa2a12cb3ee: Download complete
latest: Pulling from zabbix/zabbix-java-gateway
407ea412d82c: Pull complete
cafad5f8b858: Pull complete
1dc9b3dcdd9a: Pull complete
66b294030177: Pull complete
a6f8455af247: Pull complete
b9582210b715: Pull complete
Digest: sha256:b1ca03fddcf38758ae05a1e1a1fb8094c5510207c66ad9567d5986f28c03f625
Status: Downloaded newer image for zabbix/zabbix-java-gateway:latest
27753c4be35cc24f6f9ded1c62998e4ed84a57c3a464e246a49da6a0bf93485e
[root@luoahong ~]#
[root@luoahong ~]# docker run --name zabbix-server-mysql -t \
> -e DB_SERVER_HOST="mysql-server" \
> -e MYSQL_DATABASE="zabbix" \
> -e MYSQL_USER="zabbix" \
> -e MYSQL_PASSWORD="zabbix_pwd" \
> -e MYSQL_ROOT_PASSWORD="root_pwd" \
> -e ZBX_JAVAGATEWAY="zabbix-java-gateway" \
> --link mysql-server:mysql \
> --link zabbix-java-gateway:zabbix-java-gateway \
> -p 10051:10051 \
> -d zabbix/zabbix-server-mysql:latest
Unable to find image 'zabbix/zabbix-server-mysql:latest' locally
latest: Pulling from zabbix/zabbix-server-mysql
d6a5679aa3cf: Pull complete
7a046b910a3c: Pull complete
497545b1664f: Pull complete
58ac09628c71: Pull complete
Digest: sha256:b19befc6b83e22b8ad7f95f792fa138d142c2f67aaf936486e628675a1bf4ee3
Status: Downloaded newer image for zabbix/zabbix-server-mysql:latest
c41b9fbfa96aaf87b170f3fef7321901e9b6249a06e1ff8afaca8ed81f5a235b
[root@luoahong ~]#
[root@luoahong ~]# docker run --name zabbix-web-nginx-mysql -t \
> -e DB_SERVER_HOST="mysql-server" \
> -e MYSQL_DATABASE="zabbix" \
> -e MYSQL_USER="zabbix" \
> -e MYSQL_PASSWORD="zabbix_pwd" \
> -e MYSQL_ROOT_PASSWORD="root_pwd" \
> --link mysql-server:mysql \
> --link zabbix-server-mysql:zabbix-server \
> -p 80:80 \
> -d zabbix/zabbix-web-nginx-mysql:latest
Unable to find image 'zabbix/zabbix-web-nginx-mysql:latest' locally
latest: Pulling from zabbix/zabbix-web-nginx-mysql
c67f3896b22c: Pull complete
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
Digest: sha256:6894a50fea7ca7c092f93e0c852b1a92d4d9bb5cc572ff65fbc256acc9228251
Status: Downloaded newer image for zabbix/zabbix-web-nginx-mysql:latest
6ed643eb215dcb85248b27459ce4052348ebac526aa6996745adcf78b0b63492
[root@luoahong ~]# [root@luoahong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5522f6a8dae4 zabbix/zabbix-web-nginx-mysql:latest "docker-entrypoint.sh" 13 seconds ago Up 12 seconds 0.0.0.0:80->80/tcp, 443/tcp zabbix-web-nginx-mysql
508f433fac31 zabbix/zabbix-server-mysql:latest "docker-entrypoint.sh" 18 seconds ago Up 15 seconds 0.0.0.0:10051->10051/tcp zabbix-server-mysql
7eb8ca5a2b9e zabbix/zabbix-java-gateway:latest "docker-entrypoint.sh" 19 seconds ago Up 17 seconds 10052/tcp zabbix-java-gateway
73351adb12c7 mysql:5.7 "docker-entrypoint.s…" 19 seconds ago Up 18 seconds 3306/tcp, 33060/tcp mysql-server

2、这些命令来自哪里

https://www.zabbix.com/documentation/4.0/manual/installation/containers

来自zabbix安装在容器中

3、登录zabbix web界面

Docker:容器间互联的应用zabbix监控项目 [十]

三、zabbix服务器监控客户端实例

1、客户端安装

1、下载安装

[root@luoahong ~]# rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.2-1.el7.x86_64.rpm
Retrieving https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.2-1.el7.x86_64.rpm
warning: /var/tmp/rpm-tmp.FBRxfZ: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-agent-4.0.2-1.el7 ################################# [100%]

2、配置启动

[root@luoahong ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.228.133
[root@luoahong ~]# systemctl start zabbix-agent.service
[root@luoahong ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1207/sshd
tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 1757/zabbix_agentd
tcp6 0 0 :::22 :::* LISTEN 1207/sshd
tcp6 0 0 :::10050 :::* LISTEN 1757/zabbix_agentd
udp 0 0 0.0.0.0:25299 0.0.0.0:* 1149/dhclient
udp 0 0 0.0.0.0:68 0.0.0.0:* 1149/dhclient
udp 0 0 127.0.0.1:323 0.0.0.0:* 942/chronyd
udp6 0 0 ::1:323 :::* 942/chronyd
udp6 0 0 :::39247 :::* 1149/dhclient

2、zabbix web界面添加监控主机

Docker:容器间互联的应用zabbix监控项目 [十]

3、zabbix web端快速检测到agent的方法

1、重启zabbix-server服务

[root@luoahong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5522f6a8dae4 zabbix/zabbix-web-nginx-mysql:latest "docker-entrypoint.sh" 25 minutes ago Up 25 minutes 0.0.0.0:80->80/tcp, 443/tcp zabbix-web-nginx-mysql
508f433fac31 zabbix/zabbix-server-mysql:latest "docker-entrypoint.sh" 25 minutes ago Up 25 minutes 0.0.0.0:10051->10051/tcp zabbix-server-mysql
7eb8ca5a2b9e zabbix/zabbix-java-gateway:latest "docker-entrypoint.sh" 25 minutes ago Up 25 minutes 10052/tcp zabbix-java-gateway
73351adb12c7 mysql:5.7 "docker-entrypoint.s…" 25 minutes ago Up 25 minutes 3306/tcp, 33060/tcp mysql-server
[root@luoahong ~]# docker restart zabbix/zabbix-server-mysql
Error response from daemon: No such container: zabbix/zabbix-server-mysql
[root@luoahong ~]# docker restart zabbix-server-mysql
zabbix-server-mysql
[root@luoahong ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5522f6a8dae4 zabbix/zabbix-web-nginx-mysql:latest "docker-entrypoint.sh" 26 minutes ago Up 26 minutes 0.0.0.0:80->80/tcp, 443/tcp zabbix-web-nginx-mysql
508f433fac31 zabbix/zabbix-server-mysql:latest "docker-entrypoint.sh" 26 minutes ago Up 5 seconds 0.0.0.0:10051->10051/tcp zabbix-server-mysql
7eb8ca5a2b9e zabbix/zabbix-java-gateway:latest "docker-entrypoint.sh" 26 minutes ago Up 26 minutes 10052/tcp zabbix-java-gateway
73351adb12c7 mysql:5.7 "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 3306/tcp, 33060/tcp mysql-server

2、再次查看已经变绿

Docker:容器间互联的应用zabbix监控项目 [十]