七、gitlab-runner cache s3 缓存存储实战
部署gitlab-runner
下载地址https://gitee.com/stud_1/devops-learning/raw/master/gitlab-runner-0.36.1.tgz
需要基础环境
部署方法自行搜索安装
minion 缓存
gitlab 代码仓库
harbor 镜像仓库
sonarqube 代码检测
开整
1、解压gitlab-runne
[root@master devop]# tar -zxf gitlab-runner-0.36.1.tgz [root@master devop]# ls gitlab-runner gitlab-runner-0.36.1.tgz [root@master devop]# cd gitlab-runner/ [root@master gitlab-runner]# ls CHANGELOG.md Chart.yaml CONTRIBUTING.md LICENSE Makefile NOTICE README.md templates values.yaml
2、# 创建 访问凭据
这是一个Kubernetes命令,用于创建一个名为s3access的Secret,其中包含了访问S3服务器所需的凭据。以下是对这个命令的详解:
kubectl:Kubernetes命令行工具,用于与Kubernetes集群通信和管理。
create secret:创建一个新的Secret对象。
generic:指定要创建的Secret类型。在这里,我们创建一个通用的Secret,可以包含任意类型的数据。
--namespace=kube-ops:指定要在哪个Kubernetes命名空间中创建Secret。在这里,我们将Secret创建在名为kube-ops的命名空间中。
--from-literal=accesskey="ZfQyGWLglusTUQ1N":从字面值创建一个名为accesskey的键值对,值为ZfQyGWLglusTUQ1N。
--from-literal=secretkey="NRhzRfrrxJvhq5bmhKofVmGHa80I0wtY":从字面值创建一个名为secretkey的键值对,值为NRhzRfrrxJvhq5bmhKofVmGHa80I0wtY。
这个命令将在kube-ops命名空间中创建一个名为s3access的Secret,其中包含了两个键值对:accesskey和secretkey。这些凭据可以用于访问S3服务器。在使用这个Secret之前,请确保您已经将其与需要访问S3服务器的Pod或Deployment关联起来。
kubectl create secret generic s3access --namespace=kube-ops --from-literal=accesskey="ZfQyGWLglusTUQ1N" --from-literal=secretkey="NRhzRfrrxJvhq5bmhKofVmGHa80I0wtY"
3、修改 values.yaml 配置文件

1 ## GitLab Runner 镜像 2 ## 3 ## 默认使用 gitlab/gitlab-runner:alpine-v{VERSION} 4 ## 其中 {VERSION} 从 Chart.yaml 的 appVersion 字段中获取 5 ## 6 ## 注意:如果将镜像更改为 ubuntu 版本, 7 ## 请勿忘记更改 securityContext; 8 ## 这些镜像在不同的用户ID上运行。 9 ## 10 # image: gitlab/gitlab-runner:alpine-v11.6.0 11 12 ## 为主运行器部署指定镜像拉取策略 13 ## 如果 imageTag 是 'latest',则设为 'Always',否则设为 'IfNotPresent' 14 ## 15 ## 注意:它不适用于由此执行器启动的工作容器。 16 ## 使用 [runners.kubernetes] 中的 `pull_policy` 进行更改。 17 ## 18 ## 参考: https://kubernetes.io/docs/concepts/containers/images/#pre-pulled-images 19 ## 20 imagePullPolicy: IfNotPresent 21 22 ## 在 Pod 上指定 ImagePullSecrets 23 ## Kubernetes 支持在 Pod 上指定容器镜像注册表密钥。 24 ## 参考: https://kubernetes.io/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod 25 ## 26 # imagePullSecrets: 27 # - name: "image-pull-secret" 28 29 ## 运行器容器的存活性和就绪性探针超时(秒) 30 # probeTimeoutSeconds: 1 31 32 ## 要启动的执行器 Pod 的数量。 33 ## 34 ## 注意:使用 runnerToken 无法支持多个副本。请使用 runnerRegistrationToken 来创建多个运行器副本。 35 # replicas: 1 36 37 ## 要保留的此 Deployment 的旧 ReplicaSets 的数量。 38 # revisionHistoryLimit: 10 39 40 ## 要注册运行器的 GitLab Server URL(包括协议)。必须从您的 GitLab 实例中获取。 41 ## 参考: https://docs.gitlab.com/runner/commands/README.html#gitlab-runner-register 42 ## 43 # gitlabUrl: http://gitlab.your-domain.com/ 44 45 ## 要添加新运行器到 GitLab Server 的注册令牌。必须从您的 GitLab 实例中获取。 46 ## 参考: https://docs.gitlab.com/ce/ci/runners/README.html 47 ## 48 # runnerRegistrationToken: "" 49 50 ## GitLab Runner 用于向 GitLab Server 添加新 Runner 的 Token。 51 ## 必须从您的 GitLab 实例中检索它。这是已注册 runner 的 token。 52 ## 参考: (我们还没有相关文档,但我们想使用现有的 token) 53 ## 54 # runnerToken: "" 55 # 56 ## 在终止之前注销所有 runners 57 ## 58 ## 更新 runner 的图表版本或配置将导致 runner 容器 59 ## 被终止并重新创建。这可能会导致您的 GitLab 实例引用 60 ## 不存在的 runners。在终止前注销 runner 可以缓解这个问题。 61 ## 参考: https://docs.gitlab.com/runner/commands/README.html#gitlab-runner-unregister 62 ## 63 # unregisterRunners: true 64 65 ## 当停止 runner 时,给它时间等待其作业终止。 66 ## 67 ## 更新 runner 的图表版本或配置将导致 runner 容器 68 ## 收到一个优雅的停止请求而终止。terminationGracePeriodSeconds 69 ## 指示 Kubernetes 等待足够长的时间,以便 runner pod 优雅地终止。 70 ## 参考: https://docs.gitlab.com/runner/commands/#signals 71 terminationGracePeriodSeconds: 3600 72 73 ## 设置 certsSecretName 以便为 GitLab Runner 传递自定义证书来使用 74 ## 在同一命名空间中为 Kubernetes Secret Object 提供资源名称, 75 ## 这用于填充 /home/gitlab-runner/.gitlab-runner/certs/ 目录 76 ## 参考: https://docs.gitlab.com/runner/configuration/tls-self-signed.html#supported-options-for-self-signed-certificates 77 ## 78 # certsSecretName: 79 80 ## 配置最大并发作业数 81 ## 参考: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section 82 ## 83 concurrent: 10 84 85 ## 定义 GitLab 检查新构建的频率(以秒为单位) 86 ## 参考: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section 87 ## 88 checkInterval: 30 89 90 # 配置GitLab Runner的日志级别。可选值为:debug、info、warn、error、fatal、panic 91 # ref: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section 92 # 93 logLevel: debug # 设置日志级别为debug,用于调试 94 95 # 配置GitLab Runner的日志格式。可选值为:runner、text、json 96 # ref: https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section 97 # 98 logFormat: text # 设置日志格式为text 99 100 # 配置GitLab Runner的Sentry DSN。用于错误跟踪和实时监控 101 # ref https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-global-section 102 # 103 sentryDsn: 'your_sentry_dsn' # 设置Sentry DSN,用于错误跟踪和实时监控 104 105 # 自定义bash脚本,在调用gitlab-runner进程之前执行 106 preEntrypointScript: | # 定义预入口点脚本 107 echo "hello" # 输出“hello” 108 109 # 对于RBAC支持: 110 rbac: # 定义RBAC配置项 111 create: false # 是否创建RBAC角色,这里设置为false表示不创建 112 113 ## Define specific rbac permissions. 114 ## DEPRECATED: see .Values.rbac.rules 115 # resources: ["pods", "pods/exec", "secrets"] # 定义资源列表,如Pods、Pods/exec和Secrets等 116 # verbs: ["get", "list", "watch", "create", "patch", "delete"] # 定义对这些资源的操作,如get、list、watch等 117 118 ## Define list of rules to be added to the rbac role permissions. 119 ## Each rule supports the keys: 120 ## - apiGroups: default "" (indicates the core API group) if missing or empty. 121 ## - resources: default "*" if missing or empty. 122 ## - verbs: default "*" if missing or empty. 123 rules: [] # 定义RBAC规则列表,这里为空表示没有定义规则 124 # - resources: ["pods", "secrets"] # 定义资源列表,如Pods和Secrets等 125 # verbs: ["get", "list", "watch", "create", "patch", "delete"] # 定义对这些资源的操作,如get、list等 126 # - apiGroups: [""] # 定义API组,这里为空表示使用核心API组 127 # resources: ["pods/exec"] # 定义资源列表,如Pods/exec等 128 # verbs: ["create", "patch", "delete"] # 定义对这些资源的操作,如create、patch等 129 --- 130 ## 是否以集群范围访问 GitLab。 131 clusterWideAccess: false 132 133 ## 如果 Helm 图表中的 RBAC 被禁用(参见 rbac.create),则使用以下 Kubernetes Service Account 名称。 134 # serviceAccountName: default 135 136 ## 指定 Service Account 的注解,这对于注解如 eks.amazonaws.com/role-arn 等很有用。 137 ## 参考: https://docs.aws.amazon.com/eks/latest/userguide/specify-service-account-role.html 138 # serviceAccountAnnotations: {} 139 140 ## 是否使用 Pod 安全策略。 141 ## 参考: https://kubernetes.io/docs/concepts/policy/pod-security-policy/ 142 podSecurityPolicy: 143 enabled: false 144 resourceNames: 145 - gitlab-runner 146 147 ## 指定一个或多个用于拉取 runner 镜像的 imagePullSecrets。 148 ## 参考: https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#add-imagepullsecrets-to-a-service-account 149 # imagePullSecrets: [] 150 151 ## 配置集成的 Prometheus 指标导出器。 152 ## 参考: https://docs.gitlab.com/runner/monitoring/#configuration-of-the-metrics-http-server 153 # metrics: {} 154 --- 155 metrics: 156 enabled: true # 启用指标功能 157 158 portName: metrics # 定义指标的端口名称 159 160 port: 9252 # 定义Prometheus指标导出器的端口号 161 162 serviceMonitor: 163 enabled: false # 禁用服务监控功能 164 165 # 提供给服务监控资源的额外标签 166 # labels: {} 167 168 # 定义抓取间隔(否则将使用Prometheus的默认值) 169 # ref: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#scrape_config 170 # interval: "" 171 172 # 指定抓取协议方案,例如 https 或 http 173 # scheme: "http" 174 175 # 为服务监控提供TLS配置 176 # ref: https://github.com/helm/charts/blob/master/stable/prometheus-operator/crds/crd-servicemonitor.yaml 177 # tlsConfig: {} 178 179 # Prometheus可以从这里抓取指标的URI路径 180 # path: "/metrics" 181 182 # 在摄入样本之前应用于样本的MetricRelabelConfigs列表 183 # ref: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#metric_relabel_configs 184 # metricRelabelings: [] 185 186 # 在抓取之前应用于样本的RelabelConfigs列表 187 # ref: https://prometheus.io/docs/prometheus/latest/configuration/configuration/#relabel_config 188 # relabelings: [] 189 --- 190 ## 是否启用服务资源,例如通过prometheus-operator serviceMonitor进行指标抓取 191 service: 192 enabled: false 193 194 ## 提供服务的额外标签 195 ## 196 # 标签: {} 197 198 ## 提供服务的额外注释 199 ## 200 # 注释: {} 201 202 ## 定义特定的ClusterIP(如果不需要动态IP) 203 ## 204 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#choosing-your-own-ip-address 205 ## 206 # clusterIP: "" 207 208 ## 定义此服务的外部IP列表 209 ## 210 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#external-ips 211 ## 212 # externalIPs: [] 213 214 ## 提供特定的负载均衡器IP(例如外部负载均衡器的IP) 215 ## 216 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer 217 ## 218 # loadBalancerIP: "" 219 220 ## 提供可以访问此服务的源IP范围列表 221 ## 222 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#aws-nlb-support 223 ## 224 # loadBalancerSourceRanges: [] 225 226 ## 指定服务类型(例如ClusterIP、NodePort、Loadbalancer或ExternalName) 227 ## 228 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types 229 ## 230 type: ClusterIP 231 232 ## 如果服务类型为NodePort,指定服务的度量标准节点端口。 233 ## 234 # metrics: 235 ## 指定在哪个节点端口下,runner的prometheus度量标准可用。 236 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#nodeport 237 ## 节点端口: "" 238 239 ## 提供此服务要暴露的附加端口列表。 240 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service 241 # additionalPorts: [] 242 243 ## 是否启用服务资源,以允许通过prometheus-operator的serviceMonitor进行指标抓取 244 service: 245 enabled: false # 启用或禁用服务 246 247 ## 为服务提供额外的标签 248 ## 249 # labels: {} # 标签配置,用于标识服务 250 251 ## 为服务提供额外的注解 252 ## 253 # annotations: {} # 注解配置,用于提供更多关于服务的元数据 254 255 ## 定义特定的ClusterIP,如果不希望动态分配 256 ## 257 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#choosing-your-own-ip-address 258 ## 259 # clusterIP: "" # 集群内服务的IP地址 260 261 ## 定义此服务的外部IP地址列表 262 ## 263 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#external-ips 264 ## 265 # externalIPs: [] # 外部IP地址列表 266 267 ## 提供特定的负载均衡器IP,例如外部负载均衡器的IP 268 ## 269 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer 270 ## 271 # loadBalancerIP: "" # 负载均衡器的IP地址 272 273 ## 提供可访问此服务的源IP地址范围列表 274 ## 275 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#aws-nlb-support 276 ## 277 # loadBalancerSourceRanges: [] # 源IP地址范围列表,用于限制访问此服务的IP地址范围 278 279 ## 指定服务类型,例如ClusterIP、NodePort、Loadbalancer或ExternalName 280 ## 281 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types 282 ## 283 type: ClusterIP # 服务类型,如集群内服务、节点端口、负载均衡器或外部名称服务 284 285 ## 如果使用NodePort类型服务,指定服务的指标节点端口号```yaml 286 --- 287 # metrics: 288 289 ## 指定运行器Prometheus指标的节点端口,以便在以下端口中提供服务。 290 ## 291 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#nodeport 292 ## 293 # nodePort: "" # 节点端口,用于运行器的Prometheus指标 294 295 ## 提供此服务要暴露的其他端口列表 296 ## 297 ## 参考:https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service 298 ## 299 # additionalPorts: [] # 其他端口列表,由该服务暴露 300 301 ## 配置用于每个新作业的由运行器启动的Pods 302 ## 303 runners: 304 # runner配置,其中多行字符串被评估为模板,因此您可以在其中指定Helm值。 305 # 306 # tpl: https://helm.sh/docs/howto/charts_tips_and_tricks/#using-the-tpl-function 307 # runner配置:https://docs.gitlab.com/runner/configuration/advanced-configuration.html 308 config: | 309 [[runners]] 310 [runners.kubernetes] 311 namespace = "{{.Release.Namespace}}" # Kubernetes命名空间,用于运行器Pods的部署 312 image = "ubuntu:16.04" # 默认容器镜像,用于构建作业时未指定的情况 313 314 ## 使用的执行器类型 315 ## 316 # executor: kubernetes # 执行器类型,例如kubernetes 317 318 ## 默认使用的容器镜像(当未指定时) 319 ## 320 ## 已弃用:请参阅https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 321 # image: ubuntu:16.04 # 默认容器镜像,用于构建作业时未指定的情况 322 323 ## 指定一个或多个imagePullSecrets 324 ## 325 ## 参考:https://kubernetes.io/docs/tasks/configure-pod-container/pull-image-private-registry/ 326 ## 已弃用:请参阅https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 327 # imagePullSecrets: [] # 用于拉取私有镜像的凭据列表 328 329 ## 指定镜像拉取策略:never、if-not-present、always。如果不设置,将使用集群默认设置。 330 ## 331 ## 已弃用:请参阅https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 332 # imagePullPolicy: "" # 镜像拉取策略,例如始终、仅在不存在时、从不拉取镜像。 333 # 定义从GitLab接收新作业的并发请求数 334 # 参考:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runners-section 335 # 已弃用:参见 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 336 # 请求并发数:1 337 requestConcurrency: 1 338 339 # 指定运行器是否锁定到特定项目:true或false。默认为true。 340 # 是否锁定到特定项目:true 341 locked: true 342 343 # 指定与运行器关联的标签。逗号分隔的标签列表。 344 # 345 # 参考:https://docs.gitlab.com/ce/ci/runners/#use-tags-to-limit-the-number-of-jobs-using-the-runner 346 # 标签:"" 347 tags: "" 348 349 # 指定运行器的名称。 350 name: "" 351 352 # 指定是否应运行没有标签的作业。 353 # 如果未指定,则根据是否指定了标签,Runner将默认为true或false。在其他情况下,它将默认为false。 354 # 355 # 参考:https://docs.gitlab.com/ce/ci/runners/#runner-is-allowed-to-run-untagged-jobs 356 # 是否允许运行无标签的作业:true 357 runUntagged: true 358 359 # 指定运行器是否仅运行受保护的分支。默认为False。 360 # 361 # 参考:https://docs.gitlab.com/ee/ci/runners/#prevent-runners-from-revealing-sensitive-information 362 # 是否仅运行受保护的分支:true 363 protected: true 364 365 # 在所有容器上运行特权标志以启用Docker命令。请在阅读文档之前将其打开: 366 # 参考:https://docs.gitlab.com/runner/executors/kubernetes.html#using-dockerdind 367 # 已弃用:参见 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 368 # 是否以特权模式运行:false 369 privileged: false 370 371 # 包含runner-token和runner-registration-token的秘钥名称。 372 secret: gitlab-runner 373 374 ## 运行Kubernetes作业的命名空间(默认为此版本的同一命名空间) 375 ## 376 ## 已弃用:请参见 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 377 # namespace: 378 379 ## 容器创建后,Runner尝试连接到容器的超时时间(以秒为单位) 380 ## 参考:https://docs.gitlab.com/runner/executors/kubernetes.html 381 ## 已弃用:请参见 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 382 # pollTimeout: 180 383 384 ## 设置最大构建日志大小(以千字节为单位),默认为4096(4MB) 385 ## 参考:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runners-section 386 ## 已弃用:请参见 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 387 # outputLimit: 4096 388 389 ## 分布式Runner的缓存 390 ## 参考:https://docs.gitlab.com/runner/configuration/autoscale.html#distributed-runners-caching 391 ## 392 ## 如果您想使用基于S3的分布式缓存: 393 ## 首先,您需要取消注释General和S3设置部分。 394 ## 395 ## 创建一个名为's3access'的秘密,其中包含'accesskey'和'secretkey'。 396 ## 参考:https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/ 397 ## 398 ## $ kubectl create secret generic s3access \ 399 ## --from-literal=accesskey="YourAccessKey" \ 400 ## --from-literal=secretkey="YourSecretKey" 401 ## 参考:https://kubernetes.io/docs/concepts/configuration/secret/ 402 ## 403 ## 如果您想使用基于GCS的分布式缓存: 404 ## 首先,您需要取消注释General和GCS设置部分。 405 406 ## 使用凭据文件进行访问: 407 ## 创建一个名为'google-application-credentials'的秘密,其中包含您的应用程序凭据文件。 408 ## 参考:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnerscachegcs-section 409 ## 您可以通过以下命令进行配置: 410 ## $ kubectl create secret generic google-application-credentials \ 411 ## --from-file=gcs-application-credentials-file=./path-to-your-google-application-credentials-file.json 412 ## 参考:https://kubernetes.io/docs/concepts/configuration/secret/ 413 ## 414 ## 使用访问ID和私钥进行访问: 415 ## 创建一个名为'gcsaccess'的秘密,其中包含'gcs-access-id'和'gcs-private-key'。 416 ## 参考:https://docs.gitlab.com/runner/configuration/advanced-configuration.html#the-runnerscachegcs-section 417 ## 您可以通过以下命令进行配置: 418 ## $ kubectl create secret generic gcsaccess \ 419 ## --from-literal=gcs-access-id="YourAccessID" \ 420 ## --from-literal=gcs-private-key="YourPrivateKey" 421 ## 参考:https://kubernetes.io/docs/concepts/configuration/secret/ 422 ## 423 ## 如果您想使用基于Azure的分布式缓存: 424 ## 首先,取消注释General设置部分。 425 ## 426 ## 创建一个名为'azureaccess'的秘密,其中包含'azure-account-name'和'azure-account-key'。 427 ## 参考:https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction 428 ## 您可以通过以下命令进行配置: 429 ## $ kubectl create secret generic azureaccess \ 430 ## --from-literal=azure-account-name="YourAccountName" \ 431 ## --from-literal=azure-account-key="YourAccountKey" 432 ## 参考:https://kubernetes.io/docs/concepts/configuration/secret/ 433 434 ## 缓存配置 435 ## 缓存对象: 空的字典表示没有缓存 436 cache: {} 437 438 ## 一般设置 439 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 和 https://docs.gitlab.com/runner/install/kubernetes.html#using-cache-with-configuration-template 440 # cacheType: s3 441 # cachePath: "gitlab_runner" 442 # cacheShared: true 443 444 ## S3 设置 445 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 和 https://docs.gitlab.com/runner/install/kubernetes.html#using-cache-with-configuration-template 446 # s3ServerAddress: s3.amazonaws.com 447 # s3BucketName: <bucket_name> 448 # s3BucketLocation: <bucket_location> 449 # s3CacheInsecure: false 450 451 ## GCS 设置 452 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 和 https://docs.gitlab.com/runner/install/kubernetes.html#using-cache-with-configuration-template 453 # gcsBucketName: <bucket_name> 454 455 ## S3 的密钥名称。 456 # secretName: s3access 457 ## 使用此行进行访问,使用 gcs-access-id 和 gcs-private-key。 458 # secretName: gcsaccess 459 ## 使用此行进行访问,使用 google-application-credentials 文件。 460 # secretName: google-application-credentials 461 ## 使用此行进行访问,使用 Azure,使用 azure-account-name 和 azure-account-key。 462 # secretName: azureaccess 463 464 465 ## 构建容器特定配置 466 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 467 builds: {} 468 # cpuLimit: 200m 469 # cpuLimitOverwriteMaxAllowed: 400m 470 # memoryLimit: 256Mi 471 # memoryLimitOverwriteMaxAllowed: 512Mi 472 # cpuRequests: 100m 473 # cpuRequestsOverwriteMaxAllowed: 200m 474 # memoryRequests: 128Mi 475 # memoryRequestsOverwriteMaxAllowed: 256Mi 476 ## 服务容器特定配置 477 ## 478 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 479 services: {} 480 # cpuLimit: 200m 481 # 限制服务容器使用的 CPU 核心数。m 表示兆核心,200m 等于 0.2 核心。 482 # memoryLimit: 256Mi 483 # 限制服务容器使用的内存量。i 表示兆字节,256Mi 等于 256 兆字节。 484 # cpuRequests: 100m 485 # 服务容器启动时请求的 CPU 核心数。m 表示兆核心,100m 等于 0.1 核心。 486 # memoryRequests: 128Mi 487 # 服务容器启动时请求的内存量。i 表示兆字节,128Mi 等于 128 兆字节。 488 489 ## 助手容器特定配置 490 ## 491 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 492 helpers: {} 493 # cpuLimit: 200m 494 # 限制助手容器使用的 CPU 核心数。m 表示兆核心,200m 等于 0.2 核心。 495 # memoryLimit: 256Mi 496 # 限制助手容器使用的内存量。i 表示兆字节,256Mi 等于 256 兆字节。 497 # cpuRequests: 100m 498 # 助手容器启动时请求的 CPU 核心数。m 表示兆核心,100m 等于 0.1 核心。 499 # memoryRequests: 128Mi 500 # 助手容器启动时请求的内存量。i 表示兆字节,128Mi 等于 128 兆字节。 501 # image: "gitlab/gitlab-runner-helper:x86_64-${CI_RUNNER_REVISION}" 502 # 使用指定的镜像作为助手容器的基础镜像。其中 `${CI_RUNNER_REVISION}` 是环境变量,用于指定 GitLab Runner 的版本。 503 504 ## 助手容器安全上下文配置 505 ## 请参阅 https://docs.gitlab.com/runner/executors/kubernetes.html#using-security-context 506 ## 507 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 508 # pod_security_context: 509 # run_as_non_root: true 510 # 运行助手容器时以非 root 用户身份运行。这可以增加安全性,防止潜在的安全风险。 511 # run_as_user: 100 512 # 运行助手容器的用户 ID。设置为非 root ID 可以增强安全性。 513 # run_as_group: 100 514 # 运行助手容器的组 ID。设置为非 root 组可以增强安全性。 515 # fs_group: 65533 516 # 文件系统组 ID。设置特定的文件系统组可以控制助手容器对主机文件系统的访问权限。 517 # supplemental_groups: [101, 102] 518 # 补充组 ID。可以增加额外的组权限来控制容器的访问权限。 519 ## 指定节点容忍性以分配 CI 作业 pod 520 ## ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/ 521 ## 522 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 523 # nodeTolerations: {} 524 525 ## 指定 CI 作业 pod 的标签 526 ## 527 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 528 # podLabels: {} 529 530 ## 指定作业 pod 的注解,可用于如 iam.amazonaws.com/role 之类的注解 531 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 532 # podAnnotations: {} 533 534 ## 配置注入到构建运行时创建的 pods 中的环境变量。这些变量作为参数传递,例如 `--env "NAME=VALUE"`,到 `gitlab-runner register` 命令。 535 ## 536 ## 注意:`envVars`(见下文)仅存在于 runner pod 中,不存在于为每个构建创建的 pods 中。 537 ## 538 ## ref: https://docs.gitlab.com/runner/commands/#gitlab-runner-register 539 ## 540 ## 已弃用: 请参阅 https://docs.gitlab.com/runner/install/kubernetes.html#additional-configuration 541 # env: 542 # NAME: VALUE 543 544 545 ## 指定用于调度 runner pods 的调度器的名称。 546 ## Kubernetes 支持多个调度器配置。 547 ## ref: https://kubernetes.io/docs/reference/scheduling 548 # schedulerName: "my-custom-scheduler" 549 550 ## 配置 securitycontext 551 ## ref: http://kubernetes.io/docs/user-guide/security-context/ 552 ## 553 securityContext: 554 runAsUser: 100 555 # runAsGroup: 65533 556 fsGroup: 65533 557 # supplementalGroups: [65533] 558 559 ## 注意:针对 ubuntu 镜像的值: 560 # runAsUser: 999 561 # fsGroup: 999 562 563 ## 配置资源请求和限制 564 ## ref: http://kubernetes.io/docs/user-guide/compute-resources/ 565 ## 566 resources: {} 567 # limits: 568 # memory: 256Mi 569 # cpu: 200m 570 # requests: 571 # memory: 128Mi 572 # cpu: 100m 573 ## 服务容器特定配置 574 ### CPU 和内存限制及请求配置已弃用,请参考 GitLab Runner 的官方文档来配置这些参数。 575 ## Pod 分配的亲和性 576 ## Ref: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#affinity-and-anti-affinity 577 ## 578 affinity: {} 579 580 ## 用于 pod 分配的节点标签 581 ## Ref: https://kubernetes.io/docs/user-guide/node-selection/ 582 ## 583 nodeSelector: {} 584 # 示例:gitlab 运行器管理器不应运行在 spot 实例上,因此应将其分配给常规工作节点。 585 # node-role.kubernetes.io/worker: "true" 586 587 ## 要容忍的节点污点的列表(需要 Kubernetes >= 1.6) 588 ## Ref: https://kubernetes.io/docs/concepts/configuration/taint-and-toleration/ 589 ## 590 tolerations: [] 591 # 示例:常规工作节点可能有一个污点,因此当您使用 nodeSelector 或 affinity 将 gitlab 运行器管理器分配给节点时,需要容忍该污点。 592 # - key: "node-role.kubernetes.io/worker" 593 # operator: "Exists" 594 595 ## 配置注册命令运行时的环境变量,从而进一步控制注册过程和 config.toml 文件 596 ## ref: `gitlab-runner register --help` 597 ## ref: https://docs.gitlab.com/runner/configuration/advanced-configuration.html 598 ## 599 # envVars: 600 # - name: RUNNER_EXECUTOR 601 # value: kubernetes 602 603 ## 将注入到 pod 的 hosts 文件中的主机和 IP 列表 604 hostAliases: [] 605 # 示例: 606 # - ip: "127.0.0.1" 607 # hostnames: 608 # - "foo.local" 609 # - "bar.local" 610 # - ip: "10.1.2.3" 611 # hostnames: 612 # - "foo.remote" 613 # - "bar.remote" 614 615 ## 添加到管理器 pod 的注解 616 ## 617 podAnnotations: {} 618 # 示例: 619 # iam.amazonaws.com/role: <my_role_arn> 620 621 ## 添加到管理器 pod 的标签 622 ## 623 podLabels: {} 624 # 示例: 625 # owner.team: <my_cool_team> 626 627 ## HPA(Horizontal Pod Autoscaler)对自定义指标的支持: 628 ## 此部分使运行器能够根据定义的自定义指标进行自动缩放。 629 ## 为了使用此功能,需要使用支持的第三方适配器实现“custom.metrics.k8s.io”来启用自定义指标 API 服务器。 630 ## 示例:https://github.com/directxman12/k8s-prometheus-adapter 631 ## 632 #hpa: {} 633 # minReplicas: 1 634 # maxReplicas: 10 635 # metrics: 636 # - type: Pods 637 # pods: 638 # metricName: gitlab_runner_jobs 639 # targetAverageValue: 400m 640 641 ## 要额外挂载到容器的秘密。 642 ## 所有秘密都通过 init-runner-secrets 卷挂载,并作为只读文件放置在 init 容器的 /init-secrets 中,并最终复制到一个内存卷 runner-secrets 中,该卷在 /secrets 中挂载。 643 secrets: [] 644 # 示例: 645 # - name: my-secret 646 # - name: myOtherSecret 647 # items: 648 # - key: key_one 649 # path: path_one 650 651 ## 要挂载到容器中的其他配置文件,存储在 `/configmaps` 中。 652 ## 653 ## 请注意,一些键被运行器保留。 654 ## 查看 https://gitlab.com/gitlab-org/charts/gitlab-runner/-/blob/main/templates/configmap.yaml 以获取当前列表。 655 configMaps: {}
如下为本人线下测试配置文件
# [root@master gitlab-runner]# grep -v "#" values.yaml-backup |sed -e '/^$/d' imagePullPolicy: IfNotPresent # 指定Docker镜像的拉取策略。在这里,我们将其设置为IfNotPresent,表示仅在本地不存在镜像时从镜像仓库拉取镜像。 gitlabUrl: http://101.43.196.155:32080/ # 指定GitLab服务器的URL。在这里,我们将其设置为http://101.43.196.155:32080/。 runnerRegistrationToken: "Woq_Drxy-SSy1kQzJBZT" #用于注册Runner的令牌。 terminationGracePeriodSeconds: 3600 # 指定Pod在终止之前应该等待的时间。在这里,我们将其设置为3600秒。 concurrent: 10 # 指定Runner可以同时处理的最大任务数。在这里,我们将其设置为10。 checkInterval: 10 # 指定Runner检查新任务的时间间隔。在这里,我们将其设置为10秒。 rbac: # 指定Role-Based Access Control(基于角色的访问控制)的配置。在这里,我们启用了RBAC,并为Runner定义了一组资源和动词。
# 总的来说,这段代码定义了Runner在Kubernetes环境中应具有的访问权限,以确保它能够正常工作并执行其任务。 create: true # create: true:这个设置表示启用RBAC
# resources::这个列表定义了Runner有权访问的Kubernetes资源类型。在这里,Runner被授权访问以下资源
# "pods":表示Runner可以与Pods交互
# "pods/exec":表示Runner可以在Pods上执行命令
# "secrets":表示Runner可以访问Secrets
# "configmaps":表示Runner可以访问ConfigMaps
# "pods/attach":表示Runner可以附加到Pods上
resources: ["pods", "pods/exec", "secrets", "configmaps", "pods/attach"]
# verbs::这个列表定义了Runner可以对上述资源执行的操作。Runner可以执行以下操作
# "get":获取资源
# "list":列出资源
# "watch":监视资源的更改
# "create":创建资源
# "patch":修改资源
# "delete":删除资源
# "update":更新资源
verbs: ["get", "list", "watch", "create", "patch", "delete", "update"] rules: [] #这个部分通常用于定义更详细的访问规则,但在这里它是空的,表示没有定义其他特定的访问规则 clusterWideAccess: false # 这个设置指定Runner是否具有集群范围的访问权限。在这里,它被设置为false
,表示Runner的访问权限仅限于其所在的命名空间 podSecurityPolicy: #这个部分是关于Pod安全策略的配置 enabled: false # 这个设置指定是否启用Pod安全策略。在这里,它被设置为false
,表示不启用Pod安全策略 resourceNames: # 这个列表定义了具有特定名称的资源,这些资源将受到Pod安全策略的影响 - gitlab-runner # metrics: # 指定Prometheus指标的配置。在这里,我们启用了指标,并将其监听端口设置为9252。 enabled: true # 这表示启用了Prometheus指标。当设置为true
时,相关的服务会开始收集和导出Prometheus指标 portName: metrics # 这定义了导出Prometheus指标的端口的名称,这里是“metrics” port: 9252 # 这指定了Prometheus指标监听的端口号,这里是9252 serviceMonitor: # 这部分是关于ServiceMonitor的配置,ServiceMonitor是Prometheus中用于自动发现和监控Kubernetes服务的组件 enabled: false # 这表示ServiceMonitor功能被禁用。当设置为false
时,ServiceMonitor不会自动发现和监控Kubernetes服务
# 这段代码配置了Prometheus指标的导出,并禁用了ServiceMonitor的功能
service: #指定Kubernetes Service的配置。在这里,我们禁用了Service,因为我们不需要在集群外部访问Runner。 enabled: false # 这表示禁用了Service。因为我们不需要从集群外部访问Runner,所以不需要创建Service type: ClusterIP # 这指定了Service的类型为ClusterIP,这意味着Service将在集群内部可用,而不会从集群外部公开 runners: # 这部分是关于Runner的配置, 指定Runner的配置。在这里,我们使用了一个配置模板,并为Runner定义了一些标签和其他属性。我们还启用了Runner的特权模式,并为其配置了一个S3缓存 config: | # 这里使用了一个配置模板,并定义了一些Runner的属性。具体地,Runner将运行在指定的命名空间,并使用Ubuntu 16.04镜像 [[runners]] [runners.kubernetes] namespace = "{{.Release.Namespace}}" image = "ubuntu:16.04" tags: "k8s-runner,k8s" # 为Runner定义了标签,使其更容易被识别和管理 privileged: true # 这表示Runner将以特权模式运行,可以访问主机上的所有设备和服务 cacheType: s3 # 指定了缓存类型为S3 cachePath: "bucket" # 指定了S3缓存的路径或桶名称 cacheShared: true # 这表示缓存是共享的 s3ServerAddress: 10.0.24.7:31000 # 指定了S3服务器的地址和端口 s3BucketName: gitlab # 指定了S3桶的名称 s3CacheInsecure: true # 表示不验证S3服务器的SSL证书(不安全) secretName: s3access # 指定了用于访问S3的Secret的名称 builds: {} services: {} helpers: {} securityContext: runAsUser: 100 fsGroup: 65533 resources: {} affinity: {} nodeSelector: {} tolerations: [] hostAliases: [] podAnnotations: {} podLabels: {} secrets: [] configMaps: {}
总体来说需要修改的不是很多, 这里主要说一下 我在maven 构建的时候遇到如下图 Downloaded 每次构建都很慢 ,考虑很久决定使用minion 来做缓存持久化存储
下面展示一下整体需要修改的配置
1、首先Gitlab 页面创建项目blog_nb
2、这里查看 gitlab 对应的 gitlab-runner 配置信息 下面修改value.yaml 配置文件的时候会用到
3、修改gitlabUrl 地址上图中展示的 使用此网址注册Runner 对应地址 和Token
4、修改调整 rbac权限配置
5、 s3 缓存配置
创建访问凭据
登录 minio 页面
这里注意 bew2gitlab 这个凭据的名称记住了 下面部署 gitlab-runner 配置 S3 minio 存储的时候需要用到
kubectl create secret generic bew2gitlab --namespace=gitlab-runner --from-literal=accesskey="rwp1QFpdGHQ1qJzU" --from-literal=secretkey="YuUXD84Vqv7o0GJ0scnAW0au48jTVl3G"
注意 这里上传使用的是 3100 端口 通过NodeIP+Port访问使用的是31001 端口这里不要写错了,否则无法缓存数据
[root@master gitlab-runner]# kubectl get po,svc -n minio NAME READY STATUS RESTARTS AGE pod/minio-546899bf7c-jdwlr 1/1 Running 4 (77m ago) 2d NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/minio NodePort 10.106.90.196 <none> 9000:31000/TCP,5000:31001/TCP 2d2h [root@master gitlab-runner]#
由于我的 gitlab-runner 是部署在 K8S 环境中 所以需要给 gitlab-runner 增加 docker.sock 的权限
在 ../gitlab-runner/templates/configmap.yaml 文件里增加 容器访问本地docker.sock
# add volume config cat >>/home/gitlab-runner/.gitlab-runner/config.toml <<EOF [[runners.kubernetes.volumes.host_path]] name = "docker" mount_path = "/var/run/docker.sock" EOF
接下来就是部署 gitlab-runner
[root@master gitlab-runner]# helm install new2gitlab-runner --namespace blog-nb ../gitlab-runner NAME: new2gitlab-runner LAST DEPLOYED: Thu Feb 1 17:00:08 2024 NAMESPACE: blog-nb STATUS: deployed REVISION: 1 TEST SUITE: None NOTES: Your GitLab Runner should now be registered against the GitLab instance reachable at: "http://101.43.196.155:32080/" Runner namespace "blog-nb" was found in runners.config template. [root@master gitlab-runner]# kubectl get po -n blog-nb NAME READY STATUS RESTARTS AGE new2gitlab-runner-gitlab-runner-cdfcd4d55-zbhjr 1/1 Running 0 26s
进容器看看配置文件
[root@master gitlab-runner]# kubectl exec -it -n blog-nb new2gitlab-runner-gitlab-runner-cdfcd4d55-zbhjr -- /bin/bash Defaulted container "new2gitlab-runner-gitlab-runner" out of: new2gitlab-runner-gitlab-runner, configure (init) bash-5.0$ cd /home/gitlab-runner/.gitlab-runner/ bash-5.0$ ls config.toml bash-5.0$ ls -l total 4 -rw-r--r-- 1 gitlab-r nogroup 1605 Feb 1 09:00 config.toml bash-5.0$
bash-5.0$ cat config.toml listen_address = ":9252" concurrent = 10 check_interval = 10 log_level = "info" [session_server] session_timeout = 1800 [[runners]] name = "new2gitlab-runner-gitlab-runner-cdfcd4d55-zbhjr" url = "http://101.43.196.155:32080/" token = "2cxumSzDxfvkxsn_Wo7Z" executor = "kubernetes" [runners.custom_build_dir] [runners.cache] Type = "s3" Path = "new-test" Shared = true [runners.cache.s3] ServerAddress = "10.0.24.7:31000" AccessKey = "new2gitlab" SecretKey = "aAxxx20" BucketName = "new-gitlab" Insecure = true [runners.cache.gcs] [runners.cache.azure] [runners.kubernetes] host = "" bearer_token_overwrite_allowed = false image = "ubuntu:16.04" namespace = "blog-nb" namespace_overwrite_allowed = "" privileged = true service_account_overwrite_allowed = "" pod_annotations_overwrite_allowed = "" [runners.kubernetes.affinity] [runners.kubernetes.pod_security_context] [runners.kubernetes.build_container_security_context] [runners.kubernetes.build_container_security_context.capabilities] [runners.kubernetes.helper_container_security_context] [runners.kubernetes.helper_container_security_context.capabilities] [runners.kubernetes.service_container_security_context] [runners.kubernetes.service_container_security_context.capabilities] [runners.kubernetes.volumes] [runners.kubernetes.dns_config] [runners.kubernetes.container_lifecycle] [[runners.kubernetes.volumes.host_path]] name = "docker" mount_path = "/var/run/docker.sock"
和上面配置的预期基本一至
查看gitlab runner
创建 .ygitlab-ci.yaml 文件 进行测试
image: docker:latest stages: - build # - deploy cache: key: ${CI_COMMIT_REF_SLUG} paths: - tomcat/* - Dockerfile build: image: docker:24.0.7 stage: build script: - docker build -t my-app:latest . # 使用Dockerfile构建镜像 #deploy: # stage: deploy # script: # - docker run --rm -it -p 8080:8080 my-app:latest # 启动容器并运行应用程序
在创建个简单的Dockerfile
FROM busybox:latest MAINTAINER 'hzc <2504058156@qq.com>' RUN ls /tmp && touch /tmp/test.html RUN echo "--------------------------start--------------------" RUN cp -r /tmp/test.html /root/ RUN echo "--------------------------end--------------------"
然后开始修改一下README.md 文件随便改个东西自动跑流水线
查看流水线
下面这是我流水线的完整log
Running with gitlab-runner 14.6.1 (340a666c) on new2gitlab-runner-gitlab-runner-cdfcd4d55-zbhjr 2cxumSzD Preparing the "kubernetes" executor 00:00 Preparing environment 00:06 Getting source from Git repository 00:01 Fetching changes with git depth set to 50... Initialized empty Git repository in /builds/root/blog_nb/.git/ Created fresh repository. Checking out 790e7365 as main... Skipping Git submodules setup Restoring cache 00:00 Checking cache for main... Downloading cache.zip from http://10.0.24.7:31000/new-gitlab/new-test/project/6/main Successfully extracted cache Executing "step_script" stage of the job script 00:01 $ docker build -t my-app:latest . #0 building with "default" instance using docker driver #1 [internal] load build definition from Dockerfile #1 transferring dockerfile: 290B done #1 DONE 0.0s #2 [internal] load .dockerignore #2 transferring context: 2B done #2 DONE 0.0s #3 [internal] load metadata for docker.io/library/busybox:latest #3 DONE 0.0s #4 [1/5] FROM docker.io/library/busybox:latest #4 DONE 0.0s #5 [2/5] RUN ls /tmp && touch /tmp/test.html #5 CACHED #6 [4/5] RUN cp -r /tmp/test.html /root/ #6 CACHED #7 [3/5] RUN echo "--------------------------start--------------------" #7 CACHED #8 [5/5] RUN echo "--------------------------end--------------------" #8 CACHED #9 exporting to image #9 exporting layers done #9 writing image sha256:2c8b91b4014b68631a7895b5d12df1f6765b0d9e14d2792b37784a853e28a9f0 done #9 naming to docker.io/library/my-app:latest done #9 DONE 0.0s WARNING: current commit information was not captured by the build: git was not found in the system: exec: "git": executable file not found in $PATH Saving cache for successful job 00:01 Creating cache main... WARNING: tomcat/*: no matching files Dockerfile: found 1 matching files and directories Archive is up to date! Created cache Job succeeded
在minio 查看
本文来自博客园,作者:IT老登,转载请注明原文链接:https://www.cnblogs.com/nb-blog/p/18000936
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 我与微信审核的“相爱相杀”看个人小程序副业
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~