iliuyt / blog

1 stars 0 forks source link

etcd 安装详细 #1

Open iliuyt opened 6 years ago

iliuyt commented 6 years ago

创建目录

mkdir -p /etc/etcd.d/ 
mkdir -p /var/lib/etcd/

下载

wget https://github.com/coreos/etcd/releases/download/v3.2.9/etcd-v3.2.9-linux-amd64.tar.gz

解压

tar xzvf etcd-v3.2.9-linux-amd64.tar.gz 
mv ./etcd-v3.2.9-linux-amd64/etcd /usr/bin
mv ./etcd-v3.2.9-linux-amd64/etcdctl /usr/bin

配置

cat <<EOF | tee /etc/etcd.d/etcd.conf
#节点名称 
ETCD_NAME=$(hostname -s) 
#数据存放位置 
ETCD_DATA_DIR=/var/lib/etcd 
EOF

服务配置

cat <<EOF | tee /etc/systemd/system/etcd.service

[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target

[Service]
User=root
Type=notify
EnvironmentFile=-/etc/etcd.d/etcd.conf
ExecStart=/usr/bin/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target
EOF

启动

systemctl daemon-reload && systemctl enable etcd && systemctl start etcd

shell

#!/bin/bash

ETCD_VER=v3.2.9
GITHUB_URL=https://github.com/coreos/etcd/releases/download
DOWNLOAD_URL=${GITHUB_URL}

mkdir -p /etc/etcd.d/ 
mkdir -p /var/lib/etcd/

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

cd /tmp/etcd-download-test

wget ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz

tar xzvf etcd-v3.2.9-linux-amd64.tar.gz 
mv ./etcd-v3.2.9-linux-amd64/etcd /usr/bin
mv ./etcd-v3.2.9-linux-amd64/etcdctl /usr/bin

rm -rf /tmp/etcd-download-test

cat <<EOF | tee /etc/etcd.d/etcd.conf
#节点名称 
ETCD_NAME=$(hostname -s) 
#数据存放位置 
ETCD_DATA_DIR=/var/lib/etcd 
EOF

cat <<EOF | tee /etc/systemd/system/etcd.service 
[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target

[Service]
User=root
Type=notify
EnvironmentFile=-/etc/etcd.d/etcd.conf
ExecStart=/usr/bin/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl enable etcd -f && systemctl restart etcd

参数理解

--name:方便理解的节点名称,默认为default,在集群中应该保持唯一,可以使用 hostname
--data-dir:服务运行数据保存的路径,默认为 ${name}.etcd
--snapshot-count:指定有多少事务(transaction)被提交时,触发截取快照保存到磁盘
--heartbeat-interval:leader 多久发送一次心跳到 followers。默认值是 100ms
--eletion-timeout:重新投票的超时时间,如果 follow 在该时间间隔没有收到心跳包,会触发重新投票,默认为 1000 ms
--listen-peer-urls:和同伴通信的地址,比如 http://ip:2380,如果有多个,使用逗号分隔。需要所有节点都能够访问,所以不要使用 localhost!
--listen-client-urls:对外提供服务的地址:比如 http://ip:2379,http://127.0.0.1:2379,客户端会连接到这里和 etcd 交互
--advertise-client-urls:对外公告的该节点客户端监听地址,这个值会告诉集群中其他节点
--initial-advertise-peer-urls:该节点同伴监听地址,这个值会告诉集群中其他节点
--initial-cluster:集群中所有节点的信息,格式为 node1=http://ip1:2380,node2=http://ip2:2380,…。注意:这里的 node1 是节点的 --name 指定的名字;后面的 ip1:2380 是 --initial-advertise-peer-urls 指定的值
--initial-cluster-state:新建集群的时候,这个值为new;假如已经存在的集群,这个值为 existing
--initial-cluster-token:创建集群的token,这个值每个集群保持唯一。这样的话,如果你要重新创建集群,即使配置和之前一样,也会再次生成新的集群和节点 uuid;否则会导致多个集群之间的冲突,造成未知的错误

所有以--init开头的配置都是在bootstrap集群的时候才会用到,后续节点的重启会被忽略

集群配置脚本

这里有坑

坑1

注意端口开放

坑2

注意参数端口配置,一定要参照官方配置

坑3

官方没有给出data-dir的配置,但是我们一定要配置,否则systemctl启动不起来

坑4

设置data-dir之前一定要清理目录,否则配置可能无法起效,删除目录要根据情况而定,如果不需要数据就删除data-dir

大坑5

最少要3台服务器,否则无法执行算法选举

#!/bin/bash

rm -rf /var/lib/etcd/

# 参数1获取IP的网卡
# 参数2为discovery的地址
LOCAL_IP=`ifconfig $1|sed -n 2p|awk  '{ print $2 }'|awk -F : '{ print $2 }'`
NAME=`hostname -s`

rm -rf /var/lib/etcd/$NAME
mkdir -p /var/lib/etcd/$NAME

cat <<EOF | tee /etc/etcd.d/etcd.conf
ETCD_NAME=$NAME
ETCD_DATA_DIR=/var/lib/etcd/$NAME
ETCD_INITIAL_ADVERTISE_PEER_URLS=http://$LOCAL_IP:2380
ETCD_LISTEN_PEER_URLS=http://$LOCAL_IP:2380
ETCD_LISTEN_CLIENT_URLS=http://$LOCAL_IP:2379,http://127.0.0.1:2379
ETCD_ADVERTISE_CLIENT_URLS=http://$LOCAL_IP:2379
ETCD_DISCOVERY=$2
EOF

# 端口开放
iptables -I INPUT -p tcp -m multiport --dport 2379,2380 -j ACCEPT
iptables-save

systemctl daemon-reload && systemctl enable etcd -f && systemctl restart etcd

公共discovery

获取token地址

curl -w "\n" 'https://discovery.etcd.io/new?size=3'

https://discovery.etcd.io/fc3aae2dc792783b2a1e723622eb2951

查看节点

curl -w '\n' https://discovery.etcd.io/fc3aae2dc792783b2a1e723622eb2951

问题记录

执行

systemctl enable etcd 

报错1

Failed to execute operation: Unit file is masked

解决

查看etcd.service文件内容是否为空。可能写入内容的时候写入失败

报错2

Failed to execute operation: File exists

解决

systemctl enable etcd -f

报错3

discovery: error #0: x509: failed to load system roots and no roots provided
discovery: cluster status check: error connecting to https://discovery.etcd.io, retrying in 16s

解决

查看容器是否支持HTTPS,discovery地址是否为https地址

#