关于harbor:开源摘星计划Harbor-高可用集群设计及部署实操-视频基于离线安装方式

4次阅读

共计 15746 个字符,预计需要花费 40 分钟才能阅读完成。

本文由开源摘星打算反对我的项目 Harbor 的贡献者「勾永彪」投稿。

开源摘星打算(WeOpen Star)是由腾源会 2022 年推出的全新我的项目,旨在为开源人提供成长激励,为开源我的项目提供成长反对,助力开发者更好地理解开源,更快地逾越鸿沟,参加到开源的具体奉献与实际中。

流动详情请见:https://github.com/weopenproj…

【摘要】基于 Harbor 离线装置形式的高可用方案设计及部署。

一、环境阐明

1.1 架构图

Harbor 高可用架构图

【架构解析】:将 Harbor 的 redis 缓存组件、PostgreSQL 数据库组件迁徙到零碎内部做高可用,应用内部共享存储实现多个 Harbor 实例的数据共享,Harbor 实例可横向扩大。

1.2 主机清单

IP 地址 主机名 形容
192.168.2.107 harbor1 Harbor 实例 1,8021 端口
192.168.2.108 harbor2 Harbor 实例 2,8021 端口
192.168.2.110 harbor-data 部署 Harbor 实例的共享存储、内部数据库、内部缓存服务
192.168.2.111 / 负载平衡 VIP,8121 端口

1.3 服务版本

服务 版本要求 装置版本
Harbor / 2.3.5
Docker 17.06.0+ 19.03.8
Docker-compose 1.18.0+ v2.2.3
Redis 6.0.16 6.2.7
PostgreSQL v13.2 13.5

二、主机初始化

Harbor 实例进行初始化

  • 装置 docker
  • 装置 docker-compose
  • 配置内核参数

2.1 装置 docker

$ wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
$ yum install -y docker-ce
$ systemctl enable  --now docker
$ systemctl status docker
$ cat <<EOF > /etc/docker/daemon.json
{"registry-mirrors": ["https://xcg41ct3.mirror.aliyuncs.com"],
   "exec-opts": ["native.cgroupdriver=systemd"],  
   "registry-mirrors": ["https://3hjcmqfe.mirror.aliyuncs.com"], 
   "log-driver": "json-file",
   "log-opts": {
           "max-size": "500m",
           "max-file": "2" 
        }
}
EOF 
$ systemctl daemon-reload
$ systemctl restart docker

exec-opts”: [“native.cgroupdriver=systemd”],     #驱动器 registry-mirrors: 镜像减速地址,可多个 max-file: log 最多保留数量 live-restore: 重启 docker 不重启容器,多用于 k8s 上

2.2 装置 docker-compose

装置 docker-compose 1.18.0 以上的版本,本处装置 v2.2.3 版本。

$ wget https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64
$ mv  docker-compose-linux-x86_64 /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
$ docker-compose version
Docker Compose version v2.2.3

2.3 配置内核参数

$ modprobe br_netfilter
$ cat >> /etc/sysctl.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1      #路由转发
EOF
$ sysctl -p

三、应用 NFS 提供内部共享存储

192.168.2.110 部署 NFS 服务提供共享存储给 Harbor1 实例、Harbor2 实例应用。192.168.2.110作为 NFS 服务端,harbor 实例为客户端。

3.1 部署 NFS 服务端

1)装置并启动 nfs

$ yum  install -y  nfs-utils 
$ systemctl start nfs && systemctl enable nfs  && systemctl status nfs
$ chkconfig nfs on              #设置为开机自启

2)创立共享目录

客户端的数据将近程存入到共享目录下。

$ mkdir -p /data/harbor_data

3)批改配置

$ cat /etc/exports 
/data/harbor_data  192.168.2.0/24(rw,no_root_squash)  #容许哪个网段的客户端应用指定共享目录
$ exportfs -arv            #使配置文件失效
exporting 192.168.2.0/24:/data/harbor_data

4)重启 nfs 服务

$ systemctl restart nfs 

5)查看共享目录信息

$ showmount  -e localhost                    
export list for localhost:
/data/harbor_data

3.2 部署客户端

在 harbor1 和 harbor2 上操作

$ yum -y install nfs-utils
$ systemctl start nfs-utils &&  systemctl enable nfs-utils && systemctl status  nfs-utils

3.3 客户端挂载 NFS 共享存储

在 harbor1 和 harbor2 节点操作, 创立实例的存储目录,而后挂载到 NFS。

