CICD联动阿里云容器服务Kubernetes实践之GitLab CI篇(一)

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: 本文主要演示如何在阿里云Kubernetes容器服务中安装、注册GitLab Runner并添加kubernetes类型的executor来执行构建,并以此为基础完成一个java源码示例项目从编译构建、镜像打包到应用部署的CICD过程。

本文档以构建一个 Java 软件项目并部署到 阿里云容器服务的Kubernetes集群 为例说明如何使用 GitLab CI在阿里云Kubernetes服务上运行GitLab-Runner、配置kubernetes类型的executor并执行Pipeline。

1. 创建GitLab源码项目并上传示例代码

1.1 创建GitLab源码项目

本示例中创建的GitLab源码项目地址为:

http://xx.xx.xx.xx/demo/gitlab-java-demo.git

1.2 获取示例代码并上传至GitLab

$ git clone https://code.aliyun.com/CodePipeline/gitlabci-java-demo.git
$ git remote add gitlab http://xx.xx.xx.xx/demo/gitlab-java-demo.git
$ git push gitlab master

2. 在Kubernetes集群中安装GitLab-Runner

2.1 获取GitLab Runner的注册信息

2.1.1 项目专用Runner

Project --> Settings --> CI/CD --> Runners, 获取URL和registration token信息,如图:

image

image

2.1.2 Group Runners

Group --> Settings --> CI/CD --> Runners, 获取URL和registration token信息,如图:

image

2.1.3 Shared Runners

只能管理员创建, 进入admin/runners 页面获取url和token

image

2.2 获取并修改GitLab Runner的Helm Chart

$ git clone https://github.com/haoshuwei/ack-gitlab-runner.git

修改values.yaml文件, 示例如下:

## GitLab Runner Image
##
image: gitlab/gitlab-runner:alpine-v12.1.0

## Specify a imagePullPolicy
##
imagePullPolicy: IfNotPresent

## Default container image to use for initcontainer
init:
  image: busybox
  tag: latest

## The GitLab Server URL (with protocol) that want to register the runner against
##
gitlabUrl: http://xx.xx.xx.xx/

## The Registration Token for adding new Runners to the GitLab Server. This must
## be retreived from your GitLab Instance.
##
runnerRegistrationToken: "AMvEWrBTBu-d8czEYyfY"
## Unregister all runners before termination
##
unregisterRunners: true
  
## Configure the maximum number of concurrent jobs
##
concurrent: 10
  
## Defines in seconds how often to check GitLab for a new builds
##
checkInterval: 30
  
## For RBAC support:
##
rbac:
  create: true
  clusterWideAccess: false
    
## Configure integrated Prometheus metrics exporter
##
metrics:
  enabled: true
    
## Configuration for the Pods that that the runner launches for each new job
##
runners:
  ## Default container image to use for builds when none is specified
  ##
  image: ubuntu:16.04
  
  ## Specify the tags associated with the runner. Comma-separated list of tags.
  ##
  tags: "k8s-runner"
    
  ## Run all containers with the privileged flag enabled
  ## This will allow the docker:dind image to run if you need to run Docker
  ## commands. Please read the docs before turning this on:
  ##
  privileged: true
    
  ## Namespace to run Kubernetes jobs in (defaults to the same namespace of this release)
  ##
  namespace: gitlab
    
  cachePath: "/opt/cache"
    
  cache: {}
  builds: {}
  services: {}
  helpers: {}
  
resources: {}

2.3 安装GitLab Runner

$ helm package .
Successfully packaged chart and saved it to: /root/ack-gitlab-runner/ack-gitlab-runner-0.1.37.tgz
  
$ helm install --namespace gitlab --name gitlab-runner *.tgz

查看相关的deployment/pod启动是否成功,若成功启动, 则在GitLab上可以看到注册成功的GitLab Runner, 如图:

image

3. 缓存cache的配置

GitLab Runner中对缓存方案的支持有限,所以我们使用挂载volume的方式做缓存。在上面的示例中, 我们在安装GitLab Runner时默认使用/opt/cache目录作为缓存空间(可以修改values.yaml文件中的runners.cachePath字段)

比如建立maven缓存, 可以在variables下添加MAVEN_OPTS变量并指定本地缓存目录:

variables:
  KUBECONFIG: /etc/deploy/config
  MAVEN_OPTS: "-Dmaven.repo.local=/opt/cache/.m2/repository"

如果需要挂载新的volume, 则可以修改templates/configmap.yaml文件中的如下字段:

cat >>/home/gitlab-runner/.gitlab-runner/config.toml <<EOF
            [[runners.kubernetes.volumes.pvc]]
              name = "gitlab-runner-cache"
              mount_path = "{{ .Values.runners.cachePath }}"
EOF

即在GitLab Runner进行register之后, 在run之前, 修改config.toml的配置。

