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

查看:138
本文介绍了如何使用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天全站免登陆