通过systemd 管理 docker-etcd
###
# make sure etcd process has write access to this directory
# remove this directory if the cluster is new; keep if restarting etcd
# rm -rf /tmp/etcd/s1
# to write service file for etcd with Docker
cat > /tmp/s1.service <<EOF
[Unit]
Description=etcd with Docker
Documentation=https://github.com/coreos/etcd
[Service]
Restart=always
RestartSec=5s
TimeoutStartSec=0
LimitNOFILE=40000
ExecStart=/usr/bin/docker \
run \
--rm \
--net=host \
--name etcd-v3.3.0-rc.0 \
--volume=/docker/etcd/s1:/etcd-data \
gcr.io/etcd-development/etcd:v3.3.0-rc.0 \
/usr/local/bin/etcd \
--name s1 \
--data-dir /etcd-data \
--listen-client-urls http://localhost:2379 \
--advertise-client-urls http://localhost:2379 \
--listen-peer-urls http://localhost:2380 \
--initial-advertise-peer-urls http://localhost:2380 \
--initial-cluster s1=http://localhost:2380,s2=http://localhost:22380,s3=http://localhost:32380 \
--initial-cluster-token tkn \
--initial-cluster-state new \
--auto-compaction-retention 1
ExecStop=/usr/bin/docker stop etcd-v3.3.0-rc.0
[Install]
WantedBy=multi-user.target
EOF
sudo mv /tmp/s1.service /etc/systemd/system/s1.service
# to start service
sudo systemctl daemon-reload
sudo systemctl cat s1.service
sudo systemctl enable s1.service
sudo systemctl start s1.service
# to get logs from service
sudo systemctl status s1.service -l --no-pager
sudo journalctl -u s1.service -l --no-pager|less
sudo journalctl -f -u s1.service
# to stop service
sudo systemctl stop s1.service
sudo systemctl disable s1.service
机器环境变量设置
每台机器设置以下环境变量
export ETCD_VERSION=v3.1.0-alpha.1
export TOKEN=my-etcd-token
export CLUSTER_STATE=new
export NAME_1=etcd-node-0
export NAME_2=etcd-node-1
export NAME_3=etcd-node-2
export HOST_1=192.168.56.101
export HOST_2=192.168.56.104
export HOST_3=192.168.56.105
export CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380
每个机器安装etcd容器
进入HOST_1机器,设置环境变量和起容器。
export THIS_NAME=${NAME_1}
export THIS_IP=${HOST_1}
sudo docker run -d --net=host --name etcd quay.io/coreos/etcd:${ETCD_VERSION}
/usr/local/bin/etcd \
--data-dir=data.etcd --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
进入HOST_2机器,设置环境变量和起容器
export THIS_NAME=${NAME_2}
export THIS_IP=${HOST_2}
sudo docker run --net=host -d --name etcd quay.io/coreos/etcd:${ETCD_VERSION}
/usr/local/bin/etcd \
--data-dir=data.etcd --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
进入HOST_3机器,设置环境变量和起容器
export THIS_NAME=${NAME_3}
export THIS_IP=${HOST_3}
sudo docker run --net=host -d --name etcd quay.io/coreos/etcd:${ETCD_VERSION}
/usr/local/bin/etcd \
--data-dir=data.etcd --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
测试集群运行情况:
docker exec etcd /bin/sh -c "export ETCDCTL_API=3 && /usr/local/bin/etcdctl put foo bar"