4. 全局变量的设置

Project --> Settings --> CI/CD --> Variables, 添加GitLab Runner可用的环境变量,本示例添加3个变量如下:

  • REGISTRY_USERNAME 镜像仓库用户名
  • REGISTRY_PASSWORD 镜像仓库密码
  • kube_config kubeconfig的编码字符串

kubeconfig的编码字符串的生成:

echo $(cat ~/.kube/config | base64) | tr -d " "

5. 编写.gitlab-ci.yml

编写.gitlab-ci.yml文件完成java demo源码项目的编译构建、镜像推送和应用部署。(可参考gitlabci-java-demo源码项目中的.gitlab-ci.yml.example)

.gitlab-ci.yml示例:

image: docker:stable
stages:
  - package
  - docker_build
  - deploy_k8s
variables:
  KUBECONFIG: /etc/deploy/config
mvn_build_job:
  image: registry.cn-beijing.aliyuncs.com/codepipeline/public-blueocean-codepipeline-slave-java:0.1-63b99a20
  stage: package
  tags:
    - k8s-runner
  script:
    - mvn package -B -DskipTests
    - cp target/demo.war /opt/cache
docker_build_job:
  image: registry.cn-beijing.aliyuncs.com/codepipeline/public-blueocean-codepipeline-slave-java:0.1-63b99a20
  stage: docker_build
  tags:
    - k8s-runner
  script:
    - docker login -u $REGISTRY_USERNAME -p $REGISTRY_PASSWORD registry.cn-beijing.aliyuncs.com
    - mkdir target
    - cp /opt/cache/demo.war target/demo.war
    - docker build -t registry.cn-beijing.aliyuncs.com/gitlab-demo/java-demo:$CI_PIPELINE_ID .
    - docker push registry.cn-beijing.aliyuncs.com/gitlab-demo/java-demo:$CI_PIPELINE_ID
deploy_k8s_job:
  image: registry.cn-beijing.aliyuncs.com/codepipeline/public-blueocean-codepipeline-slave-java:0.1-63b99a20
  stage: deploy_k8s
  tags:
    - k8s-runner
  script:
    - mkdir -p /etc/deploy
    - echo $kube_config |base64 -d > $KUBECONFIG
    - sed -i "s/IMAGE_TAG/$CI_PIPELINE_ID/g" deployment.yaml
    - cat deployment.yaml
    - kubectl apply -f deployment.yaml

.gitlab-ci.yml定义了一个Pipeline, 分三个阶段步骤执行:

image: docker:stable  # Pipeline中各个步骤阶段的构建镜像没有指定时, 默认使用docker:stable镜像
stages:
  - package                # 源码打包阶段
  - docker_build         # 镜像构建和打包阶段
  - deploy_k8s           # 应用部署阶段
variables:
  KUBECONFIG: /etc/deploy/config   # 定义全局变量KUBECONFIG

maven源码打包阶段:

mvn_build_job:     # job名称
  image: registry.cn-beijing.aliyuncs.com/codepipeline/public-blueocean-codepipeline-slave-java:0.1-63b99a20  # 本阶段构建使用的构建镜像
  stage: package      # 关联的阶段名称
  tags:                     # GitLab Runner的tag
    - k8s-runner
  script:
    - mvn package -B -DskipTests  # 执行构建脚本
    - cp target/demo.war /opt/cache   # 构建物保存至缓存区

镜像构建和发布阶段:

docker_build_job:  # job名称, 已挂载docker.sock
  image: registry.cn-beijing.aliyuncs.com/codepipeline/public-blueocean-codepipeline-slave-java:0.1-63b99a20 # 本阶段构建使用的构建镜像
  tags:                      # GitLab Runner的tag
    - k8s-runner
  script:
    - docker login -u REGISTRY_USERNAME -p $REGISTRY_PASSWORD registry.cn-beijing.aliyuncs.com   # 登录镜像仓库
    - mkdir target
    - cp /opt/cache/demo.war target/demo.war
    - docker build -t registry.cn-beijing.aliyuncs.com/gitlab-demo/java-demo:$CI_PIPELINE_ID .     # 打包docker镜像,使用的tag为本次pipeline的id
    - docker push registry.cn-beijing.aliyuncs.com/gitlab-demo/java-demo:$CI_PIPELINE_ID      # 推送docker镜像

应用部署阶段:

deploy_k8s_job:   # job名称
  image: registry.cn-beijing.aliyuncs.com/codepipeline/public-blueocean-codepipeline-slave-java:0.1-63b99a20   # 本阶段构建使用的构建镜像
  stage: deploy_k8s   # 关联的阶段名称
  tags:                      # GitLab Runner的tag
    - k8s-runner
  script:
    - mkdir -p /etc/deploy
    - echo $kube_config |base64 -d > $KUBECONFIG   # 配置连接Kubernetes集群的config文件
    - sed -i "s/IMAGE_TAG/$CI_PIPELINE_ID/g" deployment.yaml  # 动态替换部署文件中的镜像tag
    - kubectl apply -f deployment.yaml

