0
点赞
收藏
分享

微信扫一扫

基于传统命令行和Docker,搭建GitLab 14.9 版本(最新版本),

微言记 2022-03-26 阅读 61
云原生

GitLab概述

GitLab概述:
是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。
Ruby on Rails 是一个可以使你开发、部署、维护 web 应用程序变得简单的框架。
GitLab拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。

GitLab官网:https://about.gitlab.com/
在这里插入图片描述

GitLab和GitHub的区别

也许在你看到上面GitLab介绍后,你会有个疑问,它怎么和GitHub那么像。没错二者确实有很大的关系和相似之处,可以说GitLab就是个高仿版的GitHub。

相同点:二者都是基于web的Git仓库,在很大程度上GitLab是仿照GitHub来做的,它们都提供了分享开源项目的平台,为开发团队提供了存储、分享、发布和合作开发项目的中心化云存储的场所。

不同点:GitHub如果要使用私有仓库,是需要付费的。GitLab可以在上面创建私人的免费仓库。GitLab让开发团队对他们的代码仓库拥有更多的控制,总的来说,如果你想远离 GitHub , GitLab 是首选。它可以从不同的来源导入项目和问题,包括 GitHub ,这使得迁移过程无忧无虑。 GitLab 界面设计精良,干净,直观,并且在用户体验和功能方面与 GitHub 相近。

Git相关概念

1、Git:是一种版本控制系统,是一个命令,是一种工具。
2、Gitlib:是用于实现git功能的开发库。
3、Github:是一个基于git实现的在线代码托管仓库,包含一个网站界面,向互联网开放。
4、Gitlab:是一个基于git实现的在线代码仓库托管软件,一般用于在企业内网搭建,相当于个人版的代码托管仓库。

Gitlab的版本:

1、Gitlab-ce:社区版(可以免费使用)
2、Gitlab-ee:企业版(收费)
3、GitLab 托管版本。

实验环境说明:

Win10+16.0的VMWare wordstation
CentOS7.5,本机IP:192.168.9.10
最低需要配置:2G+60G(稍微有点卡)

安装GitLab

1、防火墙配置

systemctl stop firewalld
systemctl enable firewalld
iptables -F

2、安装依赖

yum -y install curl policycoreutils-python openssh-server perl 

3、安装 Postfix(可选)
使用Postfix发送通知电子邮件。如果想使用其他解决方案发送电子邮件,请跳过此步骤并在安装GitLab后配置外部SMTP服务器。

yum -y install postfix 
systemctl enable postfix 
systemctl start postfix 

4、配置Yum源
这里咱们选用清华源镜像,速度比较快
清华源gitlab-ce镜像官网:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/

[root@gitlab ~]# vi /etc/yum.repos.d/gitlab.repo 
[gitlab]
name=gitlab
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
enabled=1
gpgcheck=0

5、直接安装最新版本(大约1个G)

yum -y install gitlab-ce 

6、如果想安装指定的版本

yum -y install gitlab-ce-x.x.x 
//x.x.x代表版本号 

在这里插入图片描述

配置GitLab

安装完之后,gitlab是不能用systemctl服务其管理的,有个专属命令:gitlab-ctl
1、配置Gitlab服务的IP地址

vi /etc/gitlab/gitlab.rb 
...
external_url 'http://192.168.9.10'         #指定IP,默认是80端口因为由GitLab中Nginx提供Web服务,如果向指定其它的端口直接此写成:IP:999
...

2、其实修改完这一步就可以初始化配置了

gitlab-ctl reconfigure 
//第一次配置时间较长,请耐心等待 

3、启动Gitlab

gitlab-ctl restart

4、找到root用户的密码
在新版本中,默认的root密码是放在一个目录中的:

[root@gitlab ~]# cat /etc/gitlab/initial_root_password 
# WARNING: This value is valid only in the following conditions
#          1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
#          2. Password hasn't been changed manually, either via UI or via command line.
#
#          If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

Password: Nx8rvFZe0leYdaKGgkPIAaAvOd60Sd0QVb4P/LrOQ7U=             #这个就是root用户的密码,它默认就在24h后被自动删除的,因此我们在登陆进gitlab中后首先需要更改密码

# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.

5、Web访问Gitlab
用户:admin
密码:就是那一串字符串
在这里插入图片描述
6、修改密码
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Gitlab相关目录

