第四课:企业级镜像仓库Harbor

1.Harbor 概述

Harbor是有VMWare公司开源的容器镜像仓库。事实上,Harbor是在Docker Registry上进行了相应的企业级扩展,从而获得了更加广泛的应用,这些新的企业级特性包括:管理用户界面,基于角色的访问控制,AD/LDAP集成以及审计日志等,足以满足基本企业需求。

官方网址

https://vmware.github.io/harbor/cn

组件 功能
harbor-adminiserver 配置管理中心
harbor-db Mysql数据库
harbor-jobservice 负责镜像复制
harbor-log 记录操作日志
harbor-ui web管理页面和API
nginx 前端代理,负责前端页面和镜像上传/下载转发
redis 会话
registry 镜像存储

2. Harbor 部署

  • 在线安装:从Docker Hub下载Harbor相关镜像,因此安装软件包非常小。

  • 离线安装:安装包包含部署的相关镜像,因此安装包比较大。

  • OVA安装程序:当用户具有vCenter环境时,使用此安装程序,在部署OVA后启动Harbor。

  • 离线安装

https://github.com/goharbor/harbor/releases/download/v1.9.4-rc1/harbor-offline-installer-v1.9.4-rc1.tgz

Harbor安装依赖docker-compose
先安装docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

安装Harbor

tar zxvf harbor-offline-installer-v1.9.4-rc1.tgz
cd harbor
vi harbor.yml
hostname = 192.168.1.88
harbor_admin_password = 123456

./install.sh

3. 基本使用

通过页面登录harbor

http://192.168.1.88
amdin
Harbor12345

可以在页面创建用户,创建项目仓库,赋予用户权限等操作

创建lnmp项目将第三课使用的三个镜像推送到镜像仓库中
推送命令,打标签,上传,下载

docker tag nginx:v1 192.168.1.88/lnmp/nginx:v1
docker push 192.168.1.88/lnmp/nginx:v1
docker pull 192.168.1.88/lnmp/nginx:v1

[root@192 harbor]# docker push 192.168.1.88/lnmp/nginx:v1
The push refers to repository [192.168.1.88/lnmp/nginx]
Get https://192.168.1.88/v2/: dial tcp 192.168.1.88:443: connect: connection refused

  • 配置http可信任

推送失败的原因是默认使用443,我们目前使用的是80,所以要在docker配置文件中添加可信任地址,如果默认使用的非80端口,需要在地址后跟端口号。

vi /etc/docker/daemon.json
[root@192 harbor]# cat /etc/docker/daemon.json 
{
    "registry-mirrors": ["http://f1361db2.m.daocloud.io"],
    "insecure-registries": ["192.168.1.88"]
}
  • 重启docker,docker-compose
systemctl restart docker
docker-compose up -d
  • 查看所有容器均已经正常启动
[root@192 harbor]# docker-compose ps   
      Name                     Command                       State                     Ports          
------------------------------------------------------------------------------------------------------
harbor-core         /harbor/harbor_core              Up (health: starting)                            
harbor-db           /docker-entrypoint.sh            Up (health: starting)   5432/tcp                 
harbor-jobservice   /harbor/harbor_jobservice  ...   Up (health: starting)                            
harbor-log          /bin/sh -c /usr/local/bin/ ...   Up (healthy)            127.0.0.1:1514->10514/tcp
harbor-portal       nginx -g daemon off;             Up (healthy)            8080/tcp                 
nginx               nginx -g daemon off;             Up (health: starting)   0.0.0.0:80->8080/tcp     
redis               redis-server /etc/redis.conf     Up (health: starting)   6379/tcp                 
registry            /entrypoint.sh /etc/regist ...   Up (health: starting)   5000/tcp                 
registryctl         /harbor/start.sh                 Up (health: starting)      
  • 使用我们创建的用户登录镜像仓库,并上传镜像文件
[root@192 harbor]# docker login 192.168.1.88
Username: yujia
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded
[root@192 harbor]# docker push 192.168.1.88/lnmp/nginx:v1
The push refers to repository [192.168.1.88/lnmp/nginx]
b105acf7be9d: Pushed 
9e268d190836: Pushed 
77b174a6a187: Pushed 
v1: digest: sha256:621524f7be59a8d09e0cc1091ec10f74b93c39e763ea5be483e7deb1e7859829 size: 952

至此我们可以将镜像发布出来供他人下载使用。

4. Harbor部署HTTPS

4.1 生成SSL证书

