目录
一、虚拟机设置
1.新建一台虚拟机并克隆三台,配置自定义
2.修改四台虚拟机的主机名并立即生效
3.修改四台虚拟机的网络信息
4.重启四台虚拟机的网络服务并测试网络连接
5.重启四台虚拟机,启动后关闭四台虚拟机的防火墙
6.在第一台虚拟机的/etc/hosts下面配置这四台主机映射
7.将/etc/hosts安全分发到另外三台虚拟机
8.配置四台虚拟机免密登录
9.设置四台虚拟机时间同步
二、自动安装JDK和zookeeper
1.在第一台虚拟机的/opt/目录下新建install、soft和shell三个文件
2.上传jdk、zoookeeper压缩包到第一台虚拟机/opt/install/目录下
3.编写自动安装jdk和zookeeper脚本并赋予执行权限
4.执行/opt/shell/autoinstall.sh脚本
5.重启环境变量查看是否安装成功
6.来到/opt/soft/zk345/conf目录下,修改zoo.cfg文件,添加下面的内容
7.来到/opt/soft/zk345目录下,删除datas目录下所有的文件
8.将JDK和环境变量分发给三台虚拟机,将zookeeper分发给两台虚拟机
9.删除三台虚拟机的/opt/soft/zk345/datas目录下的所有文件,配置myid
10.编辑zookeeper集群开启脚本——zkop.sh
11.编写查询集群启动状态信息批量展示脚本——showjps.sh
12.执行zkop.sh和showjps.sh脚本,开启zookeeper集群
13.查看zookeeper集群状态
14.关闭zookeeper集群
一、虚拟机设置
1.新建一台虚拟机并克隆三台,配置自定义
也可以新建好一个,然后导出ovf,再打开
2.修改四台虚拟机的主机名并立即生效
[root@localhost ~]# hostnamectl set-hostname 主机名
[root@localhost ~]#bash
[root@主机名~]# hostname
主机名
3.修改四台虚拟机的网络信息
vim /etc/sysconfig/network-script/ifcfg-ens33
4.重启四台虚拟机的网络服务并测试网络连接
systemctl restart network.service
# 或者
service network restart
5.重启四台虚拟机,启动后关闭四台虚拟机的防火墙
[root@ant165 ~]# systemctl stop firewalld
[root@ant165 ~]# systemctl disable firewalld.service
6.在第一台虚拟机的/etc/hosts下面配置这四台主机映射
[root@ant165 ~]# vim /etc/hosts
7.将/etc/hosts安全分发到另外三台虚拟机
[root@ant165 .ssh]# scp /etc/hosts root@ant166:/etc/
hosts 100% 250 18.3KB/s 00:00
[root@ant165 .ssh]# scp /etc/hosts root@ant167:/etc/
hosts 100% 250 162.4KB/s 00:00
[root@ant165 .ssh]# scp /etc/hosts root@ant168:/etc/
hosts 100% 250 169.3KB/s 00:00
8.配置四台虚拟机免密登录
[root@ant165 ~]# ssh ant165
The authenticity of host 'ant165 (192.168.180.165)' can't be established.
ECDSA key fingerprint is SHA256:hQzFzPZt1T9MooVcHRLOnmC4hYlNsaG28J65Ovi10uc.
ECDSA key fingerprint is MD5:46:97:1a:6c:02:8d:17:d6:98:d9:81:85:0e:a8:ca:5e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'ant165,192.168.180.165' (ECDSA) to the list of known hosts.
root@ant165's password:
Last login: Wed Feb 15 00:01:20 2023 from 192.168.180.1
[root@ant165 ~]# ll -al
total 32
dr-xr-x---. 3 root root 163 Feb 15 00:04 .
dr-xr-xr-x. 17 root root 224 Feb 14 23:13 ..
-rw-------. 1 root root 1419 Feb 14 23:14 anaconda-ks.cfg
-rw-------. 1 root root 312 Feb 15 00:01 .bash_history
-rw-r--r--. 1 root root 18 Dec 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 29 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 29 2013 .cshrc
drwx------. 2 root root 25 Feb 15 00:04 .ssh
-rw-r--r--. 1 root root 129 Dec 29 2013 .tcshrc
-rw-------. 1 root root 575 Feb 15 00:04 .viminfo
[root@ant165 ~]# cd .ssh
[root@ant165 .ssh]# ll
total 4
-rw-r--r--. 1 root root 184 Feb 15 00:04 known_hosts
配置四台虚拟机免密登录:
[root@ant165 .ssh]# ssh-keygen -t rsa -P ''
[root@ant165 .ssh]# ssh-copy-id ant165
#免密登录,两个命令都可以
[root@ant165 .ssh]# ssh ant165 | ssh -p22 root@ant165
在其他三台虚拟机上重复上面的命令
[root@ant165 .ssh]# ssh-keygen -t rsa -P ''
[root@ant165 .ssh]# ssh-copy-id ant165
[root@ant165 .ssh]# ssh-copy-id ant166
[root@ant165 .ssh]# ssh-copy-id ant167
[root@ant165 .ssh]# ssh-copy-id ant168
9.设置四台虚拟机时间同步
[root@ant165 ~]# yum -y install ntpdate
[root@ant165 ~]# ntpdate time.windows.com
14 Feb 16:33:52 ntpdate[11365]: step time server 40.81.94.65 offset -28800.832564 sec
[root@ant165 ~]# date
Tue Feb 14 16:33:54 CST 2023
[root@ant165 ~]# crontab -e
*/10 * * * * /usr/sbin/ntpdate time.windows.com
# 重新加载脚本
[root@ant165 ~]# systemctl reload crond
# 重启脚本
[root@ant165 ~]# systemctl restart crond
# 查看脚本状态
[root@ant165 ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-02-14 16:36:13 CST; 8s ago
Process: 11376 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
Main PID: 11386 (crond)
CGroup: /system.slice/crond.service
└─11386 /usr/sbin/crond -n
Feb 14 16:36:13 ant165 systemd[1]: Started Command Scheduler.
Feb 14 16:36:13 ant165 systemd[1]: Starting Command Scheduler...
Feb 14 16:36:13 ant165 crond[11386]: (CRON) INFO (RANDOM_DELAY will be s...)
Feb 14 16:36:13 ant165 crond[11386]: (CRON) INFO (running with inotify s...)
Feb 14 16:36:13 ant165 crond[11386]: (CRON) INFO (@reboot jobs will be r...)
Hint: Some lines were ellipsized, use -l to show in full.
二、自动安装JDK和zookeeper
1.在第一台虚拟机的/opt/目录下新建install、soft和shell三个文件
[root@ant165 opt]# mkdir install
[root@ant165 opt]# mkdir soft
[root@ant165 opt]# mkdir shell
2.上传jdk、zoookeeper压缩包到第一台虚拟机/opt/install/目录下
3.编写自动安装jdk和zookeeper脚本并赋予执行权限
[root@ant165 shell]# vim autoinstall.sh
#! /bin/bash
echo 'auto install begining...'
# global var
jdk=true
if [ "$jdk" = true ];then
echo 'jdk install set true'
echo 'setup jdk 8'
tar -zxf /opt/install/jdk-8u321-linux-x64.tar.gz -C /opt/soft
mv /opt/soft/jdk1.8.0_321 /opt/soft/jdk180
sed -i '73a\export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
sed -i '73a\export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar' /etc/profile
sed -i '73a\export JAVA_HOME=/opt/soft/jdk180' /etc/profile
sed -i '73a\# JAVA_HOME' /etc/profile
echo 'setup jdk8 success!!!'
fi
# global var
zk=true
hostname=`hostname`
if [ "$zk" = true ];then
echo 'zookeeper install set true'
echo 'setup zookeeper-3.4.5-cdh5.14.2.tar.gz'
tar -zxf /opt/install/zookeeper-3.4.5-cdh5.14.2.tar.gz -C /opt/soft/
mv /opt/soft/zookeeper-3.4.5-cdh5.14.2 /opt/soft/zk345
cp /opt/soft/zk345/conf/zoo_sample.cfg /opt/soft/zk345/conf/zoo.cfg
mkdir -p /opt/soft/zk345/datas
sed -i '12c dataDir=/opt/soft/zk345/datas' /opt/soft/zk345/conf/zoo.cfg
echo "server.0=$hostname:2287:3387" >> /opt/soft/zk345/conf/zoo.cfg
echo "0" > /opt/soft/zk345/datas/myid
sed -i '73a\export PATH=$PATH:$ZOOKEEPER_HOME/bin' /etc/profile
sed -i '73a\export ZOOKEEPER_HOME=/opt/soft/zk345' /etc/profile
sed -i '73a\#ZOOKEEPER_HOME' /etc/profile
echo 'setup zookeeper success!!!'
fi
赋予执行权限
[root@ant165 shell]# chmod 777 ./autoinstall.sh
4.执行/opt/shell/autoinstall.sh脚本
5.重启环境变量查看是否安装成功
[root@ant165 shell]# source /etc/profile
[root@ant165 shell]# javac
[root@ant165 shell]# zkServer.sh start
JMX enabled by default
Using config: /opt/soft/zk345/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@ant165 shell]# jps
11731 QuorumPeerMain
11753 Jps
[root@ant165 shell]# zkServer.sh status
JMX enabled by default
Using config: /opt/soft/zk345/bin/../conf/zoo.cfg
Mode: standalone
出现上面的信息就说明zookeeper单机版安装成功
6.来到/opt/soft/zk345/conf目录下,修改zoo.cfg文件,添加下面的内容
7.来到/opt/soft/zk345目录下,删除datas目录下所有的文件
[root@ant165 zk345]# rm -rf ./datas/*
8.将JDK和环境变量分发给三台虚拟机,将zookeeper分发给两台虚拟机
# 分发/opt下的soft目录:
[root@ant165 ~]# scp -r /opt/soft/ root@ant166:/opt/
[root@ant165 ~]# scp -r /opt/soft/ root@ant167:/opt/
[root@ant165 ~]# scp -r /opt/soft/ root@ant168:/opt/
# 分发JDK:
[root@ant165 datas]# scp -r /opt/soft/jdk180/ root@ant166:/opt/soft/
[root@ant165 datas]# scp -r /opt/soft/jdk180/ root@ant167:/opt/soft/
[root@ant165 datas]# scp -r /opt/soft/jdk180/ root@ant168:/opt/soft/
# 分发zookeeper:
[root@ant165 datas]# scp -r /opt/soft/zk345/ root@ant166:/opt/soft/
[root@ant165 datas]# scp -r /opt/soft/zk345/ root@ant167:/opt/soft/
# 分发环境变量:
[root@ant165 datas]# scp /etc/profile root@ant166:/etc/
profile 100% 2032 200.1KB/s 00:00
[root@ant165 datas]# scp /etc/profile root@ant167:/etc/
profile 100% 2032 1.3MB/s 00:00
[root@ant165 shell]# scp /etc/profile root@ant168:/etc/
profile 100% 2032 1.6MB/s 00:00
[root@ant165 shell]# ./showjps.sh
9.删除三台虚拟机的/opt/soft/zk345/datas目录下的所有文件,配置myid
[root@ant165 datas]# rm -rf /opt/soft/zk345/datas/*
[root@ant165 datas]# echo "0" > myid
[root@ant166 datas]# rm -rf /opt/soft/zk345/datas/*
[root@ant166 datas]# echo "1" > myid
[root@ant167 datas]# rm -rf /opt/soft/zk345/datas/*
[root@ant167 datas]# echo "2" > myid
10.编辑zookeeper集群开启脚本——zkop.sh
#! /bin/bash
case $1 in
"start"){
for i in ant165 ant166 ant167
do
ssh $i "source /etc/profile; /opt/soft/zk345/bin/zkServer.sh start"
done
};;
"stop"){
for i in ant165 ant166 ant167
do
ssh $i "source /etc/profile; /opt/soft/zk345/bin/zkServer.sh stop"
done
};;
"status"){
for i in ant165 ant166 ant167
do
ssh $i "source /etc/profile; /opt/soft/zk345/bin/zkServer.sh status"
done
};;
esac
赋予执行权限
[root@ant165 shell]# chmod 777 ./zkop.sh
11.编写查询集群启动状态信息批量展示脚本——showjps.sh
#! /bin/bash
for i in ant165 ant166 ant167 ant168
do
echo ---------------- $i 服务启动状态 -----------------
ssh $i "source /etc/profile; /opt/soft/jdk180/bin/jps "
done
赋予执行权限
[root@ant165 shell]# chmod 777 ./showjps.sh
12.执行zkop.sh和showjps.sh脚本,开启zookeeper集群
zookeeper集群成功开启!!!