docker+harbor+ansible 安装使用2 - 安装使用Harbor

准备完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文件拷贝到服务器,然后解压
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的访问地址,我们是内网环境,这里写内网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的访问地址,因为我们是阿里云的内网环境,所以这里填了内网IP,外网的话,要填写域名或者IP都可以,但是有内网环境最
好是用内网IP,外网有时候推送和拉取镜像都很慢,慢到怀疑人生的那种慢。
http.port: http的访问端口,如果https被打开了,那么http的访问会被重定向到https端口去
https:https的相关配置,如果需要https访问,那么这里要配置证书文件的位置,证书文件使用nginx的.pem文件就可以
harbor_admin_password: 默认的admin密码,注意这个地方安装之后就不要修改了,否则会登陆不上去。
jobservice.max_job_workers: 默认是10,这个是最大并发的worker数量,生产环境要改大一点。

配置完之后,运行
./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
在文件内加如下配置:

{
"insecure-registries" : ["172.23.x.x:81"]
}

这样就可以正常登陆172.23.x.x:81,注意一定要加端口号

接下来进入harbor的管理页面,这里由于是内网环境,我们可以通过XShell建立转移规则到localhost:81


image.png

然后浏览器打开localhost:81,登陆admin:


image.png

可以看到我们创建一个新项目xhr,接下来我们的镜像会传到这个项目下。

到服务器登陆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命令:


image.png

这样就完成了镜像的推送,在管理页面检查:


image.png

可以在这里看到pull命令:
image.png

在安装了docker的机器上,login后直接输入
docker pull 172.23.x.x/xhr/busjar
就可以把镜像拉取下来了。
但是因为我们的服务器很多,不可能每个服务器都输一遍命令,这就需要用到ansible来批量执行命令了,下面我们就来安装ansible。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容