Docker中Swarm服务发现和负载均衡原理分析

Docker中Swarm服务发现和负载均衡原理分析

Docker Swarm服务发现和负载均衡是一项支持集群中的应用和容器之间进行交互,以便将请求及时地分配到正确的容器上。考虑到流量削峰以避免单一container因大量请求而overload, 在Swarm中也会通过Load Balancer来帮助进行流量削高。Swarm 服务发现和负载均衡是由Swarm集群的每个容器服务共同实现的:1、在每个swarm容器中运行一个内部DNS代理来存储所有正在运行的服务信息。

Docker中Swarm服务发现和负载均衡原理分析?

Docker Swarm服务发现和负载均衡是一项支持集群中的应用和容器之间进行交互,以便将请求及时地分配到正确的容器上。

服务发现使得Swarm能够隐藏底层复杂性,并为成员之间的交互提供通信方法。当客户端尝试向一个特定服务发出请求时(如数据库、REST API、WebSocket服务等),所采用的方法是将资源标记为Service对象(例如db.service 或者app.service )然后内部Swarm Manager会通过Consul DNS Server去关联此标志对应的IP地张内存储相关信息。

考虑到流量削峰以避免单一container因大量请求而overload, 在Swarm中也会通过Load Balancer来帮助进行流量削高。Load balancer会不断监听并检测service中container/host 状态情况, 针对不同感知分布情况来作出相应response, 这样就能够很好地避免 traffic rush 对 container/hosts 造成影响. 有4种Load balancing mechanism:

1) Round-Robin: 根果request数量不断变化从考勤RR方式能够平衡traffic flow;

  2) Least Connections Method: 触发least connections method load balancer会首先负责monitor active TCP connections and then assigns new connection to the server with least number of active connections;

  3) IP Hash Method: Based upon source and destination IP address hash algorithm is applied which ensures that request from a particular client always reaches to same target server; and

  4) Source Affinity(client ip-based session persistence): This technique helps in tracking the requests coming from a particular client based on unique identifier like cookie or session id associated with it.

docker swarm服务发现与负载均衡原理分析?

Swarm 服务发现和负载均衡是由Swarm集群的每个容器服务共同实现的:

1、在每个swarm容器中运行一个内部DNS代理(dnsmasq)来存储所有正在运行的服务信息。该代理会作为客户端使用UDP协议监听特定端口并把泛域名映射到相应IP上。当新任务开始时或者已存在任务重新配置时,对应的ip将会重新映射。

2、Swarm集群内部使用Raft一致性去处理整体请求并保存所有修改过的数据版本号(sequence number)来避免IP地址映射出错问题. 在Raft之前,团队采用了ZooKeeper 来处理service registry 等问题。

3、通常Swarm 都会利用Ingress-routing mesh 来作为 swarm service 端口之间的 proxy (代理), 这样能够保证swam services 间相互不必感知对方便耦合性. Ingress routing mesh 也是docker swarm 最关键也是独特之处 . 集群中Service A Service B 不况出 docker_gwbridge IP 地址 , ingress routing mesh 也能很胜利/快速/便利 的将 traffic route 到正常destination .

docker swarm 服务发现

Swarm service discovery is the process by which individual services running in a Docker Swarm cluster are discovered and made available for use. It allows applications to be deployed as a group of containers, with each container designated as belonging to one or more services. The discovery mechanism makes it easy for these services to find each other, even when they are spread out across multiple nodes in the cluster.

Docker’s native swarm mode includes several built-in mechanisms for service discovery. These include DNS Round Robin, IP Tables masquerade rules, overlay networks and ingress routing mesh. Each of these methods has its own advantages and disadvantages depending on your particular application requirements.

For example, if you want an efficient way to direct traffic between clusters then using an IPTables masquerade rule would allow this without having to manually update configurations on all hosts within the cluster whenever new Members join/leave or addresses change due to scaling scenarios etc.. On the other hand if you require strong security measures such as TLS encryption and authentication then overlay networks may be a better choice since they provide secure communication paths between nodes while still allowing efficient intra-cluster communication without needing manual configuration updates every time something changes in the environment (such as Member addition/removal). Finally if you require wide scale access from external sources outside of your cluster then Ingress Routing Mesh could be used instead – providing routes from external sources into specific ports on any node within the Swarm Cluster based upon labels associated with that service instance at deployment time.