準備完docker文件,接下來就要搭建一個Harbor私有倉庫來保管鏡像。
安裝一些前置安裝包,如docker-compose等
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
curl -L https://github.com/docker/compose/releases/download/1.13.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
測試下docker-compose是否成功
docker-compose --version
從
https://github.com/goharbor/harbor/releases
下載離線安裝包,我用的時候可能是docker鏡像倉庫的原因,2.1.0的版本裝不上,一直提示找不到鏡像,所以裝了一個舊版本的1.10的。
把下載下來的tar文件拷貝到服務(wù)器,然后解壓
tar -zxf harbor-offline-installer-v1.10.0.tgz
進入解壓后的harbor目錄,先進行一些配置
vi harbor.yml
部分文件示例:
# Configuration file of Harbor
# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: 172.23.x.x #harbor的訪問地址,我們是內(nèi)網(wǎng)環(huán)境,這里寫內(nèi)網(wǎng)ip
# http related config
http:
port: 81 #Harbor的http端口
# https related config
#https: #如果需要https配置,則把這里解開,配置https端口和證書位置。
# https port for harbor, default is 443
# port: 448
# The path of cert and key files for nginx
# certificate: /xhr/certificate/5154301_xhr.xinhongren.net.pem
# private_key: /xhr/certificate/5154301_xhr.xinhongren.net.key
# external_url: https://reg.mydomain.com:8433
# The initial password of Harbor admin
# It only works in first time to install harbor
# Remember Change the admin password from UI after launching Harbor.
harbor_admin_password: Harbor12345 #默認的admin密碼
# Harbor DB configuration
database:
# The password for the root user of Harbor DB. Change this before any production use.
password: root123
max_idle_conns: 50
# Note: the default number of connections is 100 for postgres.
max_open_conns: 100
# The default data volume
data_volume: /data
# Harbor Storage settings by default is using /data dir on local filesystem
# Uncomment storage_service setting If you want to using external storage
# storage_service:
# # ca_bundle is the path to the custom root ca certificate, which will be injected into the truststore
# ca_bundle:
# # storage backend, default is filesystem, options include filesystem, azure, gcs, s3, swift and oss
# # for more info about this configuration please refer https://docs.docker.com/registry/configuration/
# filesystem:
# redirect:
# The interval of clair updaters, the unit is hour, set to 0 to disable the updaters.
updaters_interval: 12
jobservice:
# Maximum number of job workers in job service
max_job_workers: 30
有幾項配置要注意:
hostname: 是指harbor的訪問地址,因為我們是阿里云的內(nèi)網(wǎng)環(huán)境,所以這里填了內(nèi)網(wǎng)IP,外網(wǎng)的話,要填寫域名或者IP都可以,但是有內(nèi)網(wǎng)環(huán)境最
好是用內(nèi)網(wǎng)IP,外網(wǎng)有時候推送和拉取鏡像都很慢,慢到懷疑人生的那種慢。
http.port: http的訪問端口,如果https被打開了,那么http的訪問會被重定向到https端口去
https:https的相關(guān)配置,如果需要https訪問,那么這里要配置證書文件的位置,證書文件使用nginx的.pem文件就可以
harbor_admin_password: 默認的admin密碼,注意這個地方安裝之后就不要修改了,否則會登陸不上去。
jobservice.max_job_workers: 默認是10,這個是最大并發(fā)的worker數(shù)量,生產(chǎn)環(huán)境要改大一點。
配置完之后,運行
./prepare.sh
./install.sh
就完成了harbor的安裝。
安裝完成后,如果需要更改配置,那么修改harbor.yml后,先停止harbor
docker-compose down-v
然后
./prepare.sh
重啟harbor:
docker-compose up -d
就完成了對harbor的重新配置。
安裝完harbor后,我們就可以通過docker登陸到私有倉庫,但是在這之前,因為我們配置的是http方式訪問harbor,這里還需要加一項配置到docker:
/etc/docker/daemon.json
在文件內(nèi)加如下配置:
{
"insecure-registries" : ["172.23.x.x:81"]
}
這樣就可以正常登陸172.23.x.x:81,注意一定要加端口號
接下來進入harbor的管理頁面,這里由于是內(nèi)網(wǎng)環(huán)境,我們可以通過XShell建立轉(zhuǎn)移規(guī)則到localhost:81

然后瀏覽器打開localhost:81,登陸admin:

可以看到我們創(chuàng)建一個新項目xhr,接下來我們的鏡像會傳到這個項目下。
到服務(wù)器登陸harbor:
docker login 172.23.x.x:81 -u admin -p 123151
-u后面是用戶名, -p后面是密碼
然后先到之前的dockerfile目錄下,打包并推送鏡像:
docker build -t busjar .
docker tag busjar 172.23.x.x:81/xhr/busjar:latest
docker push 172.23.x.x:81/xhr/busjar:latest
tag后面的格式是固定的,一定要是harbor地址/項目名/報名,可以在這里確認push命令:

這樣就完成了鏡像的推送,在管理頁面檢查:

可以在這里看到pull命令:

在安裝了docker的機器上,login后直接輸入
docker pull 172.23.x.x/xhr/busjar
就可以把鏡像拉取下來了。
但是因為我們的服務(wù)器很多,不可能每個服務(wù)器都輸一遍命令,這就需要用到ansible來批量執(zhí)行命令了,下面我們就來安裝ansible。