環境 docker
host1 10.1.99.13shell
host2 10.1.99.14測試
host3 10.1.99.15url
host4 10.1.99.12(用於測試添加刪除節點)spa
初始化集羣3d
host1 rest
$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ --name etcd0 \ --advertise-client-urls http://10.1.99.13:2379,http://10.1.99.13:4001 \ --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http://10.1.99.13:2380 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state new
host2code
$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ --name etcd1 \ --advertise-client-urls http://10.1.99.14:2379,http://10.1.99.14:4001 \ --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http://10.1.99.14:2380 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state new
host3blog
$ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ -name etcd2 \ --advertise-client-urls http://10.1.99.15:2379,http://10.1.99.15:4001 \ --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http://10.1.99.15:2380 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state new
驗證token
#選擇任意一個節點 進入 etcd shell $ docker exec -it etcd bin/sh # 查看節點狀態 $ etcdctl member list 52a25183c1fa5a39: name=etcd0 peerURLs=http://10.1.99.13:2380 clientURLs=http://10.1.99.13:2379,http://10.1.99.13:4001 isLeader=false 69aa775a244f2954: name=etcd1 peerURLs=http://10.1.99.14:2380 clientURLs=http://10.1.99.14:2379,http://10.1.99.14:4001 isLeader=true f18991cb367747f3: name=etcd2 peerURLs=http://10.1.99.15:2380 clientURLs=http://10.1.99.15:2379,http://10.1.99.15:4001 isLeader=false #查看集羣狀態 etcdctl cluster-health member 52a25183c1fa5a39 is healthy: got healthy result from http://10.1.99.13:2379 member 69aa775a244f2954 is healthy: got healthy result from http://10.1.99.14:2379 member f18991cb367747f3 is healthy: got healthy result from http://10.1.99.15:2379 cluster is healthy #插入一條記錄 $ etcdctl set /home/etcdtest value value #讀取插入記錄 在其餘節點執行獲取到相同結果 $ etcdctl get /home/etcdtest value
集羣管理在集羣中加入一個節點
#在現有的集羣中執行如下命令 $ etcdctl member add etcd3 http://10.1.99.12:2380 Added member named etcd3 with ID 7b33d7070ed90c25 to cluster ETCD_NAME="etcd3" ETCD_INITIAL_CLUSTER="etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd3=http://10.1.99.12:2380,etcd2=http://10.1.99.15:2380" ETCD_INITIAL_CLUSTER_STATE="existing" #在對應的待加入的節點上 $ docker run --restart always -d -v /usr/share/ca-certificates/:/etc/ssl/certs -p 4001:4001 -p 2380:2380 -p 2379:2379 \ --name etcd quay.io/coreos/etcd \ etcd \ --name etcd3 \ --advertise-client-urls http://10.1.99.12:2379,http://10.1.99.12:4001 \ --listen-client-urls http://0.0.0.0:2379,http://0.0.0.0:4001 \ --initial-advertise-peer-urls http://10.1.99.12:2380 \ --listen-peer-urls http://0.0.0.0:2380 \ --initial-cluster-token etcd-cluster-1 \ --initial-cluster etcd0=http://10.1.99.13:2380,etcd1=http://10.1.99.14:2380,etcd3=http://10.1.99.12:2380,etcd2=http://10.1.99.15:2380 \ --initial-cluster-state existing 注意事項: 1 使用上面生成的配置內容分別修改對應的屬性 2 設置 --initial-cluster-state existing 刪除節點, #在現有的集羣中執行如下命令 #顯示當前集羣中的所用節點信息 $ etcdctl member list 52a25183c1fa5a39: name=etcd0 peerURLs=http://10.1.99.13:2380 clientURLs=http://10.1.99.13:2379,http://10.1.99.13:4001 isLeader=false 69aa775a244f2954: name=etcd1 peerURLs=http://10.1.99.14:2380 clientURLs=http://10.1.99.14:2379,http://10.1.99.14:4001 isLeader=true 7b33d7070ed90c25: name=etcd3 peerURLs=http://10.1.99.12:2380 clientURLs=http://10.1.99.12:2379,http://10.1.99.12:4001 isLeader=false f18991cb367747f3: name=etcd2 peerURLs=http://10.1.99.15:2380 clientURLs=http://10.1.99.15:2379,http://10.1.99.15:4001 isLeader=false #在集羣中剔除待刪除節點 $ etcdctl member remove 7b33d7070ed90c25