1) 首先配置SSH免密登录,在这里我用kafka(151)这台机器来作为启动脚本的存放和执行机器

[root@kafaka3 .ssh]# pwd

#生成SSH KEY
/root/.ssh
[root@kafaka .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
a7:82:b2:ce:c2:e0:21:7d:4e:63:7c:03:d5:3c:98:25 root@kafaka
The key's randomart image is:
+--[ RSA 2048]----+
| E*. |
| +.+ |
| . . |
| . |
| . . . S . |
|o.. *.o o |
|= o=.o... |
|.+ o. . |
| o+ |
+-----------------+
[root@kafaka .ssh]# ls -l
total 8
-rw------- 1 root root 1675 Jul 13 20:12 id_rsa
-rw-r--r-- 1 root root 393 Jul 13 20:12 id_rsa.pub #复制到另外两台机器上
[root@kafaka .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.56.152
The authenticity of host '192.168.56.152 (192.168.56.152)' can't be established.
ECDSA key fingerprint is e6:c4:48:fa:0d:76:3e:2c:3b:60:e7:61:90:ad:9a:ee.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.152's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.56.152'"
and check to make sure that only the key(s) you wanted were added. [root@kafaka .ssh]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.56.153
The authenticity of host '192.168.56.153 (192.168.56.153)' can't be established.
ECDSA key fingerprint is e6:c4:48:fa:0d:76:3e:2c:3b:60:e7:61:90:ad:9a:ee.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.56.153's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.56.153'"
and check to make sure that only the key(s) you wanted were added. #修改kafka2 & kafka3目录和文件的权限。如果不修改,可能无法正常免密登录成功
#kafka2文件权限修改
[root@kafaka2 .ssh]# chmod 700 ~/.ssh
[root@kafaka2 .ssh]# chmod 600 ~/.ssh/authorized_keys
#kafka3文件权限修改
[root@kafaka3 .ssh]# chmod 700 ~/.ssh
[root@kafaka3 .ssh]# chmod 600 ~/.ssh/authorized_keys #免密登录测试
[root@kafaka .ssh]# ssh 192.168.56.152
Last login: Tue Jul 13 20:04:08 2021 from 192.168.56.1
[root@kafaka2 ~]# exit
logout
Connection to 192.168.56.152 closed.

2)启动脚本内容

2.1)启动zookeeper脚本

[root@kafaka ~]# cat zooman.sh
#! /bin/bash
case $1 in
"start"){
echo " --------Start 192.168.56.151 Zookeeper-------"
zkServer.sh start for i in 192.168.56.152 192.168.56.153
do
echo " --------Start $i Zookeeper-------"
ssh $i "source /etc/profile; zkServer.sh start"
done
};;
"stop"){
echo " --------Stop 192.168.56.151 Zookeeper-------"
zkServer.sh stop
for i in 192.168.56.152 192.168.56.153
do
echo " --------Stop $i Zookeeper-------"
ssh $i "source /etc/profile; zkServer.sh stop"
done
};;
"status"){
echo " --------192.168.56.151 Zookeeper Status-------"
zkServer.sh status
for i in 192.168.56.152 192.168.56.153
do
echo " --------$i Zookeeper status-------"
ssh $i "source /etc/profile; zkServer.sh status"
done
};;
esac

可以用zooman.sh start启动。启动完后,可以用zooman.sh status查看zookeeper状态

[root@kafaka ~]# ./zooman.sh status
--------192.168.56.151 Zookeeper Status-------
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower
--------192.168.56.152 Zookeeper status-------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
--------192.168.56.153 Zookeeper status-------
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: follower

4)kafka启动停止脚本

[root@kafaka ~]# cat kfman.sh
#! /bin/bash
case $1 in
"start"){
echo " --------Start 192.168.56.151 Kafka Broker $j-------"
# 用于KafkaManager监控
export JMX_PORT=9988 && /usr/local/kafka_2.13-2.7.0/bin/kafka-server-start.sh -daemon /usr/local/kafka_2.13-2.7.0/config/server.properties for i in 192.168.56.152 192.168.56.153
do
echo " --------Start $i Kafka Broker $j-------"
# 用于KafkaManager监控
ssh $i "export JMX_PORT=9988 && /usr/local/kafka_2.13-2.7.0/bin/kafka-server-start.sh -daemon /usr/local/kafka_2.13-2.7.0/config/server.properties "
done
};;
"stop"){
echo " --------Stop 192.168.56.151 Kafka-------"
/usr/local/kafka_2.13-2.7.0/bin/kafka-server-stop.sh stop for i in 192.168.56.152 192.168.56.153
do
echo " --------Stop $i Kafka-------"
ssh $i "/usr/local/kafka_2.13-2.7.0/bin/kafka-server-stop.sh stop"
done
};;
esac

5)启动完成后,可以用如下命令查看brokers

(注:因为用daemon启动,一台物理服务器只能启动一个实例,所以这里我只启动了3个broker。

至于能否多个我还没研究透,如果有知道的朋友也请回复告知我一声)

[zk: localhost:2181(CONNECTED) 0] ls /
[admin, brokers, cluster, config, consumers, controller, controller_epoch, feature, isr_change_notification, latest_producer_id_block, log_dir_event_notification, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /brokers
[ids, seqid, topics]
[zk: localhost:2181(CONNECTED) 2] ls /brokers/ids
[0, 2, 4]
[zk: localhost:2181(CONNECTED) 3]

最后说明,因为我为了节约一台机器,所以脚本放在了其中的一台机器。

如果放在另一台不同的机器上,脚本可以写得更简洁,不需要分开本地与远程机器分别处理。

最新文章

  1. .net 分布式架构之分布式缓存中间件
  2. Http压测工具wrk使用指南
  3. C#中的WebBrowser控件的使用
  4. 新版Microsoft Azure Web管理控制台 - Microsoft Azure New Portal - (2)
  5. windows磁盘分区
  6. JAVA工作方式
  7. shell之eval-command
  8. js闭包测试
  9. VMWare中安装CentOS6.6不能上网的解决办法
  10. 【MyBatis学习笔记】
  11. HTML5历史管理
  12. 解决SQLServer 2008 日志无法收缩,收缩后大小不改变
  13. ubuntu 使用sudo apt-get update 出现 被配置多次导致无法升级错误解决方法
  14. Vue的生命周期
  15. ITU-T Technical Paper: QoS 测量 (目标,方法,协议)
  16. C#零基础入门-2-Visual Studio (VS)程序初始化及各组成部分
  17. codeforces Hello 2019(未写完)
  18. 通过awk 和 sed 将多余的列剔除
  19. php回话控制 cookie
  20. windows系统-phpstudy升级mysql8.0.12安装教程及修改密码和安装注意事项

热门文章

  1. 构造无限级树的框架套路,附上python/golang/php/js实现
  2. python小知识,字典
  3. 在模仿中精进数据分析与可视化01——颗粒物浓度时空变化趋势(Mann–Kendall Test)
  4. 教你用python搭建一个「生活常识解答」机器人
  5. VisualCom软件仿真平台V1.0发布(附安装包下载链接)
  6. 一致性hash原理 看这一篇就够了
  7. Spring Boot 无侵入式 实现RESTful API接口统一JSON格式返回
  8. Redis的主从数据一致性
  9. springboot项目启动,停止,重启
  10. Robot Framework中SSHLibrary 学习与总结