docker0

安装docker的主机会有一个docker0网卡(桥接模式),使用的是evth-pair技术
evth-pair是虚拟设备接口,成对出现
ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    inet 172.17.0.16/20 brd 172.17.15.255 scope global eth0...
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default
    inet 172.18.0.1/16 brd 172.18.255.255 scope global docker0...

/16是网络标识位,子网掩码是255.255.0.0,/24为255.255.255.0

容器启动的时候docker会分配给容器一个eth@ifxxx IP地址,可以在外部ping通
docker run -it -P --name tomcat01 tomcat
docker exec -it b4da99b29aef /bin/bash
ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
62: eth0@if63: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    inet 172.18.0.2/16 brd 172.18.255.255 scope global eth0
       valid_lft forever preferred_lft forever
每启动一个容器,宿主机也会多一个网卡
63: vethb4f3058@if62: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default 
    link/ether f6:e4:da:9e:1c:bc brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::f4e4:daff:fe9e:1cbc/64 scope link 
       valid_lft forever preferred_lft forever
结论:容器间可以相互ping通
docker run -d -P --name tomcat02 tomcat
docker exec -it tomcat02 /bin/bash
ping ping 172.18.0.2

image.png

--link

docker run -d -P --name tomcat03 --link tomcat02 tomcat

docker exec -it tomcat03 ping tomcat02

查看网络

docker network ls

docker network inspect 网卡ID

--link 就是在tomecat03容器的hosts中写入172.18.0.3 tomcat02 容器ID

缺点:docker0不支持容器名访问!

自定义网络

创建自定义网络

docker network create --driver bridge --subnet 192.168.0.0/16 --gateway 192.168.0.1 mynet
 
docker network ls

NETWORK ID     NAME      DRIVER    SCOPE
e7e2c109f027   mynet     bridge    local

--driver bridge 桥接
--subnet 子网
--gateway 网关

docker run 默认执行 --net bridge 参数
使用自定义网络

docker run -d -P --name tomcat01 --net mynet tomcat
docker run -d -P --name tomcat02 --net mynet tomcat

docker exec -it tomcat01 ping tomcat02

网络联通

docker network connect 网络 容器名

测试

docker run -d -P --name tomcat03 tomcat

#接入网络
docker network connect mynet tomcat03

#测试
docker exec -it tomcat01 ping tomcat03

#查看网络
docker inspect mynet

"Containers": {
            "02a670496edfd99d1542ddc3f871d53b5e66295c1b9228f119f9262ee362bcc4": {
                "Name": "tomcat03",
                "EndpointID": "ee626c9944c7732c10234feb78d7e6e59877d4ad3c531690cbf6af23103cb581",
                "MacAddress": "02:42:c0:a8:00:04",
                "IPv4Address": "192.168.0.4/16",
                "IPv6Address": ""
            },
            "b42c7b942d856b83af449e14696dc773f34d63acc5ca6d576ce2493763cc6d89": {
                "Name": "tomcat01",
                "EndpointID": "392deab3c2848252c99b122f655ed903c2f7b33b136a585dfb845105cb24150a",
                "MacAddress": "02:42:c0:a8:00:02",
                "IPv4Address": "192.168.0.2/16",
                "IPv6Address": ""
            },
            "e84de7e8d89aa59ffd866ec4620a13c4b49fa510dac6989cdba9b6b870602571": {
                "Name": "tomcat02",
                "EndpointID": "4461c14570c1a2fa87b43c15843b295af6f2a4551f9a7fcda5a27418cf6b8450",
                "MacAddress": "02:42:c0:a8:00:03",
                "IPv4Address": "192.168.0.3/16",
                "IPv6Address": ""
            }
        },

Redis部署 分片+高可用+负载均衡

1.创建网卡

docker network create redis --subnet 172.38.0.0/16

2.通过shell脚本创建6个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

3.启动docker容器1-6

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

...2-6

4.进入容器,搭建集群

docker exec -it redis-1 /bin/sh

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

Can I set the above configuration? (type 'yes' to accept): yes

5.测试集群

# redis-cli -c

#集群信息
127.0.0.1:6379> cluster info
#集群节点
127.0.0.1:6379> cluster nodes
#测试数据
127.0.0.1:6379> set a b
-> Redirected to slot [15495] located at 172.38.0.13:6379
#停掉redis-3
docker stop redis-3
#获取测试数据
127.0.0.1:6379> get a
-> Redirected to slot [15495] located at 172.38.0.14:6379
"b"

打包Docker镜像

1.构架项目
2.打包应用
3.编写Dockerfile
4.构建镜像docker build -t
5.发布运行docker push


IT小马
1.2k 声望166 粉丝

Php - Go - Vue - 云原生