jx是云原生CICD,devops的一个最佳实践之一,目前在快速的发展成熟中。最近调研了JX,这里为第2篇,使用已经安装好的jx来实践CICD,旨在让大家了解基于jx的DevOps是如何运转的,感兴趣的可以继续关注,下一篇介绍如何安装。

先上图:

一、windows 搭建开发环境(可选)

1. 安装kubectl

使用Chocolatey来安装,因此install Chocolatey:

安装Chocolatey

windows + X ,选择power shell 管理员模式,输入:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

或者cmd.exe(管理员):

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

使用Chocolatey安装kubectl

  1. 使用脚本安装

    choco install kubernetes-cli
    
    
  2. 测试是否安装成功:

    kubectl version
    
    
  3. 创建配置文件,先转到%HOME% 目录:

    For example: cd C:\users\yourusername

  4. 创建配置文件目录:

    mkdir .kube
    
    
  5. 转到配置目录:

    cd .kube
  6. 配置kubectl使用远程集群:

    New-Item config -type file

修改config文件,或者直接从集群拷贝配置文件过来

  1. 测试
C:\Users\jqpeng>kubectl get pods
NAME READY STATUS RESTARTS AGE
jenkins-bd94b5fb8-5t9kq 1/1 Running 0 10d
jenkins-x-chartmuseum-75d45b6d7f-2hk99 1/1 Running 0 10d
jenkins-x-controllercommitstatus-675dbb9c86-kth6q 1/1 Running 71 10d
jenkins-x-controllerrole-5458874c-4lnwh 1/1 Running 0 10d
jenkins-x-controllerteam-7f965c8b9c-n4kfm 1/1 Running 0 10d
jenkins-x-controllerworkflow-7675c458d-sjbfd 1/1 Running 0 10d

2.安装helm

到下载页:

https://github.com/helm/helm/releases

下载最新的helm,选择windows-amd64

wget https://storage.googleapis.com/kubernetes-helm/helm-v2.11.0-windows-amd64.zip

解压后,拷贝到C:\Windows\System32。

