基于k8s的CI/CD的实现
综述
首先,本篇文章所介绍的内容,已经有完整的实现,可以参考这里。
在微服务、DevOps和云平台流行的当下,使用一个高效的持续集成工具也是一个非常重要的事情。虽然市面上目前已经存在了比较成熟的自动化构建工具,比如jekines,还有一些商业公司推出的自动化构建工具,但他们都不能够很好的和云环境相结合。那么究竟该如何实现一个简单、快速的基于云环境的自动化构建系统呢?我们首先以一个Springboot应用为例来介绍一下整体的发布流程,然后再来看看具体如何实现。不发的步骤大体如下:
1.首先从代码仓库下载代码,比如Gitlab、GitHub等;
2.接着是进行打包,比如使用Maven、Gradle等;
3.如果要使用k8s作为编排,还需要把步骤2产生的包制作成镜像,比如用Docker等;
4.上传步骤3的镜像到远程仓库,比如Harhor、DockerHub等;
5.最后,下载镜像并编写Deployment文件部署到k8s集群;
如图1所示:
图1
从以上步骤可以看出,发布过程中需要的工具和环境至少包括:代码仓库(Gitlab、GitHub等)、打包环境(Maven、Gradle等)、镜像制作(Docker等)、镜像仓库(Harbor、DockerHub等)、k8s集群等;此外,还包括发布系统自身的数据存储等。
可以看出,整个流程里依赖的环境很多,如果发布系统不能与这些环境解耦,那么要想实现一个安装简单、功能快速的系统没有那么容易。那么有没有合理的解决方案来实现与这些环境的解耦呢?答案是有的,下面就分别介绍。
代码仓库
操作代码仓库,一般系统提供的都有对应Restful API,以GitLab系统提供的Java客户端为例,如下代码:
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>4.17.0</version>
</dependency>
比如,我们想获取某个项目的分支列表,如下代码所示:
public List<Branch> branchList(CodeRepo codeRepo, BranchListParam param) {
GitLabApi gitLabApi = gitLabApi(codeRepo);
List<Branch> list = null;
try {
list = gitLabApi.getRepositoryApi().getBranches(param.getProjectIdOrPath(), param.getBranchName());
} catch (GitLabApiException e) {
LogUtils.throwException(logger, e, MessageCodeEnum.PROJECT_BRANCH_PAGE_FAILURE);
} finally {
gitLabApi.close();
}
}
private GitLabApi gitLabApi(CodeRepo codeRepo) {
GitLabApi gitLabApi = new GitLabApi(codeRepo.getUrl(), codeRepo.getAuthToken());
gitLabApi.setRequestTimeout(1000, 5 * 1000);
try {
gitLabApi.getVersion();
}catch(GitLabApiException e) {
//如果token无效,则用账号登录
if(e.getHttpStatus() == 401 && !StringUtils.isBlank(codeRepo.getAuthUser())) {
gitLabApi = new GitLabApi(codeRepo.getUrl(), codeRepo.getAuthUser(), codeRepo.getAuthPassword());
gitLabApi.setRequestTimeout(1000, 5 * 1000);
}
}
return gitLabApi;
}
打包环境
我们以Maven为例进行说明,一般情况下,我们使用Maven打包时,需要首先安装Maven环境,接着引入打包插件,然后使用mvn clean package命令就可以打包了。比如springboot自带插件:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.5.6</version>
<configuration>
<classifier>execute</classifier>
<mainClass>com.test.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
再比如,通用的打包插件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.8.2</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/resources/assemble.xml</descriptor>
</descriptors>
<outputDirectory>../target</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
等等。然后再通过运行mvn clean package
命令进行打包。那么,在打包时如果要去除对maven环境的依赖,该如何实现呢?
可以使用嵌入式maven插件maven-embedder来实现。
具体可以这样来做,首先在平台项目里引入依赖,如下:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-embedder</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-connector-basic</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.maven.resolver</groupId>
<artifactId>maven-resolver-transport-http</artifactId>
<version>1.7.1</version>
</dependency>
运行如下代码,就可以对项目进行打包了:
String[] commands = new String[] { "clean", "package", "-Dmaven.test.skip" };
String pomPath = "D:/hello/pom.xml";
MavenCli cli = new MavenCli();
try {
cli.doMain(commands, pomPath, System.out, System.out);
} catch (Exception e) {
e.printStackTrace();
}
但是,一般情况下,我们通过maven的settings文件还会做一些配置,比如配置工作目录、nexus私服地址、Jdk版本、编码方式等等,如下:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>C:/m2/repository</localRepository>
<profiles>
<profile>
<id>myNexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>nexus</name>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>nexus</name>
<url>https://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>java11</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>11</jdk>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>myNexus</activeProfile>
</activeProfiles>
</settings>
通过查看MavenCli类发现,doMain(CliRequest cliRequest)方法有比较丰富的参数,CliRequest的代码如下:
package org.apache.maven.cli;
public class CliRequest
{
String[] args;
CommandLine commandLine;
ClassWorld classWorld;
String workingDirectory;
File multiModuleProjectDirectory;
boolean debug;
boolean quiet;
boolean showErrors = true;
Properties userProperties = new Properties();
Properties systemProperties = new Properties();
MavenExecutionRequest request;
CliRequest( String[] args, ClassWorld classWorld )
{
this.args = args;
this.classWorld = classWorld;
this.request = new DefaultMavenExecutionRequest();
}
public String[] getArgs()
{
return args;
}
public CommandLine getCommandLine()
{
return commandLine;
}
public ClassWorld getClassWorld()
{
return classWorld;
}
public String getWorkingDirectory()
{
return workingDirectory;
}
public File getMultiModuleProjectDirectory()
{
return multiModuleProjectDirectory;
}
public boolean isDebug()
{
return debug;
}
public boolean isQuiet()
{
return quiet;
}
public boolean isShowErrors()
{
return showErrors;
}
public Properties getUserProperties()
{
return userProperties;
}
public Properties getSystemProperties()
{
return systemProperties;
}
public MavenExecutionRequest getRequest()
{
return request;
}
public void setUserProperties( Properties properties )
{
this.userProperties.putAll( properties );
}
}
可以看出,这些参数非常丰富,也许可以满足我们的需求,但是CliRequest只有一个默认修饰符的构造方法,也就说只有位于org.apache.maven.cli包下的类才有访问CliRequest构造方法的权限,我们可以在平台项目里新建一个包org.apache.maven.cli,然后再创建一个类(如:DefaultCliRequest)继承自CliRequest,然后实现一个public的构造方法,就可以在任何包里使用该类了,如下代码:
package org.apache.maven.cli;
import org.codehaus.plexus.classworlds.ClassWorld;
public class DefaultCliRequest extends CliRequest{
public DefaultCliRequest(String[] args, ClassWorld classWorld) {
super(args, classWorld);
}
public void setWorkingDirectory(String directory) {
this.workingDirectory = directory;
}
}
定义好参数类型DefaultCliRequest后,我们再来看看打包的代码:
public void doPackage() {
String[] commands = new String[] { "clean", "package", "-Dmaven.test.skip" };
DefaultCliRequest request = new DefaultCliRequest(commands, null);
request.setWorkingDirectory("D:/hello/pom.xml");
Repository repository = new Repository();
repository.setId("nexus");
repository.setName("nexus");
repository.setUrl("https://repo.maven.apache.org/maven2");
RepositoryPolicy policy = new RepositoryPolicy();
policy.setEnabled(true);
policy.setUpdatePolicy("always");
policy.setChecksumPolicy("fail");
repository.setReleases(policy);
repository.setSnapshots(policy);
String javaVesion = "11";
Profile profile = new Profile();
profile.setId("java11");
Activation activation = new Activation();
activation.setActiveByDefault(true);
activation.setJdk(javaVesion);
profile.setActivation(activation);
profile.setRepositories(Arrays.asList(repository));
profile.setPluginRepositories(Arrays.asList(repository));
Properties properties = new Properties();
properties.put("java.home", "D:/java/jdk-11.0.16.2");
properties.put("java.version", javaVesion);
properties.put("maven.compiler.source", javaVesion);
properties.put("maven.compiler.target", javaVesion);
properties.put("maven.compiler.compilerVersion", javaVesion);
properties.put("project.build.sourceEncoding", "UTF-8");
properties.put("project.reporting.outputEncoding", "UTF-8");
profile.setProperties(properties);
MavenExecutionRequest executionRequest = request.getRequest();
executionRequest.setProfiles(Arrays.asList(profile));
MavenCli cli = new MavenCli();
try {
cli.doMain(request);
} catch (Exception e) {
e.printStackTrace();
}
}
如果需要设置其他参数,也可以通过以上参数自行添加。
镜像制作
一般情况下,我们在Docker环境中通过Docker命令来制作镜像,过程如下:
1.首先编写Dockerfile文件;
2.通过docker build制作镜像;
3.通过docker push上传镜像;
可以看出,如果要使用docker制作镜像的话,必须要有docker环境,而且需要编写Dockerfile文件。当然,也可以不用安装docker环境,直接使用doker的远程接口:post/build。但是,在远程服务器中仍然需要安装doker环境和编写Dockerfile。在不依赖Docker环境的情况下,仍然可以制作镜像,下面就介绍一款工具Jib的用法。
Jib是谷歌开源的一套工具,github地址,它是一个无需Docker守护进程——也无需深入掌握Docker最佳实践的情况下,为Java应用程序构建Docker和OCI镜像, 它可以作为Maven和Gradle的插件,也可以作为Java库。
比如,使用jib-maven-plugin插件构建镜像的代码如下:
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<from>
<image>openjdk:13-jdk-alpine</image>
</from>
<to>
<image>gcr.io/dhorse/client</image>
<tags>
<tag>102</tag>
</tags>
<auth>
<!--连接镜像仓库的账号和密码 -->
<username>username</username>
<password>password</password>
</auth>
</to>
<container>
<ports>
<port>8080</port>
</ports>
</container>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
然后使用命令进行构建:
mvn compile jib:build
可以看出,无需docker环境就可以实现镜像的构建。但是,要想通过平台类型的系统去为每个系统构建镜像,显然通过插件的方式,不太合适,因为需要每个被构建系统引入jib-maven-plugin插件才行,也就是需要改造每一个系统,这样就会带来一定的麻烦。那么有没有不需要改造系统的方式直接进行构建镜像呢?答案是通过Jib-core就可以实现。
首先,在使用Jib-core的项目中引入依赖,maven如下:
<dependency>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-core</artifactId>
<version>0.22.0</version>
</dependency>
然后就可以直接使用Jib-core的API来进行制作镜像,如下代码:
try {
JibContainerBuilder jibContainerBuilder = null;
if (StringUtils.isBlank(context.getProject().getBaseImage())) {
jibContainerBuilder = Jib.fromScratch();
} else {
jibContainerBuilder = Jib.from(context.getProject().getBaseImage());
}
//连接镜像仓库5秒超时
System.setProperty("jib.httpTimeout", "5000");
System.setProperty("sendCredentialsOverHttp", "true");
String fileNameWithExtension = targetFiles.get(0).toFile().getName();
List<String> entrypoint = Arrays.asList("java", "-jar", fileNameWithExtension);
RegistryImage registryImage = RegistryImage.named(context.getFullNameOfImage()).addCredential(
context.getGlobalConfigAgg().getImageRepo().getAuthUser(),
context.getGlobalConfigAgg().getImageRepo().getAuthPassword());
jibContainerBuilder.addLayer(targetFiles, "/")
.setEntrypoint(entrypoint)
.addVolume(AbsoluteUnixPath.fromPath(Paths.get("/etc/localtime")))
.containerize(Containerizer.to(registryImage)
.setAllowInsecureRegistries(true)
.addEventHandler(LogEvent.class, logEvent -> logger.info(logEvent.getMessage())));
} catch (Exception e) {
logger.error("Failed to build image", e);
return false;
}
其中,targetFiles是要构建镜像的目标文件,比如springboot打包后的jar文件。
通过Jib-core,可以很轻松的实现镜像构建,而不需要依赖任何其他环境,也不需要被构建系统做任何改造,非常方便。
镜像仓库
类似代码仓库提供的Restful API,也可以通过Restful API来操作镜像仓库,以Harbor创建一个项目为例,代码如下:
public void createProject(ImageRepo imageRepo) {
String uri = "api/v2.0/projects";
if(!imageRepo.getUrl().endsWith("/")) {
uri = "/" + uri;
}
HttpPost httpPost = new HttpPost(imageRepo.getUrl() + uri);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(5000)
.setConnectTimeout(5000)
.setSocketTimeout(5000)
.build();
httpPost.setConfig(requestConfig);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
httpPost.setHeader("Authorization", "Basic "+ Base64.getUrlEncoder().encodeToString((imageRepo.getAuthUser() + ":" + imageRepo.getAuthPassword()).getBytes()));
ObjectNode objectNode = JsonUtils.getObjectMapper().createObjectNode();
objectNode.put("project_name", "dhorse");
//1:公有类型
objectNode.put("public", 1);
httpPost.setEntity(new StringEntity(objectNode.toString(),"UTF-8"));
try (CloseableHttpResponse response = createHttpClient(imageRepo.getUrl()).execute(httpPost)){
if (response.getStatusLine().getStatusCode() != 201
&& response.getStatusLine().getStatusCode() != 409) {
LogUtils.throwException(logger, response.getStatusLine().getReasonPhrase(),
MessageCodeEnum.IMAGE_REPO_PROJECT_FAILURE);
}
} catch (IOException e) {
LogUtils.throwException(logger, e, MessageCodeEnum.IMAGE_REPO_PROJECT_FAILURE);
}
}
k8s集群
同样,k8s也提供了Restful API。同时,官方也提供了各种语言的客户端,下面以Java语言的客户端为例,来创建一个deployment。
首先,引入Maven依赖:
<dependency>
<groupId>io.kubernetes</groupId>
<artifactId>client-java</artifactId>
<version>13.0.0</version>
</dependency>
然后,使用如下代码:
public boolean createDeployment(DeployContext context) {
V1Deployment deployment = new V1Deployment();
deployment.apiVersion("apps/v1");
deployment.setKind("Deployment");
deployment.setMetadata(deploymentMetaData(context.getDeploymentAppName()));
deployment.setSpec(deploymentSpec(context));
ApiClient apiClient = this.apiClient(context.getCluster().getClusterUrl(),
context.getCluster().getAuthToken(), 1000, 1000);
AppsV1Api api = new AppsV1Api(apiClient);
CoreV1Api coreApi = new CoreV1Api(apiClient);
String namespace = context.getProjectEnv().getNamespaceName();
String labelSelector = K8sUtils.getDeploymentLabelSelector(context.getDeploymentAppName());
try {
V1DeploymentList oldDeployment = api.listNamespacedDeployment(namespace, null, null, null, null,
labelSelector, null, null, null, null, null);
if (CollectionUtils.isEmpty(oldDeployment.getItems())) {
deployment = api.createNamespacedDeployment(namespace, deployment, null, null, null);
} else {
deployment = api.replaceNamespacedDeployment(context.getDeploymentAppName(), namespace, deployment, null, null,
null);
}
} catch (ApiException e) {
if (!StringUtils.isBlank(e.getMessage())) {
logger.error("Failed to create k8s deployment, message: {}", e.getMessage());
} else {
logger.error("Failed to create k8s deployment, message: {}", e.getResponseBody());
}
return false;
}
return true;
}
private ApiClient apiClient(String basePath, String accessToken, int connectTimeout, int readTimeout) {
ApiClient apiClient = new ClientBuilder().setBasePath(basePath).setVerifyingSsl(false)
.setAuthentication(new AccessTokenAuthentication(accessToken)).build();
apiClient.setConnectTimeout(connectTimeout);
apiClient.setReadTimeout(readTimeout);
return apiClient;
}
至此,关键的技术点已经介绍完了。
也可以参考其他文章:
《DHorse系列文章之操作手册》
基于k8s的CI/CD的实现的更多相关文章
- k8s Pipline CI/CD
一.Pipeline介绍 pipeline是一套jenkins官方提供的插件,它可以用来在jenkins中实现和集成连续交付 用户可以利用Pipeline的许多功能: 代码:pipeline在代码中实 ...
- jenkins-gitlab-harbor-ceph基于Kubernetes的CI/CD运用(三)
从最基础镜像到业务容器 构建 [为gitlab项目部署做铺垫] 业务镜像设计规划 目录结构 # pwd /data/k8s/app/myapp # tree . . ├── dockerfile │ ...
- k8s Gitlab CI/CD 之自动编译Docker镜像并推送到指定的Registry
环境介绍: 说明 节点 ip 系统 Gitlab Server git.ds.com 10.0.1.179 CentOS 7.5.1804 Gitlab Runner 10.0.1.178 Cen ...
- forge k8s/kubernetes ci/cd 最佳实践
forge的官网是: http://forge.sh forge工具是一个部署工具, 将你的应用自动更新到kubernets集群中, 只需要配置简洁的配置文件和模板即可. 其它的就交给forge, 它 ...
- jenkins-gitlab-harbor-ceph基于Kubernetes的CI/CD运用(四)
前景提要 jenkins与gitlab结合,实现代码自动拉取:https://www.cnblogs.com/zisefeizhu/p/12548662.html jenkins与kubernetes ...
- jenkins-gitlab-harbor-ceph基于Kubernetes的CI/CD运用(一)
注:这部分的学习还是要靠自己多点点 多尝试尝试 这部分19年3月份我是玩的很溜的,一年没用,基本忘光光了. 学习要温故而知新! 流程拓扑图 前提准备 部署应用服务 部署kubernetes 集群:ht ...
- jenkins-gitlab-harbor-ceph基于Kubernetes的CI/CD运用(二)
一张网图 因为我们使用了Docker in Docker技术,就是把jenkins部署在k8s里.jenkins master会动态创建slave pod,使用slave pod运行代码克隆,项目构建 ...
- k8s、CI/CD、pipline介绍
参照文档: https://blog.csdn.net/qq_35299863/article/details/84329798 https://github.com/xgh2016/k8s-CICD ...
- 实例演示:如何在Kubernetes上大规模运行CI/CD
本周四晚上8:30,第二期k3s在线培训如约开播!本期课程将介绍k3s的核心架构,如高可用架构以及containerd.一起来进阶探索k3s吧! 报名及观看链接:http://z-mz.cn/PmwZ ...
- CI Weekly #19 | 关于软件开发模型的思考,以及最新 CI/CD 实践分享
五月一来,夏天便悄然而至.flow.ci 也带来了几个新的变化,帮你进一步优化开发工作流.一起来看看这几个重点功能: 支持 iOS 项目 Xcode8.3 构建 iOSer 们重点来了,flow.ci ...
随机推荐
- ifort + mkl + impi (全套intel)编译安装量子化学软件GAMESS 2022 R1版本
说明:linux下编译软件都需要先配置好该软件依赖的系统环境.系统环境可以通过软件的安装说明了解,例如:readme.md等文件或网页.这个前提条件很重要!后面正式编译出错基本都可以归结到系统环境配置 ...
- 不懂 Kubernetes 实现云原生是什么体验?
云原生的本质和最终效果 要明白什么是云原生,就要先弄明白云计算是什么有什么问题,云计算将计算资源.网络.存储等基础设施统一管理,通过资源规模化和自动化管理,实现降低资源的成本和提高资源的管理效率,云计 ...
- Webpack与Vite热更新差异对比
随着项目的日渐迭代,项目整体的代码量也会越来越多,从而导致项目体积越来越大:在Webpack时代,很多人会对历史项目(巨型项目)感到头疼,因为往往巨型项目在本地开发调试的时候会因为本地代码的修改触发H ...
- 2.Python环境搭建
Python是一个跨平台.可移植的编程语言,因此可在windows.linux/unix和Mac os x系统中安装使用.安装完成后,你会得到Python解释器环境,一个命令行交互环境,一个简单的集成 ...
- Kibana探索数据(Discover)详解
设置时间过滤器 时间过滤器按照指定的时间段展示搜索结果.设置了 index contains time-based events 和 time-field 的索引模式可以使用时间过滤器. 时间过滤器默 ...
- SpringBoot 常用读取配置文件的 3 种方法!
我们在SpringBoot框架进行项目开发中该如何优雅的读取配置呢?或者说对于一些List或者Map应该如何配置呢? 本篇主要解决如下几个问题: 1.Spring Boot有哪些常用的读取配置文件方式 ...
- PAT (Basic Level) Practice 1030 完美数列 分数 25
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列. 现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列. 输入格 ...
- typora基础和计算机五大组成部分
typora typora软件 是一款适合于IT行业文本编辑器,笔记,当下来说,非常火爆,可以使用多种语言,python java... 安装的时候路径选择可以设置一些简单便于后续查找的文件路 ...
- [笔记] CSP 初赛 部分知识整理
几年前整理的东西,要不就发到网上吧 不过现在这些东西里面也有很多考得比以前少了 卡特兰数 \(f(i)=\sum_\limits{i=0}^{n-1}{f(i)f(n-i-1)}\) 其中\(f(0) ...
- C#-14 枚举器和迭代器
一 枚举器和可枚举类型 当我们为数组使用foreach语句时,这个语句为我们依次取出了数组中的每一个元素. var arrInt = new int[] { 11, 12, 13, 14 }; for ...