image.png

evth-pair

启动的容器是带网卡的,并且是成对的 evth-pair 就是一队虚拟设备接口,成对出现,一端连接协议,一端彼此相连 Openstac、Docker容器之间的连接,ovs的连接,都是用的 evth-pair 技术

image.png

测试连通性

  1. [root@localhost ~]# docker exec -it tomcat02 ping 172.17.0.2
  2. PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
  3. 64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.374 ms
  4. 64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.092 ms
  5. #容器和容器之间是可以通信的

image.png

—link

单项绑定网络,本质就是写个host指向

  1. [root@localhost ~]# docker run -d -P --name tomcat03 --link tomcat02 tomcat
  2. WARNING: IPv4 forwarding is disabled. Networking will not work.
  3. c7c93fa009c3833788a0b98045c68be2fa37b0e543adbd7f4bcc3a1291dcf2c0
  4. [root@localhost ~]# docker exec -it tomcat03 ping tomcat02
  5. PING tomcat02 (172.17.0.3) 56(84) bytes of data.
  6. 64 bytes from tomcat02 (172.17.0.3): icmp_seq=1 ttl=64 time=0.291 ms
  7. 64 bytes from tomcat02 (172.17.0.3): icmp_seq=2 ttl=64 time=0.116 ms
  8. 64 bytes from tomcat02 (172.17.0.3): icmp_seq=3 ttl=64 time=0.123 ms
  9. 64 bytes from tomcat02 (172.17.0.3): icmp_seq=4 ttl=64 time=0.126 ms

自定义网络

image.png

网络模式: bridge:桥接docker none:不配置网络 host:和宿主机共享 container:容器网络互连(用的少,局限性大)

自定义网络好处: 可以直接维护对应的网络表不需要再一个个link,平时强烈推荐使用

  1. [root@localhost ~]# docker network create --driver bridge --subnet 192.168.1.0/24 --gateway 192.168.1.1 diynet
  2. 9a1ff71f54dc9ae2d53db1aea234b6eb1c519afea23650fe8e30a5316a38f25c
  3. [root@localhost ~]# docker network ls
  4. NETWORK ID NAME DRIVER SCOPE
  5. 6474be779eb7 bridge bridge local
  6. 9a1ff71f54dc diynet bridge local
  7. 93cf61d9aac6 host host local
  8. ea6557cc9465 none null local
  9. [root@localhost ~]# docker network inspect diynet
  10. [
  11. {
  12. "Name": "diynet",
  13. "Id": "9a1ff71f54dc9ae2d53db1aea234b6eb1c519afea23650fe8e30a5316a38f25c",
  14. "Created": "2021-07-27T21:38:38.270250531+08:00",
  15. "Scope": "local",
  16. "Driver": "bridge",
  17. "EnableIPv6": false,
  18. "IPAM": {
  19. "Driver": "default",
  20. "Options": {},
  21. "Config": [
  22. {
  23. "Subnet": "192.168.1.0/24",
  24. "Gateway": "192.168.1.1"
  25. }
  26. ]
  27. },
  28. "Internal": false,
  29. "Attachable": false,
  30. "Ingress": false,
  31. "ConfigFrom": {
  32. "Network": ""
  33. },
  34. "ConfigOnly": false,
  35. "Containers": {},
  36. "Options": {},
  37. "Labels": {}
  38. }
  39. ]
  40. # 指定自定义网络创建
  41. [root@localhost ~]# docker run -d -P --name tomcat01 --net diynet tomcat
  42. WARNING: IPv4 forwarding is disabled. Networking will not work.
  43. a55408b856e231251a797cc714f9bc6c6945d03b4b507e2fe5392953d9078d64
  44. [root@localhost ~]# docker run -d -P --name tomcat02 --net diynet tomcat
  45. WARNING: IPv4 forwarding is disabled. Networking will not work.
  46. b36969db2005b9be92b6d1a7dee86bac689713dd98410606c851c8f5a178c9fc
  47. # 再次查看diynet详情
  48. [root@localhost ~]# docker network inspect diynet
  49. [
  50. {
  51. "Name": "diynet",
  52. "Id": "9a1ff71f54dc9ae2d53db1aea234b6eb1c519afea23650fe8e30a5316a38f25c",
  53. "Created": "2021-07-27T21:38:38.270250531+08:00",
  54. "Scope": "local",
  55. "Driver": "bridge",
  56. "EnableIPv6": false,
  57. "IPAM": {
  58. "Driver": "default",
  59. "Options": {},
  60. "Config": [
  61. {
  62. "Subnet": "192.168.1.0/24",
  63. "Gateway": "192.168.1.1"
  64. }
  65. ]
  66. },
  67. "Internal": false,
  68. "Attachable": false,
  69. "Ingress": false,
  70. "ConfigFrom": {
  71. "Network": ""
  72. },
  73. "ConfigOnly": false,
  74. "Containers": {
  75. "a55408b856e231251a797cc714f9bc6c6945d03b4b507e2fe5392953d9078d64": {
  76. "Name": "tomcat01",
  77. "EndpointID": "e3219aac6eae7461c74b6aefdee19a0ba6c05395af1f6f1e00e66f55b791e208",
  78. "MacAddress": "02:42:c0:a8:01:02",
  79. "IPv4Address": "192.168.1.2/24",
  80. "IPv6Address": ""
  81. },
  82. "b36969db2005b9be92b6d1a7dee86bac689713dd98410606c851c8f5a178c9fc": {
  83. "Name": "tomcat02",
  84. "EndpointID": "fa7351d0f4bb725385247bff70820dad417e3292bd774be58ab9ff4fdb79633f",
  85. "MacAddress": "02:42:c0:a8:01:03",
  86. "IPv4Address": "192.168.1.3/24",
  87. "IPv6Address": ""
  88. }
  89. },
  90. "Options": {},
  91. "Labels": {}
  92. }
  93. ]
  94. # 测试结果
  95. [root@localhost ~]# docker exec -it tomcat01 ping 192.168.1.3
  96. PING 192.168.1.3 (192.168.1.3) 56(84) bytes of data.
  97. 64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.185 ms
  98. 64 bytes from 192.168.1.3: icmp_seq=2 ttl=64 time=0.121 ms
  99. 64 bytes from 192.168.1.3: icmp_seq=2 ttl=64 time=0.131 ms
  100. [root@localhost ~]# docker exec -it tomcat01 ping tomcat02
  101. PING tomcat02 (192.168.1.3) 56(84) bytes of data.
  102. 64 bytes from tomcat02.diynet (192.168.1.3): icmp_seq=1 ttl=64 time=0.130 ms
  103. 64 bytes from tomcat02.diynet (192.168.1.3): icmp_seq=2 ttl=64 time=0.148 ms
  104. 64 bytes from tomcat02.diynet (192.168.1.3): icmp_seq=3 ttl=64 time=0.082 ms