6. 执行pipeline

提交.gitlab-ci.yml文件后,Project gitlab-java-demo会自动检测到这个文件并自行pipeline, 如图:
image

image

7. 访问服务

如果部署文件中没有指定namespace, 则默认会部署到gitlab命名空间下:

$ kubectl -n gitlab get svc 
NAME        TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
java-demo   LoadBalancer   172.19.9.252   xx.xx.xx.xx   80:32349/TCP   1m

浏览器访问 xx.xx.xx.xx/demo 进行验证

了解更多阿里云容器服务内容,请访问 https://www.aliyun.com/product/containerservice
了解更多GitLab CI的内容,请访问 https://docs.gitlab.com/runner/

相关实践学习
Docker镜像管理快速入门
本教程将介绍如何使用Docker构建镜像,并通过阿里云镜像服务分发到ECS服务器,运行该镜像。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
22天前
|
Kubernetes 容器
k8s容器时间与服务器时间不一致问题
k8s容器时间与服务器时间不一致问题
18 0
|
11天前
|
JSON Kubernetes Go
无缝集成:在IntelliJ IDEA中利用Kubernetes插件轻松管理容器化应用
无缝集成:在IntelliJ IDEA中利用Kubernetes插件轻松管理容器化应用
21 0
无缝集成:在IntelliJ IDEA中利用Kubernetes插件轻松管理容器化应用
|
22天前
|
消息中间件 Kubernetes Kafka
Terraform阿里云创建资源1分钟创建集群一键发布应用Terraform 创建 Kubernetes 集群
Terraform阿里云创建资源1分钟创建集群一键发布应用Terraform 创建 Kubernetes 集群
15 0
|
25天前
|
Kubernetes API 调度
总结归纳Kubernetes | 一站式速查知识,助您轻松驾驭容器编排技术(水平扩展控制)
总结归纳Kubernetes | 一站式速查知识,助您轻松驾驭容器编排技术(水平扩展控制)
48 0
|
1月前
|
Kubernetes SDN 微服务
微服务与 Kubernetes 容器云的边界
【2月更文挑战第30天】该文探讨了微服务与Kubernetes集群的关系,主要关注是否应跨多集群部署。理想的状况是每个微服务对应一个Kubernetes集群,配置和注册中心在同一集群内,以减少网络延迟。
|
1月前
|
存储 Kubernetes 监控
容器服务ACK常见问题之容器服务ACK启动时readiness告警如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
1月前
|
存储 监控 Kubernetes
容器服务ACK常见问题之cmonitor-agent容器一直没起来如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
29天前
|
Ubuntu JavaScript 关系型数据库
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
在阿里云Ubuntu 20.04服务器上部署Ghost博客的步骤包括创建新用户、安装Nginx、MySQL和Node.js 18.x。首先,通过`adduser`命令创建非root用户,然后安装Nginx和MySQL。接着,设置Node.js环境,下载Nodesource GPG密钥并安装Node.js 18.x。之后,使用`npm`安装Ghost-CLI,创建Ghost安装目录并进行安装。配置过程中需提供博客URL、数据库连接信息等。最后,测试访问前台首页和后台管理页面。确保DNS设置正确,并根据提示完成Ghost博客的配置。
在阿里云Ubuntu 20.04服务器中搭建一个 Ghost 博客
|
1月前
|
存储 分布式计算 网络协议
阿里云服务器内存型r7、r8a、r8y实例区别参考
在阿里云目前的活动中,属于内存型实例规格的云服务器有内存型r7、内存型r8a、内存型r8y这几个实例规格,相比于活动内的经济型e、通用算力型u1实例来说,这些实例规格等性能更强,与计算型和通用型相比,它的内存更大,因此这些内存型实例规格主要适用于数据库、中间件和数据分析与挖掘,Hadoop、Spark集群等场景,本文为大家介绍内存型r7、r8a、r8y实例区别及最新活动价格,以供参考。
阿里云服务器内存型r7、r8a、r8y实例区别参考
|
1月前
|
SQL 弹性计算 安全
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程
当我们通过阿里云的活动购买完云服务器之后,并不是立马就能使用了,还需要我们设置云服务器密码,配置安全组等基本操作之后才能使用,有的用户还需要购买并挂载数据盘到云服务器上,很多新手用户由于是初次使用阿里云服务器,因此并不知道这些设置的操作流程,下面给大家介绍下这些设置的具体操作流程。
购买阿里云活动内云服务器之后设置密码、安全组、增加带宽、挂载云盘教程

相关产品

  • 容器计算服务
  • 容器服务Kubernetes版