Harbor Docker 鏡像倉庫搭建

Harbor是一個用於存儲和分發Docker鏡像的企業級Registry服務器,經過添加一些企業必需的功能特性,例如安全、標識和管理等,擴展了開源Docker Distribution。python

環境:Centos 7mysql

1.Docker環境安裝 並啓動本地Docker環境

#列出版本信息
yum list docker-ce --showduplicates | sort -r

#安裝指定版本
yum install docker-ce-17.03.0.ce-1.el7.centos

#安裝
yum install -y docker

#啓動
systemctl start docker

#驗證環境就緒
docker run hello-world

yum remove docker docker-common docker-selinux

yum install https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm

2.安裝Docker-Compose

Docker-Compose下載地址:https://github.com/docker/compose/releases/linux

#進入指定目錄(自由指定)
cd /usr/server
#下載
curl -L https://github.com/docker/compose/releases/download/1.14.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#可執行賦權
chmod +x /usr/local/bin/docker-compose
#查看版本驗證安裝
docker-compose --version

3.安裝Harbor

Harbor下載地址:https://github.com/vmware/harbor/releasesnginx

選擇須要的版本下載,這裏選擇1.1.2git

#下載tar包
wget https://github.com/vmware/harbor/releases/download/v1.1.2/harbor-online-installer-v1.1.2.tgz
#解壓
tar zxf harbor-online-installer-v1.1.2.tgz
#解析後進入harbor目錄
cd harbor

修改harbor.cfg配置文件,完整配置文件見最後附錄github

爲簡單起見我這裏只修改兩個位置web

a.爲了讓外網能夠訪問到 修改hostname爲機器的公網IPsql

b.修改UI管理系統的管理員密碼 harbor_admin_password = admin123docker

保存修改後運行當前目錄下的安裝腳本install.shvim

./install.sh

自動安裝腳本會下載幾個docker鏡像並啓動,

注意默認web服務nginx鏡像綁定80端口,nginx鏡像啓動失敗請先釋放80端口。

安裝完成後訪問IP或者綁定的域名就能夠訪問到Harbor的管理界面。以下:

4.Harbor的啓停

#啓動
docker-compose start

#中止
docker-compose stop

5.配置Docker

由於docker默認使用的是https鏈接,而harbor默認使用http鏈接,因此須要修改docker配置標誌insecure registry不安全倉庫的主機!
harbor也能夠設置爲https。

vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry=94.191.16.123
#只加上--insecure-registry這個參數便可

#重啓docker:
systemctl daemon-reload
systemctl restart docker.service

 

6.進階配置

6.1 修改nginx鏡像的端口

nginx鏡像默認監聽80端口,爲安全因素或避免衝突能夠改成其餘端口,此處我改成8820端口

6.1.1 編輯Harbor目錄下的docker-compose.yml文件(完整文件見附錄二),找到nginx鏡像節點 修改以下:

proxy:
    image: nginx:1.11.5
    container_name: nginx
    restart: always
    volumes:
      - ./common/config/nginx:/etc/nginx
    ports:
      - 8820:80
      - 1143:443
    depends_on:
      - mysql
      - registry
      - ui
      - log

6.1.2 修改common/templates/registry/config.yml文件加入8820端口:

#vim common/templates/registry/config.yml

auth:
  token:
    issuer: registry-token-issuer
    realm: $ui_url:8820/service/token
    rootcertbundle: /etc/registry/root.crt
    service: token-service

6.1.3 中止harbor,從新啓動並生成配置文件:

docker-compose stop
./install.sh

6.1.4 修改docker啓動文件,設置信任的主機與端口(第五步配置docker中修改端口80爲8820):

vim /usr/lib/systemd/system/docker.service  修改以下一行
ExecStart=/usr/bin/dockerd --insecure-registry=172.16.103.99:1180

6.1.5 從新啓動docker並驗證:

systemctl daemon-reload
systemctl restart docker.service

登陸驗證

docker login 94.191.16.123:8820
Username: admin
Password: 
Login Succeeded

 

遇到的問題:

1.安裝執行install.sh報錯 File "./prepare", line 60 os.makedirs(folder, mode=0600)

查緣由發如今執行python 腳本 prepare時報錯,prepare腳本基於python2.7 當前機器默認python環境被我改成python3.7 改回原版安裝便可

mv python python3.7.2.bak
mv python2.7.bak python
python --version

 

 

附錄一:harbor.cfg配置文件

## 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 = 94.191.16.123

#The protocol for accessing the UI and token/notification service, by default it is http.
#It can be set to https if ssl is enabled on nginx.
ui_url_protocol = http

#The password for the root user of mysql db, change this before any production use.
db_password = root123

#Maximum number of job workers in job service  
max_job_workers = 3 

#Determine whether or not to generate certificate for the registry's token.
#If the value is on, the prepare script creates new root cert and private key 
#for generating token to access the registry. If the value is off the default key/cert will be used.
#This flag also controls the creation of the notary signer's cert.
customize_crt = on

#The path of cert and key files for nginx, they are applied only the protocol is set to https
ssl_cert = /data/cert/server.crt
ssl_cert_key = /data/cert/server.key

#The path of secretkey storage
secretkey_path = /data

#Admiral's url, comment this attribute, or set its value to NA when Harbor is standalone
admiral_url = NA

#NOTES: The properties between BEGIN INITIAL PROPERTIES and END INITIAL PROPERTIES
#only take effect in the first boot, the subsequent changes of these properties 
#should be performed on web ui

