Docker-私有倉庫用戶認證

前言: 爲了提高私有倉庫的安全性,設置用戶密碼進行登錄

私有倉庫高級配置

使用Docker Compose搭建擁有認證,TLS的私有倉庫
需要注意的是:下面的所有操作在一個文件夾下面進行
私有倉庫地址:192.168.50.66

準備站點證書

當有一個域名,就可以使用openssl自行簽發證書。
案例:搭建私有倉庫地址:www.wanglhdocker.com
過程

# 創建CA私鑰
openssl genrsa -out "root-ca.key" 4096

# 利用私鑰創建CA根證書請求文件
openssl req -new -key "root-ca.key" \   
    -out "root-ca.csr" -sha256 \
    -subj '/C=CN/ST=Shanxi/L=Datong/O=Your Company Name/CN=Your Company Name Docker Registry CA'
# -subj /C 表示國家; /ST 表示省  /L 表示城市 /O 表示組織名 /CN通用名稱

# 配置CA根證書,新建root-ca.cnf
vim root-ca.cnf
[root-ca]
basicConstraints = critical,CA:TRUE,pathlen:1
keyUsage = critical, nonRepudiation, cRLSign, keyCertSign
subjectKeyIdentifier=hash

# 簽發根證書
openssl x509 -req -days 3650 -in "root-ca.csr" \
    -signkey "root-ca.key" -sha256 -out "root-ca.crt" \
    -extfile "root-ca.cnf" -extensions \
    root_ca
    
# 生成站點SSL私鑰
openssl genrsa -out "www.wanglhdocker.com.key" 4096

# 使用私鑰生成證書請求文件
openssl req -new -key "www.wanglhdocker.com.key" -out "site.csr" -sha256 \
    -subj '/C=CN/ST=Shanxi/L=Datong/O=Your Company Name/CN=www.wanglhdocker.com'

# 配置證書,新建site.cnf文件
vim site.cnf
[server]
authorityKeyIdentifier=keyid,issuer
basicConstraints = critical,CA:FALSE
extendedKeyUsage=serverAuth
keyUsage = critical, digitalSignature, keyEncipherment
subjectAltName = DNS:www.wanglhdocker.com, IP:127.0.0.1
subjectKeyIdentifier=hash

# 簽署站點SSL證書
openssl x509 -req -days 750 -in "site.csr" -sha256 \
    -CA "root-ca.crt" -CAkey "root-ca.key" -CAcreateserial \
    -out "www.wanglhdocker.com.crt" -extfile "site.cnf" -extensions server


##如此,就有了www.wanglhdocker.com的網站SSL私鑰www.wanglhdocker.com.key和SSL證書www.wanglhdocker.com.crt及CA根證書 root-ca.crt

#將上面的三個文件 移動到一個文件,刪除其他文件夾

配置私有倉庫

私有倉庫的默認配置文件:/etc/docker/registry/config.yml。先本地編輯,在掛載到容器

vim /etc/docker/registry/config.yml
version: 0.1
log:
  accesslog:
    disabled: true
  level: debug
  formatter: text
  fields:
    service: registry
    environment: staging
storage:
  delete:
    enabled: true
  cache:
    blobdescriptor: inmemory
  filesystem:
    rootdirectory: /var/lib/registry
auth:
  htpasswd:
    realm: basic-realm
    path: /etc/docker/registry/auth/nginx.htpasswd
http:
  addr: :443
  host: https://www.wanglhdocker.com
  headers:
    X-Content-Type-Options: [nosniff]
  http2:
    disabled: false
  tls:
    certificate: /etc/docker/registry/ssl/www.wanglhdocker.com.crt
    key: /etc/docker/registry/ssl/www.wanglhdocker.com.key
health:
  storagedriver:
    enabled: true
    interval: 10s
threshold: 3

生成http認證文件

mkdir auth
docker run --rm \
    --entrypoint htpasswd \
    registry \
    -Bbn username password > auth/nginx.htpasswd
# username password替換爲自己的用戶密碼

## 這裏的username password就是之後登錄到私有倉庫的 用戶名和密碼

編輯docker-compose.yml

vim /docker-compose.yml 		#沒有就創建
version: '3'

