分享一个在k8s中实现的插件加速的方法

思路

原理把插件地址重定向到清华源,与论坛中的方案本质一致

清华源地址

https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

查看下json中依然有很多地址指向 updates.jenkins-ci.org
所以有了如下解决思路:

  • 在jenkins所在pod中添加一个sidecar,将 updates.jenkins-ci.org 重定向到 mirrors.tuna.tsinghua.edu.cn,不对原有jenkins镜像侵入,方便以后升级

构建 proxy-tuna镜像

docker build -t dolphintwo/jenkins-plugins-proxy:v0.1 .
  • Dockerfile
FROM nginx:1.17.6-alpine
LABEL maintainer="dolphintwo dtdinghui2@gmail.com"

COPY nginx.conf /etc/nginx/nginx.conf
  • nginx.conf
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
events {
    worker_connections  1024;
}
 
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
 
        location /download/plugins {
            proxy_next_upstream http_502 http_504 error timeout invalid_header;
            proxy_set_header Host mirrors.tuna.tsinghua.edu.cn;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            rewrite /download/plugins(.*) /jenkins/plugins/$1 break;
            proxy_pass https://mirrors.tuna.tsinghua.edu.cn;
        }
    }
}

使用

然后在pod中定义sidecar,以及拦截 updates.jenkins-ci.org

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
  labels:
    name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      name: jenkins
  template:
    metadata:
      name: jenkins
      labels:
        name: jenkins
    spec:
      serviceAccountName: jenkins
      hostAliases:
      - ip: "127.0.0.1"
        hostnames:
        - "updates.jenkins-ci.org"       
      containers:
        - name: tuna-proxy
          image: dolphintwo/jenkins-plugins-proxy:v0.1
          imagePullPolicy: Always
          ports:
            - containerPort: 80
        - name: jenkins
          image: jenkins/jenkins:lts-alpine
          imagePullPolicy: Always
          ports:
            - containerPort: 8080
            - containerPort: 50000
          resources:
            limits:
              cpu: 4
              memory: 8Gi
            requests:
              cpu: 0.5
              memory: 1000Mi
          env:
            - name: LIMITS_MEMORY
              valueFrom:
                resourceFieldRef:
                  resource: limits.memory
                  divisor: 1Mi
            - name: JAVA_OPTS
              value: -Xmx$(LIMITS_MEMORY)m -XshowSettings:vm -Dhudson.slaves.NodeProvisioner.initialDelay=0 -Dhudson.slaves.NodeProvisioner.MARGIN=50 -Dhudson.slaves.NodeProvisioner.MARGIN0=0.85
          volumeMounts:
            - name: jenkins-home
              mountPath: /var/jenkins_home
      securityContext:
        fsGroup: 1000
      volumes:
        - name: jenkins-home
          persistentVolumeClaim:
            claimName: jenkins-home
  • 注意添加sidecar并指定host拦截请求
  • deployment只是本次实验中的示范,应该也可以创建有状态应用中使用

验证

