Help us learn about your current experience with the documentation. Take the survey.
教程:在 OpenShift 上使用 GitLab Runner Operator 在无 root 容器中使用 Buildah
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
本教程教你如何成功使用 buildah 工具构建镜像,
该 GitLab Runner 是通过 GitLab Runner Operator
在 OpenShift 集群上部署的。
本指南是对 在无 root OpenShift 容器中使用 Buildah 构建镜像 文档的改编,专门针对 GitLab Runner Operator。
完成本教程,你将:
前提条件
- Runner 已部署到
gitlab-runner命名空间。
配置 Buildah 镜像
我们首先基于 quay.io/buildah/stable:v1.23.1 镜像准备一个自定义镜像。
-
创建
Containerfile-buildah文件:cat > Containerfile-buildah <<EOF FROM quay.io/buildah/stable:v1.23.1 RUN touch /etc/subgid /etc/subuid \ && chmod g=u /etc/subgid /etc/subuid /etc/passwd \ && echo build:10000:65536 > /etc/subuid \ && echo build:10000:65536 > /etc/subgid # 使用 chroot,因为默认的 runc 在运行无 root 时不起作用 RUN echo "export BUILDAH_ISOLATION=chroot" >> /home/build/.bashrc # 使用 VFS,因为 fuse 不起作用 RUN mkdir -p /home/build/.config/containers \ && (echo '[storage]';echo 'driver = "vfs"') > /home/build/.config/containers/storage.conf # buildah 容器将以 `build` 用户身份运行 USER build WORKDIR /home/build EOF -
构建并推送 Buildah 镜像到容器注册表。我们推送到 GitLab 容器注册表:
docker build -f Containerfile-buildah -t registry.example.com/group/project/buildah:1.23.1 . docker push registry.example.com/group/project/buildah:1.23.1
配置服务账户
对于这些步骤,你需要在连接到 OpenShift 集群的终端中运行命令。
-
运行以下命令创建名为
buildah-sa的服务账户:oc create -f - <<EOF apiVersion: v1 kind: ServiceAccount metadata: name: buildah-sa namespace: gitlab-runner EOF -
为创建的服务账户授予使用
anyuidSCC 运行的权限:oc adm policy add-scc-to-user anyuid -z buildah-sa -n gitlab-runner -
使用 runner 配置模板 配置 Operator 使用我们刚刚创建的服务账户。创建一个包含以下内容的
custom-config.toml文件:[[runners]] [runners.kubernetes] service_account_overwrite_allowed = "buildah-*" -
从
custom-config.toml文件创建一个名为custom-config-toml的ConfigMap:oc create configmap custom-config-toml --from-file config.toml=custom-config.toml -n gitlab-runner -
通过更新其 自定义资源定义 (CRD) 文件 来设置
Runner的config属性:apiVersion: apps.gitlab.com/v1beta2 kind: Runner metadata: name: builah-runner spec: gitlabUrl: https://gitlab.example.com token: gitlab-runner-secret config: custom-config-toml
配置作业
最后一步是在你的项目中设置一个 GitLab CI/CD 配置文件,使用 我们构建的镜像和配置的服务账户:
build:
stage: build
image: registry.example.com/group/project/buildah:1.23.1
variables:
STORAGE_DRIVER: vfs
BUILDAH_FORMAT: docker
BUILDAH_ISOLATION: chroot
FQ_IMAGE_NAME: "$CI_REGISTRY_IMAGE/test"
KUBERNETES_SERVICE_ACCOUNT_OVERWRITE: "buildah-sa"
before_script:
# 登录到 GitLab 容器注册表
- buildah login -u "$CI_REGISTRY_USER" --password $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- buildah images
- buildah build -t $FQ_IMAGE_NAME
- buildah images
- buildah push $FQ_IMAGE_NAME作业应使用我们构建的镜像作为 image 关键字的值。
KUBERNETES_SERVICE_ACCOUNT_OVERWRITE 变量应具有我们创建的
服务账户名称的值。
恭喜,你已成功在无 root 容器中使用 Buildah 构建了镜像!