$ mkdir -p /data/harbor_data 
$ cat <<EOF >> /etc/fstab
192.168.2.110:/data/harbor_data /data/harbor_data nfs  defaults  0 0
EOF 
$ mount -a

挂载格局:NFSIP: 共享目录   本地目录  nfs defaults 0 0

  • 测试是否能够失常应用:
[root@harbor2 ~]# touch  /data/harbor_data/test.txt 
[root@harbor1 ~]# ls /data/harbor_data/
test.txt

四、部署 Redis 缓存服务(源码)

本处为演示环境,理论 生产环境 请对 Redis 服务做 高可用 数据备份

192.168.2.110 部署 Redis 缓存服务,为 harbor1harbor2实例提供内部 redis 缓存服务。

4.1 下载安装包

$ wget https://download.redis.io/releases/redis-6.2.7.tar.gz

4.2 装置依赖包

$ yum  install  -y  gcc  gcc-c++

4.3 源码编译

$ mkdir -p /app/
$ tar zxvf  redis-6.2.7.tar.gz  -C /app
$ cd /app/redis-6.2.7/
$ make   #编译
$ make  install   #装置

4.4 批改配置文件

redis 默认只反对本地应用, 本处须要批改几个参数:

  • 内部可连贯;
  • redis 启动形式;
  • redis 近程连贯明码;
$ vim  /app/redis-6.2.7/redis.conf 
#bind 127.0.0.1 -::1  #75 行,正文掉 bind 的行,容许任何主机连贯;daemonize yes       #259 行,将 no 批改为 yes,使 redis 能够应用守护过程形式启动;requirepass lidabai666   #903 行,设置 redis 连贯的 auth 明码(lidabai666)

4.5 启动 Redis 服务

后面配置了应用守护过程形式启动,所以间接应用 systemctl 则能够启动 redis 服务。

$ pwd
/app/redis-6.2.7
$ redis-server redis.conf

4.6 服务验证

1)查看 Redis 服务版本

$ redis-cli -v
redis-cli 6.2.7

2)查看端口

redis 默认监听 6379 端口

$ ps aux | grep  6379
root  6200  0.1  0.2 162416 10020 ?  Ssl 17:59  0:00 redis-server *:6379
root  6231  0.0  0.0 112720  984 pts/0  R+  18:01  0:00 grep --color=auto 6379

3)客户端连贯 Redis

harbor1harbor2 作为 redis 客户端

$ which redis-cli      #查看 redis-cli 工具地位
/usr/local/bin/redis-cli
[root@harbor-data redis-6.2.7]# scp /usr/local/bin/redis-cli  192.168.2.107:/usr/local/bin/
[root@harbor-data redis-6.2.7]# scp /usr/local/bin/redis-cli  192.168.2.108:/usr/local/bin/

客户端应用 redis-cli 工具连贯 Redis 服务器

[root@harbor1 ~]# redis-cli  -h 192.168.2.110 -p 6379 -a lidabai666

-a 参数指定 redis 连贯明码


五、部署 PostgreSQL 内部存储服务(源码)

192.168.2.110 主机以源码的形式装置 PostgreSQL 数据库服务,为 harbor1 和 harbor2 实例提供共享存储。

5.1 新建 postgres 用户

默认超级用户(root)不能启动 postgresql,须要手动建用户 postgres。

$ useradd postgres
$ id postgres
uid=1000(postgres) gid=1000(postgres) 组 =1000(postgres)

5.2 装置依赖包

$ yum -y install readline-devel  zlib-devel  gcc zlib

5.3 下载解压源码包

$ wget https://ftp.postgresql.org/pub/source/v13.5/postgresql-13.5.tar.gz --no-check-certificate
$ tar zxvf postgresql-13.5.tar.gz   -C  /app/

5.4 编译装置

$ cd /app/postgresql-13.5/
$ ./configure  --prefix=/usr/local/postgresql
$ make && make install

5.5 创立数据目录

$ mkdir -p /data/postgresql/data
$ chown -R postgres:postgres /usr/local/postgresql/
$ chown -R postgres:postgres /data/postgresql/data/

5.6 设置 postgres 环境变量

[root@harbor-data postgresql-13.5]# su  - postgres
[postgres@harbor-data ~]$ vim + .bash_profile
PGHOME=/usr/local/postgresql   #psql 装置目录
export PGHOME
PGDATA=/data/postgresql/data    #数据库目录
export PGDATA
PATH=$PATH:$HOME/bin:$HOME/.local/bin:$PGHOME/bin
export PATH
[postgres@harbor-data ~]$ source ./.bash_profile
[postgres@harbor-data ~]$ which psql
/usr/local/postgresql/bin/psql

