https://github.com/zq2599/blog_demoshtml
内容:全部原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;git
本文目标是为K8S环境的Gitlab Runner准备好分布式缓存,并在pipeline脚本中使用该缓存,所以,在阅读本文前建议您对GitLab CI有必定了解,最好是阅读过甚至编写过pipeline脚本;程序员
以下图所示,开发者将代码提交到GitLab后,能够触发CI脚本在GitLab Runner上执行,经过编写CI脚本咱们能够完成不少使用的功能:编译、构建、生成docker镜像、推送到私有仓库等:github
[[runners]] limit = 10 executor = "docker+machine" [runners.cache] Type = "s3" Path = "path/to/prefix" Shared = false [runners.cache.s3] ServerAddress = "s3.example.com" AccessKey = "access-key" SecretKey = "secret-key" BucketName = "runner" Insecure = false
本次实战涉及到多个服务,下面给出它们的版本信息供您参考:docker
mkdir -p /var/services/homes/zq2599/minio/gitlab_runner \ && chmod -R 777 /var/services/homes/zq2599/minio/gitlab_runner \ && mkdir -p /var/services/homes/zq2599/minio/config \ && chmod -R 777 /var/services/homes/zq2599/minio/config
sudo docker run -p 9000:9000 --name minio \ -d --restart=always \ -e "MINIO_ACCESS_KEY=access" \ -e "MINIO_SECRET_KEY=secret123456" \ -v /var/services/homes/zq2599/minio/gitlab_runner:/gitlab_runner \ -v /var/services/homes/zq2599/minio/config:/root/.minio \ minio/minio server /gitlab_runner
3. 打开values.yaml,找到cache的配置,当前cache的配置以下图,可见值为空内容的大括号,其他信息所有被注释了:shell
4. 修改后的cache配置以下图,红框1中原先的大括号已去掉,红框2中的是去掉了注释符号,内容不变,红框3中填写的是minio的访问地址,红框4中的是去掉了注释符号,内容不变:数据库
5. 上图红框4中的s3CacheInsecure参数等于false表示对minio的请求为http(若是是true就是https),但实际证实,当前版本的chart中该配置是无效的,等到运行时仍是会以https协议访问,解决此问题的方法是修改templates目录下的_cache.tpl文件,打开此文件,找到下图红框中的内容:浏览器
6. 将上图红框中的内容替换成下面红框中的样子,即删除原先的if判断和对应的end这两行,直接给CACHE_S3_INSECURE赋值:缓存
7. 以上只是cache相关的配置,helm部署GitLab Runner的其余设置还请自行处理,全部设置完成后回到values.yam所在目录,执行如下命令便可建立GitLab Runner:服务器
helm install \ --name-template gitlab-runner \ -f values.yaml . \ --namespace gitlab-runner
# 设置执行镜像 image: busybox:latest # 整个pipeline有两个stage stages: - build - test # 定义全局缓存,缓存的key来自分支信息,缓存位置是vendor文件夹 cache: key: ${CI_COMMIT_REF_SLUG} paths: - vendor/ before_script: - echo "Before script section" after_script: - echo "After script section" build1: stage: build tags: - k8s script: - echo "将内容写入缓存" - echo "build" > vendor/hello.txt test1: stage: test script: - echo "从缓存读取内容" - cat vendor/hello.txt
4. 点开build1的图标,可见此job的输出信息:
5. 点开test1的图标,可见对应的控制台输出,上一个job写入的数据被成功读取:
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界...
https://github.com/zq2599/blog_demos