kubernetes-operator|⇒ kubectl -n ops logs jenkins-5f4996b99-77r98 jenkins --tail 50
...
2019-12-27 07:36:14.430+0000 [id=84]    INFO    hudson.PluginManager#install: Starting installation of a batch of 1 plugins plus their dependencies
2019-12-27 07:36:21.510+0000 [id=85]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of localization-zh-cn on behalf of admin
2019-12-27 07:36:22.140+0000 [id=85]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading localization-zh-cn
2019-12-27 07:37:32.662+0000 [id=79]    INFO    hudson.PluginManager#install: Starting installation of a batch of 15 plugins plus their dependencies
2019-12-27 07:37:32.663+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of docker-commons on behalf of admin
2019-12-27 07:37:32.666+0000 [id=79]    INFO    hudson.model.UpdateSite$Plugin#deploy: Adding dependent install of pipeline-model-api for plugin pipeline-model-extensions
2019-12-27 07:37:33.703+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading docker-commons
2019-12-27 07:37:34.042+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of kubernetes on behalf of admin
2019-12-27 07:37:37.615+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading kubernetes
2019-12-27 07:37:43.745+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of pipeline-stage-tags-metadata on behalf of admin
2019-12-27 07:37:44.129+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading pipeline-stage-tags-metadata
2019-12-27 07:37:44.186+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of git-server on behalf of admin
2019-12-27 07:37:45.111+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading git-server
2019-12-27 07:37:45.186+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of workflow-api on behalf of admin
2019-12-27 07:37:46.614+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading workflow-api
2019-12-27 07:37:46.952+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of workflow-cps on behalf of admin
2019-12-27 07:37:47.469+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading workflow-cps
2019-12-27 07:37:48.834+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of pipeline-model-api on behalf of admin
2019-12-27 07:37:49.382+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading pipeline-model-api
2019-12-27 07:37:51.666+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of pipeline-model-extensions on behalf of admin
2019-12-27 07:37:52.192+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading pipeline-model-extensions
2019-12-27 07:37:52.308+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of kubernetes-cd on behalf of admin
2019-12-27 07:37:53.188+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading kubernetes-cd
2019-12-27 07:38:29.885+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of branch-api on behalf of admin
2019-12-27 07:38:30.424+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading branch-api
2019-12-27 07:38:31.292+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of cloudbees-folder on behalf of admin
2019-12-27 07:38:32.434+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading cloudbees-folder
2019-12-27 07:38:33.025+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of pipeline-model-definition on behalf of admin
2019-12-27 07:38:33.521+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading pipeline-model-definition
2019-12-27 07:38:35.165+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of pipeline-model-api on behalf of admin
2019-12-27 07:38:35.165+0000 [id=90]    INFO    h.m.UpdateCenter$InstallationJob#_run: Skipping duplicate install of: Pipeline: Model API@1.5.0
2019-12-27 07:38:35.166+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Installation successful: pipeline-model-api
2019-12-27 07:38:35.166+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of config-file-provider on behalf of admin
2019-12-27 07:38:35.738+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading config-file-provider
2019-12-27 07:38:36.331+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of gradle on behalf of admin
2019-12-27 07:38:39.430+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading gradle
2019-12-27 07:38:41.012+0000 [id=90]    INFO    h.model.UpdateCenter$DownloadJob#run: Starting the installation of role-strategy on behalf of admin
2019-12-27 07:38:41.536+0000 [id=90]    INFO    h.m.UpdateCenter$UpdateCenterConfiguration#download: Downloading role-strategy
kubernetes-operator|⇒ kubectl -n ops logs jenkins-5f4996b99-77r98 tuna-proxy --tail 50
127.0.0.1 - - [27/Dec/2019:07:36:22 +0000] "GET /download/plugins/localization-zh-cn/1.0.13/localization-zh-cn.hpi HTTP/1.1" 200 542952 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:34 +0000] "GET /download/plugins/docker-commons/1.16/docker-commons.hpi HTTP/1.1" 200 91018 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:43 +0000] "GET /download/plugins/kubernetes/1.22.3/kubernetes.hpi HTTP/1.1" 200 995984 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:44 +0000] "GET /download/plugins/pipeline-stage-tags-metadata/1.5.0/pipeline-stage-tags-metadata.hpi HTTP/1.1" 200 10708 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:45 +0000] "GET /download/plugins/git-server/1.9/git-server.hpi HTTP/1.1" 200 30408 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:46 +0000] "GET /download/plugins/workflow-api/2.38/workflow-api.hpi HTTP/1.1" 200 155980 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:48 +0000] "GET /download/plugins/workflow-cps/2.78/workflow-cps.hpi HTTP/1.1" 200 597525 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:51 +0000] "GET /download/plugins/pipeline-model-api/1.5.0/pipeline-model-api.hpi HTTP/1.1" 200 2436323 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:37:52 +0000] "GET /download/plugins/pipeline-model-extensions/1.5.0/pipeline-model-extensions.hpi HTTP/1.1" 200 38764 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:38:29 +0000] "GET /download/plugins/kubernetes-cd/2.2.0/kubernetes-cd.hpi HTTP/1.1" 200 34436567 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:38:31 +0000] "GET /download/plugins/branch-api/2.5.5/branch-api.hpi HTTP/1.1" 200 308984 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:38:32 +0000] "GET /download/plugins/cloudbees-folder/6.10.1/cloudbees-folder.hpi HTTP/1.1" 200 218594 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:38:35 +0000] "GET /download/plugins/pipeline-model-definition/1.5.0/pipeline-model-definition.hpi HTTP/1.1" 200 1490265 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:38:36 +0000] "GET /download/plugins/config-file-provider/3.6.3/config-file-provider.hpi HTTP/1.1" 200 196260 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:38:40 +0000] "GET /download/plugins/gradle/1.35/gradle.hpi HTTP/1.1" 200 71048 "-" "Java/1.8.0_212" "-"
127.0.0.1 - - [27/Dec/2019:07:38:41 +0000] "GET /download/plugins/role-strategy/2.16/role-strategy.hpi HTTP/1.1" 200 104551 "-" "Java/1.8.0_212" "-"

至此,发现安装已经重定向到清华源

本地安装的jenkins也可以使用该方法

3赞