刀刀叨叨 2020-02-09
服务器环境:
ip | client_port | peer_port |
---|---|---|
10.20.48.107 | 2379 | 2380 |
10.20.48.108 | 2379 | 2380 |
10.20.48.109 | 2379 | 2380 |
安装etcd:
安装比较简单,我们只需要去官方下载包就可以
wget https://github.com/coreos/etcd/releases/download/v3.1.11/etcd-v3.1.11-linux-amd64.tar.gz
tar zxvf etcd-v3.1.11-linux-amd64.tar.gz -C /usr/local/
/usr/local/
目录下 mv etcd-v3.1.11-linux-amd64.tar.gz etcdv3
ln -s /usr/local/etcdv3/etcd* /usr/local/bin/
/etc/profile
中添加环境变量export ETCDCTL_API=3
配置说明:
123456789 | name: initial-advertise-peer-urls: #该成员的对等url列表,用于向集群的其他成员通告(告诉其他成员通过什么url和我通信)listen-peer-urls: # 本地监听,接收同等节点请求的端口listen-client-urls: # 本地监听,接受客户端请求的端口advertise-client-urls: # 该成员的客户端url的列表,用于向所有人通告(告知所有人client访问我的哪儿)initial-cluster-token: # 集群唯一令牌,识别不同的集群initial-cluster: # 集群的成员,name是前面指定nameinitial-cluster-state: # 初始集群的状态,有“new,existing”data-dir: # 数据存储的目录 |
配置文件:
我们通过配置文件的方式启动
etcd1:
123456789 | name: "etcd1"initial-advertise-peer-urls: "http://10.20.48.107:2380"listen-peer-urls: "http://10.20.48.107:2380"listen-client-urls: "http://10.20.48.107:2379,http://127.0.0.1:2379"advertise-client-urls: "http://10.20.48.107:2379"initial-cluster-token: "heyhey-etcd"initial-cluster: "etcd1=http://10.20.48.107:2380,etcd2=http://10.20.48.108:2380,etcd3=http://10.20.48.109:2380"initial-cluster-state: "new"data-dir: "/home/qqdz/etcd/etcd1" |
etcd2:
123456789 | name: "etcd2"initial-advertise-peer-urls: "http://10.20.48.108:2380"listen-peer-urls: "http://10.20.48.108:2380"listen-client-urls: "http://10.20.48.108:2379,http://127.0.0.1:2379"advertise-client-urls: "http://10.20.48.108:2379"initial-cluster-token: "heyhey-etcd"initial-cluster: "etcd1=http://10.20.48.107:2380,etcd2=http://10.20.48.108:2380,etcd3=http://10.20.48.109:2380"initial-cluster-state: "new"data-dir: "/home/qqdz/etcd/etcd2" |
etcd3:
123456789 | name: "etcd3"initial-advertise-peer-urls: "http://10.20.48.109:2380"listen-peer-urls: "http://10.20.48.109:2380"listen-client-urls: "http://10.20.48.109:2379,http://127.0.0.1:2379"advertise-client-urls: "http://10.20.48.109:2379"initial-cluster-token: "heyhey-etcd"initial-cluster: "etcd1=http://10.20.48.107:2380,etcd2=http://10.20.48.108:2380,etcd3=http://10.20.48.109:2380"initial-cluster-state: "new"data-dir: "/home/qqdz/etcd/etcd3" |
启动脚本:
12345大专栏 Etcd集群配置an>678910111213141516171819202122232425262728293031323334353637383940414243444546474849 | startwork(){ nohup /usr/local/bin/etcd --config-file /home/qqdz/etcd/etcd.yml & echo "ps x | grep "etcd"" ps x|grep "etcd" }stopwork(){ SERVERLIST='etcd' for serv in $SERVERLIST do echo "stop $serv" ps aux|grep "/$serv"|sed -e "/grep/d"|awk '{print $2}'|xargs kill 2&>/dev/null sleep 1 done while test -f run.sh do count=`ps x|grep "etcd-c"|sed -e '/grep/d'|wc -l` echo "running server:"$count if [ $count -eq 0 ]; then break fi sleep 1 done}echo "-------------------start-----------------------"case $1 instop) stopwork;;start) startwork;;*) stopwork sleep 1 startwork;;esacecho "-------------------end-----------------------" |
常用命令:
获取etcd所有的keysETCDCTL_API=3 etcdctl get "" --prefix=true
ETCDCTL_API=3 etcdctl get "" --from-key
查看集群状态
在.bash_profile
中添加下面语句,这样每次登录都会自动加载变量ENDPOINTS=10.20.48.107:2379,10.20.48.108:2379,10.20.48.109:2379
export ENDPOINTS
通过下面命令查看集群状态:etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
12345678 | [ ~]$ etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status +-------------------+------------------+---------+---------+-----------+-----------+------------+| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |+-------------------+------------------+---------+---------+-----------+-----------+------------+| 10.20.48.107:2379 | 40c1a9f933c13188 | 3.1.11 | 33 kB | true | 176 | 41 || 10.20.48.108:2379 | c0598886ee4f3484 | 3.1.11 | 33 kB | false | 176 | 41 || 10.20.48.109:2379 | 3837344de92ee822 | 3.1.11 | 33 kB | false | 176 | 41 |+-------------------+------------------+---------+---------+-----------+-----------+------------+ |
通过下面命令查看健康状态:etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint health
1234 | [ ~]$ etcdctl --endpoints=$ENDPOINTS endpoint health 10.20.48.107:2379 is healthy: successfully committed proposal: took = 1.249516ms10.20.48.109:2379 is healthy: successfully committed proposal: took = 1.415811ms10.20.48.108:2379 is healthy: successfully committed proposal: took = 1.487734ms |
启动Etcd网关:etcd gateway start --endpoints=10.20.48.107:2379,10.20.48.108:2379,10.20.48.109:2379 --listen-addr="127.0.0.1:23790"
###host字段指定授权使用该证书的etcd节点IP或子网列表,需要将etcd集群的3个节点都添加其中。cp etcd-v3.3.13-linux-amd64/etcd* /opt/k8s/bin/