GitLab: 如何使用外部提供的PostgreSQL数据库
程序员文章站
2022-07-06 22:47:07
...
缺省的情况下GitLab的官方镜像中提供了一个PostgreSQL,如果希望把此数据库放在GitLab的容器之外的话需要怎么做呢?这篇文章结合示例进行说明具体的做法。
文章目录
环境准备
配置文件:GitLab
version: '2'
services:
# Version Control service: Gitlab
gitlab:
image: gitlab/gitlab-ce:12.10.5-ce.0
ports:
- "35001:80"
- "30022:22"
- "443:443"
volumes:
- ./log/:/var/log/gitlab
- ./data/:/var/opt/gitlab
- ./conf/:/etc/gitlab
restart: "no"
配置文件:PostgreSQL
liumiaocn:postgres liumiao$ cat docker-compose.yml
version: '2'
services:
# database service: PostgreSQL
postgres:
image: postgres:12.4-alpine
ports:
- "35432:5432"
volumes:
- ./data/:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=liumiao
restart: "no"
liumiaocn:postgres liumiao$
启动命令
执行命令:docker-compose up -d
结果确认
- GitLab
[aaa@qq.com gitlab]# docker-compose ps
Name Command State Ports
---------------------------------------------------------------------------------------------------------------------
gitlab_gitlab_1 /assets/wrapper Up (healthy) 0.0.0.0:30022->22/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:35001->80/tcp
[aaa@qq.com gitlab]#
- PostgreSQL
[aaa@qq.com postgres]# docker-compose ps
Name Command State Ports
-------------------------------------------------------------------------------------
postgres_postgres_1 docker-entrypoint.sh postgres Up 0.0.0.0:35432->5432/tcp
[aaa@qq.com postgres]#
使用外部PostgreSQL服务
步骤1: 设定gitlab.rb
[aaa@qq.com ~]# docker exec -it gitlab_gitlab_1 sh
# cd /etc/gitlab
# ls
gitlab-secrets.json ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key trusted-certs
gitlab.rb ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
# cp -p gitlab.rb gitlab.rb.org
# vi gitlab.rb
# diff gitlab.rb gitlab.rb.org
561,562c561,562
< gitlab_rails['db_adapter'] = "postgresql"
< gitlab_rails['db_encoding'] = "utf8"
---
> # gitlab_rails['db_adapter'] = "postgresql"
> # gitlab_rails['db_encoding'] = "unicode"
564c564
< gitlab_rails['db_database'] = "postgres"
---
> # gitlab_rails['db_database'] = "gitlabhq_production"
566,569c566,569
< gitlab_rails['db_username'] = "postgres"
< gitlab_rails['db_password'] = 'liumiao'
< gitlab_rails['db_host'] = '192.168.163.151'
< gitlab_rails['db_port'] = 35432
---
> # gitlab_rails['db_username'] = "gitlab"
> # gitlab_rails['db_password'] = nil
> # gitlab_rails['db_host'] = nil
> # gitlab_rails['db_port'] = 5432
914c914
< postgresql['enable'] = false
---
> # postgresql['enable'] = true
#
步骤2: gitlab-ctl reconfigure
执行命令:gitlab-ctl reconfigure
或者
执行命令:docker-compose restart
执行日志示例如下所示:
# gitlab-ctl reconfigure
Starting Chef Client, version 14.14.29
resolving cookbooks for run list: ["gitlab"]
Synchronizing Cookbooks:
- package (0.1.0)
- postgresql (0.1.0)
- redis (0.1.0)
- registry (0.1.0)
- mattermost (0.1.0)
- consul (0.1.0)
- gitaly (0.1.0)
- praefect (0.1.0)
- letsencrypt (0.1.0)
- nginx (0.1.0)
- runit (4.3.0)
- crond (0.1.0)
- gitlab (0.0.1)
- monitoring (0.1.0)
- acme (4.1.1)
Installing Cookbook Gems:
Compiling Cookbooks...
Recipe: gitlab::default
...省略
步骤3 : 结果确认
登录之后可以正常进行root密码的重置
重置之后也可以使用新的密码正常登录
- 进入到GitLab容器中可以看到内置的PostgreSQL的服务已经不在运行了
[aaa@qq.com gitlab]# docker exec -it gitlab_gitlab_1 sh
# gitlab-ctl status
run: alertmanager: (pid 297) 636s; run: log: (pid 296) 636s
run: gitaly: (pid 306) 636s; run: log: (pid 305) 636s
run: gitlab-exporter: (pid 289) 636s; run: log: (pid 288) 636s
run: gitlab-workhorse: (pid 291) 636s; run: log: (pid 290) 636s
run: grafana: (pid 285) 636s; run: log: (pid 284) 636s
run: logrotate: (pid 287) 636s; run: log: (pid 286) 636s
run: nginx: (pid 299) 636s; run: log: (pid 298) 636s
run: prometheus: (pid 295) 636s; run: log: (pid 294) 636s
run: redis: (pid 304) 636s; run: log: (pid 303) 636s
run: redis-exporter: (pid 293) 636s; run: log: (pid 292) 636s
run: sidekiq: (pid 307) 636s; run: log: (pid 302) 636s
run: sshd: (pid 25) 651s; run: log: (pid 24) 651s
run: unicorn: (pid 301) 636s; run: log: (pid 300) 636s
#
- 连接外部的GitLab服务即可可以看到表的结构已经创建在指定的postgres数据库中了。
[root@liumiaocn gitlab]# psql -h 192.168.163.151 -p 35432 postgres postgres
Password for user postgres:
psql (9.2.24, server 12.4)
WARNING: psql version 9.2, server version 12.0.
Some psql features might not work.
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
postgres=# \c
psql (9.2.24, server 12.4)
WARNING: psql version 9.2, server version 12.0.
Some psql features might not work.
You are now connected to database "postgres" as user "postgres".
postgres=# \dt
List of relations
Schema | Name | Type | Owner
--------+-------------------------------------------------+-------+----------
public | abuse_reports | table | postgres
public | alerts_service_data | table | postgres
public | allowed_email_domains | table | postgres
public | analytics_cycle_analytics_group_stages | table | postgres
public | analytics_cycle_analytics_project_stages | table | postgres
public | analytics_language_trend_repository_languages | table | postgres
public | appearances | table | postgres
public | application_setting_terms | table | postgres
public | application_settings | table | postgres
public | approval_merge_request_rule_sources | table | postgres
public | approval_merge_request_rules | table | postgres
public | approval_merge_request_rules_approved_approvers | table | postgres
public | approval_merge_request_rules_groups | table | postgres
public | approval_merge_request_rules_users | table | postgres
public | approval_project_rules | table | postgres
public | approval_project_rules_groups | table | postgres
public | approval_project_rules_protected_branches | table | postgres
public | approval_project_rules_users | table | postgres
...省略
public | x509_certificates | table | postgres
public | x509_commit_signatures | table | postgres
public | x509_issuers | table | postgres
public | zoom_meetings | table | postgres
(340 rows)
postgres=#
常见错误
Connection refused
- 现象:提示Connection refused
Recipe: gitlab::database_migrations
* bash[migrate gitlab-rails database] action run
[execute] rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "192.168.163.151" and accepting
TCP/IP connections on port 5432?
/opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:48:in `block (3 levels) in <top (required)>'
/opt/gitlab/embedded/bin/bundle:23:in `load'
/opt/gitlab/embedded/bin/bundle:23:in `<main>'
Tasks: TOP => gitlab:db:configure
(See full trace by running task with --trace)
- 原因:可能是由于端口号错误、IP地址错误、网络不通、防火墙设定等诸多原因,建议先用psql确认一下,比如端口号错误,可能的示例日志如下:
[aaa@qq.com gitlab]# psql -h 192.168.163.151 -p 5432 postgres postgres
psql: could not connect to server: Connection refused
Is the server running on host "192.168.163.151" and accepting
TCP/IP connections on port 5432?
[aaa@qq.com gitlab]#
数据库gitlabhq_production不存在
- 现象:database “gitlabhq_production” does not exist
gitlab_1 | * bash[migrate gitlab-rails database] action run
gitlab_1 | [execute] rake aborted!
gitlab_1 | ActiveRecord::NoDatabaseError: FATAL: database "gitlabhq_production" does not exist
gitlab_1 | /opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:48:in `block (3 levels) in <top (required)>'
gitlab_1 | /opt/gitlab/embedded/bin/bundle:23:in `load'
gitlab_1 | /opt/gitlab/embedded/bin/bundle:23:in `<main>'
gitlab_1 |
gitlab_1 | Caused by:
gitlab_1 | PG::ConnectionBad: FATAL: database "gitlabhq_production" does not exist
gitlab_1 | /opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:48:in `block (3 levels) in <top (required)>'
gitlab_1 | /opt/gitlab/embedded/bin/bundle:23:in `load'
gitlab_1 | /opt/gitlab/embedded/bin/bundle:23:in `<main>'
gitlab_1 | Tasks: TOP => gitlab:db:configure
gitlab_1 | (See full trace by running task with --trace)
gitlab_1 |
gitlab_1 | ================================================================================
gitlab_1 | Error executing action `run` on resource 'bash[migrate gitlab-rails database]'
gitlab_1 | ================================================================================
- 原因:缺省情况下,不做设定时,GitLab中的PostgreSQL的database名称为gitlabhq_production,如果使用的是外部的PostgreSQL,此数据库没有创建时自然会出错,可以手动创建也可以使用缺省的postgres数据库
参考文档
https://docs.gitlab.com/ee/administration/postgresql/external.html