查看版本 [postgres@harbor-data ~]$ psql -V psql (PostgreSQL) 13.5

5.7 初始化数据库

因为 Red Hat 系列发行版的政策,PostgreSQL 装置不会启用主动启动或主动初始化数据库。要实现数据库装置,您须要依据您的发行版执行以下步骤:

[postgres@ceph3 ~]$ initdb
......
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:    #示意初始化胜利
    pg_ctl -D /data/postgresql/data -l logfile start

5.8 启动 PostgreSQL

依据方才初始化胜利后的提醒执行启动命令!

[postgres@harbor-data ~]$ pg_ctl -D /data/postgresql/data -l logfile start
waiting for server to start.... done
server started

5.9 设置(批改)Postgresql 明码

默认 psql 本地登录是不须要明码的,即便咱们设置了明码,也不须要明码就能登录。应为配置文件 pg_hba.conf 中的 local 设置为 trust , 为了平安咱们批改为 password,就是应用明码能力登陆,(当咱们遗记明码的工夫,也能够应用这用形式,先设置为 trust 之后,批改明码,而后在设置为 password。)

[postgres@harbor-data ~]$ psql
psql (13.5)
Type "help" for help.

postgres=# \password    
Enter new password:     #输出设置的明码 Lidabai666
Enter it again:      #确认明码(再次输出)postgres=# \q    #退出

5.10  设置可近程登录 PostgreSQL

[postgres@harbor-data ~]$ vim /data/postgresql/data/postgresql.conf
listen_addresses = '*'    #60 行,监听所有地址
[postgres@harbor-data ~]$ vim + /data/postgresql/data/pg_hba.conf
local   all             all                                 password
host    all             all             0.0.0.0/0            password
host    all             all             ::1/128             password

5.10 重启 PostgreSQL

$ pg_ctl -D /data/postgresql/data -l /data/postgresql/data/postgres.log restartwaiting for server to shut down.... done
server stopped
waiting for server to start.... done
server started

5.11 创立数据库

Harbor 2.3.5 须要创立的数据库:

  • notaryserver
  • notarysigner
  • registry

目前 Harbor 仅反对 PostgraSQL 数据库,须要手动在内部的 PostgreSQL 上创立 registry、notary_signer、notary_servers 三个数据库,Harbor 启动时会主动在对应数据库下生成表。

因为本处次要是演示环境,PostgreSQL 数据库的用户就以超级管理员 postgres 为例,如果是生产环境,倡议新建用户,并授予 harbor、notary_signer、notary_servers 三个数据库绝对应的权限。

[postgres@harbor-data ~]$ psql
Password for user postgres:     #输出明码

postgres=# create database  registry;
CREATE DATABASE
postgres=# create database notary_signer;
CREATE DATABASE
postgres=# create database notary_servers;
CREATE DATABASE
postgres=# \l

5.12 创立用户

postgres=# create user server with password 'lidabai666';
CREATE ROLE
postgres=# create user signer with password 'lidabai666';
CREATE ROLE
postgres=# \du


六、负载平衡设置(Nginx + Keepalived)

应用 keepalived 和 Nginx 实现 harbor 的高可用。在 harbor1harbor2节点上装置 keepalived 服务来提供 VIP 实现负载平衡。Nginx 服务则实现将来到 VIP 的申请转发到后端服务器组 harbor

6.1 装置 nginx 和 keepalived

在 harbor1 和 harbor2 操作

$ wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
$ yum install -y nginx keepalived
$ yum -y install nginx-all-modules.noarch     #装置 nginx 的 stream 模块

nginx 从 1.9.0 开始新增了 steam 模块,用来实现四层协定的转发、代理、负载平衡等。二进制装置的 nginx 则在./configure 时增加 –with-stream 参数来装置 stream 模块。

6.2 批改 nginx 配置文件

在 harbor1 和 harbor2 的 Nginx 服务配置文件一样。
$ vim /etc/nginx/nginx.conf
user nginx;
worker_processes auto;   #主动设置 nginx 的工作过程数量
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;

events {worker_connections 1024;   #工作过程的连接数}

# 四层负载平衡,为两台 harbor 提供负载平衡
stream {log_format  main  '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';
    access_log  /var/log/nginx/harbor-access.log  main;
    upstream harbor{
       server 192.168.2.107:8021;   # harbor1
       server 192.168.2.108:8021;   # harbor2
    }
    server {
       listen  8121;  #因为 nginx 与 harbor 节点复用,这个监听端口不能是 8021,否则会抵触
       proxy_pass harbor;
    }
}
http {log_format  main  '$remote_addr - $remote_user [$time_local]"$request"''$status $body_bytes_sent "$http_referer" ''"$http_user_agent""$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    server {
        listen       80 default_server;
        server_name  _;
        location / {}}
}

