如何使用 Kubernetes 运行器在 Gitlab 中为 Maven 添加持久卷 [英] how to add persistent volume for Maven in Gitlab with Kubernetes runner

查看:12
本文介绍了如何使用 Kubernetes 运行器在 Gitlab 中为 Maven 添加持久卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

  • 服务器 A:我们在容器中运行 Gitlab.
  • 服务器 B:我们有 Kubernetes.

Gitlab 使用 Kubernetes 运行器.然后我们的一些项目使用带有 Git 和 Maven 的 docker 容器构建应用程序.

Gitlab uses Kubernetes runner. Some of our projects then build applications using docker container with Git and Maven.

Maven 总是需要将各种东西下载到它的/root/.m2 缓存中.我需要做的是创建这些作业可以使用的持久卷,因此一旦下载它,就不必在每次有人想要构建或测试某些东西时再次这样做.这些容器总是使用一个预制映像重新构建.

Maven always has to download all kinds of things into it's /root/.m2 cache. 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.

非常基本的东西,除了我对 Gitlab 和 Kubernetes 完全陌生.

Pretty basic stuff except I am absolutely new to Gitlab and Kubernetes.

我需要在哪里创建卷?我尝试在运行程序中更改 config.toml 以包含 host_path 类型的卷,但我不知道我是否成功,Maven 每次都必须下载所有要求.我什至不知道是否必须重新启动运行器容器才能应用更改,以及如何应用.这是跑步者的 config.toml :

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. 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 :

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

我不知道自己缺少什么.也许我必须在这些项目中的 .gitlab-ci.yml 中定义一些东西,或者其他东西.我查看了教程,尝试了 Gitlab 帮助页面,但仍然找不到有效的解决方案.

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. I have looked into tutorials, I have tried Gitlab help pages, but I still can't find a working solution.

运行 GitLab 社区版 11.6.5.

Running GitLab Community Edition 11.6.5.

推荐答案

1)创建一个Kubernetes PersistentVolume(我使用NFS作为PersistentVolume类型):

1) Create a Kubernetes PersistentVolume (I use NFS as PersistentVolume type) :

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) 创建一个 Kubernetes PersistentVolumeClaim :

2) create a 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) 在您的 config.toml 中引用 PersistentVolumeClaim :

3) Refer the PersistentVolumeClaim in your config.toml :

   [[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) 在 .gitlab-ci.yml 文件中,设置 MVN_OPTS 就像 @thomas 回答的那样:

4) in .gitlab-ci.yml file, set the MVN_OPTS like answered by @thomas :

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

这篇关于如何使用 Kubernetes 运行器在 Gitlab 中为 Maven 添加持久卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