微服务架构师封神之路05-将kubernetes部署信息放入.yaml文件,mvn实现自动部署
让本地minikube和maven使用同一个docker daemon
使用同一个docker daemon,就不用再docker push来pull去的。
先启动minikube,再运行docker-env,会有一个提示
minikube docker-env
......
# To point your shell to minikube's docker-daemon, run:
# eval $(minikube -p minikube docker-env)
也就是在本地minikube运行的情况下,运行指令eval $(minikube -p minikube docker-env)。之后,你的命令行环境中就可以使用minikube自带的docker实例了,本地使用maven的制作的docker镜像,对于minikube来说也是可见的,因为它们使用的是同一个docker daemon。也就不需要先push到docker registry,minikube docker daemon再pull下来了。
这样的流程虽然和实际的产品部署流程不同,但对于开发来说是十分方便的。
我们继续使用我创建的helloworld项目做为例子,
- 微服务架构师封神之路01-利用minikube部署一个最简单的应用
- 源码:https://github.com/b5wang/cloudlab
使用一个yaml文件装载所有部署信息
在文章 微服务架构师封神之路01-利用minikube部署一个最简单的应用 中,maven制作好了docker image后,我们使用kubenetes指令把image部署到了minikube上。执行了两个操作,
- 部署helloworld应用。kubectl create deployment
- 为应用创建了一个service,做为loadbalancer。kubectl expose deployment
下面我们把这两个操作所需要的信息写入.yaml文件。
在目录helloworld/src/deployments下创建一个新文件,helloworld-app.yaml。
apiVersion: v1
kind: Service
metadata:
name: helloworld
labels:
app: helloworld
spec:
selector:
app: helloworld
type: NodePort
ports:
- protocol: TCP
port: 9090
targetPort: 9090
nodePort: 30090
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloworld
labels:
app: helloworld
spec:
selector:
matchLabels:
app: helloworld
replicas: 1
template:
metadata:
labels:
app: helloworld
spec:
containers:
- name: helloworld
image: b5wang/helloworld:1.1
ports:
- containerPort: 9090
env:
- name: JAVA_OPTS
value: -server -Xms32m -Xmx32m
注意以下重要信息:
- service名字,helloworld
- service为应用helloworld服务,并且针对它的目标端口9090
- service使用minikube的固定端口30090
- 应用名叫helloworld
- 应用使用端口9090
- 应用使用image,b5wang/helloworld:1.1-SNAPSHOT
部署应用和创建service所需要的信息都在这里了。
kubectl命令支持使用yaml文件,完整的命令是,
kubectl apply -f <config_yaml_file>
在pom中执行kubectl apply
在exec-maven-plugin中增加这条指令。整个pom变成,
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
<groupId>com.b5wang.cloudlab</groupId>
<artifactId>helloworld</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloworld</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<docker.registry>b5wang</docker.registry>
<!-- dependencies' version -->
<spring-boot-dependencies.version>2.3.0.RELEASE</spring-boot-dependencies.version>
<log4j-1.2-api.version>2.13.3</log4j-1.2-api.version>
</properties>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- =========================================================================
== Copy resources into target folder ==
========================================================================= -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<!-- resources to build docker image -->
<execution>
<id>generate-docker-build</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/docker-build</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/docker</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<!-- =========================================================================
== Execute commands ==
========================================================================= -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<!-- Build docker image -->
<execution>
<id>exec-docker-build</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>docker</executable>
<commandlineArgs>build --no-cache --tag ${docker.registry}/${project.artifactId}:${project.version} -f ${project.build.directory}/docker-build/Dockerfile ${project.build.directory}</commandlineArgs>
</configuration>
</execution>
<!-- Deploy app to kubernetes with yaml -->
<execution>
<id>exec-kubectl-deploy</id>
<phase>package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>kubectl</executable>
<commandlineArgs>apply -f ${project.basedir}/src/deployments/helloworld-app.yaml</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
<!-- fix error java.lang.ClassNotFoundException: org.apache.maven.doxia.siterenderer.DocumentContent -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j-1.2-api.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
</project>
完成修改后,愉快的运行mvn clean install,来测试是否成功。
课后思考:这样部署会有什么问题?
源码:https://github.com/b5wang/cloudlab