Rexdf

The devil is in the Details.

搭了个gitlab

| Comments

今天无意中看见个CN2线路,2核+4GB+4T的VPS,打了下折之后年付18美元。这么便宜,我都不敢把服务商写出来了。当然这是OpenVZ的,目前看来还好,将来不知道会被玩坏成什么样子。

开源的自建git项目托管服务,貌似有模仿github用ruby写的gitlab,用scala基于JVM的GitBucket,以及国人用golang开发的gogs/gitea等。功能最完善的当然是gitlab了。 gogs可以跑在路由器、树莓派等低配置硬件上,听闻国内有小团队直接用树莓派挂块硬盘跑gogs作为公司的代码仓库。至于GitBucket,没试过,猜想应该具有Java一贯的容易部署高内存消耗的特性吧。

之前想装gitlab,都被官方文档的推荐内存要求吓退,最终都换成了gitea/gogs。虽然kvm可以通过swap加大内存绕过,但是好像比较卡,所以都没怎么用过。

成果

测试了下搭建私有gitlab带Web-IDE的持续集成环境。

目前能做到在gitlab网页上编辑一下文件或者本地编辑后push一下,会自动触发gitlab-runner(一个docker虚拟机环境),进行自动的编译和测试工作。如果编译与测试均成功通过,则进行部署。这样简化软件开发流程。

如下四张图

Pipelines & Jobs

GitLab CI/CD

GitLab CI/CD

GitLab Pages

GitLab Pages

自定义泛解析域名

安装

登录系统初始设置

我的惯例是如下,首先禁掉密码登录(nano /etc/ssh/sshd_config),然后更新(apt-get update && apt-get -y upgrade),重启(reboot)

安装常用软件配置等

这里只随手打一下习惯性手指条件反射记得的包

常用包安装MyBlog
apt-get install curl wget bash-completion git bmon htop iftop nano vnstat build-essential language-pack-zh-hans
locale-gen zh_CN.UTF-8

安装gitlab依赖

按照我一贯的习惯,能用包管理,尽量用包管理,docker次之,最次脚本安装。直接按照官方文档就好。

安装gitlab依赖MyBlog
sudo apt-get update
sudo apt-get install -y curl openssh-server ca-certificates postfix

安装gitlab

之前有玩过bitnami的虚拟机版本gitlab,那个是GitLab CE。然而GitLab官方在这里说GitLab Enterprise Edition版本具有社区版本(MIT Expat license)的全部功能,而且能随时使用商业版以及试用过期后回退到社区版。故而直接安装企业版。

安装gitlab官方源MyBlog
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.deb.sh | sudo bash

先加入GitLab官方的源,并导入密钥。

现在去设置一下dns,把域名指向过来。然后就可以通过包管理来安装了

安装gitlabMyBlog
sudo EXTERNAL_URL="https://gitlab.rexdf.net" apt-get install gitlab-ee

小插曲:安装过程中会自动设置letsencrypt证书,我预想的是只是配置而已,然而当我看到letsencrpt字样的dpkg error时,就发现了这安装程序原来集成了自动配置证书功能。

用户配置

打开网页就是一个密码输入界面,这里输入的是GitLab的root用户的密码。然后注册了一个普通用户,发现没有任何验证,赶紧用root用户管理员关掉任意用户注册功能。

配置CI/CD

我并没有配置太多,只是换了下favicon与logo,然后用普通权限用户的ssh实验了下private项目的pull与push。 具体过程和github与Gitbucket没啥区别,这里不赘述。 当然修改gitlab.rb之后gitlab-ctl reconfigure这个应该也是要养成习惯了。

自定义域名的GitLab Pages服务

默认是没有开启Pages服务的。由于有点想当做博客/维基用的想法,所以研究了下。也挺简单,参考官方文档,一小时搞定。

首先要泛解析另外一个域名,出于防范XSS攻击考虑,官方不推荐用gitlab服务的主域名。而且gitlab的安装程序目前似乎还不能自动配置泛解析letsencrypt证书。

Let’s Encrypt支持泛解析测试的时候就看到过尝鲜用户的报告。印象中acme.sh是支持得最好的。我用的是如下的指令

签发泛解析域名MyBlog
#安装acme.sh(会自动配置alias和crontab等)
curl https://get.acme.sh | sh
#通过环境变量传递DNSPOD的api key
export DP_Id="123456"
export DP_Key="14axxxxxbccxxxxx8a25xxxxx6xxxxx9"
#用上面的环境变量签泛解析证书
acme.sh --issue -d '*.rexdf.com' --dns dns_dp
#把证书拷贝到gitlab配置目录下
acme.sh --installcert -d '*.rexdf.com' \
--key-file /etc/gitlab/ssl/rexdf.com.pages.key \
--fullchain-file /etc/gitlab/ssl/rexdf.com.pages.crt

然后修改/etc/gitlab/gitlab.rb

找到gitlab pages相关的地方 最终如下

/etc/gitlab/gitlab.rbMyBlog
pages_external_url "https://rexdf.com/"
gitlab_pages['enable'] = true
pages_nginx['redirect_http_to_https'] = true
pages_nginx['ssl_certificate'] = "/etc/gitlab/ssl/rexdf.com.pages.crt"
pages_nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/rexdf.com.pages.key"

配置GitLab Runner

虽然有openvz官方的这篇说明openvz是可以支持docker的,然而我的这个好像不支持,下单时我应该选3.x kernel的。 不过还好gitlab-runner好像就不推荐和gitlab跑在同一个机器上。