网络连通

image.png
image.png

实际上打通就是将另一个容器的ip放到了自定义网络下 也就是一个容器两个ip段

  1. [root@localhost ~]# docker run -d -P --name tomcat-net01 --net diynet tomcat
  2. WARNING: IPv4 forwarding is disabled. Networking will not work.
  3. d9ed9f6cbab8688c4cc9583d7cd7534e2b1b99aa0c4cc2af2c136896cab77308
  4. [root@localhost ~]# docker run -d -P --name tomcat-net02 --net diynet tomcat
  5. WARNING: IPv4 forwarding is disabled. Networking will not work.
  6. 4495e3e542d95d81f0a986a8908138904246b162829f1672b4aa42f6a5ebde66
  7. [root@localhost ~]# docker run -d -P --name tomcat01 tomcat
  8. WARNING: IPv4 forwarding is disabled. Networking will not work.
  9. 3feb35fd38e67bb350e52a4b7d35cc6fa871a7e23542b2504505072027503374
  10. [root@localhost ~]# docker run -d -P --name tomcat02 tomcat
  11. WARNING: IPv4 forwarding is disabled. Networking will not work.
  12. cf7adcd0e974cbf5cbc9d117f6b7dbfccf9a52ef02a32f3679950b28f89f8516
  13. # connect打通容器和自定义网络
  14. [root@localhost ~]# docker network connect diynet tomcat01
  15. [root@localhost ~]# docker network connect diynet tomcat02
  16. # 查看效果
  17. [root@localhost ~]# docker network inspect diynet
  18. [
  19. {
  20. "Name": "diynet",
  21. "Id": "9a1ff71f54dc9ae2d53db1aea234b6eb1c519afea23650fe8e30a5316a38f25c",
  22. "Created": "2021-07-27T21:38:38.270250531+08:00",
  23. "Scope": "local",
  24. "Driver": "bridge",
  25. "EnableIPv6": false,
  26. "IPAM": {
  27. "Driver": "default",
  28. "Options": {},
  29. "Config": [
  30. {
  31. "Subnet": "192.168.1.0/24",
  32. "Gateway": "192.168.1.1"
  33. }
  34. ]
  35. },
  36. "Internal": false,
  37. "Attachable": false,
  38. "Ingress": false,
  39. "ConfigFrom": {
  40. "Network": ""
  41. },
  42. "ConfigOnly": false,
  43. "Containers": {
  44. "3feb35fd38e67bb350e52a4b7d35cc6fa871a7e23542b2504505072027503374": {
  45. "Name": "tomcat01",
  46. "EndpointID": "8800598e6f75e197295663d51d808abdff7f61a4f9c2d6b6818e40d0dc6278fb",
  47. "MacAddress": "02:42:c0:a8:01:04",
  48. "IPv4Address": "192.168.1.4/24",
  49. "IPv6Address": ""
  50. },
  51. "4495e3e542d95d81f0a986a8908138904246b162829f1672b4aa42f6a5ebde66": {
  52. "Name": "tomcat-net02",
  53. "EndpointID": "5e492c846949d0772b23c835f964e8e9974531d467a3f9c1250278d7b36febbf",
  54. "MacAddress": "02:42:c0:a8:01:03",
  55. "IPv4Address": "192.168.1.3/24",
  56. "IPv6Address": ""
  57. },
  58. "cf7adcd0e974cbf5cbc9d117f6b7dbfccf9a52ef02a32f3679950b28f89f8516": {
  59. "Name": "tomcat02",
  60. "EndpointID": "fa4270d46f4a22abe1663859714b349caf314026e844881843fb1cec5c3e6db4",
  61. "MacAddress": "02:42:c0:a8:01:05",
  62. "IPv4Address": "192.168.1.5/24",
  63. "IPv6Address": ""
  64. },
  65. "d9ed9f6cbab8688c4cc9583d7cd7534e2b1b99aa0c4cc2af2c136896cab77308": {
  66. "Name": "tomcat-net01",
  67. "EndpointID": "35d242d10811dad92f8b1207045bb18627ffba9ecc004706ae2eea0bb009a5af",
  68. "MacAddress": "02:42:c0:a8:01:02",
  69. "IPv4Address": "192.168.1.2/24",
  70. "IPv6Address": ""
  71. }
  72. },
  73. "Options": {},
  74. "Labels": {}
  75. }
  76. ]
  77. # 测试
  78. [root@localhost ~]# docker exec -it tomcat01 ping tomcat-net01
  79. PING tomcat-net01 (192.168.1.2) 56(84) bytes of data.
  80. 64 bytes from tomcat-net01.diynet (192.168.1.2): icmp_seq=1 ttl=64 time=0.347 ms
  81. 64 bytes from tomcat-net01.diynet (192.168.1.2): icmp_seq=2 ttl=64 time=0.692 ms
  82. 64 bytes from tomcat-net01.diynet (192.168.1.2): icmp_seq=3 ttl=64 time=0.118 ms
  83. [root@localhost ~]# docker exec -it tomcat01 ping tomcat-net02
  84. PING tomcat-net02 (192.168.1.3) 56(84) bytes of data.
  85. 64 bytes from tomcat-net02.diynet (192.168.1.3): icmp_seq=1 ttl=64 time=0.189 ms
  86. 64 bytes from tomcat-net02.diynet (192.168.1.3): icmp_seq=2 ttl=64 time=0.126 ms

