目录
1.5.1 创建自签CA证书配置文件:ca-config.json
1.5.2 创建自签CA证书请求文件:ca-csr.json
1.5.4 生成harbor.cluster.local证书请求文件
1.5.5 生成harbor.cluster.local证书
2.2.2 生成docker-compose.yml配置文件
1.准备工作
操作系统版本:centos7,和社区版保持一致。查看操作系统版本
[root@localhost ~]# cat /etc/redhat-release
设置Hostname
1.1 禁用防火墙和Selinux
禁用防火墙:
[root@localhost ~]# systemctl disable firewalld
禁用Selinux:
[root@localhost ~]# setenforce 0
1.2 安装证书管理工具cfssl
服务器无法直接wget下载。上传到服务器的/usr/bin下
[root@localhost ~]# scp cfssljson_linux-amd64 root@10.0.24.180:/usr/bin
指定别名:
[root@localhost bin]# mv cfssl_linux-amd64 /usr/bin/cfssl
[root@localhost bin]# mv cfssljson_linux-amd64 /usr/bin/cfssljson
[root@localhost bin]# mv cfssl-certinfo_linux-amd64 /usr/bin/cfssl-certinfo
赋权限:
[root@localhost bin]# chmod +x cfssl*
补充:如果不是在/bin目录下需要配置环境变量(如在/usr/local/bin下)
[root@localhost bin]# export PATH=/usr/local/bin:$PATH
查看安装信息
[root@localhost bin]# cfssl version
1.3 安装Docker
查看内核,要求内核在3.10以上(服务器内核为:3.10.0-957.el7.x86_64)
[root@localhost bin]# uname -a
由于网络问题,采用离线模式安装。官网下载安装包到本地:https://download.docker.com/linux/static/stable/x86_64/,通过ftp或者scp上传到服务器。下载最新稳定版本的docker镜像(docker-18.06.3-ce.tgz) 并解压到/usr/bin下。注意,需要把解压后的所有二进制文件放到/usr/bin下,而不是解压的docker目录,不然加载二进制执行文件找不到。
[root@localhost bin]# tar -zxvf docker-18.06.3-ce.tgz
[root@localhost bin]# cp docker/* /usr/bin/
将docker注册为service服务,在/etc/systemd/system/目录下新增docker.service文件。
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd --selinux-enabled=false --insecure-registry=127.0.0.1
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
# kill only the docker process, not all processes in the cgroup
KillMode=process
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
[Install]
WantedBy=multi-user.target
给docker.service文件添加执行权限
[root@localhost bin]# chmod +x /etc/systemd/system/docker.service
重新加载配置文件(每次有修改docker.service文件时都要重新加载下):
[root@localhost bin]# systemctl daemon-reload
启动:
[root@localhost bin]# systemctl start docker
设置开机启动:
[root@localhost bin]# systemctl enable docker.service
查看docker服务状态:
[root@localhost bin]# systemctl status docker
1.4安装 Docker Compose
下载Linux版本的 Compose https://github.com/docker/compose/releases。将下载下来的“docker-compose-Linux-x86_64”文件上传到服务器上,然后执行如下命令将其移动到 /usr/local/bin,并改名为“docker-compose”。
[root@localhost bin]# sudo mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose
接着执行如下命令添加可执行权限:
[root@localhost bin]# sudo chmod +x /usr/local/bin/docker-compose
docker-compose -v命令测试是否安装成功
1.5 准备自签名证书
创建证书目录:
[root@localhost bin]# mkdir -pv /root/harbor/cert
1.5.1 创建自签CA证书配置文件:ca-config.json
[root@localhost cert]# cd /root/harbor/cert
[root@localhost cert]# cat > ca-config.json << EOF
{
"signing": {
"default": {
"expiry": "8760h"
},
"profiles": {
"harbor": {
"usages": [
"signing",
"key encipherment",
"server auth",
"client auth"
],
"expiry": "8760h"
}
}
}
}
EOF
参数释义说明:
-
default
默认策略,指定了证书的默认有效期是一年(8760h);
-
harbor
表示该配置(profile)的用途是为harbor生成证书及相关的校验工作;
-
signing
表示该证书可用于签名其它证书;生成的ca.pem证书中CA=TRUE;
-
server auth
表示可以该CA对server提供的证书进行验证;
-
client auth
表示可以用该CA对client提供的证书进行验证;
-
expiry
也表示过期时间,如果不写以default中的为准。
1.5.2 创建自签CA证书请求文件:ca-csr.json
[root@localhost cert]# cat > ca-csr.json << EOF
{
"CN": "harbor.cluster.local",
"key": {
"algo": "rsa",
"size": 2048
},
"names": [{
"C": "CN",
"ST": "Shaanxi",
"L": "Xi'an",
"O": "Harbor",
"OU": "Harbor Security"
}]
}
EOF
参数释义说明:
-
CN
Common Name,浏览器使用该字段验证网站是否合法,一般写的是域名。
-
key
生成证书的算法;
-
hosts
表示哪些主机名(域名)或者IP可以使用此csr申请的证书,为空或者""表示所有的都可以使用;
-
names
一些其它的属性:
-
-
C: Country,国家
-
ST: State,州或者是省份
-
L: Locality Name,地区,城市
-
O: Organization Name,组织名称,公司名称(在K8S中常用于指定Group,进行RBAC绑定)
-
OU: Organization Unit Name,组织单位名称,公司部门
-
1.5.3 生成自签名证书
该命令会生成运行CA所必需的文件ca-key.pem(私钥)和ca.pem(证书),还会生成ca.csr(证书签名请求),用于交叉签名或重新签名。
[root@localhost cert]# cfssl gencert -initca ca-csr.json | cfssljson -bare ca
将ca.pem转换为ca.crt
[root@localhost cert]# openssl x509 -outform der -in ca.pem -out ca.crt
-
.crt:是用于存放证书,它是2进制形式存放的,不含私钥。
-
.pem:跟crt的区别是它以ASCII来表示的,可查看。
1.5.4 生成harbor.cluster.local证书请求文件
[root@localhost cert]# cat > harbor-csr.json << EOF
{
"CN": "harbor.cluster.local",
"hosts": [
"local",
"cluster.local",
"harbor.cluster.local"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [{
"C": "CN",
"ST": "Shaanxi",
"L": "Xi'an",
"O": "Harbor",
"OU": "Harbor Security"
}]
}
EOF
1.5.5 生成harbor.cluster.local证书
[root@localhost cert]# cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=harbor harbor-csr.json | cfssljson -bare harbor
查看 cert 证书信息
[root@localhost cert]# cfssl certinfo -cert harbor.pem
查看CSR(证书签名请求)信息
[root@localhost cert]# cfssl certinfo -csr harbor.csr
注意:如果不是单机版的Harbor部署,即部署的是集群版Harbor,不建议在Harbor上启用https,而是在将Harbor放置到一个LB的后边,配置LB的SSL访问,并在LB上做SSL termination,只需保证LB和用户直接为https即可,减少LB和Harbor集群再配置https的性能损耗。
2. 部署Harbor
准备Harbor安装包,下载最新稳定版并解压。https://github.com/goharbor/harbor/releases/download/v2.1.0/harbor-offline-installer-v2.1.0.tgz
[root@localhost bin]# tar -vxf harbor-offline-installer-v2.1.0.tgz -C /root/
2.1 修改配置
修改harbor.yml里hostname、certificate、private_key、data_volume等参数:
[root@localhost harbor]# cd /root/harbor
[root@localhost harbor]# cp harbor.yml.tmpl harbor.yml
[root@localhost harbor]# vim harbor.yml
2.2 安装Harbor
2.2.1加载镜像
[root@localhost harbor]# docker load -i harbor.v2.1.0.tar.gz
2.2.2 生成docker-compose.yml配置文件
增加其他功能(如果执行命令服务器一直未响应,可能服务器安装了安全扫描ds_am)
[root@localhost harbor]# ./prepare --with-clair --with-notary --with-trivy --with-chartmuseum
2.2.3 磁盘挂载补充(如已挂载则忽略):
由于harbor的yaml配置文件数据存储放在数据盘,需要300GB,所以需要将sdb盘挂载在系统下。
创建硬盘分区:
[root@localhost ~]# fdisk /dev/sdb
格式化硬盘:
[root@localhost ~]# fmkfs.ext4 /dev/sdb
建立挂载目录,并挂载硬盘:
[root@localhost ~]# mkdir /data
[root@localhost ~]# mount /dev/sdb /data
设置开机自动挂载:
[root@localhost ~]# vi /etc/fstab
#文件末尾加入下面信息
/dev/sdb /data ext4 defaults 0 0
重启服务器:
[root@localhost ~]# shutdown -r now
2.2.4 启动Harbor
[root@localhost harbor]# docker-compose up -d
查看日志目录为:/var/log/harbor
3.使用harbor
以下操作在访问节点配置,开发环境对应本地开发桌面云
3.1 Win 10下安装docker
-
安装 Docker Desktop for Windows,双击下载的 Docker for Windows Installer 安装文件,一路 Next,点击 Finish 完成安装。
-
注意:Docker 报错 error during connect: This error may indicate that the docker daemon is not running。解决:cmd命令行输入:
cd "C:\Program Files\Docker\Docker"
DockerCli.exe -SwitchDaemon
3.2 配置证书
因为我们配置使用的是自签名的证书,因此需要将前面生成的CA证书拷贝到需要访问Harbor仓库的每个Docker主机的/etc/docker/certs.d/{registry-hostname}/下。
创建证书存放目录:
[root@lirongl ~]# mkdir -p /etc/docker/certs.d/harbor.cluster.local
获取自签证书:
[root@lirongl ~]# scp root@10.0.24.180:/root/harbor/cert/ca.pem /etc/docker/certs.d/harbor.cluster.local/cd
3.3 配置域名解析
Windows 下修改hosts文件,在windows文件中找到System32-->drivers-->etc,进入到etc文件夹中就能看到hosts文件,添加对应域名
10.0.24.180 harbor.cluster.local
3.4 访问harbor UI
默认管理员账户:admin/Harbor12345.
3.4.1 docker客户端访问
-
配置docker私库地址白名单:
3.4.2 访问harbor
浏览器直接访问: