openEuler系统gitlab-runner自定义libvirt

宿主机环境配置

注:以下操作均为 root 用户,避免 gitlab-runner 用户引发的权限问题

环境:openEuler-20.03-LTS-SP3

宿主机部署runner目录分布

plantext 复制代码
gitlab-runner                                      # gitlab-runner 工作目录全路径/home/gitlab-runner
└── ci                                            # ci 标识目录
    ├── images                                    # libvirt镜像目录,用作拷贝和基准镜像存储位置
    │   ├── gitlab-runner-base.qcow2              # 基准镜像
    │   ├── runner-117-project-18-concurrent-0-job-1969.qcow2  # 执行器副本镜像,由 prepare.sh 脚本自动生成
    │   ├── runner-117-project-18-concurrent-1-job-1970.qcow2
    │   ├── runner-117-project-18-concurrent-2-job-1971.qcow2
    │   ├── runner-117-project-18-concurrent-3-job-1972.qcow2
    │   └── runner-117-project-18-concurrent-4-job-1973.qcow2
    └── libvirt-driver                            # 驱动runner的脚本目录
        ├── base.sh                               # 公用基础脚本,存变量和复用函数
        ├── cleanup.sh                            # 清除脚本,job执行完使用
        ├── prepare.sh                            # 前置脚本,job执行前使用
        └── run.sh                                # 运行脚本,job执行时使用

1. 环境包安装

bash 复制代码
# x86_64:
yum install -y tar virt-install qemu-img qemu-kvm libvirt
# aarch64:
yum install -y tar virt-install qemu-img qemu-kvm libvirt libvirt-daemon-driver-qemu

2. 添加脚本文件

3. 生成免密密钥

libvirtrun.sh 默认以ssh进行执行构建过程

复制代码
ssh-keygen -t ed25519 -C libvirt_node

4. 注册runner到gitlab站点

注:注意宿主机平台架构下载 gitlab-runner 程序

  • aarch64

    bash 复制代码
    curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-arm64
    chmod +x /usr/local/bin/gitlab-runner
    gitlab-runner install --user=root --working-directory=/home/gitlab-runner
    gitlab-runner start
    gitlab-runner register --url http://你的gitlab地址 --registration-token 你的注册Token
  • x86_64

    bash 复制代码
    curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
    chmod +x /usr/local/bin/gitlab-runner
    gitlab-runner install --user=root --working-directory=/home/gitlab-runner
    gitlab-runner start
    gitlab-runner register --url http://你的gitlab地址 --registration-token 你的注册Token

环境选择时选择 custom

执行完毕后,编辑 /etc/gitlab-runner/config.toml

plantext 复制代码
concurrent = 20                                       # 当前runner节点并发量,默认为1
check_interval = 0
shutdown_timeout = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "10-113-74-66"
  url = "http://10.113.75.183:3000/"
  id = 117
  token = "z1K__gW7yWKYuTRHAYuW"
  token_obtained_at = 2023-05-17T02:54:17Z
  token_expires_at = 0001-01-01T00:00:00Z
  executor = "custom"
  builds_dir = "/gitlab-runner/builds"		 # builds_dir需要添加
  cache_dir = "/gitlab-runner/cache"       # cache_dir需要添加
  [runners.cache]
    MaxUploadedArchiveSize = 0
  [runners.custom]
    prepare_exec = "/home/gitlab-runner/ci/libvirt-driver/prepare.sh"    # prepare_exec 需要指定
    run_exec = "/home/gitlab-runner/ci/libvirt-driver/run.sh"            # run_exec 需要指定
    cleanup_exec = "/home/gitlab-runner/ci/libvirt-driver/cleanup.sh"    # cleanup_exec 需要指定

注:builds_dircache_dir 参数指定应当确定 qcow2 的磁盘分区大小,如果构建过程占满磁盘会导致 Job 阻塞,最终超时失败。

制作 gitlab-runner-base.qcow2

  • 方法一:使用virt-builder,可参考GitLab官文Using libvirt with the Custom executor大概率检查环境时存在问题。
  • 方法二:本地编辑基准版本dasos-e2.1.1 qcow2镜像。
    本文采用方法二

1. 启动qcow2镜像

bash 复制代码
virt-install --name dasos-e2.1.1 \
  --os-variant fedora28 \
  --disk dasos-e2.1.1-x86_64.qcow2 \
  --import \
  --vcpus=2 \
  --ram=2048 \
  --network default \
  --graphics vnc,port=50500,listen=0.0.0.0 \
  --noautoconsole

# 参数释义:
# --name dasos-e2.1.1 virsh 域名称
# --os-variant fedora28 基本镜像类型
# --disk dasos-e2.1.1-x86_64.qcow2 镜像文件
# --vcpus=2 虚化CPU数量
# --ram=2048 分配内存
# --network default 使用virsh net-list 查看,此处需要存在默认网络,名称为`default`,否则无法联网
# --graphics vnc,port=50500,listen=0.0.0.0 开启VNC
# --noautoconsole 屏蔽自动切换终端

注:--disk指定aarch64镜像时添加需要参数 --arch aarch64

使用vnc软件链接:vnc://宿主机IP地址:50500,进行qcow2镜像环境编辑。

2. 配置网卡自动获取IP

编辑 /boot/grub2/grub.cfg 添加内核启动项 net.ifnames=0 biosdevname=0

复制代码
linux   /boot/vmlinuz-* root=* ro net.ifnames=0 biosdevname=0 console=XXX

添加网卡脚本/etc/sysconfig/ifcfg-eth0

plantext 复制代码
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp

HWADDR可不填
注:当前步骤执行完毕先重启校验网络是否正常

3. 安装所需要的软件包

注:git为必须安装否则Runner将无法启动qcow2镜像。

构建过程时所需的包也可以在这里安装。

bash 复制代码
yum install git

4. root的SSH免密创建

注:先切换到宿主机上执行

bash 复制代码
# 第一次需要输入密码:system
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@你的qcow2的IP地址

# 免密测试
ssh root@你的qcow2的IP地址

5. 拷贝到宿主机发布目录

bash 复制代码
cp ./dasos-e2.1.1.qcow2 /home/gitlab-runner/ci/images/gitlab-runner-base.qcow2
相关推荐
努力进修11 分钟前
跨地域传文件太麻烦?Nginx+cpolar 让本地服务直接公网访问
运维·nginx·cpolar
Qayrup20 分钟前
docker 搭建私有仓库,推送并拉取
运维·docker·容器
闪耀星星25 分钟前
debian elctron-builder
运维·debian
会飞的土拨鼠呀25 分钟前
Debian 12 笔记本合盖不休眠设置指南
运维·debian
梁正雄3 小时前
6、prometheus资源规划
运维·服务器·服务发现·prometheus·监控
晨曦之旅3 小时前
零成本体验云计算!阿贝云免费服务器深度测评
运维·服务器·云计算
工具人55553 小时前
Linux 抓取 RAM Dump 完整指南
linux·运维·安全
会飞的小蛮猪3 小时前
SkyWalking运维之路(Java探针接入)
java·运维·经验分享·容器·skywalking
天一生水water4 小时前
docker-compose安装
运维·docker·容器
Archy_Wang_15 小时前
基于BaGet 构建NuGet私有库并实现类库打包到NuGet私有库
运维·jenkins