jenkins X实践系列(4) —— jenkins X 构建提速

jx是云原生CICD,devops的一个最佳实践之一,目前在快速的发展成熟中。最近调研了JX,这里为第4篇,介绍如何加入jx构建和部署。

builder镜像下载慢

先在一台机器上下载好,然后放到本地仓库,到jenkins的setting里,修改镜像地址

cnpm

nodejs安装慢

    1. 配置使用私服
    1. 构建cnpm镜像

  1 FROM jenkinsxio/builder-nodejs:0.1.215
  2 MAINTAINER jadepeng <[email protected]>
  3 
  4 RUN npm install -g cnpm --registry=https://registry.npm.taobao.org
  5 RUN cnpm install -g antd axios babel-plugin-import mobx mobx-react mobx-react-devtools nprogress react react-app-rewire-less  react-app-rewired  react-dom react-router-dom react-scrip    ts history path
  6 

构建后,在jenkins里设置一个k8s pod template

enter description here

在jenkinsfile里就可以使用cnpm加速了

pipeline {
  agent {
    // 使用cnpm加速
    label "jenkins-cnpm"
  }
 ...
 
      steps {
        container('nodejs') {
          sh "cnpm install && npm run build"
      }
    }

加速maven:

maven配置文件

maven的配置文件通过secret jenkins-maven-settings配置,maven pod创建时,会将jenkins-maven-settings挂载到/root/.m2/,要自定义配置文件,执行:

  # 删除老配置
  kubectl delete secret jenkins-maven-settings 
  # 修改配置文件,然后创建新的jenkins-maven-settings
  kubectl create secret generic jenkins-maven-settings --from-file=./jenkinsx -n incubation

每次build都会从maven仓库下载jar包,为了避免重复下载,可以挂载一个pvc持久化maven本地仓库。

挂载共享cephfs-mavenrepos-pvc 到 /root/.mvnrepository

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
  name: cephfs-mavenrepos-pv
  labels:
    name: cephfs-mavenrepos-pv
spec:
  capacity:
    storage: 500Gi
  accessModes:
    - ReadWriteMany
  cephfs:
    monitors: 
    - 192.168.86.156:6789
    path: /mavenrepos
    user: admin
    secretRef:
      name: ceph-secret
      namespace: kube-system
    readOnly: false
  persistentVolumeReclaimPolicy: Retain
EOF

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cephfs-mavenrepos-pvc
  namespace: incubation
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: ""
  resources:
    requests:
      storage: 5Gi
  selector:
    matchLabels:
      name: cephfs-mavenrepos-pv
EOF

创建好后,到jenkins系统配置里配置挂载信息。

nexus

添加 npm proxy

name: cnjs
address: http://r.cnpmjs.org

RUN npm i --registry=http://nexus/researchnpm/

maven镜像可以添加一个阿里、csdn等proxy,加到 maven-group

chartmuseum.jenkins-x.io访问慢

chartmuseum.jenkins-x.io 经常访问不了,可以搭建私有charts服务,新增一个ingress,域名使用chartmuseum.jenkins-x.io。

{
  "kind": "Ingress",
  "apiVersion": "extensions/v1beta1",
  "spec": {
    "rules": [
      {
        "host": "chartmuseum.jenkins-x.io",
        "http": {
          "paths": [
            {
              "path": "/",
              "backend": {
                "serviceName": "jenkinsx-charts-service",
                "servicePort": 80
              }
            }
          ]
        }
      }
    ]
  }

最核心的是,让构建容器里可以访问到这个:

  • 先修改hosts

    k8s-vip chartmuseum.jenkins-x.io

k8s-vip 要替换成K8S集群的虚拟ip或者masterip

  • 修改coredns,通过hosts插件挂载服务器的hosts文件

 63   Corefile: |
 64     .:53 {
 65         errors
 66         health
 67         hosts /etc/server-hosts {
 68             fallthrough
 69         }
 70         kubernetes cluster.local.  in-addr.arpa ip6.arpa {
 71             pods insecure
 72             upstream
 73             fallthrough in-addr.arpa ip6.arpa
 74         }
 75         prometheus :9153
 76         proxy . 192.168.75.14
 77         cache 30
 78         log
 79         loop
 80         reload
 81         loadbalance
 82     }



165       volumes:
166         - name: config-volume
167           configMap:
168             name: coredns
169             items:
170             - key: Corefile
171               path: Corefile
172         - name: server-hosts
173           hostPath:
174             path: /etc/hosts

jenkins-x 部署慢

  1. 将exposecontroller手动导入上传到jenkins-x-chartmuseum,然后修改env的requirement.yaml,
 alias: expose
  name: exposecontroller
  repository: http://jenkins-x-chartmuseum:8080
  version: 2.3.82
- alias: cleanup
  name: exposecontroller
  repository: http://jenkins-x-chartmuseum:8080
  version: 2.3.82
  1. 修改jenkinsfile
  • 减少stage
  • helm init --client-only
  • 部署不适用jx step,直接使用helm命令部署
pipeline {
  options {
    disableConcurrentBuilds()
  }
  agent {
    label "jenkins-maven"
  }
  environment {
    DEPLOY_NAMESPACE = "cog"
  }
  stages {
    stage('Update Environment') {
      when {
        branch 'master'
      }
      steps {
        container('maven') {
          dir('env') {
             sh 'helm init --client-only --stable-repo-url http://charts.iflyresearch.com/ && helm repo add release http://jenkins-x-chartmuseum:8080 && helm dependency build . && helm upgrade $DEPLOY_NAMESPACE . --install --namespace $DEPLOY_NAMESPACE'
          }
        }
      }
    }
  }
}

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

猜你喜欢

转载自www.cnblogs.com/xiaoqi/p/jenkins-x-part4.html
0条评论
添加一条新回复