Etcd:修订间差异

来自Gea-Suan Lin's Wiki
跳到导航 跳到搜索
此页面具有访问限制。如果您看见此消息,则说明您没有权限访问此页面。
 
(未显示同一用户的34个中间版本)
第10行: 第10行:
</syntaxhighlight>
</syntaxhighlight>


 另外一種方式是安裝官方的binary:
 另外一種方式是安裝官方的binary ,這邊的<code>ETCD_VERSION</code>可以去[[GitHub]]上翻目前最新的版本


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
cd /tmp; wget https://github.com/etcd-io/etcd/releases/download/v3.5.9/etcd-v3.5.9-linux-amd64.tar.gz; tar xvf etcd-v3.5.9-linux-amd64.tar.gz; cd etcd-v3.5.9-linux-amd64; sudo cp etcd etcdctl etcdutl /usr/bin
ETCD_VERSION=3.5.9; cd /tmp; wget https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz; tar xvf etcd-v${ETCD_VERSION}-linux-amd64.tar.gz; cd etcd-v${ETCD_VERSION}-linux-amd64; sudo cp etcd etcdctl etcdutl /usr/bin
</syntaxhighlight>
</syntaxhighlight>


第47行: 第47行:
</syntaxhighlight>
</syntaxhighlight>


  然後 建立 對應的使用者 群組
  另外 建立<code>/etc/default/etcd</code>(要記得改<code>ETCD_INITIAL_ADVERTISE_PEER_URLS</code>、<code>ETCD_LISTEN_PEER_URLS</code> <code>ETCD_NAME</code>)


<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
sudo groupadd -r etcd; sudo groupadd -r -g etcd etcd; sudo systemctl daemon-reload; sudo systemctl enable --now etcd
#
ETCD_ADVERTISE_CLIENT_URLS="http://10.1.2.3:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.1.2.3:2380"
ETCD_INITIAL_CLUSTER="etcd-1-dev=http://10.1.2.3:2380,etcd-2-dev=http://10.1.2.4:2380,etcd-3-dev=http://10.1.2.5:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="x"
ETCD_LISTEN_CLIENT_URLS="http://10.1.2.3:2379"
ETCD_LISTEN_PEER_URLS="http://10.1.2.3:2380"
ETCD_NAME="etcd-1-dev"
</syntaxhighlight>
 
然後建立對應的使用者與群組,並且設定跑起來:
 
<syntaxhighlight lang="bash">
sudo groupadd -r etcd; sudo useradd -r -g etcd etcd; sudo mkdir /var/lib/etcd; sudo chown etcd:etcd /var/lib/etcd; sudo systemctl daemon-reload; sudo systemctl enable --now etcd
</syntaxhighlight>
 
跑一次讓cluster建立後就可以把<code>new</code>改成<code>existing</code>,避免其他的機器加入:
 
<syntaxhighlight lang="bash">
sudo sed -i 's/new/existing/' /etc/default/etcd; sudo service etcd restart
</syntaxhighlight>
 
=== Auditing ===
 
如果有Auditing需求,目前etcd只能透過把log開到debug等級才有辦法記錄。透過修改<code>/etc/default/etcd</code>,增加:
 
<syntaxhighlight lang="bash">
DAEMON_ARGS="--log-level debug"
</syntaxhighlight>
 
另外開一個<code>/etc/rsyslog.d/30-etcd.conf</code>:
 
<syntaxhighlight lang="c">
if $programname == 'etcd' then /var/log/etcd.log
& stop
</syntaxhighlight>
 
以及對應的<code>/etc/logrotate.d/etcd</code>:
 
<syntaxhighlight lang="c">
/var/log/etcd
{
     rotate 4
     weekly
     missingok
     notifempty
     compress
     delaycompress
     sharedscripts
     postrotate
         /usr/lib/rsyslog/rsyslog-rotate
     endscript
}
</syntaxhighlight>
 
=== DNS ===
 
多台etcd時可以透過[[DNS]]的<code>SRV</code> record讓client端知道有哪些服務可以用,像是這樣:
 
<syntaxhighlight lang="bash">
_etcd-client._tcp.etcd.srv.example.net. 300 IN SRV 1 1 2379 etcd-1.private.example.net.
_etcd-client._tcp.etcd.srv.example.net. 300 IN SRV 1 1 2379 etcd-2.private.example.net.
_etcd-client._tcp.etcd.srv.example.net. 300 IN SRV 1 1 2379 etcd-3.private.example.net.
</syntaxhighlight>
 
這樣<code>etcdctl</code>就可以用<code>-d etcd.srv.example.net</code>指定服務的接口:
 
