05day
自定义网络
![](https://i-blog.csdnimg.cn/blog_migrate/58d0ea6c5cc07a6a3f3dc0f3e4dd6456.png)
bridge: 桥接docker(默认)
none: 不配置网络
host: 和宿主机共享网络
container: 容器网络连通!(用的少 局限很大)
docker run -d -p --name tomcat01 tomcat
docker run -d -P --name tomcat01 --net bridge tomcat
root@fanwd-virtual-machine:/home/fanw
a90f2d21095c15974e69e1f87e2bb4afac8e6fc0e33bf1629fff37399c96ad0f
root@fanwd-virtual-machine:/home/fanw
NETWORK ID NAME DRIVER SCOPE
743f4d60fb2a bridge bridge local
45e0f08bad2d host host local
a90f2d21095c mynet bridge local
root@fanwd-virtual-machine:/home/fanw
09c4ec8051ff8d58eca0fb8ca4a0fede1cfb11ea59f53c7f0d569c565804ec3c
root@fanwd-virtual-machine:/home/fanw
oot@fanwd-virtual-machine:/home/fanw
[
{
"Name": "mynet",
"Id": "a90f2d21095c15974e69e1f87e2bb4afac8e6fc0e33bf1629fff37399c96ad0f",
"Created": "2021-03-30T15:13:18.978117034+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": ""
},
"09c4ec8051ff8d58eca0fb8ca4a0fede1cfb11ea59f53c7f0d569c565804ec3c": {
"Name": "tomcat-net-01",
"EndpointID": "5de4836259be19a1e2abd368238fbe1ae9e053f5dd71b7a2162648cfc631b9fa",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"ff3869de8750e112817ec904a4745f66330609cf1367d44072026bedc8cf8ac2": {
"Name": "tomcat-net-02",
"EndpointID": "db9f37c94464f539a0991a6709ff810be37a9195650a34ddbf0dbcb57fd1b0ec",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
root@fanwd-virtual-machine:/home/fanw
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.078 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.047 ms
64 bytes from 192.168.0.3: icmp_seq=3 ttl=64 time=0.047 ms
^C
--- 192.168.0.3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 40ms
rtt min/avg/max/mdev = 0.047/0.057/0.078/0.015 ms
redis 不同的集群使用不同的网络,保证集群是安全和健康的
mysql 不同的集群使用不同的网络,保证集群是安全和健康的
网络连通
root@fanwd-virtual-machine:/home/fanw
d57136a8ca18efa3395a3f84eaf8f464cebb97aa34db5f0989aa65e89fe5cfe5
root@fanwd-virtual-machine:/home/fanw
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d57136a8ca18 tomcat "catalina.sh run" 8 seconds ago Up 6 seconds 0.0.0.0:49155->8080/tcp tomcat03
ff3869de8750 tomcat "catalina.sh run" 9 minutes ago Up 9 minutes 0.0.0.0:49154->8080/tcp tomcat-net-02
09c4ec8051ff tomcat "catalina.sh run" 20 minutes ago Up 20 minutes 0.0.0.0:49153->8080/tcp tomcat-net-01
root@fanwd-virtual-machine:/home/fanw
ping: tomcat-net-01: Name or service not known
"09c4ec8051ff8d58eca0fb8ca4a0fede1cfb11ea59f53c7f0d569c565804ec3c": {
"Name": "tomcat-net-01",
"EndpointID": "5de4836259be19a1e2abd368238fbe1ae9e053f5dd71b7a2162648cfc631b9fa",
"MacAddress": "02:42:c0:a8:00:02",
"IPv4Address": "192.168.0.2/16",
"IPv6Address": ""
},
"d57136a8ca18efa3395a3f84eaf8f464cebb97aa34db5f0989aa65e89fe5cfe5": {
"Name": "tomcat03",
"EndpointID": "c119b954c90a92313690b51427a53f2ad52cd92e217b24566e7eae88631b012b",
"MacAddress": "02:42:c0:a8:00:04",
"IPv4Address": "192.168.0.4/16",
"IPv6Address": ""
},
"ff3869de8750e112817ec904a4745f66330609cf1367d44072026bedc8cf8ac2": {
"Name": "tomcat-net-02",
"EndpointID": "db9f37c94464f539a0991a6709ff810be37a9195650a34ddbf0dbcb57fd1b0ec",
"MacAddress": "02:42:c0:a8:00:03",
"IPv4Address": "192.168.0.3/16",
"IPv6Address": ""
root@fanwd-virtual-machine:/home/fanw
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.076 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=2 ttl=64 time=0.045 ms
64 bytes from tomcat-net-01.mynet (192.168.0.2): icmp_seq=3 ttl=64 time=0.054 ms
^C
--- tomcat-net-01 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 58ms
rtt min/avg/max/mdev = 0.045/0.058/0.076/0.014 ms
跨网络操作别人 需要connet连通
实战:Redis 集群部署
root@fanwd-virtual-machine:/home/fanw
fa0fe776f64c3e5ff66c37479dbba1def395f9982f1e2e5d4044aed76212386a
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.38.0.0/16"
}
]
},
root@fanwd-virtual-machine:/home/fanw
> 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-announce-ip 172.38.0.1${port}
> cluster-announce-port 6379
> cluster-announce-bus-port 16379
> appendonly yes
> EOF
> done
![](https://i-blog.csdnimg.cn/blog_migrate/71d489a9e873490ca37d4a67ede6be62.png)
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
开启6个Redis
![](https://i-blog.csdnimg.cn/blog_migrate/90e83a116021fc544c2e8aaab179c153.png)
![](https://i-blog.csdnimg.cn/blog_migrate/65c4d23c8e9eded84b77fa7531cbc884.png)
![](https://i-blog.csdnimg.cn/blog_migrate/78bd4fb2ddeaf6eb2ac5ee36ef69c523.png)