MENU

centos7 安装etcd

• 2019 年 05 月 28 日 • linux,kubernetes

预估

etcd是什么

etcd是一个分布式可靠的key-value存储系统,适合分布式系统的关键数据存储。
主要关注一下内容:

  • 简单:好的定义,面向用户的API,比如gRPC
  • 安全: TLS支持,客户端可选证书认证
  • 快速: 压测到1w 写入/s
  • 可靠: 使用raft分布式协议

etcd是用go写的,使用Raft一致性算法来管理一个高可用复制集日志。

开源地址在: https://github.com/etcd-io/etcd

安装

官方推荐的安装方式 https://github.com/etcd-io/etcd/releases ,直接写在二进制进行安装,并且提供了相关的脚步,注意要有科学上网网络。

这里通过另一只方式安装, 通过http://play.etcd.io/install 这个页面的提示来安装etcd。

ETCD_VER=v3.3.8

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

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

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/test-etcd --strip-components=1

# sudo cp /tmp/test-etcd/etcd* [YOUR_EXEC_DIR]
# sudo mkdir -p /usr/local/bin/ && sudo cp /tmp/test-etcd/etcd* /usr/local/bin/

/tmp/test-etcd/etcd --version
ETCDCTL_API=3 /tmp/test-etcd/etcdctl version

其实就是下载了2个二进制文件,一个是etcd服务端程序,一个是etcdctl客户端工具,如果在无网络环境下安装etcd只需要复制这两个文件过去即可。

[root@ku0 test-spring2]# ll /tmp/test-etcd/etcd*
-rwxr-xr-x 1 1000 1000 19287584 Jun 15  2018 /tmp/test-etcd/etcd
-rwxr-xr-x 1 1000 1000 16018720 Jun 15  2018 /tmp/test-etcd/etcdctl

设置 systemd相关脚步

方便后续使用,对类centos系统

# make sure etcd process has write access to this directory
# remove this directory if the cluster is new; keep if restarting etcd
# rm -rf /tmp/etcd/s1


# to write service file for etcd
cat > /tmp/etcd.service <<EOF
[Unit]
Description=etcd
Documentation=https://github.com/coreos/etcd
Conflicts=etcd.service
Conflicts=etcd2.service

[Service]
Type=notify
Restart=always
RestartSec=5s
LimitNOFILE=40000
TimeoutStartSec=0

ExecStart=/usr/local/bin/etcd --name s1 \
  --data-dir /tmp/etcd/s1 \
  --listen-client-urls http://localhost:2379 \
  --advertise-client-urls http://localhost:2379 \
  --listen-peer-urls http://localhost:2380 \
  --initial-advertise-peer-urls http://localhost:2380 \
  --initial-cluster s1=http://localhost:2380,s2=http://localhost:22380,s3=http://localhost:32380 \
  --initial-cluster-token tkn \
  --initial-cluster-state new

[Install]
WantedBy=multi-user.target
EOF
sudo mv /tmp/etcd.service /etc/systemd/system/etcd.service



# to start service
sudo systemctl daemon-reload
sudo systemctl cat etcd.service
sudo systemctl enable etcd.service
sudo systemctl start etcd.service

# to get logs from service
sudo systemctl status etcd.service -l --no-pager
sudo journalctl -u etcd.service -l --no-pager|less
sudo journalctl -f -u etcd.service

# to stop service
sudo systemctl stop etcd.service
sudo systemctl disable etcd.service

检测etcd状态

[root@ku0 tmp]# ETCDCTL_API=3 /tmp/test-etcd/etcdctl \
>   --endpoints localhost:2379,localhost:22379,localhost:32379 \
>   endpoint health
localhost:2379 is healthy: successfully committed proposal: took = 155.238816ms


localhost:32379 is unhealthy: failed to connect: dial tcp [::1]:32379: getsockopt: connection refused
localhost:22379 is unhealthy: failed to connect: dial tcp [::1]:22379: getsockopt: connection refused
Error: unhealthy cluster

可以看到只有localhost:2379这一个实例是运行成功的,因为只启动了这一个。

请输入图片描述

最后编辑于: 2019 年 08 月 04 日