Docke构建私有Registry
1. 启动registry
docker run -d -p 5000:5000 --restart=always --name="registry" -v /data/registry:/var/lib/registry registry
2. 修改配置文件
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://68rmyzg7.mirror.aliyuncs.com"],
"insecure-registries": ["192.168.1.105:5000"]
}
修改完重启docker
3. 制作本地镜像并push到registry
上传镜像需要把镜像名改成标准格式
registry服务地址:服务端口号/用户名/项目名:版本号
192.168.1.105:5000/jiuyangperp/nginx:v1
docker tar nginx:latest 192.168.1.105:5000/jiuyangperp/nginx:v1
docker push 192.168.1.105:5000/jiuyangperp/nginx:v1
4. 异地pull镜像
docker pull 192.168.1.105:5000/jiuyangperp/nginx:v1
5. 本地仓库加安全认证
生成密码:
yum install http-tools -y
mkdir /opt/registry-auth/ -p
htpasswd -Bbn admin jiuyang1205 > /opt/registry-auth/htpasswd
6. 重新启动带有密钥功能的registry容器
docker rm registry
docker -d -p 5000:5000 -v /opt/registry-auth/:/auth/ -v /data/registry:/var/lib/registry --name="registry-auth" -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd" registry
7. push镜像,需要进行登录
docker login 192.168.1.105:5000
Username:
Password:
harbor实现图形化私有镜像仓库
Harbor是一个用于存储和分发Docker镜像的企业级Registry服务器,通过添加一些企业必须的功能特性,例如安全、标识和管理等,扩展了开源Docker Distribution。作为一个企业级私有Registry服务器,Harbor提供了更好的性能和安全。提升用户 使用Registry构建和运行环境传输镜像的效率。Harbor支持安装在多个Registry节点的镜像资源复制,镜像全部保存在私有Registry中,确保数据和知识产权在公司内部网络中管控。另外,Harbor也提供了高级的安全特性,诸如用户管理,访问控制和活动审计等。
- 基于角色的访问控制—用户与Docker镜像仓库通过“项目”进行组织管理,一个用户可以对多个镜像仓库在同一命名空间(project)里有不同的权限。
- 镜像复制—镜像可以在多个Registry石磊中复制(同步)。尤其适合于负载均衡,高可用,混合云和多云的场景。
- 图形化用户界面—用户可以通过浏览器来浏览,检索当前Docker镜像仓库,管理项目和命名空间。
- AD/LDAP支持—Harbor可以继承企业内部已有的AD/LDAP,用于鉴权认证管理。
- 审计管理—所有针对镜像仓库的操作都可以被记录追溯,用于审计管理。
- 国际化—已拥有英文、中文、德文、日文和俄文的本地化版本。更多的语言将会添加进来。
- RESTful API—RESTful API 提供给管理员对于Harbor更多的操控,使得与其它的管理软件集成变得更容易。
- 部署简单—提供在线和离线两种安装工具,也可以安装到vSphere平台(OVA方式)虚拟设备。
1 Install Docker CE
apt-get update
apt-get install apt-transport-https ca-certificates software-properties-common curl
curl -fsSL https://download.docker.com/linux/ubuntu/gpg |sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update
apt-get install docker-ce
2 Install Docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.22.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
#查看版本
docker --version
Docker version 18.09.0, build 4d60db4
docker-compose --version
docker-compose version 1.22.0, build f46880fe
3 Install Harbor
wget https://storage.googleapis.com/harbor-releases/release-1.6.0/harbor-offline-installer-v1.6.0.tgz
tar -zxvf harbor-offline-installer-v1.6.0.tgz
#编辑配置文件
cd harbor/
vim harbor.cfg
hostname = www.harbor.mobi # 这里配置的监听地址,可以是域名
harbor_admin_password = password # 配置admin用户的密码
#http:
# port: 80
https:
# https port for harbor, default is 443
port: 443
# The path of cert and key files for nginx
certificate: /data/cert/www.harbor.mobi.crt
private_key: /data/cert/www.harbor.mobi.key
# 注意证书路径
所有需要使用镜像仓库的docker主机:
echo "192.168.1.150 www.harbor.mobi" >> /etc/hosts
4 运行证书一键生成脚本:
#!/bin/bash
# 在该目录下操作生成证书,正好供harbor.yml使用
mkdir -p /data/cert
cd /data/cert
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -sha512 -days 3650 -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=www.harbor.mobi" -key ca.key -out ca.crt
openssl genrsa -out www.harbor.mobi.key 4096
openssl req -sha512 -new -subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=www.harbor.mobi" -key www.harbor.mobi.key -out www.harbor.mobi.csr
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1=www.harbor.mobi
DNS.2=harbor
DNS.3=ks-allinone
EOF
openssl x509 -req -sha512 -days 3650 -extfile v3.ext -CA ca.crt -CAkey ca.key -CAcreateserial -in www.harbor.mobi.csr -out www.harbor.mobi.crt
openssl x509 -inform PEM -in www.harbor.mobi.crt -out www.harbor.mobi.cert
cp www.harbor.mobi.crt /etc/pki/ca-trust/source/anchors/www.harbor.mobi.crt
update-ca-trust
5 把这三个密钥文件复制到docke下,所有节点都需要
mkdir -p /etc/docker/certs.d/www.harbor.mobi/
cp /data/cert/www.harbor.mobi.cert /etc/docker/certs.d/www.harbor.mobi/
cp /data/cert/www.harbor.mobi.key /etc/docker/certs.d/ywww.harbor.mobi/
cp /data/cert/ca.crt /etc/docker/certs.d/www.harbor.mobi/
最终docker目录结构:
/etc/docker/certs.d/
└── www.harbor.mobi
├── www.harbor.mobi.cert <-- Server certificate signed by CA
├── www.harbor.mobi.key <-- Server key signed by CA
└── ca.crt <-- Certificate authority that signed the registry certificate
# 重启docker
systemctl restart docker.service
# 停止
docker-compose down -v
# 重新生成配置文件
./prepare --with-notary --with-clair --with-chartmuseum
# 启动
docker-compose up -d
问题:
docker login https://192.168.75.100
x509: cannot validate certificate for 192.168.75.100 because it doesn't contain any IP SANs
排查步骤:
检查harbor.yml文件中hostname变量的值是否跟生成证书使用的一致,并检查系统时间
官方步骤示例:
#set hostname
hostname: yourdomain.com
http:
port: 80
https:
# https port for harbor, default is 443
port: 443
# The path of cert and key files for nginx
certificate: /data/cert/yourdomain.com.crt
private_key: /data/cert/yourdomain.com.key
# 生成使用的相关证书
openssl genrsa -out ca.key 4096
openssl req -x509 -new -nodes -sha512 -days 3650 \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
-key ca.key \
-out ca.crt
openssl genrsa -out yourdomain.com.key 4096
openssl req -sha512 -new \
-subj "/C=CN/ST=Beijing/L=Beijing/O=example/OU=Personal/CN=yourdomain.com" \
-key yourdomain.com.key \
-out yourdomain.com.csr
cat > v3.ext <<-EOF
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1=yourdomain.com
DNS.2=yourdomain
DNS.3=hostname
EOF
openssl x509 -req -sha512 -days 3650 \
-extfile v3.ext \
-CA ca.crt -CAkey ca.key -CAcreateserial \
-in yourdomain.com.csr \
-out yourdomain.com.crt
openssl x509 -inform PEM -in yourdomain.com.crt -out yourdomain.com.cert
# 把这三个复制到docke下
cp yourdomain.com.cert /etc/docker/certs.d/yourdomain.com/
cp yourdomain.com.key /etc/docker/certs.d/yourdomain.com/
cp ca.crt /etc/docker/certs.d/yourdomain.com/
最终docker目录结构:
/etc/docker/certs.d/
└── yourdomain.com:port
├── yourdomain.com.cert <-- Server certificate signed by CA
├── yourdomain.com.key <-- Server key signed by CA
└── ca.crt <-- Certificate authority that signed the registry certificate
# 重启docker
systemctl restart docker.service
cp 192.168.75.100.crt /etc/pki/ca-trust/source/anchors/192.168.75.100.crt
update-ca-trust
# harbor证书配置
cp yourdomain.com.crt /data/cert/
cp yourdomain.com.key /data/cert/
# 重新生成配置文件
./prepare --with-notary --with-clair --with-chartmuseum
# 停止
docker-compose down -v
# 启动
docker-compose up -d