注意,需要Cross GFW,可以使用( https://azure.microsoft.com/zh-cn/free/ ) 免费一年。

初始化helm

服务端已经安装过tiller了,因此只需要client-only,另外stable repo指定本地的

helm init --client-only --stable-repo-url=http://charts.youdomain.com/

---
Creating C:\Users\jqpeng\.helm
Creating C:\Users\jqpeng\.helm\repository
Creating C:\Users\jqpeng\.helm\repository\cache
Creating C:\Users\jqpeng\.helm\repository\local
Creating C:\Users\jqpeng\.helm\plugins
Creating C:\Users\jqpeng\.helm\starters
Creating C:\Users\jqpeng\.helm\cache\archive
Creating C:\Users\jqpeng\.helm\repository\repositories.yaml
Adding stable repo with URL: http://charts.youdomain.com/
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at C:\Users\jqpeng\.helm.
Not installing Tiller due to 'client-only' flag having been set
Happy Helming!

3. 安装jx

到https://github.com/jenkins-x/jx/releases 下载最新的编译好的jx执行文件,选择jx-windows-amd64.zip下载,下载后解压,然后重新命名为jx.exe,拷贝到C:\Windows\System32。

测试:

C:\Users\jqpeng>jx version
Updated the team settings in namespace incubation
Updated the team settings in namespace incubation
NAME VERSION
jx [32m1.3.572[0m
jenkins x platform [32m0.0.2871[0m
Kubernetes cluster [32mv1.12.2[0m
kubectl [32mv1.12.1[0m
helm client [32mv2.11.0+g2e55dbe[0m
helm server [32mv2.11.0+g2e55dbe[0m
git [32mgit version 2.19.1.windows.1[0m

将服务器上的.jx目录下相关配置信息下载到本地用户目录下的.jx文件夹。

二、快速开始

1.使用quickstart

作为演示用,可以直接使用jx create quickstart,会直接使用官方的quickstart项目,键入命令,然后按提示选择即可。

D:\Project>jx create quickstart
? select the quickstart you wish to create spring-boot-rest-prometheus
? Project name spring-boot-rest-prometheus
Generated quickstart at D:\Project\spring-boot-rest-prometheus
### NO charts folder D:\Project\spring-boot-rest-prometheus\charts\spring-boot-rest-prometheus
Created project at D:\Project\spring-boot-rest-prometheus Updated the team settings in namespace incubation
? Which Git service do you wish to use https://github.com
No username defined for the current Git server!
? Do you wish to use jadepeng as the Git user name: Yes
The directory D:\Project\spring-boot-rest-prometheus is not yet using git
? Would you like to initialise git now? Yes
? Commit message: Initial import Git repository created
Updated the team settings in namespace incubation
selected pack: C:\Users\jqpeng\.jx\draft\packs\github.com\jenkins-x\draft-packs\packs\maven
? Which organisation do you want to use? jadepeng
replacing placeholders in directory D:\Project\spring-boot-rest-prometheus
app name: spring-boot-rest-prometheus, git server: github.com, org: jadepeng, Docker registry org: jadepeng
skipping directory "D:\\Project\\spring-boot-rest-prometheus\\.git"
Using Git provider [32mgithub.com at https://github.com[0m About to create repository [32mspring-boot-rest-prometheus[0m on server [32mhttps://github.com[0m with user [32mjadepeng[0m
? Enter the new repository name: spring-boot-rest-prometheus Creating repository [32mjadepeng/spring-boot-rest-prometheus[0m
Pushed Git repository to https://github.com/jadepeng/spring-boot-rest-prometheus Updated the team settings in namespace incubation
? Do you wish to use jadepeng as the user name for the Jenkins Pipeline Yes
Created Jenkins Project: http://jenkins.incubation.youdomain.com/job/jadepeng/job/spring-boot-rest-prometheus/ Watch pipeline activity via: jx get activity -f spring-boot-rest-prometheus -w
Browse the pipeline log via: jx get build logs jadepeng/spring-boot-rest-prometheus/master
Open the Jenkins console via jx console
You can list the pipelines via: jx get pipelines
When the pipeline is complete: jx get applications For more help on available commands see: https://jenkins-x.io/developing/browsing/ Note that your first pipeline may take a few minutes to start while the necessary images get downloaded! Creating GitHub webhook for jadepeng/spring-boot-rest-prometheus for url http://jenkins.incubation.youdomain.com/github-webhook/

创建过程:

  • 自动创建了spring-boot-rest-prometheus项目,并提交到github(可以使用自己的git服务器),并自动创建GitHub webhook ,这样当新代码提交到github后,会自动触发构建。
  • 将项目提交到jenkins,可以打开http://jenkins.incubation.youdomain.com/job/jadepeng/job/spring-boot-rest-prometheus/ 查看
  • jenkins会自动进行构建,可以通过jx get activity -f spring-boot-rest-prometheus -w查看构建活动
  • 通过jx get build logs jadepeng/spring-boot-rest-prometheus/master查看构建日志,
  • 通过jx console打开jenkins bule

2.查看构建日志

我们查看构建日志:

jx get build logs jadepeng/spring-boot-rest-prometheus/master

....

+ jx step helm release
No $CHART_REPOSITORY defined so using the default value of: http://jenkins-x-chartmuseum:8080
Using helmBinary helm with feature flag: none
Adding missing Helm repo: jenkins-x http://chartmuseum.jenkins-x.io
Successfully added Helm repository jenkins-x.
Adding missing Helm repo: releases http://jenkins-x-chartmuseum:8080
Successfully added Helm repository releases.
No $CHART_REPOSITORY defined so using the default value of: http://jenkins-x-chartmuseum:8080
Uploading chart file spring-boot-rest-prometheus-0.0.1.tgz to http://jenkins-x-chartmuseum:8080/api/charts
Received 201 response: {"saved":true}
[Pipeline] sh
[spring-boot-rest-prometheus] Running shell script
++ cat ../../VERSION
+ jx promote -b --all-auto --timeout 1h --version 0.0.1
Using helmBinary helm with feature flag: none
Promoting app spring-boot-rest-prometheus version 0.0.1 to namespace incubation-staging
Created Pull Request: http://github.youdomain.com/jqpeng/environment-walkertabby-staging/pulls/34

3.部署到staging环境

可以看到已经构建成功,并停留在最后一步jx promote -b --all-auto --timeout 1h --version 0.0.1

jx promote 是jx的发布命令,将构建产物部署到k8s环境。回顾下文章开始的一个图,提交到master后的代码,自动构建后会部署到staging,由于采用的gitops,会往staging环境的git仓库 environment-walkertabby-staging推送一个pullrequest:

environment-walkertabby-staging其实就是一个charts项目:

每提交一个pullrequest,其实就是在requirements.yaml,将需要部署的项目作为-staging环境的依赖,比如刚提交的pullrequest就是增加了spring-boot-rest-prometheus依赖。

@@ -13,6 +13,9 @@ dependencies:
+
- name: pailitaoservice
+
repository: http://jenkins-x-chartmuseum:8080
+
version: 0.0.3
+
+- name: spring-boot-rest-prometheus
+
+ repository: http://jenkins-x-chartmuseum:8080
+
+ version: 0.0.1
+
- name: springboot-rest-demo
+
repository: http://jenkins-x-chartmuseum:8080
+
version: 0.0.4

我们来同意下合并pullrequest,这样当staging项目构建后就会自动部署spring-boot-rest-prometheus。合并完成后,再回到日志查看:

...
+ jx promote -b --all-auto --timeout 1h --version 0.0.1
Using helmBinary helm with feature flag: none
Promoting app spring-boot-rest-prometheus version 0.0.1 to namespace incubation-staging
Created Pull Request: http://github.youdomain.com/jqpeng/environment-walkertabby-staging/pulls/34 Pull Request http://github.youdomain.com/jqpeng/environment-walkertabby-staging/pulls/34 is merged at sha 906a33b6eec14a49d248d8220e0d88416798ba6e
merge status: pending for URL http://github.youdomain.com/api/v1/jqpeng/environment-walkertabby-staging/statuses/906a33b6eec14a49d248d8220e0d88416798ba6e with target: http://jenkins.incubation.youdomain.com/job/jqpeng/job/environment-walkertabby-staging/job/master/display/redirect description: Build queued...
merge status: success for URL http://github.youdomain.com/api/v1/jqpeng/environment-walkertabby-staging/statuses/906a33b6eec14a49d248d8220e0d88416798ba6e with target: http://jenkins.incubation.youdomain.com/job/jqpeng/job/environment-walkertabby-staging/job/master/3/display/redirect description: This commit looks good
Merge status checks all passed so the promotion worked!
[Pipeline] }
[Pipeline] // container
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] cleanWs
[WS-CLEANUP] Deleting project workspace...[WS-CLEANUP] done
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // withCredentials
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline

jx已经检测到Pull Request已经通过,并且启动staging构建,直到构建结束。

这个时候,我们就可以通过jx get applications来查看部署好的应用。

D:\Project>jx get applications
APPLICATION EDIT PODS URL STAGING PODS URL PRODUCTION PODS URL
spring-boot-rest-prometheus 0.0.1 http://spring-boot-rest-prometheus.incubation-staging.youdomain.com

PODS 为0,应该是容器启动有问题,我们可以看下:

kubectl -n=incubation-staging describe pod incubation-staging-spring-boot-rest-prometheus-66966b8cbb-8fvmf
--- Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 6m51s default-scheduler Successfully assigned incubation-staging/incubation-staging-spring-boot-rest-prometheus-66966b8cbb-8fvmf to docker86-9
Normal Pulling 6m44s kubelet, docker86-9 pulling image "registry.youdomain.com/jadepeng/spring-boot-rest-prometheus:0.0.1"
Normal Pulled 6m38s kubelet, docker86-9 Successfully pulled image "registry.youdomain.com/jadepeng/spring-boot-rest-prometheus:0.0.1"
Normal Created 5m27s (x3 over 6m37s) kubelet, docker86-9 Created container
Normal Started 5m27s (x3 over 6m37s) kubelet, docker86-9 Started container
Warning Unhealthy 5m1s (x9 over 6m31s) kubelet, docker86-9 Readiness probe failed: Get http://170.22.78.7:8080/actuator/health: dial tcp 170.22.78.7:8080: connect: connection refused
Normal Pulled 4m26s (x3 over 6m10s) kubelet, docker86-9 Container image "registry.youdomain.com/jadepeng/spring-boot-rest-prometheus:0.0.1" already present on machine
Warning BackOff 97s (x15 over 5m41s) kubelet, docker86-9 Back-off restarting failed container

问题在于Readiness probe failed, 来查看下容器日志:

kubectl -n=incubation-staging logs incubation-staging-spring-boot-rest-prometheus-66966b8cbb-8fvmf

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.4.RELEASE) 2018-11-20 03:39:45.149 INFO 1 --- [ main] c.g.j.q.s.r.p.RestPrometheusApplication : Starting RestPrometheusApplication v0.0.1 on incubation-staging-spring-boot-rest-prometheus-66966b8cbb-8fvmf with PID 1 (/opt/app.jar started by root in /opt)
2018-11-20 03:39:45.442 INFO 1 --- [ main] c.g.j.q.s.r.p.RestPrometheusApplication : No active profile set, falling back to default profiles: default
2018-11-20 03:39:47.942 INFO 1 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@424c0bc4: startup date [Tue Nov 20 03:39:47 UTC 2018]; root of context hierarchy

原来java程序还没有启动成功,有可能是默认的资源限制问题,我们去掉相关限制,修改deployment.yaml,删除resources限制:

git push到仓库,等待自动构建完成,再次查看:

D:\Project>jx get applications
APPLICATION EDIT PODS URL STAGING PODS URL PRODUCTION PODS URL
spring-boot-rest-prometheus 0.0.2 1/1 http://spring-boot-rest-prometheus.incubation-staging.youdomain.com D:\Project>curl -l http://spring-boot-rest-prometheus.incubation-staging.youdomain.com/
{"hello":"world"}

可以看到,自动构建了0.0.2版本,并且已经部署成功

4. 部署到正式环境

如果在staging测试没有问题,可以推送到正式环境(production)

jx promote spring-boot-rest-prometheus  --version 0.0.2 --env production

该命令会往production环境发送一个pullrequest,同意后会部署到正式环境。

jx promote spring-boot-rest-prometheus  --version 0.0.2 --env production
Promoting app spring-boot-rest-prometheus version 0.0.2 to namespace incubation-production
? Do you wish to use jqpeng as the user name to submit the Pull Request Yes
Created Pull Request: http://github.youdomain.com/jqpeng/environment-walkertabby-production/pulls/2 pipeline jqpeng/environment-jx-dev/master
Pull Request http://github.youdomain.com/jqpeng/environment-walkertabby-production/pulls/2 is merged at sha 1f2ab843a8037f353020a81ad4289c9ede550447
merge status: pending for URL http://github.youdomain.com/api/v1/jqpeng/environment-walkertabby-production/statuses/1f2ab843a8037f353020a81ad4289c9ede550447 with target: http://jenkins.incubation.youdomain.com/job/jqpeng/job/environment-walkertabby-production/job/master/display/redirect description: Build queued...

然后再jx get applications

jx get applications
APPLICATION EDIT PODS URL STAGING PODS URL PRODUCTION PODS URL
spring-boot-rest-prometheus 0.0.2 1/1 http://spring-boot-rest-prometheus.incubation-staging.youdomain.com 0.0.2 1/1 http://spring-boot-rest-prometheus.incubation-production.youdomain.com

可以看到0.0.2已经在PRODUCTIONS环境了,测试一下:

curl -l http://spring-boot-rest-prometheus.incubation-production.youdomain.com
{"hello":"world"}

三、为项目增加新功能

gitops提倡增加新功能,先在新的分支测试通过后,然后提交PR到master分支,因此我们先创建一个新分支jqpeng-dev

git checkout -b jqpeng-dev
Switched to a new branch 'jqpeng-dev'

hello jx

修改RestPrometheusApplication.java

@GetMapping(path = "/", produces = "application/json")
@ResponseBody
public Map<String, Object> landingPage() {
Counter.builder("mymetric").tag("foo", "bar").register(registry).increment();
return singletonMap("hello", "world");
}

修改为

@GetMapping(path = "/", produces = "application/json")
@ResponseBody
public Map<String, Object> landingPage() {
Counter.builder("mymetric").tag("foo", "bar").register(registry).increment();
return singletonMap("hello", "jx");
}

保存,提交

git commit -a -m ''
git push origin jqpeng-dev

提交一个PR:

jx create pullrequest -t "#pr1 hello jx"
? Do you wish to use jadepeng as the user name to use for authenticating with git issues Yes Created PullRequest #1 at https://github.com/jadepeng/spring-boot-rest-prometheus/pull/1

可以到https://github.com/jadepeng/spring-boot-rest-prometheus/pull/1/files查看代码变更。

提交PR后,jx会自动构建PR,并部署preview环境,可以打开jenkins查看:

或者通过命令查看:

jx get build log jadepeng/spring-boot-rest-prometheus/PR-1

等自动构建完成,jx会在pr下面提交一个带预览地址的评论:

点看链接查看:

已经是hello jx了!

合并PR

打开PR页面,点击Merge pull request:

填写合并日志,提交:

PR1已经合并到master分支,等待自动构建完成,剩下的就和上面“二、快速开始”里的一样了,在staging环境进行测试,没问题的发布到生产环境。

四、小结

jx 良好的实现了gitops,利于团队协作,可以在团队进行推广实施。


作者:Jadepeng

出处:jqpeng的技术记事本--http://www.cnblogs.com/xiaoqi

您的支持是对博主最大的鼓励,感谢您的认真阅读。

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

jenkins X实践系列(2) —— 基于jx的DevOps实践的更多相关文章

  1. 中小团队基于Docker的devops实践

    笔者所在的技术团队负责了数十个项目的开发和维护工作,每个项目都至少有dev.qa.hidden.product四个环境,数百台机器,在各个系统之间疲于奔命,解决各种琐碎的问题,如何从这些琐碎的事情中解 ...

  2. 后端开发实践系列之三——事件驱动架构(EDA)编码实践

    在本系列的前两篇文章中,笔者分别讲到了后端项目的代码模板和DDD编码实践,在本文中,我将继续以编码实践的方式分享如何落地事件驱动架构. 单纯地讲事件驱动架构(Event Driven Architec ...

  3. 【转】RHadoop实践系列之一:Hadoop环境搭建

    RHadoop实践系列之一:Hadoop环境搭建 RHadoop实践系列文章,包含了R语言与Hadoop结合进行海量数据分析.Hadoop主要用来存储海量数据,R语言完成MapReduce 算法,用来 ...

  4. 十分钟速成DevOps实践

    摘要:以华为云软件开发平台DevCloud为例,十分钟简单体验下DevOps应用上云实践--H5经典小游戏上云. 本文分享自华为云社区<<DevOps实践秘籍>十分钟速成DevOps ...

  5. jenkins X实践系列(1) —— 背景知识

    本文介绍jenkins X(以下简称jx)相关的背景技术. jenkins X 简介 Jenkins X 是一个高度集成化的CI/CD平台,基于Jenkins和Kubernetes实现,旨在解决微服务 ...

  6. 「持续集成实践系列」Jenkins 2.x 搭建CI需要掌握的硬核要点

    1. 前言 随着互联网软件行业快速发展,为了抢占市场先机,企业不得不持续提高软件的交付效率.特别是现在国内越来越多企业已经在逐步引入DevOps研发模式的变迁,在这些背景催促之下,对于企业研发团队所需 ...

  7. 「持续集成实践系列 」Jenkins 2.x 构建CI自动化流水线常见技巧

    在上一篇文章中,我们介绍了Jenkins 2.x实现流水线的两种语法,以及在实际工作中该如何选择脚本式语法或声明式语法.原文可查阅:「持续集成实践系列」Jenkins 2.x 搭建CI需要掌握的硬核要 ...

  8. .NET Core微服务架构学习与实践系列文章索引目录

    一.为啥要总结和收集这个系列? 今年从原来的Team里面被抽出来加入了新的Team,开始做Java微服务的开发工作,接触了Spring Boot, Spring Cloud等技术栈,对微服务这种架构有 ...

  9. .NET Core 微服务学习与实践系列文章目录索引(2019版)

    参考网址: https://archy.blog.csdn.net/article/details/103659692 2018年,我开始学习和实践.NET Core,并开始了微服务的学习,以及通过各 ...

随机推荐

  1. maven打包子模块中的class文件

    通常在项目中都会使用maven进行多模块管理,默认被依赖的模块都会以jar包形式被引用.然而在J2EE项目中,当使用了Spring的自动扫描配置时,jar包形式的依赖class将不能被自动装配:< ...

  2. Linux - vim 编辑器

    gconf-editor # 配置编辑器 /etc/vimrc # 配置文件路径 vim +24 file # 打开文件定位到指定行 vim file1 file2 # 打开多个文件 vim -O2 ...

  3. 安卓中location.href或者location.reload 不起作用

    链接:https://www.cnblogs.com/joshua317/p/6163471.html 在移动wap中,经常会使用window.location.href去跳转页面,这个方法在绝大多数 ...

  4. Java SE 之 DAO层接口设计思想

    用图说话 好处 1.只需要定义好IBaseDao的接口方法,并只需要实现BaseDaoImpl的方法,而具体的业务类和业务类/接口的方法的基本方法(IBaseDao已定义的)并不需要再考虑实现. 2. ...

  5. 【洛谷P1896【SCOI2005】】互不侵犯King

    题目描述 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子. 输入输出格式 输入格式: 只有一行,包 ...

  6. MySQL中查询行数最多的表并且排序

    #切换到schema use information_schema; #查询数据量最大的30张表 并排序 select table_name,table_rows from tables order ...

  7. 如何利用 Python 完成验签操作

    柠檬班Python8期的佑佑以及Python7期的掠掠同学昨天都私下问华华老师如何利用Python完成验签的操作. 今天我们就以佑佑的例子来跟大家进行简单的说明以及操作! 一.什么是验签: 用非常简单 ...

  8. JS堆栈与拷贝

    一.堆栈的定义 1.栈是一种特殊的线性表.其特殊性在于限定插入和删除数据元素的操作只能在线性表的一端进行. 结论:后进先出(Last In First Out),简称为LIFO线性表.栈的应用有:数制 ...

  9. 图片的Base64编码

    Base64编码是一种图片处理格式,通过特定的算法将图片编码成一长串字符串,在页面上显示的时候,可以用该字符串来代替图片的url属性. 我们可以来看一下实际的效果 Base64编码效果 在上图中,我们 ...

  10. python学习:绝对路径和相对路径

    python学习:绝对路径和相对路径 大牛们应该对路径都很了解了,这篇文章主要给像我这样的入门小白普及常识用的,啊哈 下面的路径介绍针对windows,其他平台的暂时不是很了解. 在编写的py文件中打 ...