Microservice automation [idea plug-in one-click deployment]

directory

1. Docker combined with Idea plug-in 

2. Private server build registry

3. Use of private warehouse

4. Idea one-click project deploys a separate Docker image

5. Idea one-click project deployment Docker tag image [private library]


1. Docker combined with Idea plug-in 

1. Modify the Docker service file, comment out the "ExecStart" line, and add the following information 

vim /lib/systemd/system/docker.service
  • code show as below
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375

2. Reload configuration file

systemctl daemon-reload

3. Restart the service

systemctl restart docker

4. Firewall operation

firewall-cmd --zone=public --add-port=2375/tcp --permanent && firewall-cmd --reload && firewall-cmd --list-ports

5. Placement IDEA

 

6. View docker

 

7. Build the Springboot project, generate the Dockerfile, and complete the image generation

#1.指定基础镜像
FROM openjdk:8-jdk-alpine
#2.维护者信息
MAINTAINER xyz "[email protected]"
#3.创建/tmp目录并持久化到Docker数据文件夹,因为Spring Boot使用的内嵌Tomcat容器默认使用/tmp作为工作目录
VOLUME /tmp
#4.复制test1.jar到容器里(此处与之后的idea中使用docker插件一键发布是不一样的)
ADD test.jar /test.jar
#5.设置时区
ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
#6.声明运行时容器提供服务端口,这只是一个声明,在运行时并不会因为这个声明应用就会开启这个端口的服务
EXPOSE 8080
#7.指定容器启动程序及参数(相当于在容器中用cmd命令执行jar包)
ENTRYPOINT ["java","-jar","/test.jar"]
#下面的写法指定springboot项目启动时指定的额外参数
#ENTRYPOINT ["java","-jar","/test.jar","--spring.config.location=/usr/local/project/docker/xxl-job/config/application.yml"]

8. Package the image

docker build -t test:1.0 .

9. Start a container based on the packaged image

docker run -itd --name test01 -p 8848:8080 test:1.0

2. Private server build registry

1. Pull the Registry image (here choose to build the 2.7 version of the registry, do not use the latest version, there are BUG)

docker pull registry:2.7

2. Open the container

docker run -d \
--name myregistry \
-p 5000:5000 \
-v /usr/local/docker/registry:/var/lib/registry \
--restart=always \
registry:2.7

3. Verify whether the build is successful

curl http://127.0.0.1:5000/v2/_catalog


curl http://192.168.27.120:5000/v2/_catalog

4. Let docker trust the address of the private mirror warehouse

vi /etc/docker/daemon.json
  •  Add the following content in daemon.json, whose value is the IP and port of the registration server (registry)
"insecure-registries":["192.168.27.120:5000"]

5. Restart docker

systemctl restart docker

3. Use of private warehouse

After creating a private warehouse, you can use the docker tag to mark a mirror and push it to the warehouse

  • First check the existing mirrors on this machine, select one of them to upload
  • Mirror mark (Mirror must be marked before push, custom repository cannot have uppercase letters) 
docker tag 自定义镜像名[:版本] 私服IP:端口/仓库名[:版本]

docker tag test:1.0 192.168.27.120:5000/test:v1.0

1. Execute to view all images

docker images

2. Mirror upload

docker push 192.168.27.120:5000/test:v1.0

3. View all mirrors of the registration server

curl 127.0.0.1:5000/v2/_catalog

4. Download the image from the registry

docker pull 192.168.27.120:5000/test:v1.0

4. Idea one-click project deploys a separate Docker image

1. Add pom plugin

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--docker-maven-plugin插件打包-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>docker-maven-plugin</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <!--镜像名称-->
                    <imageName>${project.artifactId}</imageName>
                    <!--指定标签-->
                    <imageTags>
                        <imageTag>latest</imageTag>
                    </imageTags>
                    <!--基础镜像jdk1.8-->
                    <baseImage>java</baseImage>
                    <!--制作者提供本人信息-->
                    <maintainer>[email protected]</maintainer>
                    <!--切换到Root目录-->
                    <workdir>/ROOT</workdir>
                    <cmd>["java", "-version"]</cmd>
                    <entryPoint>["java", "-jar", "${project.build.finalName}.jar"]</entryPoint>

                    <!--指定DockerFile路径-->
                    <!--                    <dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>-->

                    <!--指定远程docker api地址-->
                    <dockerHost>http://192.168.119.129:2375</dockerHost>

                    <!-- 这里是复制 jar 包到 docker 容器指定目录配置 -->
                    <resources>
                        <resource>
                            <targetPath>/ROOT</targetPath>
                            <!--用于指定需要复制的根目录-->
                            <directory>${project.build.directory}</directory>
                            <!--用于指定需要复制的jar文件-->
                            <include>${project.build.finalName}.jar</include>
                        </resource>
                    </resources>
                </configuration>
            </plugin>
        </plugins>
    </build>

2. Project clear and install

3. Enter the root directory of our project to perform cmd operations

mvn docker:build

4. You can see that the deployment has been completed

5. Idea one-click project deployment Docker tag image [private library]

1. Configure the system environment (variables should be the same as the idea to connect to the dokcer ip)

2. It needs to be configured in setting.xml under maven/conf/

<pluginGroups>
  <pluginGroup>com.spotify</pluginGroup>
</pluginGroups>

3. Add Dockerfile at the same level as the pom.xml configuration file

FROM openjdk:8-jdk-alpine
MAINTAINER xyz "[email protected]"
VOLUME /tmp

#/target/...这是你自己将项目打包后的jar包名称  /test.jar
ADD /target/test-0.0.1-SNAPSHOT-exec.jar /test.jar
ENV TZ=PRC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
EXPOSE 8080
ENTRYPOINT ["java","-jar","/test.jar"]

4. Add pom plugin

      <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <classifier>exec</classifier>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <!--docker插件使用-->
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.4.10</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <!--<goals>
                            &lt;!&ndash;如果package时不想用docker打包,就注释掉这个goal&ndash;&gt;
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>-->
                    </execution>
                </executions>
                <configuration>
                    <repository>192.168.119.128:5000/test1000</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>

5. Package the project

6. Add maven plugin (​​​​​​​​​​clean package dockerfile:build -Dmaven.test.skip=true)

clean package dockerfile:build -Dmaven.test.skip=tru

7. After the maven plug-in is added, it is necessary to comment out the pom dependency first (untie the commented out plug-in in the figure)

8. Click the maven plugin to run the packaged docker image

  •  Check out the mirror! ok has been packaged

9. List of questions! ! ! focus

  1. Check whether the system variables have been configured (it is recommended to restart the computer after configuring the environment)
  2. View the setting.xml configuration under maven/conf/
<pluginGroups>
  <pluginGroup>com.spotify</pluginGroup>
</pluginGroups>

Guess you like

Origin blog.csdn.net/m0_63300795/article/details/128196021