前言
因资源成本问题,本Harbor高可用架构为最小开销方案,如果资源充足,可以将PG、Redis全部使用使用云厂商集群模式。
同时为了配置简单,并没用使用keepalived与heartbeat等高可用开源组件。
准备工作
阿里云SLB
阿里云ECS
共享存储
Redis
最小实例SLB
2c4g 俩台
阿里云NFS
阿里云Redis
操作系统为Ubuntu18.04,在俩台ECS上搭建主从PG,如果不想用阿里云redis,也可以使用ECS搭建Redis。
安装Harbor,用于导出基础harbor数据,恢复到PG集群中.
sudo chmod +x /usr/local/bin/docker-compose
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
# 添加国内阿里云
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
#更新
sudo apt-get update
# 查看docker版本
apt-cache madison docker-ce
# 安装最新版
sudo apt-get install -y docker-ce
# 安装指定19.03.6<sub>3-0</sub>ubuntu-bionic版
sudo apt-get install -y docker-ce=5:19.03.6<sub>3-0</sub>ubuntu-bionic
{
"registry-mirrors": ["https://8sab4djv.mirror.aliyuncs.com"],
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": ["https://harbor.unixsre.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
wget -P /usr/local wget https://github.com/goharbor/harbor/releases/download/v2.3.2/harbor-online-installer-v2.3.2.tgz
tar zxf /usr/local/harbor-online-installer-v2.3.2.tgz -C /data/harbor
# 修改配置文件,根据自己的需求进行修改
cd /data/harbor
cp harbor.yml.tmpl harbor.yml
# harbor.yml中按需修改或添加如下内容
# Configuration file of Harbor
# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: harbor.unixsre.com
# http related config
http:
# port for http, default is 80. If https enabled, this port will redirect to https port
port: 80
# https related config
https:
# https port for harbor, default is 443
port: 443
# The path of cert and key files for nginx
certificate: /data/harbor/ssl/unixsre.com.cer
private_key: /data/harbor/ssl/unixsre.com.key
# # Uncomment following will enable tls communication between all harbor components
# internal_tls:
# # set enabled to true means internal tls is enabled
# enabled: true
# # put your cert and key files on dir
# dir: /etc/harbor/tls/internal
# Uncomment external_url if you want to enable external proxy
# And when it enabled the hostname will no longer used
# external_url: https://reg.mydomain.com:8433
# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
# 初始password,可以修改成自己需要的,然后后续在WEBUI上自行修改。
harbor_admin_password: 1234567
## 添加禁止用户自注册
self_registration: off
## 设置只有管理员可以创建项目
project_creation_restriction: adminonly
# The default data volume
data_volume: /data/harbor
# 执行安装命令
bash /data/harbor/install.sh
# 如果对配置文件harbor.yml,需要使用./prepare脚本重新生成
./prepare
# 重启
docker-compose restart
docker login https://harbor.unixsre.com
# 拉取
docker pull busybox
# 打包
docker build -t busybox:v1 .
docker build -t busybox:v1 -f Dockerfile .
# 打TAG
docker tag busybox:latest harbor.unixsre.com/library/busybox:latest
# 上传
docker push harbor.unixsre.com/library/busybox:latest
# k3s pull
k3s crictl pull harbor.unixsre.com/library/busybox
docker container exec -it harbor-db /bin/bash
# 执行pg备份
pg_dump -U postgres registry > /tmp/registry.sql
pg_dump -U postgres notarysigner > /tmp/notarysigner.sql
pg_dump -U postgres notaryserver > /tmp/notaryserver.sql
# 复制到本地宿主机
docker container cp harbor-db:/tmp/registry.sql /data/harbor/backup_sql/
docker container cp harbor-db:/tmp/notarysigner.sql /data/harbor/backup_sql/
docker container cp harbor-db:/tmp/notaryserver.sql /data/harbor/backup_sql/
安装PG主从集群
PostgreSql主从复制是一种高可用解决方案,可以实现读写分离,实时备份,PG的主从复制是基于xlog来实现的,主库开启日志功能,从库根据主库xlog来完成数据的同步。
PG主从复制注意事项:
- 启动从库之前: 不能执行初始化,若已经初始化了需要删掉对应的目录中的数据文件。
- 启动从库之前: 需要通过base_backup从主服务器上同步配置与数据。
- 启动从库之前: 需要对同步之后的配置文件(standby.signal)进行修改。
- 从库只能读,不能写。
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# 更新源
apt-get update
# 安装PG13
apt -y install postgresql-13 postgresql-client-13 postgresql-contrib
# 验证服务是否启动成功
systemctl status postgresql@13-main.service
# 登录验证修改pwd
sudo -i -u postgres psql -p 5432
ALTER USER postgres WITH PASSWORD '1234567.com';
# 登录验证
psql -h localhost -p 5432 -U postgres
mkdir -p /data/harbor_nas/pgsql/data && chown postgres:postgres /data/harbor_nas/pgsql/data
#创建归档目录
mkdir -p /data/harbor_nas/pgsql/pg_archive && chown postgres:postgres /data/harbor_nas/pgsql/pg_archive
#给目录赋权
chmod 700 /data/harbor_nas/pgsql/pg_archive/ && chmod 700 /data/harbor_nas/pgsql/data/
Environment=PGDATA=/data/harbor_nas/pgsql/data
# 重载
systemctl daemon-reload
# 删除默认集群
pg_dropcluster --stop 13 main
# 在新目录创建集群
pg_createcluster -d /data/harbor_nas/pgsql/data 13 main
# 重启服务
systemctl restart postgresql@13-main.service
# 配置开机启动
systemctl enable postgresql@13-main.service
#开启外部访问配置
vim /etc/postgresql/13/main/pg_hba.conf
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
# 修改集群监听地址
vim /etc/postgresql/13/main/postgresql.conf
listen_addresses = '*'
# 重启服务
systemctl restart postgresql@13-main.service
CREATE ROLE replica login replication encrypted password 'Deniss_12PRO@@@';
# 添加从服务器免密登录,replica为用户,172.19.48.254为从节点的内网IP,md5为允许验证, trust为免密。
vim /etc/postgresql/13/main/pg_hba.conf
host replication replica 172.19.48.254/20 trust
# 添加主服务器postgresql.conf配置
vim /etc/postgresql/13/main/postgresql.conf
listen_addresses = '*'
max_connections = 100
archive_mode = on
archive_command = 'test ! -f /data/harbor_nas/pgsql/pg_archive/%f && cp %p /data/harbor_nas/pgsql/pg_archive/%f'
wal_level = replica
# 重启服务
systemctl restart postgresql@13-main.service
#创建数据目录
mkdir -p /data/harbor_nas/pgsql_replica/data && chown postgres:postgres /data/harbor_nas/pgsql_replica/data
#创建归档目录
mkdir -p /data/harbor_nas/pgsql_replica/pg_archive && chown postgres:postgres /data/harbor_nas/pgsql_replica/pg_archive
#给目录赋权
chmod 700 /data/harbor_nas/pgsql_replica/pg_archive/ && chmod 700 /data/harbor_nas/pgsql_replica/data/
# 添加如下配置
vim /lib/systemd/system/postgresql@.service
Environment=PGDATA=/data/harbor_nas/pgsql_replica/data/
# 重载配置
systemctl daemon-reload
#删除默认目录的集群
pg_dropcluster --stop 13 main
#在新目录创建集群
pg_createcluster -d /data/harbor_nas/pgsql_replica/data 13 main
#重启服务
systemctl restart postgresql@13-main.service
# 进入postgres用户清理初始化的数据,从主服务器复制数据。
su - postgres
rm -rf /data/harbor_nas/pgsql_replica/data/*
pg_basebackup -h 172.19.48.253 -p 5432 -U replica -Fp -Xs -Pv -R -D /data/harbor_nas/pgsql_replica/data
echo "standby_mode = 'on'" > /data/harbor_nas/pgsql_replica/data/standby.signal
# 修改从服务器配置
vim /etc/postgresql/13/main/postgresql.conf
primary_conninfo = 'host=172.19.48.253 port=5432 user=replica password=Deniss_12PRO@@@'
recovery_target_timeline = latest
max_connections = 100
hot_standby = on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on
# 启动从节点PG数据库
systemctl start postgresql@13-main.service
# 登录主节点数据库查看装
psql -h 172.19.48.253 -p 5432 -U postgres
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
---------------+------------
172.19.48.254 | async
# 至此,PG主从复制安装完成。
配置Horbor为PG主节点
CREATE USER harbor LOGIN PASSWORD 'Deniss1112s';
CREATE SCHEMA harbor;
GRANT harbor TO postgres;GRANT USAGE ON SCHEMA harbor TO postgres;
ALTER SCHEMA harbor OWNER TO postgres;
# 创建数据库
CREATE DATABASE registry OWNER harbor;
CREATE DATABASE notarysigner OWNER harbor;
CREATE DATABASE notaryserver OWNER harbor;
# 授权
GRANT ALL PRIVILEGES ON DATABASE registry TO harbor;
GRANT ALL PRIVILEGES ON DATABASE notarysigner TO harbor;
GRANT ALL PRIVILEGES ON DATABASE notaryserver TO harbor;
# 恢复数据库
psql -h localhost -U harbor registry < /data/harbor/backup_sql/registry.sql
psql -h localhost -U harbor notarysigner < /data/harbor/backup_sql/notarysigner.sql
psql -h localhost -U harbor notaryserver < /data/harbor/backup_sql/notaryserver.sql
http:
port: 80
https:
port: 443
certificate: /data/harbor/ssl/unixsre.com.cer
private_key: /data/harbor/ssl/unixsre.com.key
harbor_admin_password: 1234567
data_volume: /data/harbor_nas/harbor_data
trivy:
ignore_unfixed: false
skip_update: false
insecure: false
jobservice:
max_job_workers: 10
notification:
webhook_job_max_retry: 10
chart:
absolute_url: disabled
log:
level: info
local:
rotate_count: 50
rotate_size: 200M
location: /var/log/harbor
_version: 2.3.0
external_database:
harbor:
host: 172.19.48.253
port: 5432
db_name: registry
username: harbor
password: Deniss1112s
ssl_mode: disable
max_idle_conns: 2
max_open_conns: 0
notary_signer:
host: 172.19.48.253
port: 5432
db_name: notarysigner
username: harbor
password: Deniss1112s
ssl_mode: disable
notary_server:
host: 172.19.48.253
port: 5432
db_name: notaryserver
username: harbor
password: Deniss1112s
ssl_mode: disable
external_redis:
host: 172.19.48.253:6379
password: Deniss1589s
registry_db_index: 1
jobservice_db_index: 2
chartmuseum_db_index: 3
trivy_db_index: 5
idle_timeout_seconds: 30
proxy:
http_proxy:
https_proxy:
no_proxy:
components:
- core
- jobservice
- trivy
./prepare
docker-compose down && docker-compose up -d
PG主从故障切换
假设主库宕机或者主节点宕机,因为我们的Redis在阿里云,而Harbor的镜像数据在阿里云的NFS,要保证服务的可用性,这个时候,只需要快速的将从节点切换为主库,并且修改Harbor的配置文件,重启Harbor的服务下即可。
下面为手动操作,建议调整为脚本执行快速切换。
service postgresql@13-main stop
postgres=# select pg_promote(true,60);
# 验证是否升级为主库
/usr/lib/postgresql/13/bin/pg_controldata -D /data/harbor_nas/pgsql_replica/data/ |grep cluster
Database cluster state: in production
sed -i 's/172.19.48.253/172.19.48.254/'
./prepare
docker-compose down && docker-compose up -d
/etc/postgresql/13/main/pg_hba.conf
host replication replica 172.19.48.253/20 trust
# 切换用户
su - postgres
# 清理数据
rm -rf /data/harbor_nas/pgsql/data/*
# 同步254数据到253
pg_basebackup -h 172.19.48.254 -p 5432 -U replica -Fp -Xs -Pv -R -D /data/harbor_nas/pgsql/data/
echo "standby_mode = 'on'" > /data/harbor_nas/pgsql/data/standby.signal
# 修改253配置
vim /etc/postgresql/13/main/postgresql.conf
primary_conninfo = 'host=172.19.48.254 port=5432 user=replica password=Deniss_12PRO@@@'
recovery_target_timeline = latest
max_connections = 100
hot_standby = on
max_standby_streaming_delay = 30s
wal_receiver_status_interval = 10s
hot_standby_feedback = on
# 启动253PG服务
systemctl start postgresql@13-main.service
psql -h localhost -p 5432 -U postgres
postgres=# select client_addr,sync_state from pg_stat_replication;
client_addr | sync_state
---------------+------------
172.19.48.253 | async
灾难性故障恢复
对于不可抗拒因素是比较极端的情况,任何人都无法预料,包括当前的各种云厂商,我们只把能想到的,能做到的全部做好,我这边已经做了PG数据库的全备上传到了OSS上,Harbor的镜像数据阿里云NFS一份,OSS一份,想要灾难性恢复必须保证如下俩个前提:
- PG数据库全备可用(注意:必须可以承受丢失全备时间起止到故障时间的数据)。
- 阿里云NFS或者OSS中的Harbor镜像数据文件可用。
恢复步骤:搭建一个单节点PG,全备导入,Harbor中的配置使用单节点PG,Redis本地或者harbor启动的都可以,然后使用docker-compose启动即可,具体操作步骤不在叙述。
但是这样并不是最快的方法,还有没有更好的方案呢?当然有了,使用云服务,一切都交给云,但是就算是云也不可能保证100%的可用性,此处的灾难性故障恢复,仅做抛砖引玉,并不是最终的解决方案,只是给大家提供一个可以展开思考的思路,如果大家有更完美完善的方案,欢迎一起交流。