生成方式
可以使用openssl或者cfssl工具生成
本次使用cfssl生成字签证书。
脚本
cfssh.sh下载cfssl工具

wget https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
wget https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64
chmod +x cfssl*
mv cfssl_linux-amd64 /usr/bin/cfssl
mv cfssljson_linux-amd64 /usr/bin/cfssljson
mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo

certs.sh创建CA配置文件,CA证书签名和私钥

cat > ca-config.json <<EOF
{
    "signing": {
      "default": {
        "expiry": "87600h"
      },
      "profiles": {
        "kubernetes": {
          "expiry": "89600h",
          "usages": [
             "signing",
             "key encipherment",
             "server auth",
             "client auth"
          ]
        }
      }  
    }
}
EOF

cat > ca-csr.json <<EOF
{
    "CN": "kubernetes",
    "key": {
          "algo": "rsa",
          "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "L": "Beijing",
            "ST": "Beijing"
        }
    ]
}
EOF

cfssl gencert -initca ca-csr.json | cfssljson -bare ca -

cat > reg.cc.com-csr.json <<EOF
{
    "CN": "reg.cc.com",
    "hosts": [],
    "key": {
       "algo": "rsa",
       "size": 2048
    },
    "names": [
      {
        "C": "CN",
        "L": "Beijing",
        "ST": "Beijing"
      }
    ]
}
EOF

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes reg.cc.com-csr.json | cfssljson -bare reg.cc.com

脚本执行完毕以后会在当前目录生成文件ca-key.pem(私钥),ca.pem(证书),ca.csr(证书签名请求)

-rw-r--r-- 1 root root  322 7月  16 17:08 ca-config.json
-rw-r--r-- 1 root root  960 7月  16 17:24 ca.csr
-rw-r--r-- 1 root root  216 7月  16 17:09 ca-csr.json
-rw------- 1 root root 1679 7月  16 17:24 ca-key.pem
-rw-r--r-- 1 root root 1273 7月  16 17:24 ca.pem
-rwxr-xr-x 1 root root 1074 7月  16 17:08 certs.sh
-rwxr-xr-x 1 root root  314 7月  16 16:44 cfssl.sh
-rw-r--r-- 1 root root  968 7月  16 17:27 reg.cc.com.csr
-rw-r--r-- 1 root root  219 7月  16 17:25 reg.cccom-csr.json
-rw------- 1 root root 1675 7月  16 17:27 reg.cc.com-key.pem
-rw-r--r-- 1 root root 1318 7月  16 17:27 reg.cc.com.pem

4.2 Harbor启用HTTPS

vim harbor.yml
https:
  # https port for harbor, default is 443
  port: 443
  # The path of cert and key files for nginx
  certificate: /root/ssl/reg.cc.com.pem
  private_key: /root/ssl/reg.cc.com-key.pem
  

4.3 重新配置部署Harbor

./prepare
docker-compose down 
docker-compose up -d

4.4 将数字证书复制到Docker主机

注意:以后所有需要访问harbor的docker主机都要做这个步骤!!

#docker主机创建目录
mkdir /etc/docker/certs.d/reg.cc.com 
#harbor主机将生成的reg.cc.com.pem文件传到docker主机上并改名为reg.cc.com.crt
cp reg.cc.com.pem /etc/docker/certs.d/reg.cc.com/reg.cc.com.crt

4.5 验证

再次使用docker主机访问harbor镜像仓库,不需要在docker配置文件中添加信任http,使用https即可访问。

[root@bj-sm-s06-pek-node-02 reg.cc.com]# docker login reg.cc.com
Username: jia.yu
Password: 
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

5.harbor主从复制和高可用

avator

另外启用一台server安装harbor,安装过程同上,为方便使用备用harbor依然使用http方式启动。正常安装完成以后,在主用harbor上进行如下操作添加备用harbor。

在仓库管理栏选择新建项目,添加备用harbor的相关信息,如下:
avator

在复制管理页面添加复制管理规则,如下:
avator

以上操作完成以后,可以在docker主机推送镜像到harbor master端测试,推送成功以后,master会推送镜像到backup端。

6. harbor容器组成功能说明

avator

7. Harbor运维

方式:
备份数据持久化目录 /data
备份PG数据库

故障排查
查看日志文件 /var/log/harbor

查看容器配置文件
docker-compose.yml

查看容器运行状态,容器的启停

docker-compose ps
docker-compose down
docker-compose up -d
posted @ 2019-12-28 21:11  Doc-Yu  阅读(468)  评论(0编辑  收藏  举报