私有软件仓库 GitLab安装

1、GitLab 介绍

GitLab 是一个基于Ruby on Rails构建用于仓库管理系统的开源项目,使用Git作为代码管理工具,提供了Web界面进行访问公开的或者私有的项目

GitLab 优势

  • 开源免费
  • 可以作为 Git 代码仓库
  • 提供了方便易用的 Web 管理界面
  • 支持离线提交
  • 安全性高, 可以对不同的用户设置不同的权限,并且支持不同用户只能访问特定的代码,实现代码部分可见

2、GitLab 安装

2.1 GitLab 架构

Gitlab 是一个复杂的系统,由很多应用组成。

图片[1]-私有软件仓库 GitLab安装-李佳程的个人主页

Gitlab的服务构成

  • Nginx:静态web服务器
  • gitlab-shell:用于处理Git命令和修改authorized keys列表
  • gitlab-workhorse:轻量级的反向代理服务器,它旨在充当智能反向代理,以帮助整个 GitLab 加速
  • postgresql:数据库
  • redis:缓存数据库
  • sidekiq:用于在后台执行队列任务(异步执行)
  • unicorn:An HTTP server for Rack applications,GitLab Rails应用是托管在这个服务器上面的

2.2、安装 GitLab 要求

硬件配置要求较高:

  • 测试环境:内存4G以上
  • 生产环境:建议CPU2C以上,内存8G以上,磁盘10G以上配置,和用户数有关

数据库要求:

从GitLab 12.1 开始不再支持MySQL,只支持PostgreSQL

2.3、安装前准备

centos基于最小化服务器安装,建议修改配置如下:

# 准备epel源
# 关闭防火墙
# 关闭selinux
# 修改主机名

官方gitlab 包下载链接:

https://packages.gitlab.com/gitlab

GitLab-CE 安装包官方下载地址:

https://packages.gitlab.com/gitlab/gitlab-ce

2.4、安装

[root@git01 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-15.6.2-ce.0.el7.x86_64.rpm --no-check-certificate

[root@git01 ~]# rpm -ivh gitlab-ce-15.6.2-ce.0.el7.x86_64.rpm 
图片[2]-私有软件仓库 GitLab安装-李佳程的个人主页

2.5、修改 GitLab 配置

gitlab相关的目录:

/etc/gitlab     #配置文件目录
/run/gitlab     #运行目录,存放很多的数据库文件
/opt/gitlab     #安装目录
/var/opt/gitlab #数据目录,源代码就存放在此目录
/var/log/gitlab #日志目录

gitlab 初始化配置

[root@git01 ~]# grep "^[a-Z]" /etc/gitlab/gitlab.rb
external_url 'http://192.168.1.71'

# 增加下面行(可以和上面行写在一起),可选邮件通知设置
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "252409868@qq.com"
gitlab_rails['smtp_password'] = "授权码"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "252409868@qq.com"

# 新版中增加下面行,给root用户指定初始密码才能登录
gitlab_rails['initial_root_password'] = "12345678"

由于 Gitlab 主机功能是代码托管,默认有些额外的组件比较消耗资源,如果暂时不使用可以考虑关闭

将不需要使用的组件都设定为false

vim /etc/gitlab/gitlab.rb
prometheus['enable'] = false
prometheus['monitor_kubernetes'] =false
alertmanager['enable'] = false
node_exporter['enable'] = false
redis_exporter['enable'] = false
postgres_exporter['enable'] = false
gitlab_exporter['enable'] = false
prometheus_monitoring['enable'] =false
grafana['enable'] = false

2.6、初始化和启动服务

执行配置并启动服务:

# 每次修改完配置文件都需要执行此操作
[root@git01 ~]# gitlab-ctl reconfigure

2.7、验证 GitLab 启动完成

[root@git01 ~]# gitlab-ctl status
run: alertmanager: (pid 5242) 48s; run: log: (pid 5241) 48s
run: gitaly: (pid 3350) 676s; run: log: (pid 2560) 790s
run: gitlab-exporter: (pid 5218) 51s; run: log: (pid 5217) 51s
run: gitlab-kas: (pid 3439) 669s; run: log: (pid 2827) 778s
run: gitlab-workhorse: (pid 3448) 669s; run: log: (pid 2984) 735s
run: logrotate: (pid 2484) 803s; run: log: (pid 2493) 802s
run: nginx: (pid 3020) 730s; run: log: (pid 3031) 726s
run: node-exporter: (pid 5205) 57s; run: log: (pid 5204) 57s
run: postgres-exporter: (pid 5253) 47s; run: log: (pid 5252) 47s
run: postgresql: (pid 2683) 785s; run: log: (pid 2700) 784s
run: prometheus: (pid 5231) 49s; run: log: (pid 5230) 49s
run: puma: (pid 5311) 36s; run: log: (pid 2923) 747s
run: redis: (pid 2519) 797s; run: log: (pid 2528) 796s
run: redis-exporter: (pid 5222) 50s; run: log: (pid 5221) 50s
run: sidekiq: (pid 5271) 43s; run: log: (pid 2966) 739s

2.8、验证端口及状态

80端口是在初始化gitlib的时候启动的,因此如果之前有程序占用了相关端口,就会导致初始化失败或无
法访问

[root@git01 ~]# lsof -i :80
COMMAND  PID       USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   3020       root    7u  IPv4  49804      0t0  TCP *:http (LISTEN)
nginx   3021 gitlab-www    7u  IPv4  49804      0t0  TCP *:http (LISTEN)
nginx   3022 gitlab-www    7u  IPv4  49804      0t0  TCP *:http (LISTEN)

2.9、gitlab的常用命令

gitlab-rails #用于启动控制台进行特殊操作,如修改管理员密码、打开数据库控制台( gitlab-
               rails dbconsole)等
