Docker中要使用镜像,我们一般都会从本地、Docker Hub公共仓库或者其它第三方的公共仓库中下载镜像,但是出于安全和一些内外网的原因考虑,企业级上不会轻易使用。普通的Docker Registry又不满足需求,所以一般可以利用Harbor搭建一个企业级的私有镜像仓库。
Harbor是构建企业级私有docker镜像仓库的开源解决方案,它是Docker Registry的更高级封装,它除了提供友好的Web UI界面,角色和用户权限管理,用户操作审计等功能外,它还整合了K8s的插件(Add-ons)仓库,即Helm通过chart方式下载,管理,安装K8s插件,而chartmuseum可以提供存储chart数据的仓库(helm就相当于k8s的yum)。另外它还整合了两个开源的安全组件,一个是Notary,另一个是Clair,Notary类似于私有CA中心,而Clair则是容器安全扫描工具,它通过各大厂商提供的CVE漏洞库来获取最新漏洞信息,并扫描用户上传的容器是否存在已知的漏洞信息,这两个安全功能对于企业级私有仓库来说是非常具有意义的。
echo "* soft nofile 65535" >> /etc/security/limits.conf
echo "* hard nofile 65535" >> /etc/security/limits.conf
systemctl stop firewalld && systemctl disable firewalld
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
这里以安装在/usr/local/docker
目录为例
sudo mkdir -p /usr/local/docker && sudo mkdir -p /etc/docker
https://download.docker.com/linux/static/stable/x86_64/
以19.03.5版本为例
tar -xvf docker-19.03.5.tgz
cp docker/* /usr/bin/
cat > /etc/docker/daemon.json <
cat > /etc/systemd/system/docker.service << EOF
[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
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
EOF
chmod +x /etc/systemd/system/docker.service
systemctl daemon-reload
systemctl enable docker.service && systemctl start docker
https://github.com/docker/compose/releases
mv docker-compose-Linux-x86_64 /usr/bin/docker-compose
chmod +x /usr/bin/docker-compose
https://github.com/goharbor/harbor/releases
tar -xvf harbor-offline-installer-v1.10.2.tgz -C /usr/local/
vim /usr/local/harbor/harbor.yml
# 本机外网IP或域名,该地址供用户通过UI进行访问,不要使用127.0.0.1
hostname: 10.10.0.1
# 指定证书及密钥文件,后面会使用openssl生成
certificate: /data/cert/harbor.crt
private_key: /data/cert/harbor.key
# Harbor的管理员密码,后面可以在页面上修改
harbor_admin_password: Harbor12345
# 指定mysql数据库管理员密码
database:
# The password for the root user of Harbor DB. Change this before any production use.
password: Gsld1234!
# 数据存储目录,需要手动创建
data_volume: /data
mkdir -p /data/cert && cd /data/cert
openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 365 -out ca.crt -subj "/C=CN/L=Shanghai/O=harbor/CN=harbor-registry"
openssl req -newkey rsa:4096 -nodes -sha256 -keyout harbor.key -out server.csr -subj "/C=CN/L=Shanghai/O=harbor/CN=10.10.0.1"
openssl x509 -req -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out harbor.crt
cd /usr/local/harbor/
./install.sh
通过绑定的域名即可访问harbor页面,这里我们是IP:10.10.0.1,默认的用户名密码为admin/Harbor12345
默认的Harbor带一个library的仓库,这里我们不是用这个,新建一个新的仓库“darren”,如果不想在使用仓库的时候还要docker login的话,可以直接创建一个公开仓库
镜像仓库地址10.10.0.1
,修改需要推送到仓库的镜像的tag,格式如下:
10.10.0.1:5000/darren/registry:2.7.1
推送镜像,需要先登录仓库,用户名密码为Harbor的用户名密码
docker login 10.10.0.1
docker push 10.10.0.1/darren/registry:2.7.1
拉取镜像,因为是公开仓库,所以不需要login即可拉取
docker pull 10.10.0.1/darren/registry:2.7.1