Jenkins X–(7)虛擬機里搭建Jenkins X環境
- 2019 年 11 月 28 日
- 筆記

上一節《Jenkins X–(6)虛擬機里搭建Minikube環境》,我們已經完成了基於minikube的K8s環境的搭建,如果只是學習K8s這個環境就夠了。這一節,我們要在這個K8s集群上,搭建一套雲原生時代CICD的開源框架:Jenkins X。
jenkins-x-versions:v1.0.118 https://github.com/jenkins-x/jenkins-x-versions jx: 2.0.844 https://github.com/jenkins-x/jx jenkins-x-platform:2.0.1413 https://github.com/jenkins-x/jenkins-x-platform
jenkins-x-versions是一個包含與jenkins-x及其相關程序一起使用的packages和helm charts的一致的版本集合。jenkins-x-versions v1.0.118 對應的jx版本是2.0.844,jenkins-x-platform的版本是2.0.1413。
1、首先安裝jx
首先在本機上安裝一個命令行工具jx,用於安裝和使用Jenkins X。
jx可以安裝在MacOs,Linux和Window上。可以參考:https://github.com/jenkins-x/jx/releases
這裡執行一下命令安裝2.0.844版本的jx:
curl -L https://github.com/jenkins-x/jx/releases/download/v2.0.844/jx-linux-amd64.tar.gz | tar xzv sudo mv jx /usr/local/bin
驗證jx是否安裝成功,輸入:jx,顯示jx的命令清單。
[root@learncloudnative ~]# jx jx is a command line tool for working with Jenkins XWARNING: Unable to load managed plugins because customresourcedefinitions.apiextensions.k8s.io "plugins.jenkins.io" not found Installing: profile Set your jx profile boot Boots up Jenkins X in a Kubernetes cluster using GitOps and a Jenkins X Pipeline install Install Jenkins X in the current Kubernetes cluster uninstall Uninstall the Jenkins X platform upgrade Upgrades a resource create cluster Create a new Kubernetes cluster update cluster Updates an existing Kubernetes cluster create jenkins token Adds a new username and API token for a Jenkins server delete jenkins token Deletes one or more Jenkins user API tokens init Init Jenkins X Adding Projects to Jenkins X: import Imports a local project or Git repository into Jenkins create archetype Create a new app from a Maven Archetype and import the generated code into Git and Jenkins for CI/CD create spring Create a new Spring Boot application and import the generated code into Git and Jenkins for CI/CD create lile Create a new Lile based application and import the generated code into Git and Jenkins for CI/CD create micro Create a new micro based application and import the generated code into Git and Jenkins for CI/CD create quickstart Create a new app from a Quickstart and import the generated code into Git and Jenkins for CI/CD create quickstartlocation Create a location of quickstarts for your team Apps: create addon Creates an addon create token addon Adds a new token/login for a user for a given addon delete addon Deletes one or more addons delete token addon Deletes one or more API tokens for a user on an issue addon server delete app Deletes one or more apps from Jenkins X delete application Deletes one or more applications from Jenkins add app Adds an app Git: create git server Creates a new Git server from a URL and kind delete git server Deletes one or more Git servers create git token Adds a new API token for a user on a Git server delete git token Deletes one or more API tokens for a user on a Git server repository Opens the web page for the current Git repository in a browser Working with Kubernetes: compliance Run compliance tests against Kubernetes cluster completion Output shell completion code for the given shell (bash or zsh) context View or change the current Kubernetes context (Kubernetes cluster) environment View or change the current environment in the current Kubernetes cluster team View or change the current team in the current Kubernetes cluster namespace View or change the current namespace context in the current Kubernetes cluster prompt Generate the command line prompt for the current team and environment scan Perform a scan action shell Create a sub shell so that changes to the Kubernetes context, namespace or environment remain local to the shell status status of the Kubernetes cluster or named node Working with Applications: console Opens the Jenkins console logs Tails the log of the latest pod for a deployment open Open a service in a browser rsh Opens a terminal in a pod or runs a command in the pod sync Synchronises your local files to a DevPod Working with CloudBees application: cloudbees Opens the CloudBees app for Kubernetes for visualising CI/CD and your environments login Onboard an user into the CloudBees application Working with Environments: preview Creates or updates a Preview Environment for the current version of an application promote Promotes a version of an application to an Environment create addon environment Create an Environment Controller to handle webhooks and promote changes from GitOps create environment Create a new Environment which is used to promote your Team's Applications via Continuous Delivery delete addon environment Deletes the Environment Controller delete environment Deletes one or more Environments edit environment Edits an Environment which is used to promote your Team's Applications via Continuous Delivery get environments Display one or more Environments Working with Jenkins X resources: get Display one or more resources edit Edit a resource create Create a new resource update Updates an existing resource delete Deletes one or more resources add Adds a new resource start Starts a process such as a pipeline stop Stops a process such as a pipeline Jenkins X Pipeline Commands: step pipeline steps Jenkins X services: controller Runs a controller gc Garbage collects Jenkins X resources Other Commands: diagnose Print diagnostic information about the Jenkins X installation docs Open the documentation in a browser help Help about any command version Print the version information Options: -b, --batch-mode=false: Runs in batch mode without prompting for user input --verbose=false: Enables verbose output --version=false: version for jx Usage: jx [flags] [options] Use "jx <command> --help" for more information about a given command. Use "jx options" for a list of global command-line options (applies to all commands). [root@learncloudnative ~]#
2、安裝Jenkins-X到當前K8s集群
準備工作
jx在安裝jenkinsX到K8s集群時,需要從github上clone相關的代碼庫,比如:預覽環境和生產環境的模板代碼庫。也需要提供一個Git Server以及相關的賬號、Token來上傳相應的代碼庫。因此,驗證git是否安裝,並設置git config。
安裝Git
centos里自帶的git的版本有點低,可通過下面方法使用源碼安裝高版本git
# yum -y groupinstall Development tools# yum install gcc perl-ExtUtils-MakeMaker openssl-devel curl-devel expat-devel# yum -y remove git# wget https://www.kernel.org/pub/software/scm/git/git-2.16.5.tar.gz# tar -zxvf git-2.16.5.tar.gz# make prefix=/usr/local/git all# make prefix=/usr/local/git install# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc# source /etc/bashrc
設置Git Server Token
Git Server的token提前準備好,我這裡是用的github,可以在個人設置Setting—Personal access tokens里添加新的token。權限包括:delete_repo, read:org, read:user, repo, user:email, write:repo_hook

下載JenkinsX的鏡像
jenkinsX的鏡像需要從國外網站上下載,jx並沒有提供像minikube里指定鏡像地址的參數,這裡只能依靠之前寫的《Jenkins X–(4)不用翻牆也能下載國外的鏡像》,先提前把鏡像準備好。各位幸運的是,這些鏡像我都已經準備好了,可以在hub.docker.com里搜索,賬號:xinglongjian下一般都有,如果沒有,那就是在其他的賬號里已經有了,我就沒重複上傳。如果都沒有,可以安裝文章里提供的方法,自行上傳。
下載鏡像 |
轉成tag鏡像 |
---|---|
docker pull xinglongjian/jenkinsx:0.0.80 |
gcr.io/jenkinsxio/jenkinsx:0.0.80 |
docker pull xinglongjian/builder-jx:2.0.842-199 |
gcr.io/jenkinsxio/builder-jx:2.0.842-199 |
docker pull xinglongjian/jx:2.0.645 |
gcr.io/jenkinsxio/jx:2.0.645 |
docker pull xinglongjian/builder-maven:2.0.842-199 |
gcr.io/jenkinsxio/builder-maven:2.0.842-199 |
docker pull xinglongjian/nexus:0.1.7 |
gcr.io/jenkinsxio/nexus:0.1.7 |
docker pull xinglongjian/heapster:v1.5.2 |
k8s.gcr.io/heapster:v1.5.2 |
docker pull xinglongjian/addon-resizer:1.7 |
k8s.gcr.io/addon-resizer:1.7 |
docker pull xinglongjian/chartmuseum:v0.7.1 |
chartmuseum/chartmuseum:v0.7.1 |
docker pull xinglongjian/exposecontroller:2.3.111 |
gcr.io/jenkinsxio/exposecontroller:2.3.111 |
docker pull xinglongjian/nginx-ingress-controller:0.25.1 |
quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.25.1 |
docker pull registry:2.6.2 |
|
執行安裝
jx install --versions-ref='v1.0.118'
執行過程如下:執行過程中會設置git config,以及連接github的賬號和token,jx install根據指定的版本下載相應的代碼庫和鏡像,同時會給自己的代碼庫中創建好環境代碼庫(test,stage,production)。
[root@learncloudnative ~]# jx install --versions-ref='v1.0.118' ? Select Jenkins installation type: [Use arrows to move, space to select, type to filter] > Serverless Jenkins X Pipelines with Tekton Static Jenkins Server and Jenkinsfiles ? Select Jenkins installation type: [Use arrows to move, space to select, type to filter] Serverless Jenkins X Pipelines with Tekton > Static Jenkins Server and Jenkinsfiles ? Select Jenkins installation type: Static Jenkins Server and Jenkinsfiles Namespace jx created Context "minikube" modified. set exposeController Config Domain 192.168.117.129.nip.io Git configured for user: xinglongjian and email [email protected] helm installed and configured nginx ingress controller already enabled Set up a Git username and API token to be able to perform CI/CD ? Do you wish to use xinglongjian as the local Git user for GitHub server: Yes Select the CI/CD pipelines Git server and user ? Do you wish to use GitHub as the pipelines Git server: Yes Setting the pipelines Git server https://github.com and user name xinglongjian. Deleting and cloning the Jenkins X versions repo Cloning the Jenkins X versions repo https://github.com/jenkins-x/jenkins-x-versions.git with revision v1.0.118 to /root/.jx/jenkins-x-versions remote: Enumerating objects: 2, done. remote: Counting objects: 100% (2/2), done. remote: Compressing objects: 100% (2/2), done. remote: Total 2 (delta 0), reused 2 (delta 0), pack-reused 0 展開對象中: 100% (2/2), 完成. 來自 https://github.com/jenkins-x/jenkins-x-versions * tag v1.0.118 -> FETCH_HEAD Cloning the Jenkins X cloud environments repo to /root/.jx/cloud-environments ? A local Jenkins X cloud environments repository already exists, recreating with latest: Yes Cloning the Jenkins X cloud environments repo to /root/.jx/cloud-environments Enumerating objects: 1440, done. Total 1440 (delta 0), reused 0 (delta 0), pack-reused 1440 ? Defaulting workload build pack: Kubernetes Workloads: Automated CI+CD with GitOps Promotion Setting the team build pack to kubernetes-workloads repo: https://github.com/jenkins-x-buildpacks/jenkins-x-kubernetes.git ref: master Installing jx into namespace jx Installing jenkins-x-platform version: 2.0.1413 Deleting and cloning the Jenkins X versions repo Cloning the Jenkins X versions repo https://github.com/jenkins-x/jenkins-x-versions.git with revision v1.0.118 to /root/.jx/jenkins-x-versions remote: Enumerating objects: 2, done. remote: Counting objects: 100% (2/2), done. remote: Compressing objects: 100% (2/2), done. remote: Total 2 (delta 0), reused 2 (delta 0), pack-reused 0 展開對象中: 100% (2/2), 完成. 來自 https://github.com/jenkins-x/jenkins-x-versions * tag v1.0.118 -> FETCH_HEAD WARNING: waiting for install to be ready, if this is the first time then it will take a while to download images Jenkins X deployments ready in namespace jx Configuring the TeamSettings for ImportMode Jenkinsfile Configure Jenkins API Token Generating the API token... Logged in admin to Jenkins server at http://jenkins.jx.192.168.117.129.nip.io via legacy security realm Enable CSRF protection at: http://jenkins.jx.192.168.117.129.nip.io/configureSecurity/ Created user admin API Token for Jenkins server jenkins.jx.192.168.117.129.nip.io at http://jenkins.jx.192.168.117.129.nip.io Updating Jenkins with new external URL details http://jenkins.jx.192.168.117.129.nip.io Creating default staging and production environments ? Select the organization where you want to create the environment repository: [Use arrows to move, space to select, type to filter] > xinglongjian ? Select the organization where you want to create the environment repository: xinglongjian Using Git provider GitHub at https://github.com ? Using Git user name: xinglongjian ? Using organisation: xinglongjian Creating repository xinglongjian/environment-healerwheat-staging Creating Git repository xinglongjian/environment-healerwheat-staging Pushed Git repository to https://github.com/xinglongjian/environment-healerwheat-staging Creating staging Environment in namespace jx Created environment staging Created Jenkins Project: http://jenkins.jx.192.168.117.129.nip.io/job/xinglongjian/job/environment-healerwheat-staging/ Note that your first pipeline may take a few minutes to start while the necessary images get downloaded! Creating GitHub webhook for xinglongjian/environment-healerwheat-staging for url http://jenkins.jx.192.168.117.129.nip.io/github-webhook/ Using Git provider GitHub at https://github.com ? Using Git user name: xinglongjian ? Using organisation: xinglongjian Creating repository xinglongjian/environment-healerwheat-production Creating Git repository xinglongjian/environment-healerwheat-production Pushed Git repository to https://github.com/xinglongjian/environment-healerwheat-production Creating production Environment in namespace jx Created environment production Namespace jx-production created Created Jenkins Project: http://jenkins.jx.192.168.117.129.nip.io/job/xinglongjian/job/environment-healerwheat-production/ Note that your first pipeline may take a few minutes to start while the necessary images get downloaded! Creating GitHub webhook for xinglongjian/environment-healerwheat-production for url http://jenkins.jx.192.168.117.129.nip.io/github-webhook/ Jenkins X installation completed successfully ******************************************************** NOTE: Your admin password is: wn4vA4abe+ncULd48g~s ******************************************************** Your Kubernetes context is now set to the namespace: jx To switch back to your original namespace use: jx namespace jx Or to use this context/namespace in just one terminal use: jx shell For help on switching contexts see: https://jenkins-x.io/developing/kube-context/ To import existing projects into Jenkins: jx import To create a new Spring Boot microservice: jx create spring -d web -d actuator To create a new microservice from a quickstart: jx create quickstart [root@learncloudnative ~]#
jenkins X安裝時默認,在k8s集群中安裝一個名為jx的命名空間。同時也會為每個環境創建一個命名空間,如下表:
[root@learncloudnative ~]# kubectl get ns NAME STATUS AGE default Active 4d18h jx Active 25m jx-production Active 20m jx-staging Active 14h kube-public Active 4d18h kube-system Active 4d18h [root@learncloudnative ~]# kubectl get pod -n jx NAME READY STATUS RESTARTS AGE jenkins-5c95d5bf9b-p94qh 1/1 Running 0 23m jenkins-x-chartmuseum-67f75c4884-kc9f8 1/1 Running 0 23m jenkins-x-controllerrole-7f7d94659c-8fnbx 1/1 Running 0 23m jenkins-x-controllerteam-9775fbf47-4cggm 1/1 Running 0 23m jenkins-x-controllerworkflow-578f9cbf9b-4bmt5 1/1 Running 0 23m jenkins-x-docker-registry-589d4d79c4-dx4jq 1/1 Running 0 23m jenkins-x-heapster-fbdccf687-vchkq 2/2 Running 0 23m jenkins-x-nexus-57c8447c4-7gnw8 1/1 Running 0 23m maven-ns5xq 2/2 Running 0 20m maven-x1293 2/2 Running 0 20m [root@learncloudnative ~]# kubectl get all -n jx NAME READY STATUS RESTARTS AGE pod/jenkins-5c95d5bf9b-p94qh 1/1 Running 0 23m pod/jenkins-x-chartmuseum-67f75c4884-kc9f8 1/1 Running 0 23m pod/jenkins-x-controllerrole-7f7d94659c-8fnbx 1/1 Running 0 23m pod/jenkins-x-controllerteam-9775fbf47-4cggm 1/1 Running 0 23m pod/jenkins-x-controllerworkflow-578f9cbf9b-4bmt5 1/1 Running 0 23m pod/jenkins-x-docker-registry-589d4d79c4-dx4jq 1/1 Running 0 23m pod/jenkins-x-heapster-fbdccf687-vchkq 2/2 Running 0 23m pod/jenkins-x-nexus-57c8447c4-7gnw8 1/1 Running 0 23m pod/maven-ns5xq 2/2 Running 0 20m pod/maven-x1293 2/2 Running 0 20m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/heapster ClusterIP 10.110.166.28 <none> 8082/TCP 23m service/jenkins ClusterIP 10.99.2.200 <none> 8080/TCP 23m service/jenkins-agent ClusterIP 10.104.2.199 <none> 50000/TCP 23m service/jenkins-x-chartmuseum ClusterIP 10.111.135.247 <none> 8080/TCP 23m service/jenkins-x-docker-registry ClusterIP 10.108.62.53 <none> 5000/TCP 23m service/nexus ClusterIP 10.109.97.154 <none> 80/TCP 23m NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/jenkins 1/1 1 1 23m deployment.apps/jenkins-x-chartmuseum 1/1 1 1 23m deployment.apps/jenkins-x-controllerrole 1/1 1 1 23m deployment.apps/jenkins-x-controllerteam 1/1 1 1 23m deployment.apps/jenkins-x-controllerworkflow 1/1 1 1 23m deployment.apps/jenkins-x-docker-registry 1/1 1 1 23m deployment.apps/jenkins-x-heapster 1/1 1 1 23m deployment.apps/jenkins-x-nexus 1/1 1 1 23m NAME DESIRED CURRENT READY AGE replicaset.apps/jenkins-5c95d5bf9b 1 1 1 23m replicaset.apps/jenkins-x-chartmuseum-67f75c4884 1 1 1 23m replicaset.apps/jenkins-x-controllerrole-7f7d94659c 1 1 1 23m replicaset.apps/jenkins-x-controllerteam-9775fbf47 1 1 1 23m replicaset.apps/jenkins-x-controllerworkflow-578f9cbf9b 1 1 1 23m replicaset.apps/jenkins-x-docker-registry-589d4d79c4 1 1 1 23m replicaset.apps/jenkins-x-heapster-7489b8d655 0 0 0 23m replicaset.apps/jenkins-x-heapster-fbdccf687 1 1 1 23m replicaset.apps/jenkins-x-nexus-57c8447c4 1 1 1 23m NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE cronjob.batch/jenkins-x-gcactivities 0/30 */3 * * * False 0 <none> 23m cronjob.batch/jenkins-x-gcpods 0/30 */3 * * * False 0 <none> 23m cronjob.batch/jenkins-x-gcpreviews 0 */3 * * * False 0 <none> 23m NAME GIT URL STATUS pipelineactivity.jenkins.io/xinglongjian-environment-healerwheat-production-master-1 Running pipelineactivity.jenkins.io/xinglongjian-environment-healerwheat-staging-master-1 Running [root@learncloudnative ~]#
以界面啟動,在終端輸入:jx console,可以在瀏覽器中打開jenkins X的控制台。
[root@learncloudnative ~]# jx console Jenkins Console: http://jenkins.jx.192.168.117.129.nip.io/blue This tool has been deprecated, use 'gio open' instead. See 'gio help open' for more info.
在瀏覽器中打開這個地址,使用上面的管理員密碼登錄。

登錄後,就進入到Jenkins的主界面了,同時有我們上面自動創建的兩個環境(staging和production)流水線。

查看environment-healerwheat-production流水線的執行狀態和執行日誌。

這樣,Jenkins X的環境就搭建好了,Jenkins X是雲原生時代的CICD實現,以DevOps最佳實踐和原則為指導,真正能夠做到自動化,高效。