etcd的proxy Mode

etcd的proxy Mode

Proxy

etcd可以作为透明代理运行。这样做可以轻松地在您的基础架构中发现etcd,因为它可以作为本地服务在每台机器上运行。在这种模式下,etcd充当反向代理并将客户端请求转发到活动的etcd集群。 etcd代理不参与etcd集群的一致复制,因此既不增加韧性,也不会降低etcd集群的写入性能。

etcd目前支持两种代理模式:readwrite和readonly。默认模式是readwrite,它将读取和写入请求转发给etcd集群。只读etcd代理仅将读取请求转发给etcd集群,并将HTTP 501返回给所有写入请求。

代理将定期清理群集成员列表,以避免将所有连接发送给单个成员。

etcd代理使用的成员列表是在cluster里面通知的client的地址。这些client的URL在每个etcd集群成员的advertise-client-urls选项中指定。

一个etcd代理检查几个命令行选项来发现它的对等URL。按照优先顺序,这些选项是discoverydiscovery-srvinitial-clusterinitial-cluster选项设置为逗号分隔的一个或多个临时使用的etcd对等URL列表,以便发现永久群集。(这个设置和static的配置的集群一样)

在以这种方式建立对等URL列表之后,代理从第一可到达对等端检索客户端URL的列表(向cluster发送request,选择第一个response的对端Ip)。这些客户端URL由advertise-client-urls选项指定给etcd对等方。代理然后每隔30秒继续连接到第一个可访问的etcd集群成员,以刷新客户端URL列表。(防止数据一致发给一个node)

etcd代理不需要给予advertise-client-urls选项,因为它们从集群中检索此配置。这意味着必须为每个代理正确设置initial-cluster,而advertise-client-urls选项在非代理的节点上要保证设置的正确性。 否则,对任何etcd代理的请求将被不正确地转发。

请特别注意,不要将advertise-client-urls选项设置为指向代理自身的URL,因为这样的配置将导致代理进入循环,直到资源耗尽为止将请求转发给自身。 要纠正这两种情况,请停止etcd并使用正确的URL重新启动它。

下面的命令来声明etcd启动peer和proxy的不同:

# Use goreman to run `go get github.com/mattn/goreman`
etcd1: bin/etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
etcd2: bin/etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
etcd3: bin/etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
# in future, use proxy to listen on 2379
#proxy: bin/etcd --name infra-proxy1 --proxy=on --listen-client-urls http://127.0.0.1:2378 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --enable-pprof

总结一下etcd的 proxy的启动和peer的发现步骤:

1.etcd proxy将会执行下面的步骤来发现peer-urls:

    1.如果给proxy设置了discovery service, 那么就通过discovery service来发现cluster里面的非proxy的initial-advertise-peer-urls所有的URL。
    2.如果给proxy设置了discovery-srv,通过DNS发现peer-urls
    3.如果proxy设置了initial-cluster,那么将会变成peer-urls
    4.如果什么都没设置就采用默认的http://localhost:2380,http://localhost:7001.

2.peer-urls用来联系非proxy的cluster来发现他们的client-urls。因此client-urls是 所有cluter member的advertise-client-urls 集合

3.发给proxy的所有的request消息将被转发给client-urls

始终首先启动etcd集群成员(非proxy),然后任意的proxies。 代理必须能够到达群集成员以检索其配置,如果没有这种条件,那么将会尝试连接。 在任何代理之前启动成员可确保代理可以在稍后启动时发现客户端URL。

Using an etcd proxy

要以代理模式启动etcd,您需要提供三个标志:proxy,listen-client-urls和initial-cluster(或者discovery)。

要启动一个readwrite代理,请设置-proxy on;要启动只读代理,请设置-proxy readonly。

代理将监听listen-client-urls并将请求转发到从初始集群或发现url中发现的etcd集群。

Start an etcd proxy with a static configuration

静态配置proxy的时候只需要指定initial-cluster就行

etcd --proxy on \
--listen-client-urls http://127.0.0.1:2379 \
--initial-cluster infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380

Start an etcd proxy with the discovery service

如果你的etcd cluster 使用的是discovery service的部署方式,那么你也可以用discovery的方式启动proxy。
用discovery service启动proxy需要指定discovery 标志。proxy会一直等待etcd cluter的建立完成。然后开始转发请求信息。

etcd --proxy on \
--listen-client-urls http://127.0.0.1:2379 \
--discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de \

Fallback to proxy mode with discovery service

如果你cluster用的是discovery service模式,那么如果真实的节点的数目大于你配置的数目。那么多余的节点会自动变成readwrite的proxy模式。比如你配置的url的数量为5,但你有10个节点,那么其它5个会自动变为proxy的mdoe。当然你也可以使用discovery-fallback=’exit’ 来关闭这个功能。

