目录

极狐GitLab私有化部署指南

参考资料

修订记录

日期 内容
2022.10.15 创建文档
2023.09.20 增加16.1版本上传许可证的方式
2023.11.14 增加Runner安装示例
2023.11.24 增加安全扫描配置说明
2024.02.23 删除CentOS 8/Ubuntu 18.04支持的相关内容
2024.05.09 增加许可证密钥的导入方法,增加关闭AutoDevOps的方法
2024.10.11 更新EXTERNAL_URL的配置,增加HTTPS协议配置的方法

1. 环境资源

用户规模 0~500 500~1000
操作系统 Ubuntu 20.04 ; CentOS 7 ; Debian 9/10/11 ; AlmaLinux 8 / RHEL
节点数量 GitLab-Server * 1 ; GitLab-Runner * N (N>=0)
浏览器支持 Mozilla FireFox ; Google Chrome ; Chromium ; MicroSoft Edge ; Apple Safari
CPU(核) 8C+ 16C+
内存(GB) 16G+ 32G+
磁盘(GB) 500G 或 按需 ; SSD(不可使用NFS) 1T 或 按需 ; SSD(不可使用NFS)

2. 在线安装

2.1 安装最新版本

访问官方网站,通过官方Linux安装包方式安装:

GitLab最新版下载安装_GitLab中文免费版-极狐GitLab中文官方网站

/gitlab_self_hosted_deploy/__GitLab________1_1762149863426.png

2.2 安装指定版本

  1. 安装依赖、配置极狐GitLab 软件源镜像。

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    
    # Ubuntu
    sudo apt-get update
    sudo apt-get install -y curl openssh-server ca-certificates tzdata perl
    
    #CentOS
    sudo yum install -y curl policycoreutils-python openssh-server perl
    sudo systemctl enable sshd
    sudo systemctl start sshd
    sudo firewall-cmd --permanent --add-service=http
    sudo firewall-cmd --permanent --add-service=https
    sudo systemctl reload firewalld
    
    # 配置下载源
    curl -fsSL https://get.gitlab.cn | /bin/bash
    
  2. 查询极狐GitLab版本,如16.11.2-jh.0

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # Ubuntu
    sudo apt policy gitlab-jh 
    ## 或者
    sudo apt madison gitlab-jh 
    
    # RHEL/CentOS 7
    sudo yum list --showduplicates gitlab-jh 
    # RHEL/AlmaLinux 8
    sudo dnf list --showduplicates gitlab-jh 
    
  3. 安装指定版本极狐GitLab。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    # Ubuntu
    apt install gitlab-jh=16.11.2-jh.0
    # RHEL/CentOS  7
    yum install gitlab-jh-16.11.2-jh.0
    # RHEL/AlmaLinux 8
    dnf install gitlab-jh-16.11.2-jh.0
    
    # 不指定版本号安装最新版本
    apt install gitlab-jh
    
  4. 默认配置下需确保22、80、443端口可对外提供访问。

3. 离线安装

  1. 官方仓库下载指定版本的Linux安装包到本地,并传入离线环境中。

  2. 安装极狐GitLab。

    1
    2
    3
    4
    
    # Ubuntu / Debian
    dpkg -i <gitlab离线安装包,如gitlab-jh_15.1.0-jh.0_amd64>.deb
    # CentOS 7
    rpm -ivh <gitlab离线安装包>.rpm
    
  3. 默认配置下需确保22、80、443端口可对外提供访问。

4. 初始密码

安装完成后,极狐GitLab将随机生成一个密码并存储在/etc/gitlab/initial_root_password文件中(出于安全原因,24 小时后会自动删除),使用cat /etc/gitlab/initial_root_password命令获取root用户的初始密码。

5. 修改访问地址

编辑/etc/gitlab/gitlab.rb,修改external_url为GitLab的访问地址:

  1. 如果要设置为IP,一般只支持HTTP协议,如 http://192.168.x.x,测试阶段推荐用该方式

  2. 如果要设置为HTTP协议的域名,需要将该域名的DNS解析到GitLab所在服务器IP,如 http://gitlab.example.com

  3. 如果要设置为HTTPS协议的域名,有3种方式

    • 使用GitLab自带的Let’s Encrypt自动申请SSL证书:

      • 服务器可以访问外网
      • GitLab使用默认的80/443端口
      • 已经设置好DNS
      • 修改external_url​为该域名,如 https://gitlab.example.com
    • 使用CA机构提供的SSL证书:参考文档进行配置

    • 使用自签SSL证书:会引起其他组件、系统与GitLab通讯认证问题,处理较为复杂,不推荐使用

  4. 修改完成执行以下命令:

    1
    
    gitlab-ctl reconfigure
    

6. 修改显示语言

显示语言是按个人用户进行设置:

  1. 参照下图进入“用户中心——偏好设置” /gitlab_self_hosted_deploy/__GitLab________3_1762149863434.png

  2. 下拉找到“本地化——语言”,选择指定的语言并保存 /gitlab_self_hosted_deploy/__GitLab________4_1762149863438.png

  3. 刷新网页生效

7. 关闭AutoDevOps

不论您是否需要使用GitLab CI,都建议您关闭AutoDevOps功能,避免产生莫名的流水线错误。

  1. 在“管理中心——CICD——持续集成和部署”取消勾选“所有项目默认使用Auto DevOps流水线”,然后保存更改/gitlab_self_hosted_deploy/__GitLab________5_1762149863441.png

8. 上传许可证

  1. root/管理员账号登录极狐GitLab。

  2. 参照下图在“管理中心——设置——通用——添加许可证”中上传许可证: /gitlab_self_hosted_deploy/__GitLab________6_1762149863442.png

  3. 如果不能识别并上传.gitlab-license文件,可以用记事本打开许可证文件,复制其中的内容并参照下图在“管理中心——设置——通用——添加许可证——输入许可证密钥”粘贴: /gitlab_self_hosted_deploy/__GitLab________7_1762149863445.png

9. 安装GitLab Runner(可选)

9.1 GitLab Runner简介

如果需要使用GitLab CI实现CI/CD流水线、启用GitLab内置的代码质量扫描、启用GitLab DevSecOps能力,需要安装GitLab Runner。

GitLab Runner类似Jenkins Agent/Slave,部署在需要执行CI/CD、质量/安全扫描的服务器上,与GitLab Server通讯,接收GitLab Server下发的流水线任务并执行相关的脚本。

其中,启用GitLab内置的代码质量扫描和DevSecOps能力仅支持Docker或K8S类型的Runner,且Runner所在的服务器需要有访问外网的权限(GitLab代码质量扫描和DevSecOps能力需访问外网的漏洞库以实现实时更新。支持配置网络出口白名单,也支持离线环境部署,但较为复杂,一般通过极狐GitLab专业服务来交付,测试期间不建议考虑这些方式)。

/gitlab_self_hosted_deploy/__GitLab________8_1762149863447.png

9.2 GitLab Runner安装示例

GitLab Runner可配置在:

  • 实例级:即该Runner可被整个实例的所有项目使用。
  • 群组级:即该Runner可被该群组下的所有项目使用。
  • 项目级:即仅支持被该项目使用。

以在实例级安装Docker类型的Runner为例:

  1. 在需要安装GitLab Runner的服务器上安装好Docker

  2. 进入GitLab“管理中心——CI/CD——Runner”,点击“新建实例runner”。 /gitlab_self_hosted_deploy/__GitLab________9_1762149863449.png

  3. 设置“标签”,用于区分不同的Runner,点击“创建runner”。 /gitlab_self_hosted_deploy/__GitLab________10_1762149863451.png /gitlab_self_hosted_deploy/__GitLab________11_1762149863452.png

  4. 在安装好Docker的服务器上执行以下命令。注意,该安装方式将Runner的配置文件存放在宿主机的/srv/gitlab-runner/config目录。

    1
    2
    3
    4
    
    docker run -d --name gitlab-runner --restart always \
      -v /srv/gitlab-runner/config:/etc/gitlab-runner \
      -v /var/run/docker.sock:/var/run/docker.sock \
      gitlab/gitlab-runner:latest
    
  5. 注册Runner:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    docker exec -it gitlab-runner
    
    # Enter the GitLab instance URL (for example, https://gitlab.com/):
    # 输入第2步中,步骤1命令中的url,如:https://gitlab.jhgitlab.com
    # Enter the registration token:
    # 输入第2步中,步骤1命令中的token,如:glrt-hRsg_XR5vwgndRM2ofid
    # Enter a name for the runner. This is stored only in the local config.toml file:
    # 输入Runner的名称,如:docker
    # Enter an executor: custom, shell, ssh, docker+machine, instance, kubernetes, docker, docker-windows, parallels, virtualbox, docker-autoscaler:
    # 输入Runner的类型,这里是:docker
    # Enter the default Docker image (for example, ruby:2.7):
    # 输入默认镜像,如:ubuntu:22.04
    # Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
    
    # 验证Runner
    docker exec -it gitlab-runner verify
    # Verifying runner... is valid                        runner=hRsg_XR5v
    
  6. 注册Runner成功 /gitlab_self_hosted_deploy/__GitLab________12_1762149863454.png

  7. 如果希望该Runner运维未指定标签(Tags)的任务(Job),需要将Runner中的“运行未打标签的作业”打勾,并“保存更改” /gitlab_self_hosted_deploy/__GitLab________13_1762149863455.png

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    # 某项目的流水线脚本 .gitlab-ci.yml
    stages:          
      - build
      - deploy
    
    # 没有指定Tags,只能运行在勾选了“运行未打标签的作业”的Runner下
    build-job:       
      stage: build
      script:
        - echo "Compiling the code..."
        - echo "Compile complete."
    
    # 指定Tags为docker,只能运行在标签是“docker”的Runner下
    deploy-job:      
      stage: deploy  
      tags:
        - docker
      script:
        - echo "Deploying application..."
        - echo "Application successfully deployed."
    

9.3 GitLab Runner安装方式

其他安装方式如下:

  1. Linux(物理机、虚拟机):https://docs.gitlab.cn/runner/install/linux-repository.html
  2. MacOS(物理机、虚拟机): https://docs.gitlab.cn/runner/install/osx.html
  3. Windows(物理机、虚拟机): https://docs.gitlab.cn/runner/install/windows.html
  4. Docker:https://docs.gitlab.cn/runner/install/docker.html
  5. K8S:https://docs.gitlab.cn/runner/install/kubernetes.html