penglongli / blog

18 stars 1 forks source link

Etcd 集群搭建及 Etcdctl 工具 #26

Open penglongli opened 6 years ago

penglongli commented 6 years ago

Etcd 基于 Docker 的集群搭建

此例子的 Etcd 集群基于 Docker,版本 v2.3.7 开三个节点,分别在三个节点执行: host-1:

#!/bin/bash

VERSION=v2.3.7

docker pull quay.io/coreos/etcd/etcd:$VERSION

docker rm -f etcd

docker run -d --name etcd --restart=always \
        --net=host \
        -v /data/etcd:/data:rw \
        quay.io/coreos/etcd:$VERSION \
        -name etcd0 \
        -data-dir /data \
        -initial-advertise-peer-urls http://192.168.0.131:2380 \
        -listen-peer-urls http://192.168.0.131:2380 \
        -listen-client-urls http://192.168.0.131:2379,http://127.0.0.1:2379 \
        -advertise-client-urls http://192.168.0.131:2379 \
        -initial-cluster-token etcd-cluster-1 \
        -initial-cluster etcd0=http://192.168.0.131:2380,etcd1=http://192.168.0.134:2380,etcd2=http://192.168.0.133:2380 \
        -initial-cluster-state new

host-2:

#!/bin/bash

VERSION=v2.3.7

docker pull quay.io/coreos/etcd/etcd:$VERSION

docker rm -f etcd

docker run -d --name etcd --restart=always \
        --net=host \
        -v /data/etcd:/data:rw \
        quay.io/coreos/etcd:$VERSION \
        -name etcd1 \
        -data-dir /data \
        -initial-advertise-peer-urls http://192.168.0.134:2380 \
        -listen-peer-urls http://192.168.0.134:2380 \
        -listen-client-urls http://192.168.0.134:2379,http://127.0.0.1:2379 \
        -advertise-client-urls http://192.168.0.134:2379 \
        -initial-cluster-token etcd-cluster-1 \
        -initial-cluster etcd0=http://192.168.0.131:2380,etcd1=http://192.168.0.134:2380,etcd2=http://192.168.0.133:2380 \
        -initial-cluster-state new

host-3:

#!/bin/bash

VERSION=v2.3.7

docker pull quay.io/coreos/etcd/etcd:$VERSION

docker rm -f etcd

#mkdir -p /data/etcd

docker run -d --name etcd --restart=always \
        --net=host \
        -v /data/etcd:/data:rw \
        quay.io/coreos/etcd:$VERSION \
        -name etcd2 \
        -data-dir /data \
        -initial-advertise-peer-urls http://192.168.0.133:2380 \
        -listen-peer-urls http://192.168.0.133:2380 \
        -listen-client-urls http://192.168.0.133:2379,http://127.0.0.1:2379 \
        -advertise-client-urls http://192.168.0.133:2379 \
        -initial-cluster-token etcd-cluster-1 \
        -initial-cluster etcd0=http://192.168.0.131:2380,etcd1=http://192.168.0.134:2380,etcd2=http://192.168.0.133:2380 \
        -initial-cluster-state new

Etcd 集群节点故障恢复

假设某个节点故障然后 Crash 掉了,可以通过修改 -initial-cluster-state 的值为:existing,来实现故障节点恢复。

Etcdctl 工具使用