- zk_install.sh
- conf/config
- conf/zoo_template.cfg
- package
#!/bin/bash base_path=$(cd `dirname $0`; pwd) source ${base_path}/conf/config function install_java () { for (( i=0; i<${#zk_array[@]}; i++ )) do # 判断需要安装zookeeper的节点是否已有java环境 [zookeeper的启动依赖java环境] # 返回非零状态则表示java环境不存在,并安装java环境 # $USER 为环境变量,输出的值为当前用户的名称 [建议使用普通用户] ssh $USER@${zk_array[i]} 'java -version' &> /dev/null if [[ "$?" -ne '0' ]];then ssh $USER@${zk_array[i]} "mkdir -p ${java_path}" # 如果准备的java安装包名称不是 jdk.tar.gz ,可以重命名成 jdk.tar.gz ,或者修改 jdk.tar.gz 为当前安装包的名称 scp ${base_path}/package/jdk.tar.gz $USER@${zk_array[i]}:${pack_path}/ # 如果上一行有修改,此处的 jdk.tar.gz 也需要修改 ssh $USER@${zk_array[i]} "cd ${pack_path} && tar xf jdk.tar.gz -C ${java_path} && rm -f jdk.tar.gz" ssh $USER@${zk_array[i]} "echo "export JAVA_HOME=${java_path}" >> $HOME/.bashrc" ssh $USER@${zk_array[i]} "echo 'export PATH=$PATH:$JAVA_HOME/bin' >> $HOME/.bashrc" ssh $USER@${zk_array[i]} "echo 'source /etc/profile' >> $HOME/.bashrc" fi done } function install_zk () { # 将 conf 目录下的 zoo_template.cfg 文件重定向成 zoo_sample.cfg 文件,保持配置文件的干净 # zoo_template.cfg 文件需要提前准备好 cat ${base_path}/conf/zoo_template.cfg > ${base_path}/conf/zoo_sample.cfg # 定义一个变量conut,赋值为0,因为zookeeper是需要用到myid的,至于为什么不从1开始,程序员都懂的 count=0 for (( i=0; i<${#zk_array[@]}; i++ )) do # 每次循环,count的值则会加1 let count++ echo "server.${count}=${zk_array[i]}:2888:3888" >> ${base_path}/conf/zoo_sample.cfg done # 根据 conf 目录下的 config 文件来修改 zoo_sample.cfg 文件,以此来生成zookeeper的配置文件 # 这里只修改了端口和数据目录,其他的可以自行使用增加sed命令即可 sed -i "/clientPort/s/=.*/=${zk_clinet_port}/" ${base_path}/conf/zoo_sample.cfg sed -i "/dataDir/s#=.*#=${zk_data_dir}#" ${base_path}/conf/zoo_sample.cfg # 开始安装zookeeper for (( i=0; i<${#zk_array[@]}; i++ )) do ssh $USER@${zk_array[i]} "mkdir ${zk_path}" scp ${base_path}/package/${pack_name}.tar.gz $USER@${zk_array[i]}:${pack_path}/ ssh $USER@${zk_array[i]} "cd ${pack_path} && tar xf ${pack_name}.tar.gz -C ${zk_path} && rm -f ${pack_name}.tar.gz" ssh $USER@${zk_array[i]} "mkdir ${zk_data_dir}" scp ${base_path}/conf/zoo_sample.cfg $USER@${zk_array[i]}:${zk_path}/${pack_name}/conf/zoo.cfg done # 生成myid id_num=0 for (( i=0; i<${#zk_array[@]}; i++ )) do let id_num++ ssh $USER@${zk_array[i]} "echo ${id_num} > ${zk_data_dir}/myid" done } # 启动zookeeper function start_zk () { for (( i=0; i<${#zk_array[@]}; i++ )) do ssh $USER@${zk_array[i]} "${zk_path}/${pack_name}/bin/zkServer.sh restart" done } # 检查zookeeper的状态 function status_zk () { # 检查每个节点的zookeeper状态 for (( i=0; i<${#zk_array[@]}; i++ )) do # 利用zkServer.sh status来查看zookeeper的状态,grep -o 是只过滤Mode这个关键字,存在的情况下,只会返回Mode local check_status=$(ssh $USER@${zk_array[i]} "${zk_path}/${pack_name}/bin/zkServer.sh status | grep -o Mode") # 通过while死循环加上字符串比较来等待zookeeper服务的状态 [依赖java环境的服务,启动普遍偏慢] while [[ x"${check_status}" != x"Mode" ]] do sleep 10 # 重新赋值来刷新变量值 local check_status=$(ssh $USER@${zk_array[i]} "${zk_path}/${pack_name}/bin/zkServer.sh status | grep -o Mode") done done if [[ $? -eq 0 ]];then echo "zookeeper cluster install success" else echo "zookeeper cluster install fail" exit 2 fi } install_java install_zk start_zk status_zkconf/config
#!/bin/bash base_path=$(cd `dirnameconf/zoo_template.cfg# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=/tmp/zookeeper-3.4.10/data # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1`; pwd) # 此处的ip为zookeeper节点ip zk_hosts=' 192.168.72.107 192.168.72.108 192.168.72.109 ' # 这个是格式转换,将上面的变量转换成数组的格式 zk_array=($(printf "%qn" ${zk_hosts})) # 一般zookeeper的安装包名称格式是zookeeper-版本,这里只需要和安装包的一样就可以了,去掉后缀 pack_name='zookeeper-3.4.10' # java和zookeeper安装的目录 [注意查看磁盘空间大小,以实际情况来修改] pack_path='/approot1' java_path="${pack_path}/jdk" zk_path="${pack_path}/zookeeper" zk_data_dir="${zk_path}/${pack_name}/data" # zookeeper的默认端口是2181,根据实际需求修改 zk_clinet_port=7181
这是zookeeper安装包里面的配置文件模板
|___ jdk.tar.gz |___ zookeeper-3.4.10.tar.gzpackage
安装包存放的路径
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)