jenkins X实践系列(2) 您所在的位置:网站首页 jx格式如何打开 jenkins X实践系列(2)

jenkins X实践系列(2)

2023-10-23 16:53| 来源: 网络整理| 查看: 265

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

使用脚本安装

choco install kubernetes-cli

测试是否安装成功:

kubectl version

创建配置文件,先转到%HOME% 目录:

For example: cd C:\users\yourusername

创建配置文件目录:

mkdir .kube

转到配置目录:

cd .kube

配置kubectl使用远程集群:

New-Item config -type file

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

测试 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:

pullrequest

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

enter description here

每提交一个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限制:

删除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,同意后会部署到正式环境。

enter description here

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 landingPage() { Counter.builder("mymetric").tag("foo", "bar").register(registry).increment(); return singletonMap("hello", "world"); }

修改为

@GetMapping(path = "/", produces = "application/json") @ResponseBody public Map 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查看:

build PR1

或者通过命令查看:

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

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

点看链接查看:

已经是hello jx了!

合并PR

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

合并PR

填写合并日志,提交:

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

四、小结

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

作者:Jadepeng 出处:jqpeng的技术记事本--http://www.cnblogs.com/xiaoqi 您的支持是对博主最大的鼓励,感谢您的认真阅读。 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有