Proxy
etcd可以作為透明代理運(yùn)行。這樣做可以輕松地在您的基礎(chǔ)架構(gòu)中發(fā)現(xiàn)etcd,因?yàn)樗梢宰鳛楸镜胤?wù)在每臺機(jī)器上運(yùn)行。在這種模式下,etcd充當(dāng)反向代理并將客戶端請求轉(zhuǎn)發(fā)到活動的etcd集群。 etcd代理不參與etcd集群的一致復(fù)制,因此既不增加韌性,也不會降低etcd集群的寫入性能。
etcd目前支持兩種代理模式:readwrite和readonly。默認(rèn)模式是readwrite,它將讀取和寫入請求轉(zhuǎn)發(fā)給etcd集群。只讀etcd代理僅將讀取請求轉(zhuǎn)發(fā)給etcd集群,并將HTTP 501返回給所有寫入請求。
代理將定期清理群集成員列表,以避免將所有連接發(fā)送給單個成員。
etcd代理使用的成員列表是在cluster里面通知的client的地址。這些client的URL在每個etcd集群成員的advertise-client-urls選項(xiàng)中指定。
一個etcd代理檢查幾個命令行選項(xiàng)來發(fā)現(xiàn)它的對等URL。按照優(yōu)先順序,這些選項(xiàng)是discovery,discovery-srv和initial-cluster。initial-cluster選項(xiàng)設(shè)置為逗號分隔的一個或多個臨時使用的etcd對等URL列表,以便發(fā)現(xiàn)永久群集。(這個設(shè)置和static的配置的集群一樣)
在以這種方式建立對等URL列表之后,代理從第一可到達(dá)對等端檢索客戶端URL的列表(向cluster發(fā)送request,選擇第一個response的對端Ip)。這些客戶端URL由advertise-client-urls選項(xiàng)指定給etcd對等方。代理然后每隔30秒繼續(xù)連接到第一個可訪問的etcd集群成員,以刷新客戶端URL列表。(防止數(shù)據(jù)一致發(fā)給一個node)
etcd代理不需要給予advertise-client-urls選項(xiàng),因?yàn)樗鼈儚募褐袡z索此配置。這意味著必須為每個代理正確設(shè)置initial-cluster,而advertise-client-urls選項(xiàng)在非代理的節(jié)點(diǎn)上要保證設(shè)置的正確性。 否則,對任何etcd代理的請求將被不正確地轉(zhuǎn)發(fā)。
請?zhí)貏e注意,不要將advertise-client-urls選項(xiàng)設(shè)置為指向代理自身的URL,因?yàn)檫@樣的配置將導(dǎo)致代理進(jìn)入循環(huán),直到資源耗盡為止將請求轉(zhuǎn)發(fā)給自身。 要糾正這兩種情況,請停止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
總結(jié)一下etcd的 proxy的啟動和peer的發(fā)現(xiàn)步驟:
1.etcd proxy將會執(zhí)行下面的步驟來發(fā)現(xiàn)peer-urls:
1.如果給proxy設(shè)置了discovery service, 那么就通過discovery service來發(fā)現(xiàn)cluster里面的非proxy的initial-advertise-peer-urls所有的URL。
2.如果給proxy設(shè)置了discovery-srv,通過DNS發(fā)現(xiàn)peer-urls
3.如果proxy設(shè)置了initial-cluster,那么將會變成peer-urls
4.如果什么都沒設(shè)置就采用默認(rèn)的http://localhost:2380,http://localhost:7001.
2.peer-urls用來聯(lián)系非proxy的cluster來發(fā)現(xiàn)他們的client-urls。因此client-urls是 所有cluter member的advertise-client-urls 集合
3.發(fā)給proxy的所有的request消息將被轉(zhuǎn)發(fā)給client-urls
始終首先啟動etcd集群成員(非proxy),然后任意的proxies。 代理必須能夠到達(dá)群集成員以檢索其配置,如果沒有這種條件,那么將會嘗試連接。 在任何代理之前啟動成員可確保代理可以在稍后啟動時發(fā)現(xiàn)客戶端URL。
Using an etcd proxy
要以代理模式啟動etcd,您需要提供三個標(biāo)志:proxy,listen-client-urls和initial-cluster(或者discovery)。
要啟動一個readwrite代理,請?jiān)O(shè)置-proxy on;要啟動只讀代理,請?jiān)O(shè)置-proxy readonly。
代理將監(jiān)聽listen-client-urls并將請求轉(zhuǎn)發(fā)到從初始集群或發(fā)現(xiàn)url中發(fā)現(xiàn)的etcd集群。
Start an etcd proxy with a static configuration
靜態(tài)配置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 標(biāo)志。proxy會一直等待etcd cluter的建立完成。然后開始轉(zhuǎn)發(fā)請求信息。
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模式,那么如果真實(shí)的節(jié)點(diǎn)的數(shù)目大于你配置的數(shù)目。那么多余的節(jié)點(diǎn)會自動變成readwrite的proxy模式。比如你配置的url的數(shù)量為5,但你有10個節(jié)點(diǎn),那么其它5個會自動變?yōu)閜roxy的mdoe。當(dāng)然你也可以使用discovery-fallback='exit' 來關(guān)閉這個功能。
Promote a proxy to a member of etcd cluster
proxy不參加etcd的同步,任何時候etcd都不會去主動把自己變?yōu)閑tcd的一個成員。如果你想添加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
例子
假設(shè)你有一個成員的cluster和一個proxy,信息如下:
NAME ADDRESS
infra0 10.0.1.10
proxy0 10.0.1.11
當(dāng)你完成下面操作,集群將變?yōu)閮蓚€成員的集群。
Add a new member into the existing cluster
首先你需要用etcdctl 來增加一個成員到cluster里面,它會輸出對應(yīng)新cluster數(shù)目的正確的配置信息:
$ 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,我們可以擦出硬盤上的信息,然后重新導(dǎo)入新的配置。
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可以作為透明代理運(yùn)行。這樣做可以輕松地在您的基礎(chǔ)架構(gòu)中發(fā)現(xiàn)etcd,因?yàn)樗梢宰鳛楸镜胤?wù)在每臺機(jī)器上運(yùn)行。在這種模式下,etcd充當(dāng)反向代理并將客戶端請求轉(zhuǎn)發(fā)到活動的etcd集群。 etcd代理不參與etcd集群的一致復(fù)制,因此既不增加韌性,也不會降低etcd集群的寫入性能。
etcd目前支持兩種代理模式:readwrite和readonly。默認(rèn)模式是readwrite,它將讀取和寫入請求轉(zhuǎn)發(fā)給etcd集群。只讀etcd代理僅將讀取請求轉(zhuǎn)發(fā)給etcd集群,并將HTTP 501返回給所有寫入請求。
代理將定期清理群集成員列表,以避免將所有連接發(fā)送給單個成員。
etcd代理使用的成員列表是在cluster里面通知的client的地址。這些client的URL在每個etcd集群成員的advertise-client-urls選項(xiàng)中指定。
一個etcd代理檢查幾個命令行選項(xiàng)來發(fā)現(xiàn)它的對等URL。按照優(yōu)先順序,這些選項(xiàng)是discovery,discovery-srv和initial-cluster。initial-cluster選項(xiàng)設(shè)置為逗號分隔的一個或多個臨時使用的etcd對等URL列表,以便發(fā)現(xiàn)永久群集。(這個設(shè)置和static的配置的集群一樣)
在以這種方式建立對等URL列表之后,代理從第一可到達(dá)對等端檢索客戶端URL的列表(向cluster發(fā)送request,選擇第一個response的對端Ip)。這些客戶端URL由advertise-client-urls選項(xiàng)指定給etcd對等方。代理然后每隔30秒繼續(xù)連接到第一個可訪問的etcd集群成員,以刷新客戶端URL列表。(防止數(shù)據(jù)一致發(fā)給一個node)
etcd代理不需要給予advertise-client-urls選項(xiàng),因?yàn)樗鼈儚募褐袡z索此配置。這意味著必須為每個代理正確設(shè)置initial-cluster,而advertise-client-urls選項(xiàng)在非代理的節(jié)點(diǎn)上要保證設(shè)置的正確性。 否則,對任何etcd代理的請求將被不正確地轉(zhuǎn)發(fā)。
請?zhí)貏e注意,不要將advertise-client-urls選項(xiàng)設(shè)置為指向代理自身的URL,因?yàn)檫@樣的配置將導(dǎo)致代理進(jìn)入循環(huán),直到資源耗盡為止將請求轉(zhuǎn)發(fā)給自身。 要糾正這兩種情況,請停止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
總結(jié)一下etcd的 proxy的啟動和peer的發(fā)現(xiàn)步驟:
1.etcd proxy將會執(zhí)行下面的步驟來發(fā)現(xiàn)peer-urls:
1.如果給proxy設(shè)置了discovery service, 那么就通過discovery service來發(fā)現(xiàn)cluster里面的非proxy的initial-advertise-peer-urls所有的URL。
2.如果給proxy設(shè)置了discovery-srv,通過DNS發(fā)現(xiàn)peer-urls
3.如果proxy設(shè)置了initial-cluster,那么將會變成peer-urls
4.如果什么都沒設(shè)置就采用默認(rèn)的http://localhost:2380,http://localhost:7001.
2.peer-urls用來聯(lián)系非proxy的cluster來發(fā)現(xiàn)他們的client-urls。因此client-urls是 所有cluter member的advertise-client-urls 集合
3.發(fā)給proxy的所有的request消息將被轉(zhuǎn)發(fā)給client-urls
始終首先啟動etcd集群成員(非proxy),然后任意的proxies。 代理必須能夠到達(dá)群集成員以檢索其配置,如果沒有這種條件,那么將會嘗試連接。 在任何代理之前啟動成員可確保代理可以在稍后啟動時發(fā)現(xiàn)客戶端URL。
Using an etcd proxy
要以代理模式啟動etcd,您需要提供三個標(biāo)志:proxy,listen-client-urls和initial-cluster(或者discovery)。
要啟動一個readwrite代理,請?jiān)O(shè)置-proxy on;要啟動只讀代理,請?jiān)O(shè)置-proxy readonly。
代理將監(jiān)聽listen-client-urls并將請求轉(zhuǎn)發(fā)到從初始集群或發(fā)現(xiàn)url中發(fā)現(xiàn)的etcd集群。
Start an etcd proxy with a static configuration
靜態(tài)配置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 標(biāo)志。proxy會一直等待etcd cluter的建立完成。然后開始轉(zhuǎn)發(fā)請求信息。
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模式,那么如果真實(shí)的節(jié)點(diǎn)的數(shù)目大于你配置的數(shù)目。那么多余的節(jié)點(diǎn)會自動變成readwrite的proxy模式。比如你配置的url的數(shù)量為5,但你有10個節(jié)點(diǎn),那么其它5個會自動變?yōu)閜roxy的mdoe。當(dāng)然你也可以使用discovery-fallback='exit' 來關(guān)閉這個功能。
Promote a proxy to a member of etcd cluster
proxy不參加etcd的同步,任何時候etcd都不會去主動把自己變?yōu)閑tcd的一個成員。如果你想添加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
例子
假設(shè)你有一個成員的cluster和一個proxy,信息如下:
NAME ADDRESS
infra0 10.0.1.10
proxy0 10.0.1.11
當(dāng)你完成下面操作,集群將變?yōu)閮蓚€成員的集群。
Add a new member into the existing cluster
首先你需要用etcdctl 來增加一個成員到cluster里面,它會輸出對應(yīng)新cluster數(shù)目的正確的配置信息:
$ 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,我們可以擦出硬盤上的信息,然后重新導(dǎo)入新的配置。
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