生产环境搭建高可用Harbor

大道之行,天下为公。这篇文章主要讲述生产环境搭建高可用Harbor相关的知识,希望能为你提供帮助。
前言
因资源成本问题,本Harbor高可用架构为最小开销方案,如果资源充足,可以将PG、Redis全部使用使用云厂商集群模式。
同时为了配置简单,并没用使用keepalived与heartbeat等高可用开源组件。

准备工作

阿里云SLB
阿里云ECS
共享存储
Redis
最小实例SLB
2c4g 俩台
阿里云NFS
阿里云Redis
操作系统为Ubuntu18.04,在俩台ECS上搭建主从PG,如果不想用阿里云redis,也可以使用ECS搭建Redis。
安装Harbor,用于导出基础harbor数据,恢复到PG集群中.
  1. 安装docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
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
[[安装5]]: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

  1. Docker配置镜像加速与国内docker-cn源
sudo tee /etc/docker/daemon.json < < -EOF

"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


  1. 安装Harbor2.3
# 下载Harbor
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 /var/www/dream/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

  1. 常用命令示例
# 登录
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/ops/busybox:latest
# 上传
docker push harbor.unixsre.com/library/busybox:latest
# k3s pull
k3s crictl pull harbor.unixsre.com/library/busybox

  1. 备份harbor库,并且导出用于恢复.
# 进入容器备份
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来完成数据的同步。
【生产环境搭建高可用Harbor】PG主从复制注意事项:
  • 启动从库之前: 不能执行初始化,若已经初始化了需要删掉对应的目录中的数据文件。
  • 启动从库之前: 需要通过base_backup从主服务器上同步配置与数据。
  • 启动从库之前: 需要对同步之后的配置文件(standby.signal)进行修改。
  • 从库只能读,不能写。
  1. 分别在每个ECS安装postgresql-13
# 添加PG apt源
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

  1. 创建PG数据目录,分别在每个机器上创建.
#创建数据目录
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/

  1. 添加systemd启动配置文件中的数据目录环境变量.
vim /lib/systemd/system/postgresql@.service
Environment=PGDATA=https://www.songbingjia.com/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
localallpostgrespeer

# TYPEDATABASEUSERADDRESSMETHOD

# "local" is for Unix domain socket connections only
localallallpeer
# IPv4 local connections:
hostallall0.0.0.0/0md5
# IPv6 local connections:
hostallall::1/128md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
localreplicationallpeer
hostreplicationall127.0.0.1/32md5
hostreplicationall::1/128md5
# 修改集群监听地址
vim /etc/postgresql/13/main/postgresql.conf
listen_addresses = *
# 重启服务
systemctl restart postgresql@13-main.service

  1. 主服务器配置
# 创建具有复制流操作权限的的用户:replica
CREATE ROLE replica login replication encrypted password Deniss_12PRO@@@;
# 添加从服务器免密登录,replica为用户,172.19.48.254X为从节点的内网IP,md5为允许验证, trust为免密。
vim /etc/postgresql/13/main/pg_hba.conf
hostreplicationreplica 172.19.48.254/20trust
# 添加主服务器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

  1. 从服务器配置
# 如果前面已经在从服务器执行过了这个操作,直接可以进入postgres用户家目录清理、复制数据。
#创建数据目录
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=https://www.songbingjia.com/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
---------------+----

    推荐阅读