当前位置:   article > 正文

[docker]docker4种网络最佳实战

docker 容器 网络 配置 最佳 实践

参考: http://hicu.be/docker-container-network-types
806469-20171218171933584-306457066.png

docker默认3中网络类型

参考: https://docs.docker.com/engine/userguide/networking/

  1. $ docker network ls
  2. NETWORK ID NAME DRIVER
  3. 7fca4eb8c647 bridge bridge
  4. 9f904ee27bf5 none null
  5. cf03ee007fb4 host host
  • bridge 桥接到docker0网卡
  • host 和宿主机共享网卡
  • none 无网卡

手动创建net

手动创建net的类型是 bridge模式

  1. docker network create neta
  2. $ docker network list
  3. NETWORK ID NAME DRIVER SCOPE
  4. 19856a1ad2b4 neta bridge local

最佳实践

  1. - 分别启动b1 b2(neta)
  2. docker run -itd --name=b1 busybox
  3. docker run -itd --name=b2 --net=neta busybox
  4. - 查看他们ip
  5. docker exec -it b1 ip ad
  6. docker exec -it b2 ip ad
  7. - b2 ping b1 不通
  8. docker exec -it b2 ping 172.17.0.2
  9. - 将b1连到neta,再次ping通了
  10. docker network connect neta b1
  11. docker exec -it b2 ping 172.18.0.3
  12. - 查看b1,发现b1多了一块网卡
  13. $ docker exec -it b1 ip ad
  14. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
  15. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  16. inet 127.0.0.1/8 scope host lo
  17. valid_lft forever preferred_lft forever
  18. 10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  19. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  20. inet 172.17.0.2/16 scope global eth0
  21. valid_lft forever preferred_lft forever
  22. 14: eth1@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  23. link/ether 02:42:ac:12:00:03 brd ff:ff:ff:ff:ff:ff
  24. inet 172.18.0.3/16 scope global eth1
  25. valid_lft forever preferred_lft forever
  26. - 自定义的网卡自带dns
  27. $ docker exec -it b1 hostname
  28. 655e40ab3eed
  29. $ docker exec -it b2 hostname
  30. 3c821d2e8b05
  31. $ docker exec -it b2 ping 655e40ab3eed
  32. PING 655e40ab3eed (172.18.0.3): 56 data bytes
  33. 64 bytes from 172.18.0.3: seq=0 ttl=64 time=0.085 ms
  34. 64 bytes from 172.18.0.3: seq=1 ttl=64 time=0.129 ms
  35. - 默认dns是
  36. docker exec -it b2 cat /etc/resolv.conf
  37. nameserver 127.0.0.11

共享另一个容器的网卡

  1. - 启动b1
  2. $ docker run -itd --name b1 busybox
  3. - 启动b2,使之使用b1的网卡
  4. $ docker run -itd --net=container:b1 --name b2 busybox
  5. - 查看b1和b2的网卡 发现ip和mac一致
  6. $ docker exec b1 ip ad
  7. 4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  8. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  9. inet 172.17.0.2/16 scope global eth0
  10. valid_lft forever preferred_lft forever
  11. $ docker exec b2 ip ad
  12. 4: eth0@if5: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue
  13. link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
  14. inet 172.17.0.2/16 scope global eth0
  15. valid_lft forever preferred_lft forever
  • dokcer cnm网络模式
    9e792b8fgy1fml29u3ogwj20pz03i0t1.jpg

9e792b8fgy1fml28fq7icj20fz0amae2.jpg

  • 网络插件

docker 原生的 driver 包括 none、bridge、overlay 和 macvlan

第三方 driver 包括 flannel、weave、calico 等。

9e792b8fgy1fml24zhdbcj20hf09d415.jpg

  • 加参数配置
    9e792b8fgy1fml25e1rs9j20hq09zt9l.jpg

  • docker和k8s的对比
    9e792b8fgy1fml25sdgzhj20hq0dbgn9.jpg

docker网卡桥接
9e792b8fgy1fml25q3z93j20ch08qtan.jpg

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/501332
推荐阅读
相关标签
  

闽ICP备14008679号