Gitlab搭建

    技术2022-07-12  65

    Gitlab基本简介

    GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,可通过Web界面进行访问公开的或者私人项目,非常适合在团队内部使用

    在gitlab中有三个版本,分别是CE(社区版)、EE(企业版)、OM(RPM包完整版,里面包括nginx、redis等其它软件,比较大)。这里的编译安装版,是指CE版的源码安装官网https://docs.gitlab.com/

    Gitlab(Github)与git

    Github和Git是两回事。

    Git是版本控制系统,Github是在线的基于Git的代码托管服务

    Gitlab提供的功能:

       代码托管服务

         2.     访问权限控制

         3.     问题跟踪,bug的记录和讨论

         4.     代码审查,可以查看、评论代码

         5.     社区版基于 MIT License开源完全免费

    Gitlab安装

          1、环境要求

          #CPU

          1核心的CPU,基本上可以满足需求,大概支撑100个左右的用户,不过在运行GitLab网站的同时,还需要运行多个worker以         及后台job,显得有点捉襟见肘了。

          两核心的CPU是推荐的配置,大概能支撑500个用户.

          4核心的CPU能支撑 2,000 个用户.

          8核心的CPU能支撑 5,000 个用户

       #memory

      你需要至少4GB的可寻址内存(RAM交换)来安装和使用GitLab!操作系统和任何其他正在运行的应用程序也将使用内存,因      此请记住,在运行GitLab之前,您至少需要4GB的可用空间。使用更少的内存GitLab将在重新配置运行期间给出奇怪的错误,        在使用过程中发生500个错误.

    1GBRAM + 3GB of swap is the absolute minimum but we strongly adviseagainst this amount of memory. See the unicorn worker section belowfor more advice.

    2GBRAM + 2GB swap supports up to 100 users but it will be very slow

    4GBRAM isthe recommended memory size for all installations and supportsup to 100 users

    #Database 

    PostgreSQL

    MySQL/MariaDB

    强烈推荐使用PostgreSQL而不是MySQL/ MariaDB,因为GitLab的所有功能都不能与MySQL/ MariaDB一起使用。例如,MySQL没有正确的功能来以有效的方式支持嵌套组.

    运行数据库的服务器应至少有5-10 GB的可用存储空间,尽管具体要求取决于GitLab安装的大小

    #Node exporter

    节点导出器允许您测量各种机器资源,如内存,磁盘和CPU利用率。默认端口9100

     

    2、安装

    1) 关闭selinux和相关基本配置

    #下面的命令实现永久关闭SELinux/etc/selinux/config需要重启系统之后生效

    7行 SELINUX=permissive

    #下面的命令实现临时关闭SELinux

    [root@git ~]# getenforce #查看selinux状态 [root@git ~]# setenforce 0/1 #切换selinux状态

    #永久修改下主机名,需要重启系统之后生效

    Redhat6中修改 [root@git ~]# vi /etc/sysconfig/network NETWORKING=yes HOSTNAME=git.server.com #修改成你自己的主机名 Redhat7中修改 [root@noede1 ~]# vi /etc/hostname gitlab.server.com

    #添加域名

    [root@git ~]#cat /etc/hosts 192.168.201.131   gitlab.server.co

    2) 关闭防火墙

    Redhat6 [root@git yum.repos.d]# iptables -F              #临时关闭 [root@git yum.repos.d]# service iptables stop    #关闭防火墙服务 [root@git yum.repos.d]# chkconfig iptables off   #禁用防火墙 [root@git yum.repos.d]# chkconfig iptables --list Redhat7 [root@git gitlab_pack]# systemctl stop firewalld.service

     

    3) 配置yum源

    [root@git yum.repos.d]# cat gitlab.repo

    [gitlab-ce]

    name=gitlab-ce

    baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7

    repo_gpgcheck=0

    gpgcheck=0

    enabled=1

    gpgkey=https://packages.gitlab.com/gpg.key

    注解:

    如果想要在centos6系列上安装,只需把el7修改成el6

     

    一、 Gitlab安装方式

    编译安装

    优点:可定制性强。数据库既可以选择MySQL,也可以选择PostgreSQL;服务器既可以选择Apache,也可以选择Nginx。

    缺点:国外的源不稳定,被墙时,依赖软件包难以下载。配置流程繁琐、复杂,容易出现各种各样的问题。依赖关系多,不容易管理,卸载GitLab相对麻烦。

    [root@git yum.repos.d]# yum install curlopenssh-server openssh-clients postfix -y

    [root@git yum.repos.d]# yum install gitlab-ce-8.8.0-y (选择自己想要的版本)

    rpm包安装(此处采用RPM包安装

    优点:安装过程简单,安装速度快。采用rpm包安装方式,安装的软件包便于管理。

    缺点:数据库默认采用PostgreSQL,服务器默认采用Nginx,不容易定制

    可以单独下载rpm包http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/

    https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el6/gitlab-ce-7.10.0~omnibus.2-1.x86_64.rpm(本地正在使用的7.10.0-2版本)

    [root@git gitlab_pack]# rpm -ivh gitlab-ce-8.8.0-ce.0.el6.x86_64.rpm

    Preparing...                         ################################# [100%]

    Updating / installing...

      1:gitlab-ce-8.8.0-ce.0.el6        ################################# [100%]

    gitlab: Thank you for installing GitLab!

    注意:

           rpm 安装Gitlab的默认位置在/opt下

    二、 修改配置文件

    [root@gitlab ~]# vim  /etc/gitlab/gitlab.rb

    external_url 'http://gitlab.server.com'

    external_url 修改成自己的ip或者域名

    #修改配置文件之后,需要重新是配置文件生效下,初始化下

    [root@gitlab ~]#gitlab-ctl  reconfigure #这里会花费一定的时间(1-10min),如果这里内存小,将会花费大量时间

    #如果在此期间没有出现error,证明成功

     

    三、启动Gitlab服务

    #启动服务

    # gitlab-ctl start

    #停止服务

    # gitlab-ctl stop

    #重启服务

    # gitlab-ctl restart

    #状态

    #gitlab-ctl status

    #监控

    #gitlab-ctl  tailunicorn 监控unicorn日志

    #gitlab-ctl  tail

    四、 登陆gitlab

    访问地址http://ip

    由于第一次登陆,需要设置密码(这里的密码是管理员密码,管理员账号是root)

    sudo chmod -R o+x /var/opt/gitlab/gitlab-railsUsername: rootPassword: 5iveL!fe

     

    五、Gitlab命令使用

    start          启动所有服务

    stop          关闭所有服务

    restart          重启所有服务

    status          查看所有服务状态

    tail              查看日志信息

    service-list      列举所有启动服务

    graceful-kill      平稳停止一个服务

    reconfigure      修改配置文件后需要重新加载

    show-config      查看所有服务配置文件信息

    uninstall      卸载这个软件

    cleanse          删除gitlab数据,重新白手起家

     

    六、Gitlab配置详解

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

    unicorn配置文件 /var/opt/gitlab/gitlab-rails/etc/unicorn.rb

    nginx配置文件 /var/opt/gitlab/nginx/conf/gitlab-http.conf

    gitlab仓库默认位置 /var/opt/gitlab/git-data/repositories

     

     

    七、Gitlab强制修改用户密码

    在root用户下,执行

    [root@svr34 bin]# gitlab-rails console production

    获得用户数据,修改用户密码

    [root@svr34 bin]# gitlab-rails console production

    Loading production environment (Rails 4.2.5.2)

    irb(main):001:0> user = User.where(id: 1).first

    => #<User id: 1, email: "admin@example.com", ...

    irb(main):002:0> user.password=12345678

    => 12345678

    irb(main):003:0> user.password_confirmation=12345678

    => 12345678

    irb(main):004:0> user.save!

    => true

    irb(main):005:0> quit

    七、Ssh-key生成

    如果在windows上下载git安装后执行以下命令(这里的git是git工具并非是gitlab)

    $ git config --global user.name "thunder"                 使用的用户

    $ git config --global user.email "thunder@bpit888.com"    邮箱

    $ ssh-keygen -t rsa

    生成的key在C:\Users\aaaaa\.ssh\id_rsa.pub  (aaaa是你现在登录PC用户)

    将生成的key复制到gitlab自己的用户下面,你可以去添加多个,可以移除,但是都是在你自己的用户下,管理员给其他人配置也需要去他人的用户下面

    如果是linux系统,则直接切换到用户。

    [root@legao thunder]# su thunder

    [thunder@legao ~]$ cd /home

    [thunder@legao home]$ ssh-keygen -t rsa    (全部回车)

    Generating public/private rsa key pair.

    Enter file in which to save the key (/home/thunder/.ssh/id_rsa):

    Created directory '/home/thunder/.ssh'.

    [thunder@legao home]$ cd thunder/.ssh

    [thunder@legao .ssh]$ ll

    total 8

    -rw------- 1 thunder wheel 1675 Feb  7 11:59 id_rsa

    -rw-r--r-- 1 thunder wheel  399 Feb  7 11:59 id_rsa.pub

    [thunder@legao .ssh]$ cat id_rsa.pub 将cat出来的公钥给到gitlab既可,参考上图

    八、Gitlab的数据备份与迁移

    备份

    在备份的时候,gitlab的服务是必须是启动的,只需要停止unicron、sidekiq,在无人使用的情况下操作,否则数据不完整

    [root@localhost gitlab]# gitlab-ctl stop unicorn

    [root@localhost gitlab]# gitlab-ctl stop sidekiq

    [root@localhost gitlab]# gitlab-rake gitlab:backup:create (数据备份命令)

    可以在/var/opt/gitlab/backups中查看备份的压缩包(此目录是默认的,如果要修改请在配置文件中/etc/gitlab/gitlab.rb修改,修改完记得初始化

    [root@www backups]# pwd

    /var/opt/gitlab/backups

    [root@www backups]# ll

    总用量 1913552

    -rwxrwxrwx 1 root root 1959475200 2月   7 00:06 1517932218_gitlab_backup.tar

     

    迁移

    在更换gitlab服务器的时候一般会将源gitlab服务器上的数据迁移过来,但两者的版本必须一致,迁移的时候gitlab的服务是必须是启动的,只需要停止unicron、sidekiq

    [root@newgitlab backups]# gitlab-ctl stop unicron

    [root@newgitlab backups]# gitlab-ctl stop sidekiq

    进入默认目录/var/opt/gitlab/backups远程拷贝数据压缩包

    #scp -r root@192.168.1.159:/var/opt/gitlab/backups/*_gitlab_backup.tar /var/opt/gitlab/backups/

    # chmod 777 /var/opt/gitlab/backups/*_gitlab_backup.tar

    # cd /var/opt/gitlab/backups/

    # gitlab-rake gitlab:backup:restore BACKUP=1517932218 (数字是压缩包的编号)

    # gitlab-ctl restart     (迁移完全后必须重启)

     

    九、Gitlab的基本使用

    下载(克隆)文件

    $ git config --global user.name "thunder"                 使用的用户

    $ git config --global user.email "thunder@bpit888.com"    邮箱

    $ git clone http://www.gitlab.com/izumi/lc_wap.git        下载到本地(http/ssh)

    上传文件

    $ git init                                             初始化

    $ git remote add origin git@www.newgitlab.com:root/nysupervisor.git   

    $ git remote rm origin                                删除源始的

    $ git remote add origin git@www.newgitlab.com:root/nysupervisor.git

    $ git add .                                            添加目录下面所有文件

    $ git commit -m "first test"                            提交

    $ git push -u origin master                            push主分支

     

    Processed: 0.009, SQL: 9