14

docker学习笔记–制作kafka镜像 |坐而言不如起而行! 二丫讲梵

 3 years ago
source link: http://www.eryajf.net/2949.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
本文预计阅读时间 8 分钟

1,准备工作

1,下载kafka。

  1. wget https://mirrors.cnnic.cn/apache/kafka/0.11.0.2/kafka_2.11-0.11.0.2.tgz

然后上传到服务器。

2,基础镜像。

需要准备一个jdk镜像。

  1. docker pull eryajf/jdk:1.8

2,创建镜像

1,原料准备。

创建一个目录,有如下内容。

  1. [root@jenkins kafka]$ls
  2. Dockerfile kafka.tar run.sh

其中kafka.tar是在解压之后,重命名为kafka,然后再次打包而成,这样方便后续操作。

2,查看内容。

查看两个文件的内容。

  1. [root@jenkins kafka]$cat run.sh
  2. #!/bin/bash
  3. source /etc/profile
  4. sed -i '34s/^#//g' /opt/kafka/config/server.properties
  5. sed -i '63s/tmp/opt\/kafka/g' /opt/kafka/config/server.properties
  6. /opt/kafka/bin/zookeeper-server-start.sh -daemon /opt/kafka/config/zookeeper.properties
  7. sleep 3
  8. /opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties

服务通过run.sh进行初始化,里边简单配置了一下kafka,然后启动。

  1. [root@jenkins kafka]$cat Dockerfile
  2. FROM eryajf/jdk:1.8
  3. MAINTAINER eryajf <[email protected]>
  4. ENV LANG en_US.UTF-8
  5. ADD kafka.tar /opt
  6. ADD run.sh /
  7. EXPOSE 9092
  8. ENTRYPOINT [ "sh", "-c", "/run.sh" ]

3,构建镜像。

  1. [root@jenkins kafka]$docker build -t test .
  2. Sending build context to Docker daemon 236.7 MB
  3. Step 1/7 : FROM 192.168.112.69/public/jdk:1.8
  4. ---> 88d717766e50
  5. Step 2/7 : MAINTAINER eryajf <[email protected]>
  6. ---> Using cache
  7. ---> ce9a81c38f6c
  8. Step 3/7 : ENV LANG en_US.UTF-8
  9. ---> Using cache
  10. ---> d8ed2ae6a64f
  11. Step 4/7 : ADD kafka.tar /opt
  12. ---> 1e6337f7c14d
  13. Removing intermediate container b1127716d99e
  14. Step 5/7 : ADD run.sh /
  15. ---> 1ca96a5d1728
  16. Removing intermediate container 4c5750914f16
  17. Step 6/7 : EXPOSE 9092
  18. ---> Running in a62aca3a277d
  19. ---> ce25e74f2acb
  20. Removing intermediate container a62aca3a277d
  21. Step 7/7 : ENTRYPOINT sh -c /run.sh
  22. ---> Running in 77536f76e370
  23. ---> 9d368303d96e
  24. Removing intermediate container 77536f76e370
  25. Successfully built 9d368303d96e

3,启动验证

启动一下刚刚构建的镜像。

  1. [root@jenkins kafka]$docker run -itd -p 9092:9092 --name aa test
  2. c49f55f91edf1c6ecdf2c37d842f250a562a55df164a51571cfc71747f330e13

进入镜像创建消息验证。

  1. [root@jenkins kafka]$docker exec -it aa sh
  2. sh-4.2#cd opt/kafka/bin
  3. sh-4.2# ./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
  4. Created topic "test".
  5. sh-4.2# ./kafka-topics.sh --list --zookeeper localhost:2181
  6. test
  7. sh-4.2# ./kafka-console-producer.sh --broker-list localhost:9092 --topic test
  8. >hello world
  9. >hello eryajf
  10. 然后退出一下这个终端,查看一下刚刚的消息
  11. sh-4.2# ./kafka-console-consumer.sh --zookeeper localhost:2181 --topic test --from-beginning
  12. Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].
  13. hello world
  14. hello eryajf

可以看出,消息收发都是正常的。

71cfeb93ly1gkpus05c30j20sg14uqey.jpg

4,上传公用

1,阿里云。

  1. [root@jenkins kafka]$ docker tag test registry.cn-hangzhou.aliyuncs.com/eryajf/kafka:2.11
  2. [root@jenkins kafka]$ docker push registry.cn-hangzhou.aliyuncs.com/eryajf/kafka:2.11

2,官方。

  1. [root@jenkins kafka]$ docker tag test eryajf/kafka:2.11
  2. [root@jenkins kafka]$ docker push eryajf/kafka:2.11

有需要使用的,可以直接通过上边两个地址进行pull使用。

  1. docker pull registry.cn-hangzhou.aliyuncs.com/eryajf/kafka:2.11
  2. docker pull eryajf/kafka:2.11

使用注意,其中将kafka的消息目录定义在了/opt/kafka/kafka-logs目录,需要持久化的,可以针对这个目录进行持久化!


weinxin

二丫讲梵 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明<十>docker学习笔记–制作kafka镜像

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK