docker网络
查看docker0
docker如何处理网络
# 查看容器内部的网络地址 ip addr,容器启动时会得到docker分配的 eth0@if39,ip地址
[root@VM-4-6-centos /]# docker exec -it tomcat01 ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
38: eth0@if39: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet 172.17.0.2/16 brd 172.17.255.255 scope global eth0
valid_lft forever preferred_lft forever
# linux ping 容器内部,eth0@if39
[root@VM-4-6-centos /]# ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.043 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.042 ms
64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.041 ms
64 bytes from 172.17.0.2: icmp_seq=4 ttl=64 time=0.051 ms
每启动一个docker容器,docker就会给docker容器分配一个ip,只要安装了docker,就会有一个网卡docker0,桥接模式,使用的技术是 veth-pair
测试 ip addr,发现多了一个网卡
再启动一个容器,发现又多了一个网卡
进入新启动容器内部
发现这个容器的网卡,都是一对的 例如:38: eth0@if39
evth-pair 就是一对虚拟设备接口,是成对出现的,一段连着协议,一段连着彼此
有这个特性,veth-pair 充当一个桥梁,连接各种虚拟设备
OpenStac,docker容器之间的连接,OVS的连接,都是使用veth-pair技术
# 容器1与容器2 ping
[root@VM-4-6-centos /]# docker exec -it tomcat01 ping 172.17.0.3
PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.
64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from 172.17.0.3: icmp_seq=3 ttl=64 time=0.058 ms
容器与容器之间是可以 ping 通的
tomcat01与tomcat02通信,不是直接通信,是通过docker0进行转发
所有容器在不指定网络的情况下,都是docker0路由的,docker会给容器分配一个默认的可用ip
docker中所有的网络接口都是虚拟的,转发效率高
只要容器删除,对应网桥一对就没了
查看docker网络配置
[root@VM-4-6-centos /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
198357e49663 bridge bridge local # 桥接网络
75b58b57cfa1 host host local
ed80f5867272 none null local
通过 docker network inspect 网络id 查看配置
link
编写了一个微服务,database url=ip:port,项目不重启,数据库ip换掉了,处理这个问题,使用名字访问容器
# 直接ping是无法ping通的
[root@VM-4-6-centos /]# docker exec -it tomcat02 ping tomcat01
ping: tomcat01: Name or service not known
# 解决 --link
[root@VM-4-6-centos /]# docker run -d -p 8082:8080 --name tomcat03 --link tomcat02 tomcat
1122bf60a5c18de3af234caebfa24e004acc853c5bf0449743db9358f6dceed1
[root@VM-4-6-centos /]# 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.104 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.054 ms
64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.051 ms
# 反向无法ping通
[root@VM-4-6-centos /]# docker exec -it tomcat02 ping tomcat03
ping: tomcat03: Name or service not known
# 查看 tomcat03 hosts文件
[root@VM-4-6-centos /]# 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 cf174a1b5a41
172.17.0.4 1122bf60a5c1
本质:--link 就是在hosts配置中增加了一个 172.17.0.3 tomcat02 cf174a1b5a41
不建议使用 --link,docker0问题:不支持容器名访问
自定义网络
查看所有的docker网络
网络模式
bridge:桥接模式(默认)
none:不配置网络
host:和宿主机共享网络
container:容器网络连通(使用少,局限大,不建议使用)
测试
# 直接启动命令 --net bridge,就是默认的docker0
docker run -d -p 8080:8080 --name tomcat01 tomcat
docker run -d -p 8080:8080 --net bridge --name tomcat01 tomcat
# docker0特点,默认域名不能访问,--link可以打通连接
# 自定义网络
# --driver bridge 桥接
# --subnet 192.168.0.0/16 子网 192.168.0.2~192.168.255.255
# --gateway 192.168.0.1 网关
docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
[root@VM-4-6-centos /]# docker network ls
NETWORK ID NAME DRIVER SCOPE
198357e49663 bridge bridge local
75b58b57cfa1 host host local
a08726f536e8 mynet bridge local
ed80f5867272 none null local
自己创建的网络
测试
# 在自定义网络下运行容器
[root@VM-4-6-centos /]# docker run -d -p 8080:8080 --name tomcat02-net --net mynet tomcat
5d5a05421388aa9c67264d27319db6287bb48c236887e9e886a8eaa612f672b3
[root@VM-4-6-centos /]# docker run -d -p 8081:8080 --name tomcat01-net --net mynet tomcat
407c6a46a07ce42a4619924d96b075dc93d43be952602876bbb4a59c9f5ce201
[root@VM-4-6-centos /]# docker network inspect mynet
[
{
"Name": "mynet",
"Id": "a08726f536e8dc278bfc7c7750e3ef61c0afb6922bcfc4847a5f905923260a52",
"Created": "2021-09-06T17:15:46.250366164+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": {
"407c6a46a07ce42a4619924d96b075dc93d43be952602876bbb4a59c9f5ce201": {
"Name": "tomcat01-net",
"EndpointID": "80b127167020d0be22f10e91681173bb8b1ac3297019757fa253743bb6b53aac",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
},
"5d5a05421388aa9c67264d27319db6287bb48c236887e9e886a8eaa612f672b3": {
"Name": "tomcat02-net",
"EndpointID": "7569b8f529748201ea1826e1f9a17f67918c5343c888408da7c0d7819dc2184e",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
# 测试ping连接
[root@VM-4-6-centos /]# docker exec -it tomcat01-net ping 192.168.0.2
PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from 192.168.0.2: icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from 192.168.0.2: icmp_seq=3 ttl=64 time=0.055 ms
64 bytes from 192.168.0.2: icmp_seq=4 ttl=64 time=0.053 ms
# 现在不使用 --link 也可以访问
[root@VM-4-6-centos /]# docker exec -it tomcat02-net ping tomcat01-net
PING tomcat01-net (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.050 ms
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.053 ms
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=5 ttl=64 time=0.063 ms
自定义的网络docker都已经帮我们维护好了对应关系,推荐这样使用
好处:
redis-不同的集群使用不同的网络,保证了集群是安全和健康
mysql-不同的集群使用不同的网络,保证了集群是安全和健康
网络连通
测试 mynet网络与容器tomcat01打通
docker network connect mynet tomcat01
查看网络 mynet
docker network inspect mynet
一个容器两个ip
# tomcat01能够访问tomcat01-net
[root@VM-4-6-centos /]# docker exec -it tomcat01 ping tomcat01-net
PING tomcat01-net (192.168.0.3) 56(84) bytes of data.
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=2 ttl=64 time=0.053 ms
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=3 ttl=64 time=0.053 ms
64 bytes from tomcat01-net.mynet (192.168.0.3): icmp_seq=4 ttl=64 time=0.052 ms
# tomcat02不能访问tomcat01-net
[root@VM-4-6-centos /]# docker exec -it tomcat02 ping tomcat01-net
ping: tomcat01-net: Name or service not known
需要跨网络操作别人,就需要用docker network connect 连通网络
部署redis集群
# 创建redis网卡
[root@VM-4-6-centos /]# docker network create redis --subnet 172.38.0.0/16
# 通过脚本创建六个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
# 启动redis容器
# 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
# 以此类推...
# 进入其中一个redis容器,由于redis镜像没有/bin/bash命令所以用/bin/sh命令
[root@VM-4-6-centos /]# docker exec -it redis-1 /bin/sh
/data # ls
appendonly.aof nodes.conf
# 搭建集群
/data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13
:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
redis cli -c是集群 redis cli 是单机
查看节点
docker搭建redis集群完成