Etcd单机部署

A distributed, reliable key-value store for the most critical data of a distributed system

下载 Etcd二进制文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ETCD_VER=v3.5.0

# choose either URL
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}

rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz

/tmp/etcd-download-test/etcd --version
/tmp/etcd-download-test/etcdctl version
/tmp/etcd-download-test/etcdutl version

# Move them to the bin folder
mv /tmp/etcd-download-test/etcd /usr/local/bin
mv /tmp/etcd-download-test/etcdctl /usr/local/bin
mv /tmp/etcd-download-test/etcdutl /usr/local/bin

启动测试

1
2
3
4
5
6
7
8
9
etcd \
--name 'default' \
--data-dir '/data/etcd/' \
--listen-client-urls 'http://0.0.0.0:2379' \
--advertise-client-urls 'http://0.0.0.0:2379' \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://0.0.0.0:2380 \
--initial-cluster default=http://0.0.0.0:2380 \
--enable-v2=true

配置 etcd

/etc/etcd目录中创建一个名为etcd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
mkdir /etc/etcd
cat > /etc/etcd/etcd.conf <<EOF
# [Member Flags]
ETCD_NAME=default
ETCD_DATA_DIR=“/data/etcd/”
ETCD_LISTEN_CLIENT_URLS=http://0.0.0.0:2379
#ETCD_LISTEN_PEER_URLS=http://0.0.0.0:2380
# [Cluster Flags]
ETCD_ADVERTISE_CLIENT_URLS=http://0.0.0.0:2379
#ETCD_INITIAL_ADVERTISE_PEER_URLS=http://0.0.0.0:2380
#ETCD_INITIAL_CLUSTER="default=http://0.0.0.0:2380"
ETCD_ENABLE_V2="true"
EOF

创建 systemd 单元文件

/lib/system/systemd处创建文件etcd.service,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cat > /lib/systemd/system/etcd.service <<EOF
[Unit]
Description=etcd key-value store
Documentation=https://github.com/etcd-io/etcd
After=network.target

[Service]
Type=notify
EnvironmentFile=/etc/etcd/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=always
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target
EOF

启动 Etcd

1
2
systemctl enable etcd
systemctl start etcd

docker启动参考

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
docker run -d \
-p 2379:2379 \
-p 2380:2380 \
-v /data/etcd:/etcd-data/member \
--name etcd \
quay.io/coreos/etcd:v3.5.0 \
/usr/local/bin/etcd \
--name default \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://0.0.0.0:2380 \
--initial-advertise-peer-urls http://0.0.0.0:2380 \
--initial-cluster default=http://0.0.0.0:2380 \
--initial-cluster-token tkn \
--initial-cluster-state new \
--log-level info \
--logger zap \
--log-outputs stderr