1、/var/opt/gitlab/git-data/repositories/:仓库默认存储目录
2、/opt/gitlab: 应用程序代码和相应的依赖程序
3、/var/opt/gitlab:gitlab各个组件的数据目录 gitlab-ctl reconfigure 命令编译后的应用数据和配置文件,不需要人为修改配置
4、/etc/gitlab: 配置文件目录
5、/etc/gitlab/gitlab.rb gitlab的主配置文件
6、/var/log/gitlab:此目录下存放了 gitlab 各个组件产生的日志
7、/var/opt/gitlab/backups/:备份文件生成的目录

gitlab服务构成

我们可以简单的使用命令去查看gitlab的各个服务

[root@gitlab ~]# gitlab-ctl status
run: alertmanager: (pid 37391) 7897s; run: log: (pid 23446) 86173s
run: gitaly: (pid 27160) 84993s; run: log: (pid 22723) 86306s
run: gitlab-exporter: (pid 27178) 84992s; run: log: (pid 23331) 86193s
run: gitlab-kas: (pid 37373) 7898s; run: log: (pid 23048) 86287s
run: gitlab-workhorse: (pid 27190) 84991s; run: log: (pid 23222) 86211s
run: grafana: (pid 37402) 7897s; run: log: (pid 23724) 86115s
run: logrotate: (pid 46634) 2277s; run: log: (pid 22627) 86320s
run: nginx: (pid 37383) 7898s; run: log: (pid 23246) 86205s
run: node-exporter: (pid 27226) 84990s; run: log: (pid 23298) 86199s
run: postgres-exporter: (pid 27232) 84989s; run: log: (pid 23484) 86167s
run: postgresql: (pid 27242) 84989s; run: log: (pid 22887) 86295s
run: prometheus: (pid 27251) 84988s; run: log: (pid 23408) 86179s
run: puma: (pid 37334) 7931s; run: log: (pid 23131) 86223s
run: redis: (pid 27268) 84987s; run: log: (pid 22667) 86314s
run: redis-exporter: (pid 27274) 84987s; run: log: (pid 23372) 86185s
run: sidekiq: (pid 37309) 7938s; run: log: (pid 23159) 86217s

Nginx:静态web服务器。
gitlab-shell:用于处理Git命令和修改authorized keys列表。
gitlab-workhorse: 轻量级的反向代理服务器。
logrotate:日志文件管理工具。
postgresql:数据库。
redis:缓存数据库。
sidekiq:用于在后台执行队列任务(异步执行)。
unicorn:An HTTP server for Rack applications,GitLab Rails应用是托管在这个服务器上面的。
1、Gitlab的工作流程:
在这里插入图片描述
GitLab Shell
GitLab Shell有两个作用:为GitLab处理Git命令、修改authorized keys列表。
当通过SSH访问GitLab Server时,GitLab Shell会:
限制执行预定义好的Git命令(git push, git pull, git annex)
调用GitLab Rails API 检查权限
执行pre-receive钩子(在GitLab企业版中叫做Git钩子)
执行你请求的动作 处理GitLab的post-receive动作
处理自定义的post-receive动作
当通过http(s)访问GitLab Server时,工作流程取决于你是从Git仓库拉取(pull)代码还是向git仓库推送(push)代码。
如果你是从Git仓库拉取(pull)代码,GitLab Rails应用会全权负责处理用户鉴权和执行Git命令的工作;
如果你是向Git仓库推送(push)代码,GitLab Rails应用既不会进行用户鉴权也不会执行Git命令,它会把以下工作交由GitLab Shell进行处理:
调用GitLab Rails API 检查权限
执行pre-receive钩子(在GitLab企业版中叫做Git钩子)
执行你请求的动作
处理GitLab的post-receive动作
处理自定义的post-receive动作

GitLab Workhorse
GitLab Workhorse是一个敏捷的反向代理。它会处理一些大的HTTP请求,比如文件上传、文件下载、Git push/pull和Git包下载。其它请求会反向代理到GitLab Rails应用,即反向代理给后端的unicorn。

这里只是简单介绍以下Gitlab的服务构成,更多知识,我还需要继续学习,完善。

Gitlab中文语言包(汉化)

Gitlab在12版本之后就自带中文了,所以我们不用在需要自打补丁,如下设置:

在这里插入图片描述
向下拉,找到"Language":
在这里插入图片描述
然后点击下方的"save change",最后在刷新即可:
在这里插入图片描述

Gitlab备份

如果是生产环境,备份是必须的。需要备份的文件:配置文件和数据文件。

1、备份配置文件