<syntaxhighlight lang="bash">
etcdctl -d etcd.srv.example.net member list
</syntaxhighlight>
 
=== 權限 ===
 
etcd可以透過user與role的設定控管權限,先看有哪些user以及role:
 
<syntaxhighlight lang="bash">
etcdctl -d etcd.srv.example.net user list
etcdctl -d etcd.srv.example.net role list
</syntaxhighlight>
 
第一次可以設定<code>root</code>帳號(以及密碼),另外設定<code>root</code>權限。這兩個名字在etcd內是特殊身分,直接擁有最高權限:
 
<syntaxhighlight lang="bash">
etcdctl -d etcd.srv.example.net role add root
etcdctl -d etcd.srv.example.net user add root
etcdctl -d etcd.srv.example.net user grant-role root root
</syntaxhighlight>
 
然後可以啟動:
 
<syntaxhighlight lang="bash">
etcdctl -d etcd.srv.example.net auth enable
</syntaxhighlight>
 
接下來可以測試有提供使用者資訊與沒有提供時的差異:
 
<syntaxhighlight lang="bash">
etcdctl -d etcd.srv.example.net user list
etcdctl -d etcd.srv.example.net --user=root:password user list
</syntaxhighlight>
 
另外比較特別的是所有key的指定方法,需要使用空的條件<code>'-{}-'</code>與<code>--from-key</code>的參數這樣組合出來。
 
這邊包含了<code>readonly</code>的user與role的建立:
 
<syntaxhighlight lang="bash">
etcdctl -d etcd.srv.example.net --user=root:password user add readonly
etcdctl -d etcd.srv.example.net --user=root:password role add readonly
etcdctl -d etcd.srv.example.net --user=root:password user grant-role readonly readonly
etcdctl -d etcd.srv.example.net --user=root:password role grant-permission readonly read '' --from-key
</syntaxhighlight>
 
== 操作 ==
 
先看有哪些機器活著:
 
<syntaxhighlight lang="bash">
etcdctl --endpoints=10.1.2.3:2379 member list
</syntaxhighlight>
 
如果[[DNS]] record已經設定好,可以用<code>-d etcd.srv.example.net</code>指定cluster,而非單一伺服器:
 
<syntaxhighlight lang="bash">
etcdctl -d etcd.srv.example.net member list
</syntaxhighlight>
 
接著可以測試如果存取不同台時,結果會不會同步:
 
<syntaxhighlight lang="bash">
etcdctl --endpoints=10.1.2.3:2379 put /foo test1234
etcdctl --endpoints=10.1.2.3:2379 get /foo
etcdctl --endpoints=10.1.2.4:2379 get /foo
etcdctl --endpoints=10.1.2.5:2379 get /foo
etcdctl --endpoints=10.1.2.3:2379 del /foo
etcdctl --endpoints=10.1.2.3:2379 get /foo
</syntaxhighlight>
</syntaxhighlight>


== 相關連結 ==
== 相關連結 ==


* [[etcd-adminer]]
* [[Kubernetes]]
* [[Kubernetes]]
* [[teller]]


== 外部連結 ==
== 外部連結 ==

2023年10月24日 (二) 16:24的最新版本

etcd是一套提供给分散式系统用的Key-Value Store。

安装

Ubuntu下可以直接安装,但要注意目前Ubuntu 22.04还是3.3版,预设会是v2 data,在3.4以后就会是v3 data,会有转移的成本:

sudo apt install -y etcd; sudo apt clean

另外一种方式是安装官方的binary,这边的ETCD_VERSION可以去GitHub上翻目前最新的版本:

ETCD_VERSION=3.5.9; cd /tmp; wget https://github.com/etcd-io/etcd/releases/download/v${ETCD_VERSION}/etcd-v${ETCD_VERSION}-linux-amd64.tar.gz; tar xvf etcd-v${ETCD_VERSION}-linux-amd64.tar.gz; cd etcd-v${ETCD_VERSION}-linux-amd64; sudo cp etcd etcdctl etcdutl /usr/bin

设定

如果是Ubuntu套件安装的可以先跳过这段,如果是透过binary安装的可以把systemd的设定放在/etc/systemd/system/etcd.service下(这其实是从Ubuntu套件里捞出来的):

[Unit]
Description=etcd - highly-available key value store
Documentation=https://etcd.io/docs
Documentation=man:etcd
After=network.target
Wants=network-online.target

