由於一個業務etcd存在單點,因此單獨搭建一個集羣,替換掉原來的單點,在數據同步的時候還折騰了一下,好記性好比爛筆頭!!!linux
OLD
etcd=172.0.254.66git
NEW
etcd1=172.0.254.5
etcd2=172.0.254.6
etcd3=172.0.254.7github
在腳本處填寫3臺ETCD集羣的IP,並在每臺服務器執行shell
#!/bin/bash # 下載二進制etcd並安裝 version=v3.1.11 downloadUrl=https://github.com/etcd-io/etcd/releases/download etcd1=172.0.254.5 etcd2=172.0.254.6 etcd3=172.0.254.7 localIp=$(ip a show eth0|grep -o -P '(\d*\.){3}\d*'|head -1) if [ "$localIp" == "$etcd1" ];then etcdNum="etcd-1" elif [ "$localIp" == "$etcd2" ];then etcdNum="etcd-2" elif [ "$localIp" == "$etcd3" ];then etcdNum="etcd-3" else echo "local server ip is not etcd server:${localIp}"; exit fi mkdir -p /soft curl -L ${downloadUrl}/${version}/etcd-${version}-linux-amd64.tar.gz -o /soft/etcd-${version}-linux-amd64.tar.gz cd /soft && tar -xf /soft/etcd-${version}-linux-amd64.tar.gz mv /soft/etcd-${version}-linux-amd64 /soft/etcd /soft/etcd/etcd --version /soft/etcd/etcdctl version # etcd配置文件 mkdir -p /soft/etcd/conf cat >/soft/etcd/conf/etcd.yml <<EOF name: $etcdNum data-dir: /data/etcd listen-client-urls: http://${localIp}:2379,http://127.0.0.1:2379 advertise-client-urls: http://${localIp}:2379,http://127.0.0.1:2379 listen-peer-urls: http://${localIp}:2380 initial-advertise-peer-urls: http://${localIp}:2380 initial-cluster: etcd-1=http://${etcd1}:2380,etcd-2=http://${etcd2}:2380,etcd-3=http://${etcd3}:2380 initial-cluster-token: etcd-cluster-token initial-cluster-state: new EOF nohup /soft/etcd/etcd --config-file=/soft/etcd/conf/etcd.yml >>/soft/etcd/stdout.out 2>&1 & ps -ef|grep etcd
經過一個shell腳本管理進程
vim /etc/init.d/etcdjson
#!/bin/bash # chkconfig: - 00 00 # description: etcd manager # date=2020.11.05 # 用於管理進程啓動關閉查看 # 啓動程序文件 command=/soft/etcd/etcd function func_getpid() { pid=$(ps -ef | grep "$command"|grep -v "grep"|awk '{print $2}') } function func_start(){ func_getpid [ -n "$pid" ] && { echo "[start] $command is already unning,exit";exit; } nohup /soft/etcd/etcd --config-file=/soft/etcd/conf/etcd.yml >>/soft/etcd/stdout.out 2>&1 & if [ $? == 0 ];then echo "[start] suscess" else echo "[start] error" fi } function func_stop(){ func_getpid for i in ${pid[@]} do kill -9 $i || echo "[stop] error" sleep 1 && echo "[stop] $command pid:$i stoped" done } function func_status(){ func_getpid if [ ! -n "$pid" ];then echo "[check] $command is already stoped" else for i in ${pid[@]} do echo "[check] $command is running,pid is $i" done fi } function func_manager() { case "$1" in start) func_start func_status ;; stop) func_stop func_status ;; status) func_status ;; restart) func_status func_stop func_start func_status ;; *) echo "Arguments use start|status|stop|restart" ;; esac } if [ "$#" -ne "1" ];then echo "Arguments number need eq 1" exit 1 fi func_manager $1
make-mirror簡介
參考:https://www.mankier.com/1/etc...vim
make-mirror命令的字面意思是:製做一個目標etcd集羣的鏡像,主要用來多個etcd集羣合併,但要求集羣間的key不能重複,咱們是一個現有的單點遷移到空集羣,所以恰好知足bash
查看etcdctl make-mirror命令解釋服務器
ETCDCTL_API=3 etcdctl make-mirror [options] <destination> [flags]
make-mirror Makes a mirror at the destination etcd cluster
$ etcdctl make-mirror --help NAME: make-mirror - Makes a mirror at the destination etcd cluster USAGE: etcdctl make-mirror [options] <destination> OPTIONS: --dest-cacert="" Verify certificates of TLS enabled secure servers using this CA bundle --dest-cert="" Identify secure client using this TLS certificate file for the destination cluster --dest-insecure-transport[=true] Disable transport security for client connections --dest-key="" Identify secure client using this TLS key file --dest-prefix="" destination prefix to mirror a prefix to a different prefix in the destination cluster --no-dest-prefix[=false] mirror key-values to the root of the destination cluster --prefix="" Key-value prefix to mirror GLOBAL OPTIONS: --cacert="" verify certificates of TLS-enabled secure servers using this CA bundle --cert="" identify secure client using this TLS certificate file --command-timeout=5s timeout for short running command (excluding dial timeout) --dial-timeout=2s dial timeout for client connections --endpoints=[127.0.0.1:2379] gRPC endpoints --hex[=false] print byte strings as hex encoded strings --insecure-skip-tls-verify[=false] skip server certificate verification --insecure-transport[=true] disable transport security for client connections --key="" identify secure client using this TLS key file --user="" username[:password] for authentication (prompt if password is not supplied) -w, --write-out="simple" set the output format (fields, json, protobuf, simple, table)
同步etcd數據
在其中一臺新的etcd服務器執行,例如在etcd1:172.0.254.5上面執行curl
ETCDCTL_API=3 etcdctl make-mirror --endpoints=http://172.0.253.66:2379 http://172.0.254.5:2379
export ETCDCTL_API=3 etcdctl get --prefix --keys-only "" #查看全部key
# 導出全部key的值 for i in `etcdctl get --prefix --keys-only "" |sort`;do etcdctl get $i >>/tmp/etcdcheck.log;done
將新老etcd集羣的數據導出來對比一下鍵值,看看是否一致,一致則經過
注:這裏只適合小數據量對比ide