在另外一台也是极端便宜的小鸡上装了个gitlab-runner

安装docker

参考docker官方文档

安装dockerMyBlog
apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
apt-get update
sudo apt-get install docker-ce

安装gitlab-runner及其配置

参考官方文档

安装gitlab-runnerMyBlog
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install gitlab-runner

配置相对来说比较方便,Runner executor选docker,然后镜像选择ruby:2.3就好了

向gitlab注册gitlab-runnerMyBlog
sudo gitlab-runner register

另外gitlab-runner配置在/etc/gitlab-runner/config.toml

错误

This job is stuck, because you don’t have any active runners that can run this job.

应该在runner上面勾选Run untagged jobs [] Indicates whether this runner can pick jobs without tags

时间成本

看了下付款记录,11点付款,下午2点多开始想搭gitlab,到现在写完本文下午6点钟。 整体上比较简单,只要照着官方文档复制粘贴命令就好了。

维护

2018年10月2日

今天好像Debian系列的libc都升级了,于是惯例升级下系统。然而gitlab这次遇到了点儿问题记录下。

问题

升级错误MyBlog
apt-get update && apt-get -y dist-upgrade
Get:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [107 kB]
...
Get:17 http://archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [279 kB]
Fetched 3,792 kB in 2s (1,377 kB/s)
Reading package lists... Done
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
gitlab-ee linux-libc-dev
2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 494 MB of archives.
After this operation, 1,921 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 linux-libc-dev amd64 4.4.0-137.163 [850 kB]
Get:2 https://packages.gitlab.com/gitlab/gitlab-ee/ubuntu xenial/main amd64 gitlab-ee amd64 11.3.1-ee.0 [493 MB]
Fetched 494 MB in 15s (32.8 MB/s)
(Reading database ... 118431 files and directories currently installed.)
Preparing to unpack .../linux-libc-dev_4.4.0-137.163_amd64.deb ...
Unpacking linux-libc-dev:amd64 (4.4.0-137.163) over (4.4.0-135.161) ...
Preparing to unpack .../gitlab-ee_11.3.1-ee.0_amd64.deb ...
gitlab preinstall: Automatically backing up only the GitLab SQL database (excluding everything else!)
Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
[SKIPPED]
Dumping uploads ...
[SKIPPED]
Dumping builds ...
[SKIPPED]
Dumping artifacts ...
[SKIPPED]
Dumping pages ...
[SKIPPED]
Dumping lfs objects ...
[SKIPPED]
Dumping container registry images ...
[DISABLED]
Creating backup archive: 1538469788_2018_10_02_11.3.0-ee_gitlab_backup.tar ... done
Uploading backup archive to remote storage ... skipped
Deleting tmp directories ... done
done
Deleting old backups ... skipping
Unpacking gitlab-ee (11.3.1-ee.0) over (11.3.0-ee.0) ...
dpkg: error processing archive /var/cache/apt/archives/gitlab-ee_11.3.1-ee.0_amd64.deb (--unpack):
unable to stat other new file '/opt/gitlab/embedded/postgresql/9.6.8/share/extension/lo--1.0--1.1.sql': Cannot allocate memory
Errors were encountered while processing:
/var/cache/apt/archives/gitlab-ee_11.3.1-ee.0_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

如果继续运行 apt-get update && apt-get -y dist-upgrade 则是类似如下的错误

再次安装依然错误MyBlog
dpkg: error processing archive /var/cache/apt/archives/gitlab-ee_11.3.1-ee.0_amd64.deb (--unpack):
unable to stat './opt/gitlab/embedded/lib/ruby/gems/2.4.0/gems/graphql-1.8.1/spec/dummy/tmp/cache/assets/sprockets/v3.0/ij/ijLUBXXLleeHIaUQp-SQmpq0HpA44rYlbW5OJbKpnsg.cache' (which I was about to install): Cannot allocate memory
dpkg-deb: error: subprocess paste was killed by signal (Broken pipe)
Errors were encountered while processing:
/var/cache/apt/archives/gitlab-ee_11.3.1-ee.0_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

探索

期初我以为是内存的原因,于是/opt/gitlab/bin/gitlab-ctl status 然后 /opt/gitlab/bin/gitlab-ctl stop以及/opt/gitlab/bin/gitlab-ctl start postgresql/opt/gitlab/bin/gitlab-ctl start postgres-exporter。这样内存占用减小到了206M/4G,而且开了另外一个终端实时监控内存使用情况,发现运行dpkg -i /var/cache/apt/archives/gitlab-ee_11.3.1-ee.0_amd64.deb过程中,内存从来就没超过1G。于是基本否定内存不足的情况了。

解决

运行如下三条命令即可解决

解决MyBlog
xx:~# apt-get install -f
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
gitlab-ee
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
2 not fully installed or removed.
After this operation, 1,524 MB disk space will be freed.
Do you want to continue? [Y/n]
(Reading database ... 118428 files and directories currently installed.)
Removing gitlab-ee (11.3.0-ee.0) ...
Setting up linux-libc-dev:amd64 (4.4.0-137.163) ...
xx:~# dpkg -i /var/cache/apt/archives/gitlab-ee_11.3.1-ee.0_amd64.deb
xx:~# gitlab-ctl restart

真实原因

老版本依然在运行中,而且没有卸载。所以新版本无法覆盖。

Comments