Promote a proxy to a member of etcd cluster

proxy不参加etcd的同步,任何时候etcd都不会去主动把自己变为etcd的一个成员。如果你想添加proxy到etcd,需要下面四个步骤

use etcdctl to add the proxy node as an etcd member into the existing cluster
stop the etcd proxy process or service
remove the existing proxy data directory
restart the etcd process with new member configuration

例子

假设你有一个成员的cluster和一个proxy,信息如下:

NAME    ADDRESS
infra0  10.0.1.10
proxy0  10.0.1.11

当你完成下面操作,集群将变为两个成员的集群。

Add a new member into the existing cluster

首先你需要用etcdctl 来增加一个成员到cluster里面,它会输出对应新cluster数目的正确的配置信息:

$ etcdctl -endpoint http://10.0.1.10:2379 member add infra1 http://10.0.1.11:2380
added member 9bf1b35fc7761a23 to cluster

ETCD_NAME="infra1"
ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380"
ETCD_INITIAL_CLUSTER_STATE=existing

Stop the proxy process

停止proxy,我们可以擦出硬盘上的信息,然后重新导入新的配置。

ps aux | grep etcd
kill %etcd_proxy_pid%

如果用的systemd启动的etcd

sudo systemctl stop etcd

Remove the existing proxy data dir

rm -rf %data_dir%/proxy

Start etcd as a new member

最后,重新启动etcd用最新的配置:

$ export ETCD_NAME="infra1"
$ export ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380"
$ export ETCD_INITIAL_CLUSTER_STATE=existing
$ etcd --listen-client-urls http://10.0.1.11:2379 \
--advertise-client-urls http://10.0.1.11:2379 \
--listen-peer-urls http://10.0.1.11:2380 \
--initial-advertise-peer-urls http://10.0.1.11:2380 \
--data-dir %data_dir%

如果你用systemd跑的etcd,那么你可以编辑service 文件来得到正确的配置。然后使用下面命令

sudo systemd restart etcd# etcd的proxy Mode #

Proxy

etcd可以作为透明代理运行。这样做可以轻松地在您的基础架构中发现etcd,因为它可以作为本地服务在每台机器上运行。在这种模式下,etcd充当反向代理并将客户端请求转发到活动的etcd集群。 etcd代理不参与etcd集群的一致复制,因此既不增加韧性,也不会降低etcd集群的写入性能。

etcd目前支持两种代理模式:readwrite和readonly。默认模式是readwrite,它将读取和写入请求转发给etcd集群。只读etcd代理仅将读取请求转发给etcd集群,并将HTTP 501返回给所有写入请求。

代理将定期清理群集成员列表,以避免将所有连接发送给单个成员。

etcd代理使用的成员列表是在cluster里面通知的client的地址。这些client的URL在每个etcd集群成员的advertise-client-urls选项中指定。

一个etcd代理检查几个命令行选项来发现它的对等URL。按照优先顺序,这些选项是discoverydiscovery-srvinitial-clusterinitial-cluster选项设置为逗号分隔的一个或多个临时使用的etcd对等URL列表,以便发现永久群集。(这个设置和static的配置的集群一样)

在以这种方式建立对等URL列表之后,代理从第一可到达对等端检索客户端URL的列表(向cluster发送request,选择第一个response的对端Ip)。这些客户端URL由advertise-client-urls选项指定给etcd对等方。代理然后每隔30秒继续连接到第一个可访问的etcd集群成员,以刷新客户端URL列表。(防止数据一致发给一个node)

etcd代理不需要给予advertise-client-urls选项,因为它们从集群中检索此配置。这意味着必须为每个代理正确设置initial-cluster,而advertise-client-urls选项在非代理的节点上要保证设置的正确性。 否则,对任何etcd代理的请求将被不正确地转发。

请特别注意,不要将advertise-client-urls选项设置为指向代理自身的URL,因为这样的配置将导致代理进入循环,直到资源耗尽为止将请求转发给自身。 要纠正这两种情况,请停止etcd并使用正确的URL重新启动它。

下面的命令来声明etcd启动peer和proxy的不同:

# Use goreman to run `go get github.com/mattn/goreman`
etcd1: bin/etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
etcd2: bin/etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
etcd3: bin/etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof
# in future, use proxy to listen on 2379
#proxy: bin/etcd --name infra-proxy1 --proxy=on --listen-client-urls http://127.0.0.1:2378 --initial-cluster 'infra1=http://127.0.0.1:12380,infra2=http://127.0.0.1:22380,infra3=http://127.0.0.1:32380' --enable-pprof

