Kafka之--自动启动zookeeper & kafka 脚本

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 keys 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) cant 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.152s 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) cant 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.153s 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]

 

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

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

Kafka之--自动启动zookeeper & kafka 脚本

上一篇:排列硬币


下一篇:数字逻辑基础_贰