Linux centos7 docker部署gitlab私有服务器

陆承宣
2023-12-01

Docker部署gitlab私有服务器,那么,这里必须要有两个软件啦,一个是Docker,一个是gitlab。鉴于很多同学还是不太了解这两个东西,并且观看了不少的其他大神的相关博客,发现对于Docker和gitlab的版本问题基本不讲,如果是测试练习用,当然没什么问题,如果是需要上生产,提供给开发同学使用,那么,毫无疑问,算是耍流氓啦!~~~

前言

Docker-----服务虚拟化,容器化的先锋产品,目前来说,业内首选,好处实在是太多啦,比如,环境隔离,沙箱环境,轻量化,可灵活迁移,开发环境打包封装等等,Docker就像一道美味的菜肴,从色香味来说,总有一个地方能够抓住男人和女人的胃。(go语言编写的软件,运行效率也比较高哦)

Docker有两种版本ce社区版和ee专业收费版,一般情况下指的是ce社区版,此处需要注意,除非特指。

gitlab----GitLab 是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面进行访问公开的或者私人项目。它拥有与 Github 类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序 (Wall) 进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。  (ruby语言混合python编写的一个代码托管私有平台)

好啦,说人话,gitlab是一个项目集合,一个开发使用的管理代码平台,有简单易用的web界面,可以当做一个私有的GitHub,因此,有用户管理系统,权限管理系统,简单的网际通信,文件碎片化分布式存储系统。(gitlab有内置用到nginx,postgresql,redis,因此,物理机部署gitlab会造成一定的混乱,Docker就很好的解决了这个问题)。

gitlab有两种版本,ce社区版和ee专业收费版,一般情况下指的是ce社区版,此处需要注意,除非特指。本文内都是使用Docker和gitlab的ce版本。

版本介绍

Docker的版本现在推荐使用docker-ce-20.10.7版本,使用该版本的原因是基于安全方面的考虑,18版本太老旧,19有安全方面的问题。

gitlab的版本现在推荐使用gitlab-14.1版本,使用该版本的原因同样是基于安全方面的考虑。12版本和13版本都有比较严重的安全漏洞。目前所了解的是,14版本功能更加丰富,有支持k8s,这个好像是有点不好抗拒的诱惑啊。

Docker和gitlab的安装方式

Docker的安装通常是二进制安装包方式安装,这个就不需要过多介绍了,详情参见我的博客:docker的离线安装以及本地化配置_zsk_john的博客-CSDN博客

gitlab的安装方式通常是yum在线安装,安装方法为:

一,yum安装方式(最为简单)

配置yum源

vim /etc/yum.repos.d/gitlab-ce.repo

复制以下内容:

[gitlab-ce]

name=Gitlab CE Repository

baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/

gpgcheck=0

enabled=1

在执行命令,指定需要安装的版本

yum install gitlab-ce-x.x.x

二,离线安装方式

配置本地yum源仓库,使用本机系统ISO文件挂载,执行命令安装gitlab相关依赖:

(1)yum install policycoreutils libsemanage audit-libs libselinux libsepol python-IPy setools-libs  checkpolicy policycoreutils-python -y

(2)rpm -ivh gitlab-ce-12.5.0-ce.0.el7.x86_64.rpm 此文件在百度网盘:链接:https://pan.baidu.com/s/1md_1-HFz2sADUSV7t8vVAA 
提取码:gitc 

解压这个文件,找到这个RPM文件

(3)vim /etc/gitlab/gitlab.rb

external_url 'http://192.168.43.11:24973' 将本机IP和自定义端口写入文件保存

(4)gitlab-ctl reconfigure    (只有gitlab的主配置文件 gitlab.rb文件有改动,执行此命令使得修改生效)

(5)netstat -antup |grep nginx

netstat -antup|grep nginx
tcp        0      0 0.0.0.0:24973           0.0.0.0:*               LISTEN      2968/nginx: master  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      960/nginx: master p 
tcp        0      0 0.0.0.0:8060            0.0.0.0:*               LISTEN      2968/nginx: master  
tcp        0      0 127.0.0.1:8060          127.0.0.1:52080         ESTABLISHED 2972/nginx: worker

