暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Jenkins X--(7)虚拟机里搭建Jenkins X环境

DevOps亮哥 2019-11-05
752

上一节《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。


首先安装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 ~]#


      安装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 xinglongjian@qq.com
            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最佳实践和原则为指导,真正能够做到自动化,高效。


                文章转载自DevOps亮哥,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                评论