docker网络.md

#docker 网络模式

环境 centos7.4 , Docker version 17.12.0-ce

docker自带网络类型 bridge,host,none,container,overlay,macvlan

#本地网络

bridge 		# --net=bridge 默认模式,NAT转发
host # --net=host 使用宿主机网络
container # --net=container:容器名或ID ,共用其它容器网络
none # --net=none 无网卡

#跨主机网络

overlay  #vxlan模式
macvlan #使用外部lan,需手动配置
#可以使用其它网络插件

#查看docker网络

docker network ls

#docker修改本地默认网段

#docker启动默认创建docker0网桥172.17网段
#添加自定义网段
SetOPTS=" --bip=192.168.55.1/24"
sed -i "s#^ExecStart.*#& $SetOPTS #" /usr/lib/systemd/system/docker.service
grep 'ExecStart' /usr/lib/systemd/system/docker.service
#重启docker
systemctl daemon-reload
systemctl restart docker ifconfig docker0 |egrep 'inet |flags' #查看docker0网桥ip已改
#可使用自定义网桥,创建br0,修改配置 -b=br0

#docker本地网络实践

#不指定网络,使用默认bridge
docker run -dit --name busybox1 busybox
docker exec -it busybox1 ifconfig #查看网络 #host网络
docker run -dit --name busybox-host --net=host busybox
docker exec -it busybox-host ifconfig #查看网络,与宿主机一样 #container网络
docker run -dit --name busybox2 --net=container:busybox1 busybox #使用busybox1网络
docker exec -it busybox1 ifconfig #查看网络 #none没网络
docker run -dit --name busybox-none --net=none busybox
docker exec -it busybox-none ifconfig

#创建本地网络mynet1(自定义网络能设置固定ip)

docker network create --subnet 192.168.58.0/24 --gateway 192.168.58.1 mynet1
docker run -dit --name busybox3 --network mynet1 --ip=192.168.58.18 busybox
docker exec busybox3 ifconfig |egrep 'inet|flags' #查看容器网络

#docker 容器 添加网卡

docker network connect mynet1 --ip 192.168.58.8 busybox1  #给容器添ip
docker exec $(docker ps -l -q) ifconfig |egrep 'inet|flags' #查看网络
docker network disconnect mynet1 busybox1 #断开网络,再次查看网络……

#删除测试容器、网络

docker rm -f busybox1 busybox2 busybox3 busybox-host busybox-none
docker network rm mynet1

#docker跨主机通信

#overlay网络

本次在docker swarm集群操作

创建docker swarm 集群参考http://www.cnblogs.com/elvi/p/8424378.html

docker swarm集群创建容器,会自动创建overlay网络ingress

docker network ls #查看网络

#创建overlay类型网络mynet2,并设置网段、网关

docker network create -d overlay --subnet 192.168.59.0/24 --gateway 192.168.59.1 mynet2
docker network inspect mynet2 #查看网络参数

#swarm集群使用自定义网段mynet2

docker service create --name nginx2  --replicas 2 \
-p 81:80 --network mynet2 hub.test.com:5000/almi/nginx:0.1
#hub.test.com:5000/almi/nginx:0.1 为内网私有仓库镜像
docker service ls
#swarm集群创建容器默认2个网卡,增加了自定义网卡
docker exec $(docker ps -l -q) ifconfig |egrep 'inet|flags' #查看网络

#删除测试

docker service rm nginx2
docker network rm mynet2
docker network prune #删除未使用的网卡

# macvlan网络

#宿主机开启网卡混杂模式

ip link set eth0 promisc on
ifconfig eth0 |egrep 'inet |flags'

#创建macvlan模式网络macnet

docker network create -d macvlan -o parent=eth0 macnet \
--subnet 172.16.50.0/24 --gateway 172.16.50.1

#创建容器

docker run -d --name nginx3 --net macnet hub.test.com:5000/almi/nginx:0.1
docker exec nginx3 ping -c 4 nginx3 #测试获取ip成功
#其它docker节点创建网络、容器,然后ping测试

#删除测试

docker rm -f $(docker ps -l)
docker network rm macnet

本次macvlan测试环境为虚拟机,获取外部vlan失败

宿主机配置vlan网络+macvlan模式,实现docker vlan网络

使用macvlan,无法与宿主机通信

最新文章

  1. 常用.NET库使用总结
  2. C#模拟http 发送post或get请求
  3. 定制Eclipse IDE之功能篇(一)
  4. 0511 backlog 项目管理
  5. Oracle10G的Sga_max_size和sga_target应该如何设置啊!
  6. Android横竖屏切换处理
  7. file_operations结构2
  8. VS2010中xercesc配置及简单示例
  9. 【C++继承与派生之二】有子对象的派生类的构造函数
  10. Hadoop 实现多文件输出
  11. mongodb 慢SQL查询
  12. ch11 持有对象
  13. [GitHub]第四讲:合并分支
  14. 备忘录模式 Memento 快照模式 标记Token模式 行为型 设计模式(二十二)
  15. 快速搭建react项目骨架(按需加载、redux、axios、项目级目录等等)
  16. saltstack部署配置
  17. shell编程学习笔记(九):Shell中的case条件判断
  18. SecureCRT问题
  19. easyui-switchbutton js判断是否启动
  20. WC----命令行实现对文件信息的统计

热门文章

  1. Scrum培训心得体会
  2. word字号
  3. LeetCode 70 Climbing Stairs(爬楼梯)(动态规划)(*)
  4. 介绍编译的less的几种IDE工具
  5. gch文件学习
  6. 离线 + 位优化 - SGU 108 Self-numbers 2
  7. josn 格式 解析
  8. php -- PHP5中file_get_contents函数获取带BOM的utf-8文件内容
  9. Codeforces Round #265 (Div. 2)
  10. IOC和AOP的一些基本概念