#************************BEGIN INITIAL PROPERTIES************************

#Email account settings for sending out password resetting emails.

#Email server uses the given username and password to authenticate on TLS connections to host and act as identity.
#Identity left blank to act as username.
email_identity = 

email_server = smtp.mydomain.com
email_server_port = 25
email_username = sample_admin@mydomain.com
email_password = abc
email_from = admin <sample_admin@mydomain.com>
email_ssl = false

##The initial password of Harbor admin, only works for the first time when Harbor starts. 
#It has no effect after the first launch of Harbor.
#Change the admin password from UI after launching Harbor.
harbor_admin_password = admin123

##By default the auth mode is db_auth, i.e. the credentials are stored in a local database.
#Set it to ldap_auth if you want to verify a user's credentials against an LDAP server.
auth_mode = db_auth

#The url for an ldap endpoint.
ldap_url = ldaps://ldap.mydomain.com

#A user's DN who has the permission to search the LDAP/AD server. 
#If your LDAP/AD server does not support anonymous search, you should configure this DN and ldap_search_pwd.
#ldap_searchdn = uid=searchuser,ou=people,dc=mydomain,dc=com

#the password of the ldap_searchdn
#ldap_search_pwd = password

#The base DN from which to look up a user in LDAP/AD
ldap_basedn = ou=people,dc=mydomain,dc=com

#Search filter for LDAP/AD, make sure the syntax of the filter is correct.
#ldap_filter = (objectClass=person)

# The attribute used in a search to match a user, it could be uid, cn, email, sAMAccountName or other attributes depending on your LDAP/AD  
ldap_uid = uid 

#the scope to search for users, 1-LDAP_SCOPE_BASE, 2-LDAP_SCOPE_ONELEVEL, 3-LDAP_SCOPE_SUBTREE
ldap_scope = 3 

#Timeout (in seconds)  when connecting to an LDAP Server. The default value (and most reasonable) is 5 seconds.
ldap_timeout = 5

#Turn on or off the self-registration feature
self_registration = on

#The expiration time (in minute) of token created by token service, default is 30 minutes
token_expiration = 30

#The flag to control what users have permission to create projects
#The default value "everyone" allows everyone to creates a project. 
#Set to "adminonly" so that only admin user can create project.
project_creation_restriction = everyone

#Determine whether the job service should verify the ssl cert when it connects to a remote registry.
#Set this flag to off when the remote registry uses a self-signed or untrusted certificate.
verify_remote_cert = on
#************************END INITIAL PROPERTIES************************
#############

 

附錄二:docker-compose.yml

version: '2'
services:
  log:
    image: vmware/harbor-log:v1.1.2
    container_name: harbor-log 
    restart: always
    volumes:
      - /var/log/harbor/:/var/log/docker/:z
    ports:
      - 127.0.0.1:1514:514
    networks:
      - harbor
  registry:
    image: vmware/registry:2.6.1-photon
    container_name: registry
    restart: always
    volumes:
      - /data/registry:/storage:z
      - ./common/config/registry/:/etc/registry/:z
    networks:
      - harbor
    environment:
      - GODEBUG=netdns=cgo
    command:
      ["serve", "/etc/registry/config.yml"]
    depends_on:
      - log
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "registry"
  mysql:
    image: vmware/harbor-db:v1.1.2
    container_name: harbor-db
    restart: always
    volumes:
      - /data/database:/var/lib/mysql:z
    networks:
      - harbor
    env_file:
      - ./common/config/db/env
    depends_on:
      - log
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "mysql"
  adminserver:
    image: vmware/harbor-adminserver:v1.1.2
    container_name: harbor-adminserver
    env_file:
      - ./common/config/adminserver/env
    restart: always
    volumes:
      - /data/config/:/etc/adminserver/config/:z
      - /data/secretkey:/etc/adminserver/key:z
      - /data/:/data/:z
    networks:
      - harbor
    depends_on:
      - log
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "adminserver"
  ui:
    image: vmware/harbor-ui:v1.1.2
    container_name: harbor-ui
    env_file:
      - ./common/config/ui/env
    restart: always
    volumes:
      - ./common/config/ui/app.conf:/etc/ui/app.conf:z
      - ./common/config/ui/private_key.pem:/etc/ui/private_key.pem:z
      - /data/secretkey:/etc/ui/key:z
      - /data/ca_download/:/etc/ui/ca/:z
    networks:
      - harbor
    depends_on:
      - log
      - adminserver
      - registry
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "ui"
  jobservice:
    image: vmware/harbor-jobservice:v1.1.2
    container_name: harbor-jobservice
    env_file:
      - ./common/config/jobservice/env
    restart: always
    volumes:
      - /data/job_logs:/var/log/jobs:z
      - ./common/config/jobservice/app.conf:/etc/jobservice/app.conf:z
      - /data/secretkey:/etc/jobservice/key:z
    networks:
      - harbor
    depends_on:
      - ui
      - adminserver
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "jobservice"
  proxy:
    image: vmware/nginx:1.11.5-patched
    container_name: nginx
    restart: always
    volumes:
      - ./common/config/nginx:/etc/nginx:z
    networks:
      - harbor
    ports:
      - 80:80
      - 443:443
      - 4443:4443
    depends_on:
      - mysql
      - registry
      - ui
      - log
    logging:
      driver: "syslog"
      options:  
        syslog-address: "tcp://127.0.0.1:1514"
        tag: "proxy"
networks:
  harbor:
    external: false
相關文章
相關標籤/搜索