Skip to content

Instantly share code, notes, and snippets.

@x893675
Last active January 10, 2021 09:36
Show Gist options
  • Save x893675/8050a6ce9c80053040e0d0bf7ea9269a to your computer and use it in GitHub Desktop.
Save x893675/8050a6ce9c80053040e0d0bf7ea9269a to your computer and use it in GitHub Desktop.
etcd cluster deploy scripts
#!/bin/bash
CERTS=$PWD/cert
ETCD_IMAGE=k8s.gcr.io/etcd:3.4.3-0
NODE1=(node1 192.168.234.130:2379 192.168.234.130:2380 127.0.0.1:2381)
NODE2=(node2 192.168.234.130:3379 192.168.234.130:3380 127.0.0.1:3381 node2=https://192.168.234.130:3380,node1=https://192.168.234.130:2380)
NODE3=(node3 192.168.234.130:4379 192.168.234.130:4380 127.0.0.1:4381 node3=https://192.168.234.130:4380,node2=https://192.168.234.130:3380,node1=https://192.168.234.130:2380)
startEtcdNode(){
local -a extra_cmd=()
local name=$1
# advertise client endpoint, eg: 192.168.234.130:2379
local client_ep=$2
# advertise client endpoint, eg: 192.168.234.130:2380
local peer_ep=$3
# advertise client endpoint, eg: 127.0.0.1:2381
local metric_ep=$4
if [[ $# < 4 ]];then
echo "parameter error\n"
exit 1
elif [[ $# == 5 ]];then
extra_cmd+=" --initial-cluster-state=existing --initial-cluster=$5"
else
extra_cmd+=" --initial-cluster=$1=https://$3"
fi
local -a cmd=()
cmd+="docker run -d --net host -v ${CERTS}:/opt/pki/etcd ${ETCD_IMAGE} \
etcd --name=${name} --snapshot-count=10000 \
--trusted-ca-file=/opt/pki/etcd/ca.crt \
--peer-trusted-ca-file=/opt/pki/etcd/ca.crt \
--peer-cert-file=/opt/pki/etcd/peer.crt \
--peer-key-file=/opt/pki/etcd/peer.key \
--cert-file=/opt/pki/etcd/server.crt \
--peer-client-cert-auth=true \
--client-cert-auth=true \
--key-file=/opt/pki/etcd/server.key \
--advertise-client-urls https://${client_ep} \
--initial-advertise-peer-urls https://${peer_ep} \
--listen-client-urls https://${client_ep},https://127.0.0.1:${client_ep#*:} \
--listen-peer-urls https://${peer_ep} \
--listen-metrics-urls http://${metric_ep} \
${extra_cmd}"
${cmd}
}
addEtcdMember(){
bash $PWD/etcdctl.sh $@
}
# add node1
startEtcdNode ${NODE1[0]} ${NODE1[1]} ${NODE1[2]} ${NODE1[3]}
sleep 5
addEtcdMember member add ${NODE2[0]} --peer-urls=https://${NODE2[2]}
sleep 5
# add node2
startEtcdNode ${NODE2[0]} ${NODE2[1]} ${NODE2[2]} ${NODE2[3]} ${NODE2[4]}
sleep 5
# add node3
addEtcdMember member add ${NODE3[0]} --peer-urls=https://${NODE3[2]}
sleep 5
startEtcdNode ${NODE3[0]} ${NODE3[1]} ${NODE3[2]} ${NODE3[3]} ${NODE3[4]}
#!/bin/bash
PWD=$PWD
CLIENT_EPS=https://192.168.234.130:2379,https://192.168.234.130:3379,https://192.168.234.130:4379
docker run --rm -it \
--net host \
-v $PWD/cert:/opt/pki/etcd \
k8s.gcr.io/etcd:3.4.3-0 etcdctl \
--cacert /opt/pki/etcd/ca.crt \
--cert /opt/pki/etcd/server.crt \
--key /opt/pki/etcd/server.key \
--endpoints ${CLIENT_EPS} \
$@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment