标签:pat gateway port 创建 建议 addition down mod pair
思考一个场景,我们编写了一个微服务器, database url=ip; 项目不重启,数据库ip换掉了,我们希望可以处理这个问题,可以名字来进行访问容器?
[root@kuangshen /]# docker exec -it tomcat02 ping tomcat01
ping: tomcat01: Name or service not known
[root@kuangshen /]#
?
#如何能解决呢????
# 通过 --link就可以解决了
[root@kuangshen /]# docker run -d -P --name tomcat03 --link tomcat02 tomcat
7b5505b465a4b5bc8163fb84d67027cbfe7cd79ce2d5730555d1b413c15c0515
[root@kuangshen /]# docker exec -it tomcat03 ping tomcat02
PING tomcat02 (172.17.0.3) 56(84) bytes of data.
64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.094 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.067 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.073 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=4 ttl=64 time=0.080 ms
?
#反向可以ping通吗?不可以
[root@kuangshen /]# docker exec -it tomcat02 ping tomcat03
ping: tomcat03: Name or service not known
?
tomcat03 就是本地配置了tomcat02的配置!
#查看hosts配置,在这里原理发现!
[root@kuangshen /]# docker exec -it tomcat03 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.3 tomcat02 a3aa888739e1
172.17.0.4 7b5505b465a4
--link 就是我们在hosts配置中增加了一个172.17.0.3 tomcat02 a3aa888739e1
现在Docker已经不建议使用--link了
需要自定义网络!不使用docker0
docker0问题:它不支持容器名连接访问!
查看所有的docker网络
网络模式
bridge: 桥接docker搭桥(默认, 自己创建也使用bridge模式)
none: 不配置网络
host: 和宿主机共享网络
container:容器内网络连通(用的少!局限很大)
测试
# 首先把原来的容器删掉
[root@kuangshen /]# docker rm -f $(docker ps -aq)
7b5505b465a4
a983ec358d46
a3aa888739e1
?
# 我们直接启动的命令 --net bridge ,而这个就是我们的docker0
[root@kuangshen /]# docker run -d -P --name tomcat01 --net bridge tomcat
[root@kuangshen /]# docker run -d -P --name tomcat01 tomcat #原来的连接方式
?
#docker0特点:默认的, 域名不能访问, --link可以打通直接连接
?
# 我们可以自定义一个网络!
#--driver bridge
#--subnet 192.168.0.0/16 子网
#--gateway 192.168.0.1 网关
[root@kuangshen /]# docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
f8a54b35cabfa5170445de6994b940d1b50b1c54751b9597e40f5d6e9277fd22
[root@kuangshen /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
67bad398d895 bridge bridge local
13b82c23a994 host host local
f8a54b35cabf mynet bridge local
3003f1b6412e none null local
?
我们自己的网络就创建好了:
[root@kuangshen /]# docker run -d -P --name tomcat-net-01 --net mynet tomcat
ed92a0d162c910dea054fabd95dbfefb2b198e627461c0e3c0b82ffbb1d40608
[root@kuangshen /]# docker run -d -P --name tomcat-net-02 --net mynet tomcat
e2acac20abb933833c05a36ffc1cedd2239beb7f9954b27f7ec4de53451c9a88
?
root@kuangshen /]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "f8a54b35cabfa5170445de6994b940d1b50b1c54751b9597e40f5d6e9277fd22",
"Created": "2021-05-28T15:30:05.397447622+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "192.168.0.0/16",
"Gateway": "192.168.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"e2acac20abb933833c05a36ffc1cedd2239beb7f9954b27f7ec4de53451c9a88": {
"Name": "tomcat-net-02",
"EndpointID": "12dc69619de088580fd6704726be53704656f5f9c9a0efe4ef654702edc19f22",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"ed92a0d162c910dea054fabd95dbfefb2b198e627461c0e3c0b82ffbb1d40608": {
"Name": "tomcat-net-01",
"EndpointID": "4519625dfd9545f0c909e0f4bb7cfbfb4ea02fb1716a85dd6e2338f032984add",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
[root@kuangshen /]#
?
# 再次测试连接
[root@kuangshen /]# docker exec -it tomcat-net-01 ping 192.168.0.3
PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.100 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.060 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.071 ms
64 bytes from 192.168.0.3: icmp_seq=4 ttl=64 time=0.068 ms
^C
--- 192.168.0.3 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 0.060/0.074/0.100/0.018 ms
?
# 现在不使用--link 也可以ping名字了!
[root@kuangshen /]# docker exec -it tomcat-net-01 ping tomcat-net-02
PING tomcat-net-02 (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.058 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.066 ms
64 bytes from tomcat-net-02.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.068 ms
^C
--- tomcat-net-02 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2ms
rtt min/avg/max/mdev = 0.058/0.064/0.068/0.004 ms
[root@kuangshen /]#
?
我们自定义的网络docker都已经帮我们维护好了对应的关系,推荐我们平时使用自定义的网络!
好处:
不同的集群使用不同的网络,保证集群是安全和健康的。
[root@kuangshen /]# docker run -d -P --name tomcat01 tomcat
88085710eb2cfc01cc49288e508a578816527963033e621def1e58951643c463
[root@kuangshen /]# docker run -d -P --name tomcat02 tomcat
db121f1e61ee454f04947f2fefa7e3bf1a3c40d7fc7ca54b7dcfae7ad29a4e89
[root@kuangshen /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
db121f1e61ee tomcat "catalina.sh run" 12 seconds ago Up 11 seconds 0.0.0.0:49162->8080/tcp, :::49162->8080/tcp tomcat02
88085710eb2c tomcat "catalina.sh run" 43 seconds ago Up 42 seconds 0.0.0.0:49161->8080/tcp, :::49161->8080/tcp tomcat01
e2acac20abb9 tomcat "catalina.sh run" 18 minutes ago Up 18 minutes 0.0.0.0:49160->8080/tcp, :::49160->8080/tcp tomcat-net-02
ed92a0d162c9 tomcat "catalina.sh run" 19 minutes ago Up 19 minutes 0.0.0.0:49159->8080/tcp, :::49159->8080/tcp tomcat-net-01
[root@kuangshen /]# docker exec -it tomcat01 ping tomcat-net-01
ping: tomcat-net-01: Name or service not known
?
#测试打通 tomcat01 - mynet
#连通之后就是将tomcat01放到了mynet网络下
?
# 相当于一个容器两个ip地址
?
?
[root@kuangshen /]# docker network connect mynet tomcat01
[root@kuangshen /]# docker network inspect mynet
?
# 01连通ok
[root@kuangshen /]# docker exec -it tomcat01 ping tomcat-net-01
PING tomcat-net-01 (192.168.0.2) 56(84) bytes of data.
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=1 ttl=64 time=0.072 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.064 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.060 ms
^C
--- tomcat-net-01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.060/0.065/0.072/0.008 ms
# 02是依旧打不通的
[root@kuangshen /]# docker exec -it tomcat02 ping tomcat-net-01
ping: tomcat-net-01: Name or service not known
[root@kuangshen /]#
?
结论:假设要跨网络操作容器, 就需要使用docker network connect连通!
shell脚本!
#首先移除的其他容器
[root@kuangshen /]# docker rm -f $(docker ps -aq)
# 创建网卡
[root@kuangshen /]# docker network create redis --subnet 172.38.0.0/16
dbfc21f8ec481c26a5c621fd444bc8fbff878a89b9625dfd79a1a73fbbbe63a8
[root@kuangshen /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
67bad398d895 bridge bridge local
13b82c23a994 host host local
f8a54b35cabf mynet bridge local
3003f1b6412e none null local
dbfc21f8ec48 redis bridge local
[root@kuangshen /]# docker network inspect redis
[
{
"Name": "redis",
"Id": "dbfc21f8ec481c26a5c621fd444bc8fbff878a89b9625dfd79a1a73fbbbe63a8",
"Created": "2021-05-28T16:18:33.026029065+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.38.0.0/16"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {},
"Options": {},
"Labels": {}
}
]
?
?
# 通过脚本创建六个redis配置
for port in $(seq 1 6); \
do \
mkdir -p /mydata/redis/node-${port}/conf
touch /mydata/redis/node-${port}/conf/redis.conf
cat << EOF >/mydata/redis/node-${port}/conf/redis.conf
port 6379
bind 0.0.0.0
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.38.0.1${port}
cluster-announce-port 6379
cluster-announce-bus-port 16379
appendonly yes
EOF
done
?
docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
-v /mydata/redis/node-${port}/data:/data \
-v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf; \
?
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \
-v /mydata/redis/node-1/data:/data \
-v /mydata/redis/node-1/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
?
docker run -p 6372:6379 -p 16372:16379 --name redis-2 \
-v /mydata/redis/node-2/data:/data \
-v /mydata/redis/node-2/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.12 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
?
docker run -p 6373:6379 -p 16373:16379 --name redis-3 \
-v /mydata/redis/node-3/data:/data \
-v /mydata/redis/node-3/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.13 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
?
?
docker run -p 6374:6379 -p 16374:16379 --name redis-4 \
-v /mydata/redis/node-4/data:/data \
-v /mydata/redis/node-4/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.14 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
?
?
docker run -p 6375:6379 -p 16375:16379 --name redis-5 \
-v /mydata/redis/node-5/data:/data \
-v /mydata/redis/node-5/conf/redis.conf:/etc/redis/redis.conf \
-d --net redis --ip 172.38.0.15 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
?
docker run -p 6376:6379 -p 16376:16379 --name redis-6 \
-v /mydata/redis/node-6/data:/data \
-v /mydata/redis/node-6/conf/redis.conf:/etc/redis/redis.conf \