总结一下etcd的 proxy的启动和peer的发现步骤:

1.etcd proxy将会执行下面的步骤来发现peer-urls:

    1.如果给proxy设置了discovery service, 那么就通过discovery service来发现cluster里面的非proxy的initial-advertise-peer-urls所有的URL。
    2.如果给proxy设置了discovery-srv,通过DNS发现peer-urls
    3.如果proxy设置了initial-cluster,那么将会变成peer-urls
    4.如果什么都没设置就采用默认的http://localhost:2380,http://localhost:7001.

2.peer-urls用来联系非proxy的cluster来发现他们的client-urls。因此client-urls是 所有cluter member的advertise-client-urls 集合

3.发给proxy的所有的request消息将被转发给client-urls

始终首先启动etcd集群成员(非proxy),然后任意的proxies。 代理必须能够到达群集成员以检索其配置,如果没有这种条件,那么将会尝试连接。 在任何代理之前启动成员可确保代理可以在稍后启动时发现客户端URL。

Using an etcd proxy

要以代理模式启动etcd,您需要提供三个标志:proxy,listen-client-urls和initial-cluster(或者discovery)。

要启动一个readwrite代理,请设置-proxy on;要启动只读代理,请设置-proxy readonly。

代理将监听listen-client-urls并将请求转发到从初始集群或发现url中发现的etcd集群。

Start an etcd proxy with a static configuration

静态配置proxy的时候只需要指定initial-cluster就行

etcd --proxy on \
--listen-client-urls http://127.0.0.1:2379 \
--initial-cluster infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380,infra2=http://10.0.1.12:2380

Start an etcd proxy with the discovery service

如果你的etcd cluster 使用的是discovery service的部署方式,那么你也可以用discovery的方式启动proxy。
用discovery service启动proxy需要指定discovery 标志。proxy会一直等待etcd cluter的建立完成。然后开始转发请求信息。

etcd --proxy on \
--listen-client-urls http://127.0.0.1:2379 \
--discovery https://discovery.etcd.io/3e86b59982e49066c5d813af1c2e2579cbf573de \

Fallback to proxy mode with discovery service

如果你cluster用的是discovery service模式,那么如果真实的节点的数目大于你配置的数目。那么多余的节点会自动变成readwrite的proxy模式。比如你配置的url的数量为5,但你有10个节点,那么其它5个会自动变为proxy的mdoe。当然你也可以使用discovery-fallback=’exit’ 来关闭这个功能。

Promote a proxy to a member of etcd cluster

proxy不参加etcd的同步,任何时候etcd都不会去主动把自己变为etcd的一个成员。如果你想添加proxy到etcd,需要下面四个步骤

use etcdctl to add the proxy node as an etcd member into the existing cluster
stop the etcd proxy process or service
remove the existing proxy data directory
restart the etcd process with new member configuration

例子

假设你有一个成员的cluster和一个proxy,信息如下:

NAME    ADDRESS
infra0  10.0.1.10
proxy0  10.0.1.11

当你完成下面操作,集群将变为两个成员的集群。

Add a new member into the existing cluster

首先你需要用etcdctl 来增加一个成员到cluster里面,它会输出对应新cluster数目的正确的配置信息:

$ etcdctl -endpoint http://10.0.1.10:2379 member add infra1 http://10.0.1.11:2380
added member 9bf1b35fc7761a23 to cluster

ETCD_NAME="infra1"
ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380"
ETCD_INITIAL_CLUSTER_STATE=existing

Stop the proxy process

停止proxy,我们可以擦出硬盘上的信息,然后重新导入新的配置。

ps aux | grep etcd
kill %etcd_proxy_pid%

如果用的systemd启动的etcd

sudo systemctl stop etcd

Remove the existing proxy data dir

rm -rf %data_dir%/proxy

Start etcd as a new member

最后,重新启动etcd用最新的配置:

$ export ETCD_NAME="infra1"
$ export ETCD_INITIAL_CLUSTER="infra0=http://10.0.1.10:2380,infra1=http://10.0.1.11:2380"
$ export ETCD_INITIAL_CLUSTER_STATE=existing
$ etcd --listen-client-urls http://10.0.1.11:2379 \
--advertise-client-urls http://10.0.1.11:2379 \
--listen-peer-urls http://10.0.1.11:2380 \
--initial-advertise-peer-urls http://10.0.1.11:2380 \
--data-dir %data_dir%

如果你用systemd跑的etcd,那么你可以编辑service 文件来得到正确的配置。然后使用下面命令

sudo systemd restart etcd
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值