集群部署

  1. # 通过shell脚本来创建六个redis配置
  2. for port in $(seq 1 6);
  3. do
  4. mkdir -p /mydata/redis/node-${port}/conf
  5. touch /mydata/redis/node-${port}/conf/redis.conf
  6. cat << eof >/mydata/redis/node-${port}/conf/redis.conf
  7. port 6379
  8. bind 0.0.0.0
  9. cluster-enabled yes
  10. cluster-node-timout 5000
  11. cluster-announce-ip 172.38.0.1${port}
  12. cluster-announce-bus-port 16379
  13. appendonly yes
  14. eof
  15. done
  16. # shell脚本启动
  17. for port in $(seq 1 6);
  18. do
  19. docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \
  20. -v /mydata/redis/node-${port}/data:/data \
  21. -v /mydata/redis/node-${port}/conf/redis.conf:/etc/redis/redis.conf \
  22. -d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf;
  23. done
  24. # 创建集群
  25. 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
  26. # 进入集群
  27. docker exec -it redis-1 /bin/sh
  28. redis-cli -c
  29. cluster info
  30. cluster nodes
  31. # set 值,查看哪个节点处理了
  32. set a b
  33. # 关闭当前运行的master节点,再查看集群
  34. docker stop redis-3
  35. redis-cli -c
  36. get a