对于docker的实际工作中的使用总结

工作中会遇到 拿到一个项目的代码需要让它快速跑起来,那么首先需要部署环境,相对应的组件,比如说mysql,redis,kafka等等,这种情况可以用docker镜像容器

使用docker在linux环境部署运行 kafka zookeeper集群

1.安装docker

2.修改加速镜像

3.启动docker

4.安装dockercompose

5.编辑yml

6.在当前创建.env文件 内容为  KAFKA_HOSTNAME=106.12.112.136

6.执行 docker-compose pull&up -d

yum install -y yum-utils \
           device-mapper-persistent-data \
           lvm2 --skip-broken
# 设置docker镜像源
yum-config-manager \
    --add-repo \
    https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    
sed -i 's/download.docker.com/mirrors.aliyun.com\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo

yum makecache fast
yum install -y docker-ce
# 关闭
systemctl stop firewalld
# 禁止开机启动防火墙
systemctl disable firewalld
#查看是否关闭防火墙
systemctl status firewalld
systemctl start docker  # 启动docker服务
systemctl stop docker  # 停止docker服务
systemctl restart docker  # 重启docker服务
docker -v
##创建文件夹
sudo mkdir -p /etc/docker
##在文件夹内新建一个daemon.json文件
{
"registry-mirrors": [
 "https://5sr5uink.mirror.aliyuncs.com",
 "https://docker.mirrors.ustc.edu.cn",
 "https://hub-mirror.c.163.com",
 "https://mirror.baidubce.com"
  ],
 "insecure-registries": ["harbor.xxx.com"]
}
##重新加载文件
sudo systemctl daemon-reload
##重启docker
sudo systemctl restart docker
docker images
mkdir /usr/local/kafka


#创建docker-compose.yml
version: '3.7'

services:
  zookeeper:
    image: wurstmeister/zookeeper
    volumes:
       - ./data/zookeeper/data:/data
    ports:
      - "2181:2181"
    restart: always
  kafka1:
    image: wurstmeister/kafka
    depends_on:
      - zookeeper
    ports:
      - "9092:9092"
    volumes:
      - ./data/kafka/node_1/data:/kafka
    environment:
      KAFKA_BROKER_ID: 1
      KAFKA_LISTENERS: INSIDE://:19092,OUTSIDE://:9092
      KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka1:19092,OUTSIDE://${KAFKA_HOSTNAME}:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      JMX_PORT: 9999
    restart: always
  kafka2:
    image: wurstmeister/kafka
    depends_on:
      - zookeeper
    ports:
      - "9093:9093"
    volumes:
      - ./data/kafka/node_2/data:/kafka
    environment:
      KAFKA_BROKER_ID: 2
      KAFKA_LISTENERS: INSIDE://:19093,OUTSIDE://:9093
      KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka2:19093,OUTSIDE://${KAFKA_HOSTNAME}:9093
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      JMX_PORT: 9999
    restart: always
  kafka3:
    image: wurstmeister/kafka
    depends_on:
      - zookeeper
    ports:
      - "9094:9094"
    volumes:
      - ./data/kafka/node_3/data:/kafka
    environment:
      KAFKA_BROKER_ID: 3
      KAFKA_LISTENERS: INSIDE://:19094,OUTSIDE://:9094
      KAFKA_ADVERTISED_LISTENERS: INSIDE://kafka3:19094,OUTSIDE://${KAFKA_HOSTNAME}:9094
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: INSIDE:PLAINTEXT,OUTSIDE:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: INSIDE
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
      JMX_PORT: 9999
    restart: always

  kafka-manager:
    image: kafkamanager/kafka-manager
    ports:
      - 19092:9000
    environment:
      ZK_HOSTS: "zookeeper:2181"
    depends_on:
      - zookeeper
      - kafka1
      - kafka2
      - kafka3
    restart: always

 后面安装mysql的时候遇到了

问题1:

mysql8 Authentication plugin ‘caching_sha2_password

解决:

https://blog.csdn.net/weixin_43770790/article/details/123162023

 

 问题2:

docker部署kafka-manager时可能创建集群不成功  报Unimplemented for /kafka-manager/mutex

 

执行

docker-compose exec  zookeeper bash
 
执行:
create /kafka-manager/mutex/locks " "
create /kafka-manager/mutex/leases " "

 

 

1.如果本地跑起来,要先装docker  ,在https://www.docker.com/products/docker-desktop/官网上,下载desktop(在windows下看装docker主要是检测镜像是否正常运行)

 

 2.还需要WSL2(印象中就是上一步会有提示  点WSL2进入一个链接,下载运行即可),重启desktop

 3.在当前Module下创建Dockerfile

 

FROM adoptopenjdk:11-openj9    --这个是项目运行依赖的jdk版本 

WORKDIR /opt

ADD ./target/{xxx服务名}.jar ./app.jar --这里是项目打包的路径

ENV TZ Asia/Shanghai --这里固定写死
ENV JAVA_OPTS=""

EXPOSE 33111 --当前服务的端口号

ENTRYPOINT ["sh", "-c", "exec java $JAVA_OPTS -Duser.timezone=GMT+08 -Djava.security.egd=file:/dev/./urandom -jar app.jar"] --执行cmd 类似java -jar这种执行操作


4.

 

        4.1  进入到Dockerfile同级目录的控制台

 

 

docker build -t registry.cn-hangzhou.aliyuncs.com/yuexin-mining/dispatch-service .
docker run -it -p 23114:23114 --rm registry.cn-hangzhou.aliyuncs.com/yuexin-mining/dispatch-service
docker push registry.cn-hangzhou.aliyuncs.com/yuexin-mining/dispatch-service

 

所有的项目都要打包上传

 

4.2 创建并且登录harbor   docker login --username=acr@1990509508831777 registry.cn-hangzhou.aliyuncs.com   


4.3
docker-compose pull

 

 

完成,直接登录到服务器即可

 

 

posted @ 2022-08-05 11:19  蔡徐坤1987  阅读(218)  评论(0编辑  收藏  举报