services:
  registry:
    image: registry
    ports:
      - "443:443"
    volumes:
      - ./:/etc/docker/registry
      - registry-data:/var/lib/registry

volumes:
  registry-data:

修改hosts,並啓動

vim /etc/hosts
   127.0.0.1 www.wanglhdocker.com
docker-compose up -d

注意: 當沒有安裝docker-comopse這個命令的時候,需要進行安裝。
安裝方法:

curl -L https://github.com/docker/compose/releases/download/1.24.0-rc1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
mv /use/local/bin/docker-compose /usr/bin/docker-compose
chmod +x /usr/bin/docker-compose

詳細請參考文檔:安裝Docker Compose

重新啓動
docker-compose up -d

注意: 在啓動docker-compose如果由於關閉了防火牆,而沒有重啓docker.server,會出現下面的錯誤

Creating network "ssl_default" with the default driver
ERROR: Failed to Setup IP tables: Unable to enable SKIP DNAT rule:  (iptables failed: iptables --wait -t nat -I DOCKER -i br-dc7a16c6d51f -j RETURN: iptables: No chain/target/match by that name.
 (exit status 1))

解決:

systemctl restart docker(或者service restart docker)
docker-compose up -d
 Creating network "ssl_default" with the default driver
 Creating volume "ssl_registry-data" with default driver
 Creating ssl_registry_1 ... done

這樣就搭建了一個具有權限認證、TLS的私有倉庫。

測試

在內部進行測試(192.168.50.66)

mkdir -p /etc/docker/certs.d/www.wanglhdocker.com
cp ssl/root-ca.crt /etc/docker/certs.d/www.wanglhdocker.com/ca.crt

docker login wanglhdocker.com

docker pull centos:7

docker tag centos:7 www.wanglhdocker.com/username/centos:7

docker push www.wanglhdocker.com/username/centos:7

docker image rm www.wanglhdocker.com/username/centos:7

docker pull www.wanglhdocker.com/username/centos:7

docker logout

docker pull www.wanglhdocker.com/username/centos:7
    Error response from daemon: Get http://www.wanglhdocker.com/v2/: dial tcp 127.0.0.1:80: connect: connection refused
    
docker tag centos:7 www.wanglhdocker.com/username/centos:7

docker push  www.wanglhdocker.com/username/centos:7
    The push refers to repository [www.wanglhdocker.com/username/centos]
    d69483a6face: Preparing 
    no basic auth credentials

說明: 從上面的測試可以看出,當使用443安全認證之後,可以用username password登錄到自己的私有倉庫。接下來進行下一步的認證,從內網或外網的其他機器訪問這個私有倉庫。

需要注意的是:由於這裏是自己自行簽發的CA根證書,會導致不被系統信任,所以需要下載自己自行簽發的CA根證書部署到其他機器。
部署:

# 環境準備
# 1.將自行簽發的CA根證書下載,並部署到192.168.50.111
# 2.私有倉庫的地址爲192.168.50.66(當有外網IP嗎,這個IP就是外網IP)
# 3.查看私有倉庫的443端口是否正常啓動
# 4.在192.168.50.111上部署hosts

# 私有倉庫(192.168.50.66):
scp /etc/docker/certs.d/www.wanglhdocker.com/ca.crt 192.168.50.111:/root

# 客戶端(192.168.50.111):
mkdir -p /etc/docker/certs.d/www.wanglhdocker.com
mv /root/ca.crt /etc/docker/certs.d/www.wanglhdocker.com
echo "192.168.50.66 www.wanglhdocker.com" >> /etc/hosts

# 用戶名 username passowrd登錄到192.168.50.66
docker login www.wanglhdocker.com

# 進行測試,看能夠pull到私有倉庫的centos:7
docker pull www.wanglhdocker.com/username/centos:7

docker image ls     #能夠看到www.wanglhdocker.com/username/centos:7就表示能夠從自己創建的私有倉庫讀取鏡像服務

在這裏插入圖片描述
在這裏插入圖片描述在這裏插入圖片描述

到這裏,整個實驗基本完成,在過程中遇到問題,歡迎評論。
不足之處 ,還望指正!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章