[Service]
Environment=DAEMON_ARGS=
Environment=ETCD_NAME=%H
Environment=ETCD_DATA_DIR=/var/lib/etcd/default
EnvironmentFile=-/etc/default/%p
Type=notify
User=etcd
PermissionsStartOnly=true
#ExecStart=/bin/sh -c "GOMAXPROCS=$(nproc) /usr/bin/etcd $DAEMON_ARGS"
ExecStart=/usr/bin/etcd $DAEMON_ARGS
Restart=on-abnormal
#RestartSec=10s
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
Alias=etcd2.service

另外建立/etc/default/etcd(要记得改ETCD_INITIAL_ADVERTISE_PEER_URLSETCD_LISTEN_PEER_URLSETCD_NAME):

#
ETCD_ADVERTISE_CLIENT_URLS="http://10.1.2.3:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.1.2.3:2380"
ETCD_INITIAL_CLUSTER="etcd-1-dev=http://10.1.2.3:2380,etcd-2-dev=http://10.1.2.4:2380,etcd-3-dev=http://10.1.2.5:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="x"
ETCD_LISTEN_CLIENT_URLS="http://10.1.2.3:2379"
ETCD_LISTEN_PEER_URLS="http://10.1.2.3:2380"
ETCD_NAME="etcd-1-dev"

然后建立对应的使用者与群组,并且设定跑起来:

sudo groupadd -r etcd; sudo useradd -r -g etcd etcd; sudo mkdir /var/lib/etcd; sudo chown etcd:etcd /var/lib/etcd; sudo systemctl daemon-reload; sudo systemctl enable --now etcd

跑一次让cluster建立后就可以把new改成existing,避免其他的机器加入:

sudo sed -i 's/new/existing/' /etc/default/etcd; sudo service etcd restart

Auditing

如果有Auditing需求,目前etcd只能透过把log开到debug等级才有办法记录。透过修改/etc/default/etcd,增加:

DAEMON_ARGS="--log-level debug"

另外开一个/etc/rsyslog.d/30-etcd.conf

if $programname == 'etcd' then /var/log/etcd.log
& stop

以及对应的/etc/logrotate.d/etcd

/var/log/etcd
{
        rotate 4
        weekly
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        postrotate
                /usr/lib/rsyslog/rsyslog-rotate
        endscript
}

DNS

多台etcd时可以透过DNSSRV record让client端知道有哪些服务可以用,像是这样:

_etcd-client._tcp.etcd.srv.example.net. 300 IN SRV 1 1 2379 etcd-1.private.example.net.
_etcd-client._tcp.etcd.srv.example.net. 300 IN SRV 1 1 2379 etcd-2.private.example.net.
_etcd-client._tcp.etcd.srv.example.net. 300 IN SRV 1 1 2379 etcd-3.private.example.net.

这样etcdctl就可以用-d etcd.srv.example.net指定服务的接口:

etcdctl -d etcd.srv.example.net member list

权限

etcd可以透过user与role的设定控管权限,先看有哪些user以及role:

etcdctl -d etcd.srv.example.net user list
etcdctl -d etcd.srv.example.net role list

第一次可以设定root帐号(以及密码),另外设定root权限。这两个名字在etcd内是特殊身分,直接拥有最高权限:

etcdctl -d etcd.srv.example.net role add root
etcdctl -d etcd.srv.example.net user add root
etcdctl -d etcd.srv.example.net user grant-role root root

然后可以启动:

etcdctl -d etcd.srv.example.net auth enable

接下来可以测试有提供使用者资讯与没有提供时的差异:

etcdctl -d etcd.srv.example.net user list
etcdctl -d etcd.srv.example.net --user=root:password user list

另外比较特别的是所有key的指定方法,需要使用空的条件''--from-key的参数这样组合出来。

这边包含了readonly的user与role的建立:

etcdctl -d etcd.srv.example.net --user=root:password user add readonly
etcdctl -d etcd.srv.example.net --user=root:password role add readonly
etcdctl -d etcd.srv.example.net --user=root:password user grant-role readonly readonly
etcdctl -d etcd.srv.example.net --user=root:password role grant-permission readonly read '' --from-key

操作

先看有哪些机器活著:

etcdctl --endpoints=10.1.2.3:2379 member list

如果DNS record已经设定好,可以用-d etcd.srv.example.net指定cluster,而非单一伺服器:

etcdctl -d etcd.srv.example.net member list

接著可以测试如果存取不同台时,结果会不会同步:

etcdctl --endpoints=10.1.2.3:2379 put /foo test1234
etcdctl --endpoints=10.1.2.3:2379 get /foo
etcdctl --endpoints=10.1.2.4:2379 get /foo
etcdctl --endpoints=10.1.2.5:2379 get /foo
etcdctl --endpoints=10.1.2.3:2379 del /foo
etcdctl --endpoints=10.1.2.3:2379 get /foo

相关连结

外部连结