此时,本机ip+2968端口就可以登录gitlab啦(gitlab12 13版本会直接提示你重置root密码,14版本需要find / -name initial_root_password,通常在/opt/gitlab/etc/gitlab/这个路径下,登录密码保存在这个文件内。账号都是root




 

上面简单介绍了一下yum和离线安装方式,现在开始上正菜,docker-compose编排方式安装gitlab-ce-14.4.2

一,  docker环境的配置

这个前面已经说过了,看上面的内容即可,基本内容,不在重复。

二,拉取gitlab的镜像

命令:docker search gitlab  选择第一个镜像拉取:

[root@hdp-1 ~]# docker search gitlab
NAME                                         DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
gitlab/gitlab-ce                             GitLab Community Edition docker image based …   3460                 [OK]
sameersbn/gitlab                             Dockerfile to build a GitLab image for the D…   1181                 [OK]
gitlab/gitlab-runner                         GitLab CI Multi Runner used to fetch and run…   749                  [OK]
gitlab/gitlab-ee                             GitLab Enterprise Edition docker image based…   288                  
twang2218/gitlab-ce-zh                       汉化的 GitLab 社区版 Docker Image                     251                  [OK]
jangrewe/gitlab-ci-android                   GitLab CI image for building Android apps       60                   [OK]
edbizarro/gitlab-ci-pipeline-php             Docker images for build and test PHP applica…   38                   
ulm0/gitlab                                  GitLab Docker image for ARM                     36                   
gitlab/gitlab-runner-helper                                                                  35                   
klud/gitlab-runner                           GitLab Runner for ARM devices                   27                   
centurylink/gitlab                           This image uses the image from sameersbn / g…   26                   [OK]
ciricihq/gitlab-sonar-scanner                Gitlab Docker container to run sonar-scanner…   16                   [OK]
gitlab/gitlab-ce-qa                          GitLab QA has a test suite that allows end-t…   6                    
timoschwarzer/gitlab-monitor                 A browser-based monitor dashboard for GitLab…   4                    [OK]
samcontesse/gitlab-merge-request-resource    A concourse resource to check for new merge …   3                    [OK]
sgillespie/gitlab-runner                     A Dockerized GitLab Runner that automaticall…   3                    [OK]
mastertinner/gitlab-merge-request-resource   A concourse resource to check for new merge …   2                    [OK]
stanfordlegion/gitlab-ci                     An image for Gitlab CI.                         1                    [OK]
boldcommerce/gitlab-merge-request-resource   Reverted build of Swisscom's resource until …   1                    [OK]
lorands/gitlab-merge-request-resource        This is a fork of swisscom/gitlab-merge-requ…   1                    [OK]
adamkl/gitlab-merge-request-resource         Customized with support for gitlab ssh on di…   0                    [OK]
edtan1/gitlab-merge-request-resource         GitLab Merge Request resource for ConcourseCI   0                    [OK]
onaci/gitlab-runner                          auto-registering gitlab runner                  0                    
gitlab/gitlab-ee-qa                          GitLab QA has a test suite that allows end-t…   0                    
mvisonneau/gitlab-ci-pipelines-exporter      Prometheus / OpenMetrics exporter for GitLab…   0                    [OK]

docker pull gitlab/gitlab-ce   拉取镜像

docker images  查看镜像文件,这个文件还蛮大,刚制作好的,2.31G比较大,输出如下:

[root@hdp-1 ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED       SIZE
gitlab/gitlab-ce   latest    7b8db4329c1c   10 days ago   2.31GB

三,编写docker-compose 编排文件, 文件名称为gitla.yaml ,此文件存放位置任意,本例中放在了root目录下,文件内容如下:

version: '3.1'
services:
  gitlab:
    container_name: gitlab
    image: gitlab/gitlab-ce
    restart: always
    environment:
      TZ: Asia/Shanghai
      GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://192.168.88.11:12380'
    ports:
      - '12380:12380'
      - '443:443'
      - '1222:22'
    volumes:
      - /opt/gitlab/etc:/etc/gitlab
      - /opt/gitlab/log:/var/log/gitlab
      - /opt/gitlab/opt:/var/opt/gitlab
    privileged: true
    logging:
      driver: "json-file"
      options:
        max-file: '3'
        max-size: "20mi"

这里需要注意volume挂载和端口映射这两个地方,/opt/gitlab/这个宿主机目录请根据自己的实际磁盘大小来设定,比如,/opt所在分区太小了,就换到别的空闲大的分区、

第一次启动gitlab。拉取容器内的文件到宿主机的挂载目录。此时,我们应该能够看到/opt/gitlab/目录下有文件了

[root@hdp-1 ~]# docker-compose -f gitlab.yaml up -d
WARNING: Found orphan containers (mysql-5.7.22) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating gitlab ... done

查看/opt/gitlab/目录 

[root@hdp-1 ~]# ls -alh /opt/gitlab/
total 2.7M
drwxr-xr-x  13 root root  250 Nov 18 15:41 .
drwxr-xr-x.  4 root root   38 Nov 17 21:07 ..
drwxr-xr-x   2 root root  172 Nov 11 23:33 bin
-rw-r--r--   1 root root 194K Sep 27 22:46 dependency_licenses.json
drwxr-xr-x  18 root root  215 Nov 11 23:35 embedded
drwxrwxr-x  13 root root 4.0K Nov 18 22:19 etc
drwxr-xr-x   2 root root  291 Sep 27 22:46 init
-rw-r--r--   1 root root 2.5M Sep 27 22:46 LICENSE
drwxr-xr-x   2 root root  278 Nov 11 23:34 licenses
drwxr-xr-x   2 root root 4.0K Nov 11 23:34 LICENSES
drwxr-xr-x  20 root root  326 Nov 18 15:50 log
drwxr-xr-x  20 root root 4.0K Nov 18 23:11 opt
drwxr-xr-x   2 root root  276 Sep 27 22:46 service
drwxr-xr-x  18 root root  291 Sep 27 22:46 sv
drwxr-xr-x   4 root root   33 Nov 11 21:05 var
-rw-r--r--   1 root root  31K Sep 27 22:46 version-manifest.json
-rw-r--r--   1 root root  12K Sep 27 22:46 version-manifest.txt

此时,需要停止一次容器,执行命令(这里的警告可以忽略,主要是我启动了一个MySQL镜像,这个镜像也使用的是默认网络桥接模式):

[root@hdp-1 ~]# docker-compose -f gitlab.yaml down
Stopping gitlab ... done
WARNING: Found orphan containers (mysql-5.7.22) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Removing gitlab ... done
Removing network root_default
ERROR: error while removing network: network root_default id eb5e03288eeb7b2e3a9a107c5966c1b90bcab34a71d3ad5051a56fa2c2054c37 has active endpoints

编辑宿主机内所拉取的gitlab容器内的文件gitlab.rb(此文件是gitlab的主配置文件)

放开注释,将宿主机的ip写入这一行:

##! https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
external_url 'http://192.168.88.11:12380'

## Roles for multi-instance GitLab

 第二次启动gitlab,进入容器,也就是总共执行三个命令docker-compose -f gitlab.yaml up -d docker ps -a  docker exec -it gitlab /bin/bash

这里注意,第二个命令docker ps -a 的输出中(health: starting)表示正在启动服务,但没完全好,status是Up 4 minutes (healthy) 的时候表示服务完全启动好了

[root@hdp-1 ~]# docker-compose -f gitlab.yaml up -d
WARNING: Found orphan containers (mysql-5.7.22) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating gitlab ... done


[root@hdp-1 ~]# docker ps -a
CONTAINER ID   IMAGE              COMMAND                  CREATED          STATUS                             PORTS                                                                        NAMES
1bbd9f6269c3   gitlab/gitlab-ce   "/assets/wrapper"        45 seconds ago   Up 44 seconds (health: starting)   0.0.0.0:443->443/tcp, 80/tcp, 0.0.0.0:2380->2380/tcp, 0.0.0.0:1222->22/tcp   gitlab
218c5ed40c4c   mysql:5.7.22       "docker-entrypoint.s…"   8 hours ago      Up 3 hours                         0.0.0.0:3306->3306/tcp  

[root@hdp-1 ~]# docker exec -it gitlab /bin/bash
root@1bbd9f6269c3:/# 

 进入容器后执行命令 gitlab-ctl reconfigure  大概的输出如下:

root@1bbd9f6269c3:/# gitlab-ctl reconfigure
Starting Chef Infra Client, version 15.17.4
resolving cookbooks for run list: ["gitlab"]
Synchronizing Cookbooks:
  - gitlab (0.0.1)
  - package (0.1.0)
  - postgresql (0.1.0)
  - logrotate (0.1.0)
  - redis (0.1.0)
。。。
Recipe: gitlab::database_reindexing_disable
  * crond_job[database-reindexing] action delete
    * file[/var/opt/gitlab/crond/database-reindexing] action delete (up to date)
     (up to date)

Running handlers:
Running handlers complete
Chef Infra Client finished, 1/723 resources updated in 07 seconds
gitlab Reconfigured!

 第三次启动就可以正式使用啦(两个命令 docker-compose -f gitlab.yaml down  docker-compose -f gitlab.yaml up -d):

[root@hdp-1 ~]# docker-compose -f gitlab.yaml down
Stopping gitlab ... done
WARNING: Found orphan containers (mysql-5.7.22) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Removing gitlab ... done
Removing network root_default
ERROR: error while removing network: network root_default id eb5e03288eeb7b2e3a9a107c5966c1b90bcab34a71d3ad5051a56fa2c2054c37 has active endpoints
[root@hdp-1 ~]# docker-compose -f gitlab.yaml up -d
WARNING: Found orphan containers (mysql-5.7.22) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.
Creating gitlab ... done
[root@hdp-1 ~]# docker ps -a
CONTAINER ID   IMAGE              COMMAND                  CREATED         STATUS                            PORTS                                                                        NAMES
1d4126cb05b9   gitlab/gitlab-ce   "/assets/wrapper"        7 seconds ago   Up 6 seconds (health: starting)   0.0.0.0:443->443/tcp, 80/tcp, 0.0.0.0:12380->12380/tcp, 0.0.0.0:1222->22/tcp   gitlab
218c5ed40c4c   mysql:5.7.22       "docker-entrypoint.s…"   8 hours ago     Up 3 hours                        0.0.0.0:3306->3306/tcp                                                       mysql-5.7.22

docker ps -a  查看容器状态,直到状态是(healthy)时,就可以打开浏览器登录gitlab啦(大概需要等待5分钟,看服务器的配置啦,内存越高越快哦):

第一次登录的密码存放在宿主机的这个文件内,也就是容器内的这个文件内:

/opt/gitlab/etc/initial_root_password      

[root@hdp-1 ~]# cat /opt/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: unD/I9/kBatpsRZyJ6XBqE/5XlfWFBJo9vH8op1QrDg=

# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
[root@hdp-1 ~]# docker exec -it gitlab /bin/bash
root@09eaa091f1ff:/# 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: unD/I9/kBatpsRZyJ6XBqE/5XlfWFBJo9vH8op1QrDg=

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

 如果超过24小时没有登陆gitlab,密码失效了怎么办?

在进入容器,执行命令 gitlab-ctl reconfigure 重新生成密码即可啦。

附录:

配置邮件服务器

如果是非内网环境,可连接外网,那么,想要让 GitLab 给你发送邮件,还要配置一下邮件服务器,这里以QQ邮箱的 IMAP/SMTP服务 来配置。

打开邮箱->设置->账户,然后开启 IMAP/SMTP服务,然后根据文档获取 授权码 ,这步比较重要。

然后打开在系统中打开文件 /srv/gitlab/config/gitlab.rb 文件,可以使用geditvivim等等编辑器编辑。找到 Email Settings的注释位置,然后添加以下内容

### Email Settings
gitlab_rails['smtp_enable'] = true # 开启 SMTP 功能
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465 # 端口不可以选择587,测试过会发送邮件失败
gitlab_rails['smtp_user_name'] = "212****71@qq.com" # * 你的邮箱账号,也可以是ge**lin@qq.com等
gitlab_rails['smtp_password'] = "cdns********fdgc" # * 授权码,不是密码
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = '212****71@qq.com' # * 发件人信息,必须跟‘smtp_user_name’保持一致,否则报错
gitlab_rails['smtp_domain'] = "qq.com" # 修改并不影响 可修改为exmail.qq.com/james.com 经测试完全不影响,删除也不影响,并且发送的邮件都是一样的。

总结: 

(1)docker-compose编排文件让我们管理docker容器更为方便,快捷,但需要注意,此文件不要泄漏,因为关键信息都可以在文件内找到。

(2)我们使用编排文件外接挂载了gitlab的主要配置文件,使得我们只需要在本地编辑修改配置文件,

然后第一个命令:

docker exec gitlab gitlab-ctl reconfigure

第二个命令,关闭已启动的gitlab容器:

docker-compose -f 编排文件 down

第三个命令,再次启动gitlab容器:

docker exec -it  down docker-compose -f 编排文件 up -d

 就可以快速的使更改应用到容器内。

 

 

 类似资料: