一键部署ETCD集群脚本

Posted 执酒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一键部署ETCD集群脚本相关的知识,希望对你有一定的参考价值。

#!/bin/bash
set -x
set -e
 
#更改这里的IP, 只支持部署3个节点etcd集群
declare -A NODE_MAP=(["etcd0"]="192.168.0.153" ["etcd1"]="192.168.0.154" ["etcd2"]="192.168.0.164")
 
etcddownload()
{
    ETCD_VER=v3.2.9    #指定要安装的版本号
    DOWNLOAD_URL=https://github.com/coreos/etcd/releases/download
    [ -f ${PWD}/temp-etcd/etcd ]  && return
    curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o ${PWD}/etcd-${ETCD_VER}-linux-amd64.tar.gz
    mkdir -p ${PWD}/temp-etcd && tar -zxf ${PWD}/etcd-${ETCD_VER}-linux-amd64.tar.gz -C ${PWD}/temp-etcd --strip-components=1
}
 
etcdconfig()
{
    local node_index=$1
 
cat <<EOF >${PWD}/${node_index}.conf
ETCD_NAME=${node_index}
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://${NODE_MAP[${node_index}]}:2380"
ETCD_LISTEN_PEER_URLS="http://${NODE_MAP[${node_index}]}:2380"
ETCD_LISTEN_CLIENT_URLS="http://${NODE_MAP[${node_index}]}:2379,http://127.0.0.1:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://${NODE_MAP[${node_index}]}:2379"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-378"
ETCD_INITIAL_CLUSTER="etcd0=http://${NODE_MAP[\'etcd0\']}:2380,etcd1=http://${NODE_MAP[\'etcd1\']}:2380,etcd2=http://${NODE_MAP[\'etcd2\']}:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
# ETCD_DISCOVERY=""
# ETCD_DISCOVERY_SRV=""
# ETCD_DISCOVERY_FALLBACK="proxy"
# ETCD_DISCOVERY_PROXY=""
#
# ETCD_CA_FILE=""
# ETCD_CERT_FILE=""
# ETCD_KEY_FILE=""
# ETCD_PEER_CA_FILE=""
# ETCD_PEER_CERT_FILE=""
# ETCD_PEER_KEY_FILE=""
EOF
}
 
etcdgen_unit()
{
cat <<EOF >${PWD}/etcd.service
[Unit]
Description=Etcd Server
After=network.target
 
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd
EnvironmentFile=-/etc/etcd/10-etcd.conf
ExecStart=/usr/bin/etcd
Restart=always
RestartSec=8s
LimitNOFILE=40000
 
[Install]
WantedBy=multi-user.target
EOF
}
 
SSH_OPTS="-oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null -oLogLevel=ERROR -C"
etcdscp()
{
  local host="$1"
  local src=($2)
  local dst="$3"
  scp -r ${SSH_OPTS} ${src[*]} "${host}:${dst}"
}
etcdssh()
{
  local host="$1"
  shift
  ssh ${SSH_OPTS} -t "${host}" "$@" >/dev/null 2>&1
}
etcdssh_nowait()
{
  local host="$1"
  shift
  ssh ${SSH_OPTS} -t "${host}" "nohup $@" >/dev/null 2>&1 &
}
 
etcddeploy()
{
    for key in ${!NODE_MAP[@]}
    do
        etcdconfig $key
        etcdssh "root@${NODE_MAP[$key]}" "mkdir -p /var/lib/etcd /etc/etcd"
        etcdscp "root@${NODE_MAP[$key]}" "${key}.conf" "/etc/etcd/10-etcd.conf"
        etcdscp "root@${NODE_MAP[$key]}" "etcd.service" "/usr/lib/systemd/system"
        etcdscp "root@${NODE_MAP[$key]}" "${PWD}/temp-etcd/etcd ${PWD}/temp-etcd/etcd" "/usr/bin"
        etcdscp "root@${NODE_MAP[$key]}" "${PWD}/temp-etcd/etcd ${PWD}/temp-etcd/etcdctl" "/usr/bin"
        etcdssh "root@${NODE_MAP[$key]}" "chmod 755 /usr/bin/etcd*"
        etcdssh_nowait "root@${NODE_MAP[$key]}" "systemctl daemon-reload && systemctl enable etcd && nohup systemctl start etcd"
    done
 
}
 
etcdclean()
{
  for key in ${!NODE_MAP[@]}
  do
    rm -f ${PWD}/${key}.conf
  done
  rm -f ${PWD}/etcd.service
}
 
 
#etcddownload
etcdgen_unit
etcddeploy
etcdclean
 
echo -e "\\033[32m 部署完毕! 执行 etcdctl cluster-health,检测是否OK。\\033[0m"

 

1、准备三台机器,系统为centos7

yum install -y epel-release vim bash-c* net-tools lrzsz

2、关闭防火墙和selinux

# systemctl stop firewalld && systemctl disable firewalld

# vim /etc/selinux/config

SELINUX=disabled

# setenforce 0

3、校对时间

yum install -y ntp ntpdate && ntpdate pool.ntp.org

4、设置ssh互信

5、执行脚本即可。

# bash deploy-etcd.sh

原地址:http://www.cnblogs.com/keithtt/p/6649951.html

以上是关于一键部署ETCD集群脚本的主要内容,如果未能解决你的问题,请参考以下文章

etcd集群部署

Kubernetes单Master节点集群二进制部署

kubernets集群二进制单节点部署

kubernets集群二进制单节点部署

ETCD 集群部署

二进制部署K8S单节点