检测 nginx 配置文件语法

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6.3 批改 keepalived 配置

本处以 harbor1 为 keepalived 服务的主节点,harbor2 为 keepalived 的备节点。主备节点的 keepalived 配置文件不一样。

1)主节点(harbor1)

[root@harbor1 ~]# cat  /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   notification_email {859281177@qq.com}
   router_id master1
}

vrrp_instance lidabai {
    state MASTER
    interface ens33
    mcast_src_ip:192.168.2.107 
    virtual_router_id 107
    priority 100
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {192.168.2.111/24  #虚构 VIP 地址}
    track_script {chk_nginx}
}
##### 健康检查
vrrp_script chk_nginx {      
    script "/etc/keepalived/check_nginx.sh"
    interval 2
    weight -20
}

2)备节点(harbor2)

[root@harbor2 ~]# cat  /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {859281177@qq.com}
   router_id master2
}

vrrp_instance lidabai {
    state BACKUP
    interface ens33
    mcast_src_ip:192.168.2.108
    virtual_router_id 107
    priority 80    #权重
    advert_int 1
    nopreempt
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {192.168.2.111/24}
    track_script {chk_nginx}
}
vrrp_script chk_nginx {
  script "/etc/keepalived/check_nginx.sh"
  interval 2
  weight -20
}

6.4 编写健康检查脚本

在主备节点(harbor1 和 harbor2)同样操作。
$ vim /etc/keepalived/check_nginx.sh 
#!/bin/bash
#1、判断 Nginx 是否存活
counter=`ps -C nginx --no-header | wc -l`
if [$counter -eq 0]; then
    #2、如果不存活则尝试启动 Nginx
    service nginx start
    sleep 2
    #3、期待 2 秒后再次获取一次 Nginx 状态
    counter=`ps -C nginx --no-header | wc -l`
    #4、再次进行判断,如 Nginx 还不存活则进行 Keepalived,让地址进行漂移
    if [$counter -eq 0]; then
        service  keepalived stop
    fi
fi
$ chmod +x /etc/keepalived/check_nginx.sh 

6.5 启动服务

先启动 master1 和 master2 节点上的 nginx 服务,再启动 keepalived 服务

1)启动 nginx 服务

[root@harbor1 ~]# systemctl enable --now nginx   #启动 nginx 服务并设置开机自启
[root@harbor2 ~]# systemctl enable --now nginx
[root@harbor1 ~]# systemctl status nginx.service 
[root@harbor2 ~]# systemctl status nginx.service

2)启动 keepalived 服务

[root@harbor1 ~]# systemctl enable --now keepalived
[root@harbor2 ~]# systemctl enable --now keepalived
[root@harbor1 ~]# systemctl status keepalived.service
[root@harbor2 ~]# systemctl status keepalived.service

6.6 查看 VIP

在 harbor1 节点查看 VIP 是否胜利绑定。
[root@harbor1 ~]# ip addr
......
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:f1:a3:65 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.107/24 brd 192.168.2.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.2.111/24 scope global secondary ens33     #VIP 地址
       valid_lft forever preferred_lft forever
    inet6 fe80::80b0:1d7f:b5d4:19e8/64 scope link tentative dadfailed 
......

通过 ifconfig 是无奈查看到 VIP 的,通过 hostname -I 命令也能够查看到 VIP。


七、部署 Harbor 实例 1

在 harbor1 192.168.2.107 主机上部署 harbor 服务

7.1 下载解压离线安装包

$ mkdir /app   #创立装置目录
$ wget https://github.com/goharbor/harbor/releases/download/v2.3.5/harbor-offline-installer-v2.3.5.tgz
$ tar zxvf harbor-offline-installer-v2.3.5.tgz  -C  /app/

7.2 批改配置文件

将配置文件模板复制为配置文件,而后批改对应参数。

[root@harbor1 ~]# cd /app/harbor/
[root@harbor1 harbor]# cp harbor.yml.tmpl  harbor.yml
[root@harbor1 harbor]# vim harbor.yml
hostname: 192.168.2.107
http:
  port: 8021

#勾销 https 平安加密拜访形式:#https:
#  port: 443
#  certificate: /your/certificate/path
#  private_key: /your/private/key/path