tar -czvf $(date "+etc-gitlab-%s.tar").tar.gz /etc/gitlab/

2、备份数据文件
默认数据备份目录是/var/opt/gitlab/backups,手动创建备份文件:

gitlab-rake gitlab:backup:create

更加贴切的是指定个计划任务(crontab),这里不在演示

Gitlab相关命令

0、帮助命令

gitlab-ctl --help

1、重新加载gitlab配置文件:

gitlab-ctl reconfigure

2、重新启动gitlab:

gitlab-ctl restart

3、停止服务

gitlab-ctl stop

4、查看某个组件的日志

gitlab-ctl tail nginx

5、查看所有组件运行状态

gitlab-ctl status

6、数据备份

gitlab-backup

这里列出最常用的命令,其它的慢慢学习。

基于Docker安装GitLab(14.9版本)

1、安装Docker

# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start

2、使用阿里云配置镜像加速

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://pve03e8m.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

3、拉取Gitlab镜像
镜像我是从Github上拉取的
镜像地址:
https://hub.docker.com/r/gitlab/gitlab-ce/

#拉取镜像
docker pull gitlab/gitlab-ce:14.9.0-ce.0
#查看镜像
[root@gitlab ~]# docker images
REPOSITORY         TAG           IMAGE ID       CREATED      SIZE
gitlab/gitlab-ce   14.9.0-ce.0   47e6c191a128   4 days ago   2.45GB

4、启动容器

docker run \
 -itd \
 --publish 443:443 --publish 81:80 --publish 1022:22 \        #--publish(相当于-p端口映射,前面是宿主机的端口后面是容器内gitlab服务的端口)
 -v /usr/local/gitlab/etc:/etc/gitlab \                       #挂载卷
 -v /usr/local/gitlab/log:/var/log/gitlab \
 -v /usr/local/gitlab/opt:/var/opt/gitlab \
 --restart always \                              #重启容器
 --privileged=true \                             #授予root权限
 --name gitlab \                                 #指定容器名字
 gitlab/gitlab-ce:14.9.0-ce.0                    #镜像名称

5、获取root的输出密码:
由于我这里弄的挂载卷,所以我可以之即通过虚拟机的/usr/local/gitlab/etc去查看

[root@gitlab ~]# vi /usr/local/gitlab/etc/initial_root_password 
# WARNING: This value is valid only in the following conditions
#          1. If provided manually (either via `GITLAB_ROOT_PASSWORD` environment variable or via `gitlab_rails['initial_root_password']` setting in `gitlab.rb`, it was provided before database was seeded for the first time (usually, the first reconfigure run).
#          2. Password hasn't been changed manually, either via UI or via command line.
#
#          If the password shown here doesn't work, you must reset the admin password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.

Password: 9LtYxC2zBzAihpylxBzd4NxXJiILJfpZwbrx6Wancc8=                     #root的初始密码。

# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.

下面就可以在Web界面登陆了:
用户:root
密码:9LtYxC2zBzAihpylxBzd4NxXJiILJfpZwbrx6Wancc8=
后续的更改密码等操作和上面的就一样了。

基于Docker部署的gitlab配置

上面能ok运行Gitlab没问题,但是在Gitlab上创建项目的时候,生成项目的URL访问地址是按照容器的hostname来生成的,也就是容器id。作为gitlab服务器,我们需要一个固定的URL访问地址,于是需要进入到容器去配置Gitlab的主配置文件:gitlab.rb。当然由于在宿主机挂载的卷也可以在宿主机上操作,但是好需要重启Gitlab,所以进入到容器操作更方便。

1、进入容器

#进入容器
docker exec -it gitlab  /bin/bash

2、修改配置

vi /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml
...
gitlab:
    ## Web server settings (note: host is the FQDN, do not include http://)
    host: 192.168.9.10                    #改为Gitlab服务器的ip地址
    port: 81                              #修改为映射的端口
    https: false

    # The maximum time puma can spend on the request. This needs to be smaller than the worker timeout.
    # Default is 95% of the worker timeout
    max_request_duration_seconds: 57

    # Uncommment this line below if your ssh host is different from HTTP/HTTPS one
    # (you'd obviously need to replace ssh.host_example.com with your own host).
    # Otherwise, ssh host will be set to the `host:` value above
    ssh_host: 192.168.9.10:1022              #一样的操作。
...

之后exit退出容器即可。

Gitlab就简单介绍到这,后续在不断学习…

举报

相关推荐

0 条评论