07: docker 容器互联
容器间的互联
1:.容器间的互联(--link 是单方向的!!!)
[root@k8s129 ~]# docker run -d centos_ssh:v1.2 (启动一个容器)
54fbb1a6fb35bde3asdasd7s8sd7s9sd89sd99s9d998gf99r9cxsdf9987089098v
[root@k8s129 ~]# docker run -d --name ssh2 centos_ssh:v1.2 (指定名字ssh2在起一个容器)
ec44d6049142bde333e679eb23e23047404d3352ef4dc8cf94409470a808569c
[root@k8s129 ~]# docker ps (查看两个容器状态)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec44d6049142 centos_ssh:v1.2 "/usr/sbin/sshd -D" 3 seconds ago Up 1 second 22/tcp ssh2
54fbb1a6fb35 centos_ssh:v1.2 "/bin/bash /init.sh" 10 days ago Up 9 minutes 0.0.0.0:32768->22/tcp optimistic_buck
[root@k8s129 ~]# docker inspect ssh2|grep -i "ipadd" (查看容器的IP地址)
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.3",
"IPAddress": "172.17.0.3",
# 起一个容器busybox:latest, 通过--link 连接到ssh2容器里, ssh2:ssh2_alias(把ssh2起一个别名叫ssh2_alias)
[root@k8s129 ~]# docker run -it --link ssh2:ssh2_alias busybox:latest
或者
[root@k8s129 ~]# docker run -it --link ssh2:ssh2_alias nginx:latest /bin/bash
/ # ping ssh2
PING ssh2 (172.17.0.3): 56 data bytes
64 bytes from 172.17.0.3: seq=0 ttl=64 time=0.153 ms
64 bytes from 172.17.0.3: seq=1 ttl=64 time=0.090 ms
/ # cat /etc/hosts (为什么ping容器名字能识别出IP,是因为--link后,会自动添加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.3 ssh2_alias ec44d6049142 ssh2
172.17.0.4 cbf163ddf81c
/ #
#查看在正在运行的容器
[root@k8s129 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cbf163ddf81c busybox:latest "sh" 2 minutes ago Up 2 minutes charming_easley
ec44d6049142 centos_ssh:v1.2 "/usr/sbin/sshd -D" 16 minutes ago Up 16 minutes 22/tcp ssh2
54fbb1a6fb35 centos_ssh:v1.2 "/bin/bash /init.sh" 10 days ago Up 26 minutes 0.0.0.0:32768->22/tcp optimistic_buck
[root@k8s129 ~]#
通过一个列子来体验link的用处:
使用docker运行zabbix-server(本地没有这些镜像,运行的时候在在线去下载镜像)
如下是参考zabbix的官网,如何使用docker安装zabbix。感兴趣的可以自己去观察找到这一段。
https://www.zabbix.com/documentation/4.0/manual/installation/containers
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
docker run --name zabbix-java-gateway -t \
-d zabbix/zabbix-java-gateway:latest
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
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