ETCD快速入門-02 ETCD安裝

2.ETCD安裝

    etcd 安裝可以通過源碼構建也可以使用官方構建的二進制文件進行安裝。我們以二進制文件爲例,系統爲CentOS 7.9,操作步驟如下所示:

2.1 Linux

ETCD_VER=v3.5.4

# 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

# start a local etcd server
/tmp/etcd-download-test/etcd

# write,read to etcd
/tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 put foo bar
/tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 get foo

2.2 Docker

rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \
  docker rmi gcr.io/etcd-development/etcd:v3.5.4 || true && \
  docker run \
  -p 2379:2379 \
  -p 2380:2380 \
  --mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \
  --name etcd-gcr-v3.5.4 \
  gcr.io/etcd-development/etcd:v3.5.4 \
  /usr/local/bin/etcd \
  --name s1 \
  --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 s1=http://0.0.0.0:2380 \
  --initial-cluster-token tkn \
  --initial-cluster-state new \
  --log-level info \
  --logger zap \
  --log-outputs stderr

docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcd --version"
docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl version"
docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl endpoint health"
docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl put foo bar"
docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl get foo"
docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdutl version"

以上爲官方的示例文件,其網址爲:https://github.com/etcd-io/etcd/releases/

    其實安裝是非常簡單的,下載二進制文件壓縮包,解壓之後,就可以直接運行了。

2.3 初始化ETCD

2.3.1 直接啓動ETCD並初始化

    在安裝完成後,需要啓動etcd和配置初始化參數,常見的啓動默認參數如下所示:

./etcd \
--name=surpass \
--data-dir=/opt/etcd.db  \
--listen-client-urls=http://localhost:2379 \
--listen-peer-urls=http://localhost:2380 \
--advertise-client-urls=http://localhost:2379 \
--initial-advertise-peer-urls=http://localhost:2380 \
--initial-cluster=surpass=http://localhost:2380 \
--initial-cluster-state=new \
--initial-cluster-token=etcd-cluster \
--logger=zap

    在實際etcd服務運行環境,還需要考慮 etcd 進程的狀態監控、開機服務自動啓動、便捷的服務啓停和其他運維要求,因此很少會自己在命令行直接運行 etcd 服務。多節點集羣高可用部署時,推薦使用 systemd 系統服務、supervisor 管理服務、Kubernetes Static Pod 管理服務、Kubernetes DaemonSet 管理服務 或者 更加智能的 Kubernetes etcd-operator 等方式

2.3.2 通過systemd管理

    直接使用systemd管理的操作步驟如下所示:

  • 1、將解壓後的二進制文件etcd文件移動統一規劃目錄
\cp -f etcd /usr/local/bin
  • 2、準備配置文件和數據目錄
mkdir -p /etc/etcd /home/data/etcd
  • 3、準備啓動的配置文件
# vim /etc/etcd/etcd.conf
ETCD_NAME="surpass"
ETCD_DATA_DIR="/home/data/etcd"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379"
ETCD_LISTEN_PEER_URLS="http://localhost:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
ETCD_INITIAL_CLUSTER="surpass=http://localhost:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_LOGGER="zap"
  • 4、準備systemd服務文件,參考文件如下所示:
# vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=ETCD Service
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
EnvironmentFile=/etc/etcd/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
  • 5、啓動並運行服務
# systemctl daemon-reload
# systemctl start etcd
# systemctl enable etcd
  • 6、驗證ETCD狀態
# etcdctl endpoint  status --cluster -w table

原文地址:https://www.jianshu.com/p/1691493555ba
本文同步在微信訂閱號上發佈,如各位小夥伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關注:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章