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. luogu 1314 欧拉回路

    欧拉路径:一笔画的路径 欧拉回路:一笔画的回路 两者判断方法一样但是输出略有不同.并且还有Fleury(弗罗莱)算法,但是我不会.. 这里就用dfs就好 判断条件: 1)图的连通性(可用并查集判断) ...

  2. .NET Framework 系统版本支持表

    .tg {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;} .tg td{font-family:Arial, sans ...

  3. QDoubleSpinBox浮点型数字调节框

    样式: import sys from PyQt5.QtWidgets import QApplication, QWidget, QDoubleSpinBox class Demo(QWidget) ...

  4. MySQL之当数据库数据源被锁(Table Metadata Lock)时的解决方案

    0.发生的原因分析:[hibernate的线程池连接导致了不能修改被锁定数据库的数据库型模式] 1.关掉hibernate的所有线程池(选择退出IDE或者其他办法) 2.查看被锁的进程ID:show ...

  5. [C++]指针与多级指针(图解)

    声明:如需引用或者摘抄本博文源码或者其文章的,请在显著处注明,来源于本博文/作者,以示尊重劳动成果,助力开源精神.也欢迎大家一起探讨,交流,以共同进步- 0.0 演示: /* @author:John ...

  6. JavaScript之Ajax(一)创建Ajax对象

    // 支持浏览器的源码 function AjaxObject() { var AjaxRequest; // 缓存XHR对象便于 Ajax 使用 try { // Opera 8.0+, Firef ...

  7. python技巧 switch case语句

    不同于C语言和SHELL,python中没有switch case语句,关于为什么没有,官方的解释是这样的 使用Python模拟实现的方法: def switch_if(fun, x, y):    ...

  8. 【通信】Jave代码中生成url http请求

    /** * 向指定 URL 发送POST方法的请求 * * @param url * 发送请求的 URL * @param param * 请求参数,请求参数应该是 name1=value1& ...

  9. 【vue报错】——listen EADDRINUSE :::8080 解决方案

    题原因: 此项错误表示 8080 端口被占用 解决方案一: 打开cmd输入:netstat -ano|findstr "8080"查看所有端口信息,并通过findstr “8080 ...

  10. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 20165230

    2018-2019-2 <网络对抗技术>Exp0 Kali安装 20165230 一.下载kali 下载地址 下载Kali Linux 64 bit VMware VM版本 下载vmwar ...