Step 1node
Mac下安裝etcddocker
brew install etcd
執行ssh
etcd --version
確保安裝成功curl
Step 2
使用docker-machine建立3個docker主機url
docker-machine create --driver virtualbox box0 docker-machine create --driver virtualbox box1 docker-machine create --driver virtualbox box2
確認生成成功,記住每一個docker主機的ip地址spa
docker-machine ls
Step 3
建立三個目錄node1,node2,node3在下一步會將主機的node1,node2,node3目錄經過--volume=nodex:/etcd-data 掛載到相應的container。3d
使用docker-machine ssh登入三個docker主機code
docker-machine ssh box0
Step 4
在三個docker主機上啓動etcd。
box0主機上執行blog
docker run --rm --name etcd \ -p 2379:2379 \ -p 2380:2380 \ --volume=node1:/etcd-data \ quay.io/coreos/etcd \ /usr/local/bin/etcd \ --data-dir=/etcd-data --name node1 \ --initial-advertise-peer-urls http://192.168.99.106:2380 --listen-peer-urls http://0.0.0.0:2380 \ --advertise-client-urls http://192.168.99.106:2379 --listen-client-urls http://0.0.0.0:2379 \ --initial-cluster-state new \ --initial-cluster-token docker-etcd \ --initial-cluster node1=http://192.168.99.106:2380,node2=http://192.168.99.107:2380,node3=http://192.168.99.108:2380
box1主機上執行token
docker run --rm --name etcd \ -p 2379:2379 \ -p 2380:2380 \ --volume=node2:/etcd-data \ quay.io/coreos/etcd \ /usr/local/bin/etcd \ --data-dir=/etcd-data --name node2 \ --initial-advertise-peer-urls http://192.168.99.107:2380 --listen-peer-urls http://0.0.0.0:2380 \ --advertise-client-urls http://192.168.99.107:2379 --listen-client-urls http://0.0.0.0:2379 \ --initial-cluster-state new \ --initial-cluster-token docker-etcd \ --initial-cluster node1=http://192.168.99.106:2380,node2=http://192.168.99.107:2380,node3=http://192.168.99.108:2380
box2主機上執行
docker run --rm --name etcd \ -p 2379:2379 \ -p 2380:2380 \ --volume=node3:/etcd-data \ quay.io/coreos/etcd \ /usr/local/bin/etcd \ --data-dir=/etcd-data --name node3 \ --initial-advertise-peer-urls http://192.168.99.108:2380 --listen-peer-urls http://0.0.0.0:2380 \ --advertise-client-urls http://192.168.99.108:2379 --listen-client-urls http://0.0.0.0:2379 \ --initial-cluster-state new \ --initial-cluster-token docker-etcd \ --initial-cluster node1=http://192.168.99.106:2380,node2=http://192.168.99.107:2380,node3=http://192.168.99.108:2380
PS:將ip地址換成各自經過docker-machine ls命令看到的ip
正常執行後應該經過docker ps可以看到
Step 5
查看集羣成員狀態
在任意一臺docker主機上進入docker容器
docker exec -it 49a0eeb7e3df /bin/sh
執行etcdctl member list可查看到全部成員的狀態
Step 6
確認
訪問任何一臺docker主機的ip
curl http://192.168.99.106:2379/v2/members
設定鍵值
curl http://192.168.99.106:2379/v2/keys/test -XPUT -d value="test value"
取得鍵值
curl http://192.168.99.106:2379/v2/keys/test