柚子快報(bào)邀請(qǐng)碼778899分享:容器 Docker 鏡像倉庫
目錄
1、搭建私有 registry
服務(wù)端創(chuàng)建鏡像倉庫
客戶端推送鏡像
鏡像導(dǎo)入導(dǎo)出
2、Nginx 代理 registry 倉庫
SSL 證書 & https 協(xié)議
SSL證書
https協(xié)議
SSL 的驗(yàn)證流程
客戶端安裝 Nginx
使用 openssl 生成CA根證書和根證書key
創(chuàng)建 Nginx 服務(wù)證書
配置啟動(dòng) Nginx
服務(wù)端配置 docker
上傳鏡像
registry 倉庫鏡像刪除
3、Harbor 倉庫搭建
Docker-compose
創(chuàng)建 Harbor 服務(wù)證書
部署 Harbor
配置 Nginx 代理 Harbor
上傳鏡像
1、搭建私有 registry
主機(jī)名 ip 安裝服務(wù) 角色 node-10 192.168.137.110 docker Registry客戶端 node-11 192.168.137.111 docker,Registry Registry服務(wù)器
服務(wù)端創(chuàng)建鏡像倉庫
# 服務(wù)端拉取Registry鏡像
[root@node-11 ~] docker pull registry
mkdir -p /data/docker/registry
# 啟動(dòng)Registry倉庫
docker run -d --name registry1 --restart=always -p 5000:5000 \
-v /data/docker/registry:/var/lib/registry registry
# 查看倉庫中的鏡像
curl -X GET http://192.168.137.111:5000/v2/_catalog
客戶端推送鏡像
① 修改images倉庫中鏡像的名字
在鏡像名字前加registry倉庫地址和端口, 使之和registry倉庫地址匹配, 只有Docker Hub 上的官方鏡像可以省略倉庫地址和項(xiàng)目名字,因?yàn)槟J(rèn)鏡像下載地址就是docker hub
# 修改images倉庫中鏡像的名字
docker tag centos:7 192.168.137.111:5000/centos7:v1
② 客戶端添加私有倉庫地址:vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.137.111:5000"],
"registry-mirrors": ["https://w0ckwo1v.mirror.aliyuncs.com"]
}
----------------------------------------------------------------
# 重啟docker
systemctl restart docker
③ 推送鏡像到鏡像倉庫
docker push 192.168.137.111:5000/centos7:v1
# 查看倉庫中的鏡像
curl -X GET http://192.168.137.111:5000/v2/_catalog
④ 測(cè)試?yán)∷接袀}庫鏡像
# 刪除有原有鏡像
docker rmi 192.168.137.111:5000/centos7:v1
# 拉取私有倉庫鏡像
Docker pull 192.168.137.111:5000/centos7:v1
鏡像導(dǎo)入導(dǎo)出
# 導(dǎo)出鏡像
docker save -o centos7.tar centos:7
# 刪除原有鏡像
docker rmi centos:7
# 導(dǎo)入鏡像
docker load < centos-7.tar | docker load -i centos-7.tar
2、Nginx 代理 registry 倉庫
SSL 證書 & https 協(xié)議
SSL證書 https協(xié)議 SSL 證書,也稱為 SSL 服務(wù)器證書,是遵守 SSL 協(xié)議的一種數(shù)字證書,由全球信任的證書頒發(fā)機(jī)構(gòu) (CA) 驗(yàn)證服務(wù)器身份后頒發(fā),將 SSL 證書安裝在網(wǎng)站服務(wù)器上,可以使用https加密協(xié)議訪問網(wǎng)站。 ssl證書用來認(rèn)證服務(wù)器真實(shí)身份,釣魚欺詐網(wǎng)站泛濫,用戶如何識(shí)別網(wǎng)站是釣魚網(wǎng)站還是安全網(wǎng)站?網(wǎng)站部署全球信任的SSL證書后,瀏覽器內(nèi)置安全機(jī)制,實(shí)時(shí)查驗(yàn)證書狀態(tài),通過瀏覽器向用戶展示網(wǎng)站認(rèn)證信息,讓用戶輕松識(shí)別網(wǎng)站真實(shí)身份,防止釣魚網(wǎng)站仿冒。 https協(xié)議可以看成是HTTP+SSL的結(jié)合體,https實(shí)現(xiàn)網(wǎng)站加密傳輸,用戶通過http協(xié)議訪問網(wǎng)站時(shí),瀏覽器和服務(wù)器之間是明文傳輸,這就意味著用戶填寫的密碼、帳號(hào)、交易記錄等機(jī)密信息都是明文,隨時(shí)可能被泄露、竊取、篡改,被黑客加以利用。網(wǎng)站安裝SSL證書后,使用https加密協(xié)議訪問網(wǎng)站,可激活客戶端瀏覽器到網(wǎng)站服務(wù)器之間的"SSL加密通道"(SSL協(xié)議),實(shí)現(xiàn)高強(qiáng)度雙向加密傳輸,防止傳輸數(shù)據(jù)被泄露或篡改。
SSL 的驗(yàn)證流程
使用證書認(rèn)證比我們之前秘鑰認(rèn)證多了一個(gè)用戶瀏覽器訪問證書頒發(fā)機(jī)構(gòu)驗(yàn)證服務(wù)器頒發(fā)公鑰的合法性。瀏覽器默認(rèn)集成了證書頒發(fā)機(jī)構(gòu)。
證書認(rèn)證采用非對(duì)稱加密,公鑰和私鑰都可以用來加密數(shù)據(jù),用另一個(gè)解開,公鑰加密數(shù)據(jù),然后私鑰解密的情況被稱為加密解密,私鑰加密數(shù)據(jù),公鑰解密一般被稱為簽名和驗(yàn)證簽名
客戶端安裝 Nginx
主機(jī)名 ip 安裝服務(wù) 角色 node-10 192.168.137.110 docker、Nginx Registry客戶端 node-11 192.168.137.111 docker,Registry Registry服務(wù)器
# 安裝nginx依賴包
yum -y install pcre-devel zlib-devel openssl openssl-devel epel-release
# 安裝nginx
yum install -y nginx
使用 openssl 生成CA根證書和根證書key
如果你有經(jīng)過認(rèn)證機(jī)構(gòu)認(rèn)證的證書,則直接使用將證書放入nginx目錄下即可。如果沒有,則使用openssl創(chuàng)建自簽名證書
① 查看ca證書默認(rèn)配置:cat /etc/pki/tls/openssl.cnf
vim /etc/pki/tls/openssl.cnf
-----------------openssl.cnf---------------------------
req_extensions = v3_req
-----------------openssl.cnf---------------------------
② 創(chuàng)建根證書key
cd /etc/pki/CA/
openssl genrsa -out private/cakey.pem 2048
# 生成根證書
openssl req -new -x509 -key private/cakey.pem -days 3650 -out cacert.pem
req 證書請(qǐng)求的子命令,請(qǐng)求生成證書 x509 表示輸出證書,X509是通用的證書文件格式 days 證書有效期 OpenSSL生成密鑰文件的格式就只有PEM和DER兩種格式,PEM的是將密鑰用base64編碼(base64就是一種基于64個(gè)可打印字符來表示二進(jìn)制數(shù)據(jù)的方法)表示出來的,直接打開你能看到一串的英文字母,DER格式是二進(jìn)制的密鑰文件
創(chuàng)建 Nginx 服務(wù)證書
[root@node-10 CA] mkdir /etc/pki/CA/ssl
cd /etc/pki/CA/ssl/
openssl genrsa -out nginx.key 2048
① 為nginx web服務(wù)器生成證書簽署請(qǐng)求
# 為nginx web服務(wù)器生成證書簽署請(qǐng)求
[root@cong10 ssl] openssl req -new -key nginx.key -out nginx.csr
Common Name一定要是訪問nginx的web服務(wù)器的域名。這里使用registry.test.com域名訪問后端鏡像倉庫。
② 創(chuàng)建證書序列號(hào)文件、證書索引文件:第一次創(chuàng)建的時(shí)候需要給予證書序列號(hào)
[root@node-10 ssl] touch /etc/pki/CA/{serial,index.txt}
echo 00 > /etc/pki/CA/serial
# 使用私有CA根據(jù)請(qǐng)求簽發(fā)服務(wù)端證書
openssl ca -in nginx.csr -keyfile /etc/pki/CA/private/cakey.pem -cert \
/etc/pki/CA/cacert.pem -days 3650 -out nginx.crt
配置啟動(dòng) Nginx
① 編寫nginx配置文件:vim /etc/nginx/nginx.conf
upstream docker-registry {
ip_hash;
server 192.168.137.111:5000;
server 192.168.137.111:5001;
}
server {
listen 80;
listen [::]:80;
server_name registry.test.com;
location / {
return 301 https://$host$request_uri;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name registry.test.com;
ssl_certificate /etc/pki/CA/ssl/nginx.crt;
ssl_certificate_key /etc/pki/CA/ssl/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
auth_basic "Registry realm";
auth_basic_user_file /etc/nginx/nginx.htpasswd;
proxy_pass http://docker-registry;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto $scheme;
proxy_connect_timeout 900;
proxy_read_timeout 900;
proxy_send_timeout 900;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
② 配置nginx認(rèn)證
yum -y install httpd-tools
htpasswd -bc /etc/nginx/nginx.htpasswd admin 123456
③ 啟動(dòng) Nginx
# 啟動(dòng)nginx
systemctl enable nginx
systemctl start nginx
# 查看nginx端口
netstat -antup | grep nginx
④ 配置hosts解析
vim /etc/hosts
192.168.137.110 registry.test.com
⑤ 訪問測(cè)試
# curl命令訪問
curl -k -u admin:123456 https://registry.test.com/v2/_catalog
服務(wù)端配置 docker
① 添加hosts解析
[root@node-11 ~] vim /etc/hosts
192.168.137.110 registry.test.com
② 添加registry倉庫
[root@node-11 ~] vim /etc/docker/daemon.json
{
"insecure-registries": ["https://registry.test.com"],
"registry-mirrors": ["https://0u0do0ns.mirror.aliyuncs.com"]
}
注意:如果有多個(gè)私有倉庫,請(qǐng)?jiān)趇nsecure-registries列表里使用逗號(hào)分隔
③ 重啟docker
systemctl restart docker
# 登陸registry倉庫
docker login https://registry.test.com
上傳鏡像
# 給busybox鏡像打標(biāo)簽
docker tag busybox registry.test.com/busybox
# 推送鏡像
docker push registry.test.com/busybox
# 查看倉庫鏡像
curl -k -u admin:123456 https://registry.test.com/v2/_catalog
curl -k -u admin:123456 'https://registry.test.com/v2/busybox/tags/list'
registry 倉庫鏡像刪除
# 刪除repo
docker exec registry1 rm -rf /var/lib/registry/docker/registry/v2/repositories/centos7
# 清除blob
docker exec registry1 registry garbage-collect /etc/docker/registry/config.yml
# 查看registry倉庫鏡像
curl -k -u admin:123456 https://registry.test.com/v2/_catalog
3、Harbor 倉庫搭建
項(xiàng)目地址:GitHub - goharbor/harbor: An open source trusted cloud native registry project that stores, signs, and scans content.
Harbor是由VMware公司開源的企業(yè)級(jí)的Docker Registry管理項(xiàng)目,它包括權(quán)限管理(RBAC)、LDAP、日志審核、管理界面、自我注冊(cè)、鏡像復(fù)制和中文支持等功能,具有web管理功能,操作其實(shí)是非常簡單易行的。
Harbor是一個(gè)基于Apache協(xié)議的開源軟件,主要開發(fā)語言為go,harbor本身也是基于docker鏡像,使用docker-compose來進(jìn)行編排部署。
https://github.com/goharbor/harbor/releases/download/v2.6.0/harbor-online-installer-v2.6.0.tgz
Docker-compose
項(xiàng)目地址: https://github.com/docker/compose
Compose 是用于定義和運(yùn)行多容器 Docker 應(yīng)用程序的工具。通過 Compose,你可以使用 YML 文件來配置應(yīng)用程序需要的所有服務(wù)。然后,使用docker-compose up,就可以從YML 文件配置中創(chuàng)建并啟動(dòng)所有服務(wù)。
# Docker-compose下載(新版本docker集成compose,無需安裝)
curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# 查看docker-compose版本
docker-compose -v
#新版本docker compose使用方法
[root@node-11 ~] docker compose version
創(chuàng)建 Harbor 服務(wù)證書
[root@node-10 ssl] mkdir /etc/pki/CA/harbor && cd /etc/pki/CA/harbor
openssl genrsa -out harbor.key 2048
# 為harbor服務(wù)器生成證書簽署請(qǐng)求
openssl req -new -key harbor.key -out harbor.csr
# 使用私有CA根據(jù)請(qǐng)求簽發(fā)服務(wù)端證書
openssl ca -in harbor.csr -keyfile /etc/pki/CA/private/cakey.pem -cert \
/etc/pki/CA/cacert.pem -days 3650 -out harbor.crt
# 拷貝證書到cong12
scp -r /etc/pki/CA/harbor/ 192.168.137.111:/etc/pki/CA
部署 Harbor
① 解壓軟件包
tar -zxvf harbor-online-installer-v2.6.0.tgz -C /usr/local/
ls /usr/local/harbor/
② 配置文件harbor.yml
cd /usr/local/harbor/
mv harbor.yml.tmpl harbor.yml
vim harbor.yml
--------------------- harbor.yml -----------------------
hostname: 192.168.137.111
https:
# https port for harbor, default is 443
port: 443
# The path of cert and key files for nginx
certificate: /etc/pki/CA/harbor/harbor.crt
private_key: /etc/pki/CA/harbor/harbor.key
--------------------- harbor.yml -----------------------
③ 安裝Harbor倉庫
./install.sh
# 查看Harbor依賴的鏡像及其健康狀況
docker-compose -f docker-compose.yml ps
④ 登錄Harbor并創(chuàng)建羨慕:賬戶為admin ,密碼為 Harbor12345,該密碼的初始配置在harbor.yml文件中
點(diǎn)擊鏡像倉庫可以看到推送命令,這里包含有docker以及k8s的Helm
配置 Nginx 代理 Harbor
① 添加harbor.conf:[root@node-10 ~] vim /etc/nginx/conf.d/harbor.conf
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name harbor.test.com;
ssl_certificate /etc/pki/CA/harbor/harbor.crt;
ssl_certificate_key /etc/pki/CA/harbor/harbor.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass https://192.168.137.111;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forward-Proto $scheme;
proxy_connect_timeout 900;
proxy_read_timeout 900;
proxy_send_timeout 900;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
---------------------------------------------
# 重載 nginx
nginx -s reload
② 修改hosts:vim /etc/hosts
192.168.137.110 harbor.test.com
③ 添加harbor倉庫:vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.137.111:5000","https://harbor.test.com"],
"registry-mirrors": ["https://0u0do0ns.mirror.aliyuncs.com"]
}
-----------------------------------------------
# 重啟docker
[root@node-10 ~] systemctl restart docker
上傳鏡像
① 推送鏡像
# 登陸harbor
[root@node-10 ~] docker login https://harbor.test.com
# 推送鏡像
[root@node-10 ~] docker tag busybox:latest harbor.test.com/docker/busybox:v2
[root@node-10 ~] docker push harbor.test.com/docker/busybox:v2
② 拉取鏡像
docker pull harbor.test.com/docker/busybox:v1
柚子快報(bào)邀請(qǐng)碼778899分享:容器 Docker 鏡像倉庫
相關(guān)文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。