## 启用内部代理,启用后 hostname 将不再应用
external_url: http:192.168.2.111:8121

## 配置共享存储,即挂载的 NFS 目录
data_volume: /data/harbor_data

_version: 2.3.0

## 配置内部数据库
external_database:
   harbor:
     host: 192.168.2.110   # 数据库主机地址
     port: 5432    # 数据库端口
     db_name: registry    # 数据库名称
     username: postgres   # 连贯该数据库的用户名
     password: Lidabai666   # 连贯数据库的明码
     ssl_mode: disable
     max_idle_conns: 2
     max_open_conns: 0
   notary_signer:   
     host: 192.168.2.110
     port: 5432
     db_name: notary_signer
     username: postgres
     password: Lidabai666
     ssl_mode: disable
   notary_server:
     host: 192.168.2.110
     port: 5432
     db_name: notary_server
     username: postgres
     password: Lidabai666
     ssl_mode: disable

## 配置内部 Redis 实例:external_redis:      
   host: 192.168.2.110:6379   #redis 服务 IP 地址和端口号。如果 redis 是哨兵模式,这里应该是 host_sentinel1:port_sentinel1,host_sentinel2:port_sentinel2
   password:  lidabai666  #连贯内部 redis 服务的明码
#  sentinel_master_set:  #仅在应用 Sentinel 模式(哨兵模式)时应用
   registry_db_index: 1
   jobservice_db_index: 2   #job 服务的数据库索引
   chartmuseum_db_index: 3  #chartmuseum 插件的 Redis 索引
   trivy_db_index: 5   #Trivy 扫描器的数据索引
   idle_timeout_seconds: 30  #超时工夫

#启用 metrics 数据采集插件:metric:
   enabled: true   
   port: 9090
   path: /metrics

7.3 将配置文件注入到组件中

将 harbor.yml 配置文件的内容注入到各组件的配置文件中。

[root@harbor1 harbor]# ./prepare

7.4 装置 Harbor

装置期间会主动导入镜像,执行实现会主动启动 Harbor 服务。

[root@harbor1 harbor]# ./install.sh --with-trivy --with-chartmuseum

-Harbor has been installed and started successfully.- 示意装置胜利!

7.5 查看服务状态

[root@harbor1 harbor]# docker-compose ps

7.6 浏览器登录 Harbor UI

应用实例主机 IP+ 端口在浏览器拜访 harbor UI Http://192.168.2.107:8021

用户名 :admin\
明码:Harbor12345

八、部署 Harbor 实例 2

操作步骤跟部署 Harbor 实例 1 统一,仅将 harbor.yml 文件中 hostname 的值批改为以后主机的 IP 地址即可。

$ mkdir /app
$ wget https://github.com/goharbor/harbor/releases/download/v2.3.5/harbor-offline-installer-v2.3.5.tgz
$ tar zxvf harbor-offline-installer-v2.3.5.tgz  -C  /app/
$ scp  192.168.2.107:/app/harbor/harbor.yml /app/harbor/
$ vim /app/harbor/harbor.yml’hostname: 192.168.2.108

九、服务验证

9.1 浏览器拜访 VIP 和端口

http://192.168.2.111:8121

在 Harbor UI 界面测试镜像推送、拉取、创立用户、创立我的项目等是否失常

9.2 命令行登录 Harbor

$ docker login http://192.168.2.111:8121 -u admin -p Harbor12345

呈现报错:\
Error response from daemon: Get https://192.168.2.111:8121/v2/: http: server gave HTTP response to HTTPS client 在 docker 配置文件中增加参数:

[root@harbor1 harbor]# vim  /etc/docker/daemon.json
{"registry-mirrors": ["https://xcg41ct3.mirror.aliyuncs.com"],
   "exec-opts": ["native.cgroupdriver=systemd"],
   "registry-mirrors": ["https://3hjcmqfe.mirror.aliyuncs.com"],
   "insecure-registries": ["192.168.2.111:8121"],
   "log-driver": "json-file",
   "log-opts": {
           "max-size": "500m",
           "max-file": "2"
        }
}
[root@harbor1 harbor]# systemctl restart docker   #而后重启 docker

9.3 向 Harbor 推送镜像

[root@harbor1 harbor]# docker pull alpine:3.16
[root@harbor1 harbor]# docker tag alpine:3.16 192.168.2.111:8121/library/alpine:3.16
[root@harbor1 harbor]# docker  push 192.168.2.111:8121/library/alpine:3.16

而后能够在 Harbor UI 界面查看到镜像曾经推送胜利!

正文完
 0