gitlab-psql  #数据库命令行
gitlab-rake  #数据备份恢复等数据操作

# 客户端命令行操作行
gitlab-ctl
gitlab-ctl reconfigure  #修改过配置后需要执行重新配置
gitlab-ctl stop         #停止gitlab
gitlab-ctl start        #启动gitlab
gitlab-ctl restart      #重启gitlab
gitlab-ctl status       #查看组件运行状态
gitlab-ctl tail         #查看所有日志
gitlab-ctl tail nginx   #查看某个组件的日志
gitlab-ctl service-list #列出服务
[root@git01 ~]# gitlab-psql
psql (13.8)
Type "help" for help.

gitlabhq_production=# help
You are using psql, the command-line interface to PostgreSQL.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
gitlabhq_production=# \db
         List of tablespaces
    Name    |    Owner    | Location
------------+-------------+----------
 pg_default | gitlab-psql |
 pg_global  | gitlab-psql |
(2 rows)

gitlabhq_production=# \q
[root@git01 ~]# 
# 查看服务列表

[root@git01 ~]# gitlab-ctl service-list
alertmanager*
gitaly*
gitlab-exporter*
gitlab-kas*
gitlab-workhorse*
logrotate*
nginx*
node-exporter*
postgres-exporter*
postgresql*
prometheus*
puma*
redis*
redis-exporter*
sidekiq*

2.10、一键安装 GitLab 脚本

[root@git02 ~]# cat gitlab_install.sh
#!/bin/bash

#说明:安装GitLab 服务器内存建议至少4G,root密码至少8位

GITLAB_VERSION=15.6.2

URL="https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-${GITLAB_VERSION}-ce.0.el7.x86_64.rpm"

GITLAB_ROOT_PASSWORD=12345678

SMTP_PASSWORD=zkgjekodlklfbhdf

HOST=`hostname -I|awk '{print $1}'`

. /etc/os-release

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $" OK "
    elif [ $2 = "failure" -o $2 = "1" ] ;then
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}

install_gitlab() {
    wget -P /usr/local/src/ $URL --no-check-certificate || { color  "下载失败!" 1 ;exit ; }
    PACK="/usr/local/src/${URL##*/}"
    if [ $ID = "centos" -o $ID = "rocky" ];then
       yum -y install $PACK
    else
    dpkg -i  $PACK
    fi
    if [ $? -eq 0 ];then
           color "安装gitlab完成!" 0
    else
           color "安装gitlab失败!" 1
            exit
    fi
}

config_gitlab() {
    sed -i.bak  "/^external_url.*/c external_url \'http://$HOST\'" /etc/gitlab/gitlab.rb
    cat >> /etc/gitlab/gitlab.rb <<EOF
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "252409868@qq.com"
gitlab_rails['smtp_password'] = "$SMTP_PASSWORD"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "252409868@qq.com"
gitlab_rails['initial_root_password'] = "$GITLAB_ROOT_PASSWORD"
EOF

    gitlab-ctl reconfigure
    gitlab-ctl status
    if [ $?  -eq 0 ];then
        echo
        color "Gitlab安装完成!" 0
        echo "-------------------------------------------------------------------"
        echo -e "请访问链接: \E[32;1mhttp://$HOST/\E[0m"
        echo -e "用户和密码: \E[32;1mroot/${GITLAB_ROOT_PASSWORD}\E[0m"
    else
        color "Gitlab安装失败!" 1
        exit
    fi
}

install_gitlab
config_gitlab

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享