简单研究docker下gitlab服务器的安装过程。其实是为了后面研究jenkins自动部署。

其安装也可以用docker安装或者是直接宿主机中安装,参考: https://about.gitlab.com/install

GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务。可以认为GitLab是免费版的Github,主要功能都差不多。

1. gitlab安装

1.安装相关依赖

sudo yum install -y curl policycoreutils-python openssh-server
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

2.安装Postfix以发送通知邮件

sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix

3. 下载rpm包

https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

4. 下载后上传到linux服务器

rpm -ivh ./gitlab-ce-10.0.0-ce.0.el6.x86_64.rpm 
[root@localhost gitlab]# rpm -ivh ./gitlab-ce-10.0.0-ce.0.el6.x86_64.rpm 
warning: ./gitlab-ce-10.0.0-ce.0.el6.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:gitlab-ce-10.0.0-ce.0.el6        ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.
       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ \`/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/
Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
  sudo gitlab-ctl reconfigure
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

5. 修改IP和端口

修改/etc/gitlab/gitlab.rb

vi /etc/gitlab/gitlab.rb

修改IP和端口,注意这个配置在原来存在,需要用 / external_url 查找后修改:

external_url 'http://192.168.1.128:8080'

6. 重新加载配置后重启

gitlab-ctl reconfigure
gitlab-ctl restart

补充:我启动后报错得到如下信息:

查看日志信息:

(1) 查看状态

[root@localhost ~]# gitlab-ctl status
run: gitaly: (pid 8876) 425s; run: log: (pid 713) 922s
run: gitlab-monitor: (pid 8881) 425s; run: log: (pid 683) 924s
run: gitlab-workhorse: (pid 8884) 424s; run: log: (pid 676) 924s
run: logrotate: (pid 8895) 423s; run: log: (pid 670) 924s
run: nginx: (pid 10608) 15s; run: log: (pid 669) 924s
run: node-exporter: (pid 8903) 423s; run: log: (pid 681) 924s
run: postgres-exporter: (pid 8910) 422s; run: log: (pid 711) 922s
run: postgresql: (pid 8918) 421s; run: log: (pid 701) 923s
run: prometheus: (pid 10620) 14s; run: log: (pid 674) 924s
run: redis: (pid 8931) 420s; run: log: (pid 700) 923s
run: redis-exporter: (pid 8934) 420s; run: log: (pid 702) 923s
run: sidekiq: (pid 10596) 15s; run: log: (pid 704) 923s
run: unicorn: (pid 10575) 17s; run: log: (pid 667) 925s

(2) 查看端口:

[root@localhost ~]# netstat -nao|grep 8080
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 127.0.0.1:8080          127.0.0.1:58594         TIME_WAIT   timewait (35.76/0/0)
tcp        0      0 127.0.0.1:8080          127.0.0.1:58608         TIME_WAIT   timewait (42.49/0/0)
tcp        0      0 127.0.0.1:8080          127.0.0.1:58570         TIME_WAIT   timewait (23.64/0/0)
tcp        0      0 127.0.0.1:8080          127.0.0.1:58630         TIME_WAIT   timewait (57.46/0/0)

(3)查看日志:

==> /var/log/gitlab/nginx/current <==
2020-09-13_14:07:03.59922 2020/09/13 10:07:02 [emerg] 12265#0: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2020-09-13_14:07:04.10335 2020/09/13 10:07:02 [emerg] 12265#0: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2020-09-13_14:07:04.61290 2020/09/13 10:07:02 [emerg] 12265#0: bind() to 0.0.0.0:8080 failed (98: Address already in use)
2020-09-13_14:07:05.12143 2020/09/13 10:07:02 [emerg] 12265#0: still could not bind()

发现端口被占用

(4) 解决办法:

停止服务:

gitlab-ctl stop

修改端口:

vi /etc/gitlab/gitlab.rb

下面端口修改为8888

unicorn['port'] = 8888

(5)我在中间还报错:

2020-09-13_14:14:06.12114 2020/09/13 10:14:06 redis: dialing "unix", "/var/opt/gitlab/redis/redis.socket"
2020-09-13_14:14:06.12122 2020/09/13 10:14:06 error: keywatcher: dial unix /var/opt/gitlab/redis/redis.socket: connect: no such file or directory
2020-09-13_14:16:47.33884 time="2020-09-13T10:16:47-04:00" level=info msg="Error running query on database:  pg_vacuum_analyze dial unix /var/opt/gitlab/postgresql/.s.PGSQL.5432: connect: no such file or directory\n" source="postgres_exporter.go:919"
2020-09-13_14:16:47.33891 time="2020-09-13T10:16:47-04:00" level=info msg="Error running query on database:  pg_replication dial unix /var/opt/gitlab/postgresql/.s.PGSQL.5432: connect: no such file or directory\n" source="postgres_exporter.go:919"

解决:赋予权限:

sudo chmod 755 /var/opt/gitlab/postgresql
sudo chmod 755 /var/opt/gitlab/redis
sudo chmod 755 /var/opt/gitlab/

(6) 重新加载以及重启

(7) free查看内存:

[root@localhost postgresql]# free
              total        used        free      shared  buff/cache   available
Mem:        2895200     1086972     1200244       56200      607984     1567808
Swap:       2097148           0     2097148
[root@localhost postgresql]# free
              total        used        free      shared  buff/cache   available
Mem:        2895200     1914688      371976       56420      608536      739832

发现内存在一直减少,应该是gitlab初始化没完成。

(8)等待内存稳定后访问:

2.gitlab简单使用

1. 第一次访问需要设置密码

我将密码简单的设为:  qwe123123

2. 登陆

默认账号是root,密码是上面设置的qwe123123

3.这里我们简单的创建一个项目,并且将本地的项目推送到gitlab(简单的操作下)

(1) New->new project (有点类似于github)

(2) windows客户端拉取代码修改之后推送到gitlab

liqiang@root MINGW64 /g
$ git clone http://192.168.1.128:8080/root/ssmTemplate.git
Cloning into 'ssmTemplate'...
remote: Counting objects: 31, done.
remote: Total 31 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (31/31), done.
liqiang@root MINGW64 /g
$ cd ssmTemplate/
liqiang@root MINGW64 /g/ssmTemplate (master)
mvnw*  mvnw.cmd  pom.xml  README.md  src/
liqiang@root MINGW64 /g/ssmTemplate (master)
$ echo 'xx' >> 1.txt
liqiang@root MINGW64 /g/ssmTemplate (master)
$ git add .
warning: LF will be replaced by CRLF in 1.txt.
The file will have its original line endings in your working directory.
liqiang@root MINGW64 /g/ssmTemplate (master)
$ git commit -am "xx"
[master 737c9a7] xx
 1 file changed, 1 insertion(+)
 create mode 100644 1.txt
liqiang@root MINGW64 /g/ssmTemplate (master)
$ git push origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 269 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To http://192.168.1.128:8080/root/ssmTemplate.git
   b5f9057..737c9a7  master -> master

(3)gitlab服务器查看提交记录:

(4) gitlab服务器查看文件存储位置:

默认的仓库存储路径在 /var/opt/gitlab/git-data目录下,仓库存储在子目录repositories里面,可以通过修改/etc/gitlab/gitlab.rb文件中git_data_dirs参数来自定义父目录

[root@localhost postgresql]# tree /var/opt/gitlab/git-data
/var/opt/gitlab/git-data
└── repositories
    └── root
        └── ssmTemplate.git
            ├── config
            ├── description
            ├── HEAD
            ├── hooks -> /opt/gitlab/embedded/service/gitlab-shell/hooks
            ├── hooks.old.1600008315
            │   ├── applypatch-msg.sample
            │   ├── commit-msg.sample
            │   ├── post-update.sample
  ........

补充:gitlab相关命令

 启动服务:gitlab -ctl start
 查看状态:gitlab -ctl status
 停掉服务:gitlab -ctl stop
 重启服务:gitlab -ctl restart
 让配置生效:gitlab -ctl reconfigure

补充:防火墙相关命令

[root@localhost ~]# firewall-cmd --state  
running
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# firewall-cmd --state
not running
[root@localhost ~]# systemctl disable firewalld.service
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
Sep 13 09:42:46 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewa....
Sep 13 09:42:59 localhost.localdomain systemd[1]: Started firewalld - dynamic firewal....
Sep 13 09:48:46 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewa....
Sep 13 09:48:51 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewal....
Hint: Some lines were ellipsized, use -l to show in full.

补充:gitlab修改服务器地址

vi /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml

修改后重启gitlab即可。