简体   繁体   English

如何使用Kubernetes运行程序在Gitlab中为Maven添加持久卷

[英]how to add persistent volume for Maven in Gitlab with Kubernetes runner

Situation: 情况:

  • server A: we run Gitlab in a container. 服务器-答:我们在容器中运行Gitlab。
  • server B: we have Kubernetes. 服务器B:我们有Kubernetes。

Gitlab uses Kubernetes runner. Gitlab使用Kubernetes运行程序。 Some of our projects then build applications using docker container with Git and Maven. 然后,我们的一些项目使用带有Git和Maven的docker容器构建应用程序。

Maven always has to download all kinds of things into it's /root/.m2 cache. Maven总是必须将各种东西下载到它的/root/.m2缓存中。 What I need to do is create a persistent volume that these jobs can use, so once it's downloaded, it doesn't have to do it again each time someone wants to build or test something. 我需要做的是创建一个可以供这些作业使用的持久卷,因此,一旦下载了该卷,就不必在每次有人要构建或测试某些东西时都再次执行该操作。 These containers are always built anew using one premade image. 这些容器始终使用一个预制的映像重新构建。

Pretty basic stuff except I am absolutely new to Gitlab and Kubernetes. 非常基本的东西,除了我对Gitlab和Kubernetes绝对陌生。

Where do I need to create the volume? 我需要在哪里创建卷? I tried to change config.toml in the runner to include host_path type volume, but I don't know if I succeeded and Maven certainly has to download all the requirements every time. 我试图在运行程序中更改config.toml以包含host_path类型的卷,但我不知道我是否成功,并且Maven当然必须每次都下载所有要求。 I don't even know if the runner container has to be restarted for the changes to be applicated, and how. 我什至不知道是否必须重新启动运行器容器才能应用更改,以及如何更改。 This is the runner's config.toml : 这是跑步者的config.toml:

listen_address = "[::]:9252"
concurrent = 4
check_interval = 3
log_level = "info"

[session_server]
  session_timeout = 1800

[[runners]]
  name = "runner-gitlab-runner-c55d9bf98-2nn7c"
  url = "https://private_network:8443/"
  token = "yeah, token"
  executor = "kubernetes"
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]
  [runners.kubernetes]
    host = ""
    bearer_token_overwrite_allowed = false
    image = "ubuntu:16.04"
    namespace = "gitlab-managed-apps"
    namespace_overwrite_allowed = ""
    privileged = true
    service_account_overwrite_allowed = ""
    pod_annotations_overwrite_allowed = ""
    [runners.kubernetes.volumes.host_path]
      name = "maven-volume"
      mount_path = "/root/.m2"
      read_only = false

I don't know enough to know what I am missing. 我不了解我所缺少的。 Maybe I have to define something in .gitlab-ci.yml in those projects, or something else. 也许我必须在那些项目中的.gitlab-ci.yml中定义某些内容或其他内容。 I have looked into tutorials, I have tried Gitlab help pages, but I still can't find a working solution. 我已经研究了教程,尝试了Gitlab帮助页面,但是仍然找不到有效的解决方案。

Running GitLab Community Edition 11.6.5. 运行GitLab社区版11.6.5。

I would use a separate cache per project, using this in your build configuration 我会在每个项目中使用单独的缓存,在构建配置中使用它

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=./.m2/repository"
cache:
  paths:
    - ./.m2/repository
  # share cache across branches
  key: "$CI_BUILD_REF_NAME"

This prevents interference between separate project builds. 这样可以防止不同项目构建之间的干扰。 You can find a references configuration from the gitlab guys: https://gitlab.com/gitlab-org/gitlab-ci-yml/blob/master/Maven.gitlab-ci.yml 您可以从gitlab专家那里找到参考配置: https ://gitlab.com/gitlab-org/gitlab-ci-yml/blob/master/Maven.gitlab-ci.yml

1) Create a Kubernetes PersistentVolume (I use NFS as PersistentVolume type) : 1)创建一个Kubernetes PersistentVolume(我使用NFS作为PersistentVolume类型):

apiVersion: v1
kind: PersistentVolume
metadata:
  name: gitlabrunner-nfs-volume
spec:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 15Gi
  mountOptions:
  - nolock
  nfs:
    path: /kubernetes/maven/
    server: NFS_SERVER_IP
  persistentVolumeReclaimPolicy: Recycle

2) create a Kubernetes PersistentVolumeClaim : 2)创建一个Kubernetes PersistentVolumeClaim:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gitlabrunner-claim
  namespace: gitlab
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 15Gi
  volumeName: gitlabrunner-nfs-volume
status:
  accessModes:
  - ReadWriteMany
  capacity:
    storage: 15Gi

3) Refer the PersistentVolumeClaim in your config.toml : 3)在您的config.toml中参考PersistentVolumeClaim:

   [[runners.kubernetes.volumes.pvc]]
     mount_path = "/cache/maven.repository"
     name = "gitlabrunner-claim"

This enables to mount the volume each time a container is launched with this configuration. 这使得每次使用此配置启动容器时都可以安装卷。

4) in .gitlab-ci.yml file, set the MVN_OPTS like answered by @thomas : 4)在.gitlab-ci.yml文件中,设置MVN_OPTS就像@thomas回答的那样:

variables:
  MVN_OPTS: "-Dmaven.repo.local=/cache/maven.repository"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM