Hello World

行走即是圆梦,回望亦是前行。

0%

GitLab搭建教程

前言

这里简单记录下GitLab的搭建教程

参考教程1
参考教程2

安装环境

阿里云服务器 CentOS 7.5 64位

1
2
$ uname -a
Linux 3.10.0-1062.18.1.el7.x86_64 #1 SMP Tue Mar 17 23:49:17 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

安装必要的依赖

  1. 安装ssh协议

    1
    2
    3
    4
    5
    6
    # 安装ssh
    $ sudo yum install -y curl policycoreutils-python openssh-server
    # 开机自启
    $ sudo systemctl enable sshd
    # 启动ssh
    $ sudo systemctl start sshd
  2. 安装防火墙

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # 安装防火墙
    $ yum install firewalld systemd -y
    # 开机自启
    $ systemctl enable firewalld
    # 安装防火墙
    $ service firewalld start
    # 添加HTTP服务到firewalld
    $ sudo firewall-cmd --permanent --add-service=http
    # 重启防火墙
    $ sudo systemctl reload firewalld
  3. 安装postfix以发送邮件

    1
    2
    3
    4
    5
    6
    # 安装postfix
    $ sudo yum install postfix
    # 开机自启
    $ sudo systemctl enable postfix
    # 启动postfix
    $ sudo systemctl start postfix

    如果启动失败,报错Failed to start Postfix Mail Transport Agent.,修改如下配置

    1
    2
    $ vim /etc/postfix/main.cf
    inet_interfaces = all

安装GitLab服务

  1. 下载gitlab镜像 清华大学开源镜像站

    1
    $ wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.9.2-ce.0.el7.x86_64.rpm
  2. 安装GitLab

    1
    $ rpm -i gitlab-ce-12.9.2-ce.0.el7.x86_64.rpm
  3. 修改服务ip和端口

    1
    $ vim /etc/gitlab/gitlab.rb

    external_url 修改为 http://ip:port,例如 http://127.0.0.1:8090

  4. 8090端口添加到防火墙中

    1
    $ firewall-cmd --zone=public --add-port=8090/tcp --permanent
  5. 重启防火墙

    1
    $ sudo systemctl reload firewalld
  6. 重置GitLab

    1
    $ gitlab-ctl reconfigure
  7. 启动GitLab

    1
    $ gitlab-ctl restart
  8. 开机自启

    1
    $ systemctl enable gitlab-runsvdir.service
  9. 访问GitLab
    浏览器直接输入http://ip:port访问即可

升级GitLab

  1. 下载gitlab镜像 清华大学开源镜像站

    1
    $ wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.9.3-ce.0.el7.x86_64.rpm
  2. 关闭部分GitLab服务

    1
    2
    3
    4
    5
    $ gitlab-ctl stop unicorn
    $ gitlab-ctl stop sidekiq
    $ gitlab-ctl stop nginx
    # 或者 && 执行
    $ gitlab-ctl stop unicorn && gitlab-ctl stop sidekiq && gitlab-ctl stop nginx
  3. 升级GitLab

    1
    $ rpm -Uvh gitlab-ce-12.9.3-ce.0.el7.x86_64.rpm
  4. 重新配置gitlab

    1
    $ gitlab-ctl reconfigure
  5. 重启gitlab

    1
    $ gitlab-ctl restart
  6. 查看版本号

    1
    2
    $ cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
    $ gitlab-rake gitlab:env:info

配置自动备份

  1. 配置文件/etc/gitlab/gitlab.rb

    1
    2
    3
    4
    gitlab_rails['manage_backup_path'] = true
    gitlab_rails['backup_path'] = "/data/gitlab/backups" #备份目录
    gitlab_rails['backup_archive_permissions'] = 0644 #生成的文件权限
    gitlab_rails['backup_keep_time'] = 864000 #保留10天
  2. 手动执行备份

    1
    $ gitlab-rake gitlab:backup:create

    注:需要手动备份如下相关配置文件:

    1
    2
    /etc/gitlab/gitlab.rb
    /etc/gitlab/gitlab-secrets.json

    备份文件生成至 /data/gitlab/backups
    文件名例如:1587048780_2020_04_16_12.9.2_gitlab_backup.tar

  3. 自动定时备份

    1
    2
    3
    $ crontab -e
    # 每天5点执行备份
    0 5 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1
  4. 恢复备份

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 停止连接数据库的进程
    $ sudo gitlab-ctl stop unicorn
    $ sudo gitlab-ctl stop sidekiq
    # 拷贝备份至默认路径
    $ cp 1587048780_2020_04_16_12.9.2_gitlab_backup.tar /var/opt/gitlab/backups/
    # 给git用户授权
    $ chown git:git 1587048780_2020_04_16_12.9.2_gitlab_backup.tar
    # 恢复1587048780这个备份文件,将覆盖GitLab数据库!
    $ sudo gitlab-rake gitlab:backup:restore BACKUP=1587048780
    # 重启 GitLab
    $ sudo gitlab-ctl restart
    # 检查 GitLab
    $ sudo gitlab-rake gitlab:check SANITIZE=true

    注:恢复后可能出现web端点击项目报错500,则需要将如下配置文件拷贝到恢复环境上,然后gitlab-ctl reconfigure & gitlab-ctl restart即可

    1
    2
    /etc/gitlab/gitlab.rb
    /etc/gitlab/gitlab-secrets.json

安装问题说明

上述步骤执行后,浏览器访问出现502

  1. 查看配置文件/etc/gitlab/gitlab.rb中的端口号是否被占用
  2. GitLab服务占用内存太多,导致服务器崩溃
    参考教程
    教程1
    教程2

自己安装过程中的问题:

安装完成浏览器访问出现502,因为看到资料说GitLab默认端口为8080,所以配置external_url 时修改为 http://ip:8080,按照上述文章,经过一番折腾,尝试过创建swap分区,但问题依然没有解决,并反复确认发现8080并没有被占用,后又执行gitlab-ctl status,多次执行发现unicorn的pid一直在变大,而其他服务正常,说明unicorn启动失败,查看日志gitlab-ctl tail unicorn,然后查看配置文件/etc/gitlab/gitlab.rb,发现# unicorn['port'] = 8080,说明unicorn的默认端口为8080,将external_url 修改为 http://ip:8090,重置GitLab并重启,访问正常。

注:其实参考教程中是将端口设置为9090的,由于看到资料说默认端口是8080才设置的http://ip:8080导致出现502,但也对安装过程有了更深刻的印象

启动后内存占用高

修改配置/etc/gitlab/gitlab.rb 参考教程

  1. 减小进程数

    1
    unicorn['work_processes'] = 2

    默认是2,官方建议是CPU核心数加一,可以提高服务器的响应速度。
    如果内存只有4G,或者服务器上有其它业务,请勿修改,以免内存不足。
    注:这个参数最小值是2,设为1,服务器可能会卡死。

  2. 减小postgresql缓存

    1
    postgresql['shared_buffers'] = "128MB"

    默认是256MB,可适当改小

  3. 减少postgresql并发数

    1
    postgresql['max_worker_processes'] = 4

    默认是8,可适当改小

  4. 减少sidekiq并发数

    1
    sidekiq['concurrency'] = 8

    默认是25,可适当改小

  5. 启用swap分区
    教程

修改完配置以后,需要执行下面的命令使配置生效:

1
2
$ sudo gitlab-ctl reconfigure
$ sudo gitlab-ctl restart

备注:按照上述方案修改后,阿里云2核4G的服务器,安装12.x版本Gitlab占用依然达到2G以上,由于服务器上不止运行Gitlab,并且对Gitlab版本要求不高,所以选择了宝塔上的GitLab中文社区版 8.8.5