简体   繁体   English

Docker容器访问大量IP

[英]Docker container access to swarm IPs

When running docker containers a swarm cluster do the containers have access to all the IPs of the cluster nodes via ENV variables or otherwise? 在运行Docker容器群群集时,容器是否可以通过ENV变量或其他方式访问群集节点的所有IP?

I want to run an Elasticsearch instance on each node in my swarm the cluster. 我想在集群的每个节点上运行一个Elasticsearch实例。 And they will discovery each other in unicast mode. 他们将以单播模式彼此发现。 Therefore each Elasticsearch instanc needs to be configured with the list of IPs in the cluster. 因此,每个Elasticsearch实例都需要配置集群中的IP列表。

If you mean that container of one node can access container's IP of other node , then it is not possible . 如果您的意思是一个节点的容器可以访问另一节点的容器的IP,那么这是不可能的。 You have to use weave tool to connect container across different node or other tool . 您必须使用编织工具跨不同节点或其他工具连接容器。

If you are using latest Docker (1.13+) with a swam overlay network, you should be able to get all the cluster's node IPs through DNS round robin.(--endpoint-mode dnsrr) 如果您正在使用带有覆盖网络的最新Docker(1.13+),则应该能够通过DNS轮询获得所有群集的节点IP。(-endpoint-mode dnsrr)

1) Create an overlay network. 1)创建一个覆盖网络。

https://docs.docker.com/engine/swarm/networking/ https://docs.docker.com/engine/swarm/networking/

docker network create \
  --driver overlay \
  my-network

2) Verify swam nodes: 2)验证游泳节点:

docker@node1:~$ docker node ls
ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS
5l07yt2itiee60xfq7g6c01e4 *   node1               Ready               Active              Leader
pckn7qo3xpbxvs89ni6whyql3     node2               Ready               Active              

3) Create an alpine container on each nodes using "global" mode: 3)使用“全局”模式在每个节点上创建一个高山容器:

docker service create --mode global --endpoint-mode dnsrr --name testservice --detach=true --network my-network alpine ash -c "apk update;apk add drill; ping docker.com"

4) verify service is running: 4)验证服务是否正在运行:

docker@node1:~$ docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
lmy5s3flw763        testservice         global              2/2                 alpine:latest 

5) Verify that containers were deployed on individual nodes: 5)验证容器已部署在各个节点上:

$ docker-machine ssh node1 "docker ps"
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
4c7055b01479        alpine:latest       "ash -c 'apk updat..."   2 minutes ago       Up 2 minutes                            testservice.5l07yt2itiee60xfq7g6c01e4.atvascigh3rvxvlzttaotkrua

$ docker-machine ssh node2 "docker ps"
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
28da546aa0d5        alpine:latest       "ash -c 'apk updat..."   2 minutes ago       Up 2 minutes                            testservice.pckn7qo3xpbxvs89ni6whyql3.ebjz4asni4w1f0srna0p3vj4a

6) Confirm individual virtual IP of each containers on node1 and node2: 6)确认节点1和节点2上每个容器的单独虚拟IP:

| => docker-machine ssh node1 "docker exec 4c7055b01479 ash -c 'ip addr'|grep eth0"
349: eth0@if350: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue state UP 
    inet 10.0.0.2/24 scope global eth0


| => docker-machine ssh node2 "docker exec 28da546aa0d5 ash -c 'ip addr'|grep eth0"
319: eth0@if320: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1450 qdisc noqueue state UP 
    inet 10.0.0.3/24 scope global eth0

7) Get the container IP addresses for all containers in cluster using Drill dns tool : 7)使用Drill dns工具获取群集中所有容器的容器IP地址:

| => docker-machine ssh node1 "docker exec 4c7055b01479 ash -c 'drill testservice'"
;; ->>HEADER<<- opcode: QUERY, rcode: NOERROR, id: 60920
;; flags: qr rd ra ; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0 
;; QUESTION SECTION:
;; testservice. IN  A

;; ANSWER SECTION:
testservice.    600 IN  A   10.0.0.3
testservice.    600 IN  A   10.0.0.2

;; AUTHORITY SECTION:

;; ADDITIONAL SECTION:

;; Query time: 0 msec
;; SERVER: 127.0.0.11
;; WHEN: Thu Jul 20 19:20:49 2017
;; MSG SIZE  rcvd: 83

8) Verify that containers can ping each other: 8)确认容器可以互相ping通:

docker-machine ssh node1 "docker exec 4c7055b01479 ash -c 'ping -c2 10.0.0.3'"
PING 10.0.0.3 (10.0.0.3): 56 data bytes
64 bytes from 10.0.0.3: seq=0 ttl=64 time=0.539 ms
64 bytes from 10.0.0.3: seq=1 ttl=64 time=0.731 ms

--- 10.0.0.3 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.539/0.635/0.731 ms



docker-machine ssh node2 "docker exec 28da546aa0d5 ash -c 'ping -c2 10.0.0.2'"
PING 10.0.0.2 (10.0.0.2): 56 data bytes
64 bytes from 10.0.0.2: seq=0 ttl=64 time=0.579 ms
64 bytes from 10.0.0.2: seq=1 ttl=64 time=0.736 ms

--- 10.0.0.2 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.579/0.657/0.736 ms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM