溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Docker私有Registry如何在CentOS6.X下安裝

發(fā)布時(shí)間:2021-11-12 14:10:30 來(lái)源:億速云 閱讀:134 作者:小新 欄目:云計(jì)算

這篇文章主要介紹了Docker私有Registry如何在CentOS6.X下安裝,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

說(shuō)明:

docker.yy.com 這是docker registry服務(wù)器的域名也就是你的公司docker私有服務(wù)器的主機(jī)地址,假定ip是192.168.2.114;因?yàn)閔ttps的SSL證書(shū)不能用IP地址,我就隨便起了個(gè)名字。

registry 服務(wù)器作為上游服務(wù)器處理docker鏡像的最終上傳和下載,用的是官方的鏡像。

nginx 1.4.x 是一個(gè)用nginx作為反向代理服務(wù)器


[X] Docker Server端配置

安裝依賴

yum -y install gcc make file && \
yum -y install tar pcre-devel pcre-staticopenssl openssl-devel httpd-tools

配置SSL

(1) 編輯/etc/hosts,把docker.yy.com的ip地址添加進(jìn)來(lái),例如:

192.168.2.114 docker.yy.com

(2) 生成根密鑰

先把

/etc/pki/CA/cacert.pem
/etc/pki/CA/index.txt
/etc/pki/CA/index.txt.attr
/etc/pki/CA/index.txt.old
/etc/pki/CA/serial
/etc/pki/CA/serial.old

刪除掉!

cd /etc/pki/CA/
openssl genrsa -out private/cakey.pem 2048

(3) 生成根證書(shū)

openssl req -new -x509 -key private/cakey.pem -out cacert.pem

輸出:

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:youyuan
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:docker.yy.com
Email Address []:

會(huì)提示輸入一些內(nèi)容,因?yàn)槭撬接械?,所以可以隨便輸入,最好記住能與后面保持一致,特別是"Common Name"。上面的自簽證書(shū)cacert.pem應(yīng)該生成在/etc/pki/CA下。

(4) 為我們的nginx web服務(wù)器生成ssl密鑰

mkdir -p /etc/nginx/ssl
cd /etc/nginx/ssl
openssl genrsa -out nginx.key 2048

我們的CA中心與要申請(qǐng)證書(shū)的服務(wù)器是同一個(gè),否則應(yīng)該是在另一臺(tái)需要用到證書(shū)的服務(wù)器上生成。

(5) 為nginx生成證書(shū)簽署請(qǐng)求

openssl req -new -key nginx.key -out nginx.csr

輸出:

You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:youyuan
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:docker.yy.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

同樣會(huì)提示輸入一些內(nèi)容,Commone Name一定要是你要授予證書(shū)的服務(wù)器域名或主機(jī)名,challenge password不填。

(6) 私有CA根據(jù)請(qǐng)求來(lái)簽發(fā)證書(shū)

touch /etc/pki/CA/index.txt
touch /etc/pki/CA/serial
echo 00 > /etc/pki/CA/serial
openssl ca -in nginx.csr -out nginx.crt

輸出:

Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 0 (0x0)
        Validity
            Not Before: Dec  9 09:59:20 2014 GMT
            Not After : Dec  9 09:59:20 2015 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = beijing
            organizationName          = youyuan
            commonName                = docker.yy.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                5D:6B:02:FF:9E:F8:EA:1B:73:19:47:39:4F:88:93:9F:E7:AC:A5:66
            X509v3 Authority Key Identifier:
                keyid:46:DC:F1:A5:6F:39:EC:6E:77:03:3B:C4:34:03:7E:B8:0A:ED:99:41

Certificate is to be certified until Dec  9 09:59:20 2015 GMT (365 days)
Sign the certificate? [y/n]:y


1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

同樣會(huì)提示輸入一些內(nèi)容,選擇y就可以了!


安裝,配置,運(yùn)行nginx

(1) 添加組和用戶:

groupadd www -g 58
useradd -u 58 -g www www

(2) 下載nginx源文件:

cd /tmp
wget http://nginx.org/download/nginx-1.4.6.tar.gz
cp ./nginx-1.4.6.tar.gz /tmp/

(3) 編譯,安裝nginx:

tar zxvf ./nginx-1.4.6.tar.gz
cd ./nginx-1.4.6 && \
  ./configure --user=www --group=www --prefix=/opt/nginx \
  --with-pcre \
  --with-http_stub_status_module \
  --with-http_ssl_module \
  --with-http_addition_module  \
  --with-http_realip_module \
  --with-http_flv_module && \
  make && \
  make install
cd /tmp
rm -rf /tmp/nginx-1.4.6/
rm /tmp/nginx-1.4.6.tar.gz

(4) 生成htpasswd

htpasswd -cb /opt/nginx/conf/.htpasswd ${USER} ${PASSWORD}

(5) 編輯/opt/nginx/conf/nginx.conf文件

#daemon off;

# 使用的用戶和組
user  www www;
# 指定工作進(jìn)程數(shù)(一般等于CPU總核數(shù))
worker_processes  auto;

# 指定錯(cuò)誤日志的存放路徑,錯(cuò)誤日志記錄級(jí)別選項(xiàng)為:[debug | info | notic | warn | error | crit]
error_log  /var/log/nginx_error.log  error;

#指定pid存放的路徑
#pid        logs/nginx.pid;

# 指定文件描述符數(shù)量
worker_rlimit_nofile 51200;

events {
    # 使用的網(wǎng)絡(luò)I/O模型,Linux推薦epoll;FreeBSD推薦kqueue
    use epoll;
    # 允許的最大連接數(shù)
    worker_connections  51200;
    multi_accept on;
}

http {
  include       mime.types;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$upstream_addr"';

  access_log  /var/log/nginx_access.log  main;

  # 服務(wù)器名稱哈希表的桶大小,該默認(rèn)值取決于CPU緩存
  server_names_hash_bucket_size 128;
  # 客戶端請(qǐng)求的Header頭緩沖區(qū)大小
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;

  # 啟用sendfile()函數(shù)
  sendfile        on;
  tcp_nopush      on;
  tcp_nodelay     on;

  keepalive_timeout  65;

  upstream registry {
    server 127.0.0.1:5000;
  }

  server {
    listen       443;
    server_name  192.168.2.114;

    ssl                  on;
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    client_max_body_size 0; # disable any limits to avoid HTTP 413 for large image uploads

    # required to avoid HTTP 411: see Issue #1486 (https://github.com/docker/docker/issues/1486)
    chunked_transfer_encoding on;

    location / {
      auth_basic "registry";
      auth_basic_user_file /opt/nginx/conf/.htpasswd;

      root   html;
      index  index.html index.htm;

      proxy_pass                  http://registry;
      proxy_set_header  Host           $http_host;
      proxy_set_header  X-Real-IP      $remote_addr;
      proxy_set_header  Authorization  "";

      client_body_buffer_size     128k;
      proxy_connect_timeout       90;
      proxy_send_timeout          90;
      proxy_read_timeout          90;
      proxy_buffer_size           8k;
      proxy_buffers               4 32k;
      proxy_busy_buffers_size     64k;  #如果系統(tǒng)很忙的時(shí)候可以申請(qǐng)更大的proxy_buffers 官方推薦*2
      proxy_temp_file_write_size  64k;  #proxy緩存臨時(shí)文件的大小
    }
    location /_ping {
      auth_basic off;
      proxy_pass http://registry;
    }
    location /v1/_ping {
      auth_basic off;
      proxy_pass http://registry;
    }
  }
}

(6) 驗(yàn)證配置

/opt/nginx/sbin/nginx -t

輸出:

nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

(7) 啟動(dòng)nginx:

/opt/nginx/sbin/nginx

(8) 驗(yàn)證nginx是否啟動(dòng):

ps -ef | grep -i 'nginx'

如下輸出就表明nginx一切正常!

root     27133     1  0 18:58 ?        00:00:00 nginx: master process /opt/nginx/sbin/nginx
www      27134 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27135 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27136 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27137 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27138 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27139 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27140 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27141 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27142 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27143 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27144 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27145 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27146 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27147 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27148 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27149 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27150 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27151 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27152 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27153 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27154 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27155 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27156 27133  0 18:58 ?        00:00:00 nginx: worker process
www      27157 27133  0 18:58 ?        00:00:00 nginx: worker process
root     27160 42863  0 18:58 pts/0    00:00:00 grep -i nginx

配置,運(yùn)行Docker

(1) 停止docker

service docker stop

(2)編輯/etc/sysconfig/docker文件,加上如下一行

DOCKER_OPTS="--insecure-registry docker.yy.com --tlsverify --tlscacert /etc/pki/CA/cacert.pem"

(3) 把根證書(shū)復(fù)制到/etc/docker/certs.d/docker.yy.com/目錄下

mkdir -p /etc/docker/certs.d/docker.yy.com/
cp /etc/pki/CA/cacert.pem /etc/docker/certs.d/docker.yy.com/ca-certificates.crt

(4) 啟動(dòng)docker

service docker start

下載,配置,運(yùn)行registryimage

(1) 獲取Image

docker pull registry

(2) 運(yùn)行Image

mkdir -p /opt/registry
docker run -d -e STORAGE_PATH=/registry -v /opt/registry:/registry -p 127.0.0.1:5000:5000 --name registry registry

命令稍加解釋一下: -p 127.0.0.1:5000:5000 registry 作為上游服務(wù)器,這個(gè) 5000 端口可以不用映射出來(lái),因?yàn)樗械耐獠吭L問(wèn)都是通過(guò)前端的nginx來(lái)提供,nginx 可以在私有網(wǎng)絡(luò)訪問(wèn) registry 。

(3) 驗(yàn)證registry:

用瀏覽器輸入: https://docker.yy.com 或者:curl -i -k https://abc:123@docker.yy.com

服務(wù)端的配置就到此完成!


[X] Docker客戶端配置

(1) 編輯/etc/hosts,把docker.yy.com的ip地址添加進(jìn)來(lái),例如:

192.168.2.114 docker.yy.com

(2) 把docker registry服務(wù)器端的根證書(shū)追加到ca-certificates.crt文件里

先從docker registry服務(wù)器端把文件/etc/pki/CA/cacert.pem拷貝到本機(jī),然后執(zhí)行命令:

cat ./cacert.pem >> /etc/pki/tls/certs/ca-certificates.crt

(3) 驗(yàn)證docker.yy.com下的registry:

用瀏覽器輸入: https://docker.yy.com 或者:curl -i -k https://abc:123@docker.yy.com

(4) 使用私有registry步驟:

  • 登錄: docker login -u abc -p 123 -e "test@gmail.com" https://docker.yy.com

  • 給container起另外一個(gè)名字: docker tag centos:centos6 docker.yy.com/centos:centos6

  • 發(fā)布: docker push docker.yy.com/centos:centos6


[X] Server端,操作私有倉(cāng)庫(kù)的步驟:

1. 從官方pull下來(lái)image!

docker push centos:centos6

2. 查看image的id

執(zhí)行docker images
輸出:

root@pts/0 # docker images
REPOSITORY                                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos                                       centos6             25c5298b1a36        8 days ago          215.8 MB

3. 給image賦予一個(gè)私有倉(cāng)庫(kù)的tag

docker tag 25c5298b1a36 docker.yy.com/centos:centos6

4. push到私有倉(cāng)庫(kù)

docker push docker.yy.com/centos:centos6

5. 查看image

docker images
輸出:

root@pts/0 # docker images
REPOSITORY                                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos                                       centos6             25c5298b1a36        8 days ago          215.8 MB
docker.yy.com/centos                         centos6             25c5298b1a36        8 days ago          215.8 MB

[X] Client端,操作私有倉(cāng)庫(kù)的步驟:

1. 從私有倉(cāng)庫(kù)pull下來(lái)image!

docker pull docker.yy.com/centos:centos6

2. 查看image

docker images
輸出:

root@pts/0 # docker images
REPOSITORY                                   TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
docker.yy.com/centos                         centos6             25c5298b1a36        8 days ago          215.8 MB

Docker的Web管理界面Shipyard

[0] 編輯/etc/sysconfig/docker文件

DOCKER_OPTS里添加-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock,例如:

DOCKER_OPTS="-H tcp://0.0.0.0:4243 -H unix:///var/run/docker.sock --insecure-registry docker.yy.com --tlsverify --tlscacert /etc/pki/CA/cacert.pem --registry-mirror=http://d194d5cb.m.daocloud.io"

[1] Start an data volume instance of RethinkDB:

docker run -it -d --name shipyard-rethinkdb-data --entrypoint /bin/bash shipyard/rethinkdb -l

[2] Start RethinkDB with using the data volume container:

docker run -it -P -d --name shipyard-rethinkdb --volumes-from shipyard-rethinkdb-data shipyard/rethinkdb

If your server is directly accessible on Internet,
please note your RethinkDB installation may publicly listen to
ports 49153 (local instance), 49154 (cluster) and 49155 (web interface) and so accessible to all.

[3] Start the Shipyard controller:

docker run -it -p 8080:8080 -d --name shipyard --link shipyard-rethinkdb:rethinkdb shipyard/shipyard

Shipyard will create a default user account with the usernameadmin and the passwordshipyard.
You should then be able to open a browser tohttp://<your-host-ip>:8080and see the Shipyard login.

附錄:

(1) 弊端:

server端可以login到官方的Docker Hub,可以pull,push官方和私有倉(cāng)庫(kù)!
client端只能操作搭設(shè)好的私有倉(cāng)庫(kù)!
私有倉(cāng)庫(kù)不能search!

(2) 優(yōu)點(diǎn):

所有的build,pull,push操作只能在私有倉(cāng)庫(kù)的server端操作,降低企業(yè)風(fēng)險(xiǎn)!

(3) 當(dāng)client端docker login到官方的https://index.docker.io/v1/網(wǎng)站,出現(xiàn)x509: certificate signed by unknown authority錯(cuò)誤時(shí)

重命名根證書(shū)! mv /etc/pki/tls/certs/ca-certificates.crt /etc/pki/tls/certs/ca-certificates.crt.bak
重啟docker服務(wù)! service docker restart!

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Docker私有Registry如何在CentOS6.X下安裝”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI