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"
其实安装是非常简单的,下载二进制文件压缩包,解压之后,就可以直接运行了。
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,或扫描下面的二维码添加关注: