Apache Kafka运维常用命令
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.管理Kafka服务的命令
1>.开启kafka服务
[root@node106.yinzhengjie.org.cn ~]# kafka-server-start.sh /home/softwares/kafka_2.11-0.10.2.1/config/server.properties >> /dev/null &
2>.停止kafka服务(刷新日志到本地且迁移当前broker的所有leader的parition)
[root@node106.yinzhengjie.org.cn ~]# kafka-server-stop.sh
温馨提示:
需要注意的是,我们在停止kafka集群时,如果时partition的副本数为2的话,不推荐大家同时停掉2台broker,而且在生产环境中执行该脚本时可能需要等一段时间broker进程才会彻底停止,此期间最好不要使用kill命令去强行杀死进程。
优雅关闭:
Kafka broker可能因为故障停机,但很多时候需要进行计划内的运维操作。这时候可使用更优雅的方式来关闭broker进程,而不是简单地kill掉。优雅关闭有两点好处:
1>.会将所有logs刷写到磁盘,以便在重启时避免log恢复步骤,log恢复是比较耗时地,因此这回让计划内的重启加快。
2>.会在关闭前,让该Broker是leader的所有partition把leader迁移到其他broker副本的服务器上去。这会使leader的转移加快,并使每个分区不可用的时间缩小到几毫秒。
优雅关闭的脚本存放在kafka的安装目录下的bin目录下,脚本名称为"kafka-server-stop.sh",可以看到仍是使用kill来进行的。需要注意,上述好处第一条不需要额外配置,第二条需要设置controlled.shutdown.enable=true,而官方默认的配置就是true,因此我们不需要特意去修改该属性。
3>.查看kafka进程ID
[root@node106.yinzhengjie.org.cn ~]# jps | grep Kafka 16434 Kafka [root@node106.yinzhengjie.org.cn ~]#
二.kafka-topics.sh命令使用案例
1>.创建主题(kafka-topics.sh)
[root@node106.yinzhengjie.org.cn ~]# kafka-topics.sh --zookeeper node107.yinzhengjie.org.cn:2181,node108.yinzhengjie.org.cn:2181 --create --partitions 10 --replication-factor 2 --topic yinzhengjie-kafka Created topic "yinzhengjie-kafka". [root@node106.yinzhengjie.org.cn ~]#
2>.查看主题列表
[root@node106.yinzhengjie.org.cn ~]# kafka-topics.sh --zookeeper node107.yinzhengjie.org.cn:2181,node108.yinzhengjie.org.cn:2181 --list yinzhengjie-kafka [root@node106.yinzhengjie.org.cn ~]#
[root@node106.yinzhengjie.org.cn ~]# ansible kafka -m shell -a 'ls -l /home/data/kafka/logs' node107.yinzhengjie.org.cn | SUCCESS | rc=0 >> total 4 -rw-r--r-- 1 root root 0 Jul 11 15:45 cleaner-offset-checkpoint -rw-r--r-- 1 root root 56 Jul 11 15:45 meta.properties -rw-r--r-- 1 root root 0 Jul 11 15:45 recovery-point-offset-checkpoint -rw-r--r-- 1 root root 0 Jul 11 15:45 replication-offset-checkpoint node106.yinzhengjie.org.cn | SUCCESS | rc=0 >> total 12 -rw-r--r-- 1 root root 0 Jul 11 15:45 cleaner-offset-checkpoint -rw-r--r-- 1 root root 56 Jul 11 15:45 meta.properties -rw-r--r-- 1 root root 70 Jul 11 17:43 recovery-point-offset-checkpoint -rw-r--r-- 1 root root 70 Jul 11 17:44 replication-offset-checkpoint drwxr-xr-x 2 root root 110 Jul 11 17:43 yinzhengjie-kafka-0 drwxr-xr-x 2 root root 110 Jul 11 17:43 yinzhengjie-kafka-1 drwxr-xr-x 2 root root 110 Jul 11 17:43 yinzhengjie-kafka-3 node108.yinzhengjie.org.cn | SUCCESS | rc=0 >> total 12 -rw-r--r-- 1 root root 0 Jul 11 15:18 cleaner-offset-checkpoint -rw-r--r-- 1 root root 56 Jul 11 15:18 meta.properties -rw-r--r-- 1 root root 48 Jul 11 17:43 recovery-point-offset-checkpoint -rw-r--r-- 1 root root 48 Jul 11 17:44 replication-offset-checkpoint drwxr-xr-x 2 root root 110 Jul 11 17:43 yinzhengjie-kafka-0 drwxr-xr-x 2 root root 110 Jul 11 17:43 yinzhengjie-kafka-8 [root@node106.yinzhengjie.org.cn ~]#