创建网络语法格式
默认情况下只有一个物理网卡,只有一个物理地址,macvlan可以虚拟出多个mac地址,从而去和其他宿主机的容器进行通讯
使用macvlan通讯时必须指定ip,都这默认从第一个ip开始分配
一个网卡只能创建一个网络类型
缺点:不能与宿主机的ip地址进行通讯
两台docker宿主机都需要做
[root@docker01 ~]# docker network create --driver macvlan --subnet 192.168.81.0/24 --gateway 192.168.81.2 -o parent=ens33 macvlan1
cc64864688d381d0fac00aba628d704cf99eeec5571a61710701d42693118a1e
[root@docker02 ~]# docker network create --driver macvlan --subnet 192.168.81.0/24 --gateway 192.168.81.2 -o parent=ens33 macvlan1
cc64864688d381d0fac00aba628d704cf99eeec5571a61710701d42693118a1e
docker1
[root@docker01 ~]# docker run -it --network macvlan1 --ip=192.168.81.10 centos69_ssh_df:v3 /bin/bash
docker2
[root@docker02 ~]# docker run -it --network macvlan1 --ip=192.168.81.20 nginx:latest /bin/bash
[root@ae9c8fc4562f /]# ping -c1 192.168.81.20
PING 192.168.81.20 (192.168.81.20) 56(84) bytes of data.
64 bytes from 192.168.81.20: icmp_seq=1 ttl=64 time=0.321 ms
--- 192.168.81.20 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.321/0.321/0.321/0.000 ms
[root@ae9c8fc4562f /]#
[root@docker01 ~]# ip link set ens33 promisc on
设置网卡为混杂模式
IP地址 | 角色类型 |
---|---|
192.168.81.210 | docker01 |
192.168.81.220 | docker02 |
192.168.81.230 | docker-consul |
[root@nfs ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"live-restore":true
}
[root@docker03 ~]# docker run -d -p 8500:8500 -h consul --name consul progrium/consul -server --bootstrap
参数:
-d 后台
-p 端口映射
-h 指定容器主机名
--name 容器名称
-server --bootstrap是consul的参数
docker01
[root@docker01 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"live-restore": true,
"cluster-store": "consul://192.168.81.230:8500",
"cluster-advertise": "192.168.81.210:2376"
}
[root@docker01 ~]# systemctl restart docker
docker02
[root@docker01 ~]# vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"],
"live-restore": true,
"cluster-store": "consul://192.168.81.230:8500",
"cluster-advertise": "192.168.81.220:2376"
}
[root@docker01 ~]# systemctl restart docker
[root@docker01 ~]# docker network create -d overlay overlay_1
b99266d65640b5e45fbf9f2f58ff91864e8d543a43402c23c4e542f9aa595367
docker01上
[root@docker01 ~]# docker run -it --network overlay_1 --name ssh01 centos69_ssh_df:v3 /bin/bash
docker02上
[root@docker02 ~]# docker run -it --network overlay_1 --name ssh02 centos69_ssh_df:v3 /bin/bash
两边都能ping通说明成功
docker network inspect overlay_1都能看到那些容器使用了什么ip
在启动文件中开启2375端口
[root@docker01 ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
重载docker
[root@docker01 ~]# systemctl daemon-reload
[root@docker01 ~]# systemctl restart docker
远程操作
[root@docker01 ~]# docker -H 192.168.81.220 images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos69_ssh_df v3 2ccb494fa7b0 8 days ago 367MB
jumpserver/jms_all latest a42e2037b4a5 2 weeks ago 1.46GB
nginx latest 2622e6cca7eb 3 weeks ago 132MB
httpd 2.4 ccbcea8a6757 3 weeks ago 166MB
1)已经使用着的overlay删除时会报以下错误
这个报错表示有其他容器再使用overlay_1的网络
[root@docker01 ~]# docker network rm overlay_1
Error response from daemon: error while removing network: network overlay_1 id 717bde8563a1dcc6f2e713e90b4a503dce778a63789d08dcaa72b91769bc99e6 has active endpoints
2)使用docker network inspect overlay_1看他的两个参数
[root@docker01 ~]# docker network inspect overlay_1
找到参数1和参数2
3)使用docker network disconnect 后面跟着这两个参数执行即可
[root@docker01 ~]# docker network disconnect -f overlay_1 ssh_3
disconnect表示端口一个容器和某个网络的连接
[root@docker01 ~]# docker network rm overlay_1
overlay_1