ETCD的安装可以GIT上直接下载tar.gz包的方式安装,也可以直接使用yum安装方式,无论哪种方式,主要是需要对/etc/etcd/etcd.config文件和/usr/lib/systemd/system/etcd.service文件的修改.这里以yum安装为例:
(1)yum安装etcd (当前yum库内最新etcd版本为2.3.7)
yum -y install etcd
(2)配置etcd.config文件
tee /etc/etcd/etcd.conf <<-'EOF'
ETCD_NAME=etcd01
ETCD_DATA_DIR="/var/lib/etcd/etcd01"
ETCD_LISTEN_PEER_URLS="http://192.168.111.90:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.111.90:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.111.90:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.111.90:2379"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster1"
ETCD_INITIAL_CLUSTER="etcd01=http://192.168.111.90:2380,etcd02=http://192.168.111.91:2380,etcd03=http://192.168.111.92:2380"
EOF
各个节点按照自己IP不同来对应修改该文件。其中需要注意ETCD_NAME
和ETCD_DATA_DIR要根据自己的名称区别开来。记得每个节点的名称不同,对应的DIR也是不同的。
对于ETCD_INITIAL_CLUSTER需要把所有节点都配置进去。
(3)配置etcd.service文件
tee /usr/lib/systemd/system/etcd.service <<-'EOF'
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\" --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\" --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\" --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\" "
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
(4)记得关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd
[root@localhost home]# etcdctl member list
f46934562c544c4: name=etcd1 peerURLs=http://192.168.111.90:2380 clientURLs=http://192.168.111.90:2379 isLeader=true
50b49a1d0073e00a: name=etcd2 peerURLs=http://192.168.111.91:2380 clientURLs=http://192.168.111.91:2379 isLeader=false
7957da5c545c210b: name=etcd0 peerURLs=http://192.168.111.92:2380 clientURLs=http://192.168.111.92:2379 isLeader=false
[root@localhost home]# etcdctl set /mytest "mytest in 192.168.111.92"
mytest in 192.168.111.92
[root@localhost home]# etcdctl get /mytest
mytest in 192.168.111.92
[root@localhost ~]# etcdctl get /mytest
mytest in 192.168.111.92
ETCD_NAME="etcd-1"
ETCD_DATA_DIR="/var/lib/etcd/"
#ETCD_WAL_DIR=""
#ETCD_SNAPSHOT_COUNT="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_LISTEN_PEER_URLS="http://localhost:2380" # 去掉默认的配置
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:7001"
#ETCD_LISTEN_CLIENT_URLS="http://localhost:2379" # 去掉默认的配置
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#
#[cluster]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.111.90:7001"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
ETCD_INITIAL_CLUSTER="etcd-1=http://192.168.111.90:7001,etcd-2=http://192.168.111.91:7001,etcd-3=http://192.168.111.92:7001"
# 此处的含义为,要配置包含有3台机器的etcd集群
ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
#ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.111.90:4001">
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_SRV=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#
#[proxy]
#ETCD_PROXY="off"
#ETCD_PROXY_FAILURE_WAIT="5000"
#ETCD_PROXY_REFRESH_INTERVAL="30000"
#ETCD_PROXY_DIAL_TIMEOUT="1000"
#ETCD_PROXY_WRITE_TIMEOUT="5000"
#ETCD_PROXY_READ_TIMEOUT="0"
#
#[security]
#ETCD_CERT_FILE=""
#ETCD_KEY_FILE=""
#ETCD_CLIENT_CERT_AUTH="false"
#ETCD_TRUSTED_CA_FILE=""
#ETCD_PEER_CERT_FILE=""
#ETCD_PEER_KEY_FILE=""
#ETCD_PEER_CLIENT_CERT_AUTH="false"
#ETCD_PEER_TRUSTED_CA_FILE=""
#
#[logging]
#ETCD_DEBUG="false"
# examples for -log-package-levels etcdserver=WARNING,security=DEBUG
#ETCD_LOG_PACKAGE_LEVELS=""