RHEL automatically installs the Zookeeper shell script

Source: Internet
Author: User
A: The machines running this script, LinuxRHEL6B, C, D ,...: The machine on which zookeepercluster is to be installed. LinuxRHEL6 first makes sure that you can log on to the machine B, C, D ,... and then you can run this script on A: $. /install_zookeeper prerequisites: the repo must be configured on machines B, C, and D. This script uses the repo of cdh5.

A: This script runs on Linux RHEL6.

B, C, D,...: The machine on which zookeeper cluster is to be installed, Linux RHEL6

First, you can log on to machine B, C, D, and ,... and then you can run the script on:

$./Install_zookeeper

Prerequisites:

B, C, D machine must be configured with repo, this script uses cdh5 repo, the following content is saved to:/etc/yum. repos. d/cloudera-cdh5.repo:

Cloudera-cdh5
# Packages for Cloudera's Distribution for Hadoop, Version 5, on RedHat or CentOS 6 x86_64
Name = Cloudera's Distribution for Hadoop, Version 5
Base url = http://archive.cloudera.com/cdh5/redhat/6/x86_64/cdh/5/
Gpgkey = http://archive.cloudera.com/cdh5/redhat/6/x86_64/cdh/RPM-GPG-KEY-cloudera
Gpgcheck = 1
Enabled = 1

The automatic installation script automatically installs zookeeper on machines B, C, and D and configures the relevant configuration files. But they are not started. Supports 1, 3, 5, and 7 servers.

#! /Bin/bash
#
# @ File
# Install_zookeeper.sh
#
# @ Date
#2014-12-21
#
# @ Author
# Cheungmine@hgdb.net
#
# @ Version
# 0.0.1pre
#
# @ Usage
#./Install_zookeeper.sh
######################################## ########################################

#*************************************** ********************
# Split_to_array
# Split string into array
#*************************************** ********************
Function split_to_array (){
OLD_IFS = "$ IFS"
IFS = "$2"
Array = ($1)
IFS = "$ OLD_IFS"
}


#*************************************** ********************
# Install_zookeeper
# Install zookeeper on 1, 3 or 5 servers
#
# Parameters:
# ClientPort-the port at which the clients will connect
# Servers-varying arguments: 1, 3, 5, up to 7
# "ZkServer: serverPort: appPort"
# ZkServer-ipaddr of zookeeper server
# ServerPort-communication port for zookeeper servers
# AppPort-communication port between zookeeper with other applications
#
# Example:
#1) install_zookeeper 2181 zk1 zk2 zk3
#2) install_zookeeper 2181 192.168.122.201 192.168.122.202 192.168.122.203
#3) install_zookeeper "2181" "192.168.122.201: 2888: 3888" "192.168.122.202: 2888: 3888" "192.168.122.203: 2888: 3888"
#4) install_zookeeper "2181:/var/lib/zookeeper" "192.168.122.201: 2888: 3888" "192.168.122.202: 2888: 3888" "192.168.122.203: 2888: 3888"
#*************************************** ********************
ERR_INVALID_ZK_SERVERS = 1001

Function install_zookeeper (){
Echo-e" Install zookeeper on cluster ..."
# Chk_root

Local ret clientPort dataDir len I ZOO_CFG server serverPort appPort destip destlogin

ServerPort = 2888
AppPort = 3888

# Parse the first argument
Split_to_array $1 ":"

# The port at which the clients will connect
ClientPort =$ {array [0]}

# The directory where the snapshot is stored
DataDir = "/var/lib/zookeeper"
If [$ {# array [*]}-eq 2]; then
DataDir =$ {array [1]}
Fi

Echo-e" ClientPort: $ clientPort"
Echo-e" DataDir: $ dataDir"

# Zookeeper configure file
ZOO_CFG = "/usr/lib/zookeeper/conf/zoo. cfg"

# Get list of servers: args
Shift
Local argc =$ #

If [$ argc-eq 1-o $ argc-eq 3-o $ argc-eq 5-o $ argc-eq 7]; then
Echo-e" Zookeeper servers in cluster: [$ argc]"
Else
Echo-e" Invalid zookeeper servers: [$ argc]"
Exit $ ERR_INVALID_ZK_SERVERS;
Fi

Local argv = "$ @"

OLD_IFS = "$ IFS"
IFS = ""
Local args = ($ argv)
IFS = "$ OLD_IFS"

# Array variable
Local ipaddrs = ()
Local servers = ()

Local sid = 0
For a in $ {args [@]}
Do
Let sid ++

# Check if server format is either:
# ServerIP
# Or:
# ServerIP: serverPort: appPort
Split_to_array $ ":"
ServerIP =$ {array [0]}

If [$ {# array [*]}-ne 3]; then
A = "$ serverIP: $ serverPort: $ appPort ";
Fi

Local server = "server. $ sid = $"
Servers [sid-1] = $ server
Echo $ server

Ipaddrs [sid-1] = $ serverIP
Done

# Output array to one line string: echo $ {servers [@]}
# Get length of array
Len =$ {# servers [*]}
I = 0
While [$ I-lt $ len]
Do
Let sid = I + 1
Destip =$ {ipaddrs [$ I]}
Destlogin = root @ $ destip
Echo-e" Login ing server. $ sid: $ destip... \ c"

Ret = 'ssh $ destlogin "yum install-y zookeeper-server & service zookeeper-server init -- myid = $ sid "'

Ret = 'ssh $ destlogin "echo '#! {Install_zookeeper@hgdb.net ==> '> $ ZOO_CFG "'

For s in $ {servers [*]}
Do
Ret = 'ssh $ destlogin "echo '$ s'> $ ZOO_CFG "'
Done

Ret = 'ssh $ destlogin "echo '#! <= Install_zookeeper@hgdb.net} '> $ ZOO_CFG "'

Echo-e "OK ."

Let I ++
Done

Echo" Zookeeper cluster installation completed successfully! "
}

#===================================================== ======================================
Install_zookeeper "2181" "192.168.122.201" "192.168.122.202" "192.168.122.203"

Modify the last line according to the Configuration:

Install_zookeeper "2181" "192.168.122.201" "192.168.122.202" "192.168.122.203"

Note: You need to stop the firewall on each machine in zk-cluster and then start zookeeper:

$/Usr/lib/zookeeper/bin/zkServer. sh start-foreground

-------------------------------------- Split line --------------------------------------

ZooKeeper cluster configuration http://www.linuxidc.com/Linux/2013-06/86348.htm

Use ZooKeeper to implement distributed shared lock http://www.linuxidc.com/Linux/2013-06/85550.htm

Distributed service framework ZooKeeper-manage data http://www.linuxidc.com/Linux/2013-06/85549.htm in distributed environment

ZooKeeper Cluster Environment Construction Practice http://www.linuxidc.com/Linux/2013-04/83562.htm

ZooKeeper server cluster environment configuration test http://www.linuxidc.com/Linux/2013-04/83559.htm

ZooKeeper cluster installation http://www.linuxidc.com/Linux/2012-10/72906.htm

-------------------------------------- Split line --------------------------------------

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2014-12/110709.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.