Created
October 10, 2019 08:45
-
-
Save saosir/c91b07b60ee6aeacce95709f39efdea5 to your computer and use it in GitHub Desktop.
zookeeper单机集群批量部署脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env sh | |
| cd $(dirname $0) | |
| ZK_VERSION=3.4.14 | |
| ZK_CLUSTER_NUM=3 | |
| ZK_PKG="zookeeper-"$ZK_VERSION".tar.gz" | |
| ZK_PATH="zookeeper-"$ZK_VERSION | |
| ZK_DATA_DIR=/tmp/zookeeper | |
| start() { | |
| cd $ZK_PATH | |
| for (( i = 1; i <= $ZK_CLUSTER_NUM; i++ )); do | |
| ./bin/zkServer.sh start conf/zoo${i}.cfg > /dev/null | |
| done | |
| } | |
| stop() { | |
| cd $ZK_PATH | |
| for (( i = 1; i <= $ZK_CLUSTER_NUM; i++ )); do | |
| ./bin/zkServer.sh stop conf/zoo${i}.cfg > /dev/null | |
| done | |
| } | |
| restart() { | |
| stop; | |
| echo "sleeping........."; | |
| sleep 3; | |
| start; | |
| } | |
| install() { | |
| echo "start to install "${ZK_PATH} | |
| if [[ ! -f $ZK_PKG ]]; then | |
| echo "download ${ZK_PKG}..." | |
| curl -sSLO https://archive.apache.org/dist/zookeeper/zookeeper-${ZK_VERSION}/zookeeper-${ZK_VERSION}.tar.gz | |
| if [[ $? != 0 ]]; then | |
| exit -1 | |
| fi | |
| echo "download ${ZK_PKG} complete" | |
| fi | |
| echo "extra ${ZK_PKG} to ${ZK_PATH}" | |
| tar xzf $ZK_PKG | |
| cd $ZK_PATH | |
| rm -f conf/zoo[0-9].cfg | |
| for (( i = 1; i <= $ZK_CLUSTER_NUM; i++ )); do | |
| zoo_cfg=conf/zoo${i}.cfg | |
| zoo_data_dir=${ZK_DATA_DIR}${i} | |
| rm -rf ${zoo_data_dir} | |
| mkdir -p ${zoo_data_dir} | |
| echo ${i} > ${zoo_data_dir}/myid | |
| cp conf/zoo_sample.cfg $zoo_cfg | |
| sed -i "s|clientPort=.*|clientPort=218${i}|g" "$zoo_cfg" | |
| sed -i "s|dataDir=.*|dataDir=${zoo_data_dir}|g" "$zoo_cfg" | |
| for (( j = 1; j <= $ZK_CLUSTER_NUM; j++ )); do | |
| echo "server.${j}"=127.0.0.1:228${j}:238${j} >> $zoo_cfg | |
| done | |
| done | |
| } | |
| case "$1" in | |
| 'start') | |
| start | |
| ;; | |
| 'stop') | |
| stop | |
| ;; | |
| 'install') | |
| install | |
| ;; | |
| 'restart') | |
| restart | |
| ;; | |
| *) | |
| echo "usage: $0 {install|start|stop|restart}" | |
| exit 1 | |
| ;; | |
| esac |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
根据需要修改变量
使用
sh zk_cluster.sh install自动下载安装包安装sh zk_cluster.sh start启动集群sh zk_cluster.sh stop关闭集群