kubernetes集群搭建harbor仓库,单机搭建harbor以及docker registry仓库搭建,内附踩坑细节
Kubnernetes集群搭建(Harbor)私服仓库搭建Kubernetes集群仓库之前最好先从Docker私服仓库循序渐进开始。本文档会从Docker私服安装开始一步一步走向集群私服仓库(Harbor)搭建。1.Docker私服仓库搭建Docker提供了Docker-registry用于搭建私服仓库,Docker-registry是基于Python的开源项目,既可以运行在真正的主机...
Kubnernetes集群搭建(Harbor)私服仓库
搭建Kubernetes集群仓库之前最好先从Docker私服仓库循序渐进开始。本文档会从Docker私服安装开始一步一步走向集群私服仓库(Harbor)搭建。
1.Docker私服仓库搭建
Docker提供了Docker-registry用于搭建私服仓库,Docker-registry是基于Python的开源项目,既可以运行在真正的主机上,也可以运行在docker容器中,docker官方已经提供了Docker-registry镜像。我们将介绍这两种搭建方式。
注意:操作系统是基于centos7进行讲解。
1.基于容器方式搭建
安装docker-ce
yum install docker-ce进行安装,不再进行讲解。
下载仓库镜像
docker pull registry通过docker将仓库下载下来。
启动仓库镜像
docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry registry 注意前面的端口映射,因为镜像仓库会将上传的镜像存放在容器的/tmp/registry里面,如果容器一旦被删除,相关镜像会被删除,所以需要将容器中的位置挂载到宿主机上面,通过-v参数 -v /opt/data/registry:/tmp/registry ,可以将镜像路径映射到宿主机/opt/data/registry里面,这样删除了容器,但是上传的镜像任然存在。
修改docker配置文件
因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。修改docker启动配置文件:
vim /usr/lib/systemd/system/docker.service
找到 ExecStart
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.0.153:5000
添加--insecure-registry 10.0.81.36:5000,注意将ip地址修改成自己的。然后重启docker。
测试
可以随便下载一个镜像,docker pull mysql,将下载下来的镜像进行修改一下,例如:docker tag mysql localhots:mysql,将mysql镜像修改成localhost:mysql镜像。docker push localhost:mysql将自己的镜像进行上传。然后将镜像删除,通过docker pull 镜像名称,如果能下载下来,代表私服创建成功。curl http:// 10.0.81.36:5000/v2/_catalog
2.基于宿主机方式搭建仓库
安装依赖库
安装下面插件:
yum install -y python-devel;
yum install -y libevent-devel;
yum install -y python-pip;特别注意,现在这种方式已经无法安装,安装步骤如下:
安装步骤: 第一步:下载tar包 wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb 第二步:解压tar包 tar -zxvf pip-8.0.2.tar.gz 第三步:进入安装文件 cd pip-8.0.2 第四步:安装 python setup.py build 第五步:安装 python setup.py install 如果没有意外的话,pip安装完成,如果出了意外我他妈也不知道,自己百度。老子java程序员,不是python程序员,反正我一步安装成功。
yum install -y gcc;
yum install -y xz-devel;
安装Python相关的模块
安装必要工具 yum-utils ,它的功能是管理repository及扩展包的工具 (主要是针对repository)
$ sudo yum install yum-utils
使用yum-builddep为Python3构建环境,安装缺失的软件依赖,使用下面的命令会自动处理.
$ sudo yum-builddep python
完成后下载Python3的源码包(笔者以Python3.5为例),Python源码包目录: https://www.python.org/ftp/python/ ,截至发博当日Python3的最新版本为 3.7.0
$ curl -O https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
最后一步,编译安装Python3,默认的安装目录是 /usr/local 如果你要改成其他目录可以在编译(make)前使用 configure 命令后面追加参数 “–prefix=/alternative/path” 来完成修改。
$ tar xf Python-3.5.0.tgz $ cd Python-3.5.0 $ ./configure $ make $ sudo make install
至此你已经在你的CentOS系统中成功安装了python3、pip3、setuptools,查看python版本
$ python3 -V
如果你要使用Python3作为python的默认版本,你需要修改一下 bashrc 文件,增加一行alias参数
alias python='/usr/local/bin/python3.5'
由于CentOS 7建议不要动/etc/bashrc文件,而是把用户自定义的配置放入/etc/profile.d/目录中,具体方法为
vi /etc/profile.d/python.sh
输入alias参数 alias python=’/usr/local/bin/python3.5’,保存退出
如果非root用户创建的文件需要注意设置权限
chmod 755 /etc/profile.d/python.sh
重启会话使配置生效
source /etc/profile.d/python.sh
安装docker-registry
docker-registry安装有两种方式:一种直接通过python3 -m pip install docker-registry安装,一种使用源码安装;这边直接通过第一种方式进行安装了。
python3 -m install docker-registry
2.Docker搭建harbor 私有仓库
Docker容器应用的开发和运行离不开可靠的镜像管理,虽然Docker官方也提供了公共的镜像仓库,但是从安全和效率等方面考虑,部署我们私有环境内的Registry也是非常必要的。Harbor是由VMware公司开源的企业级的Docker Registry管理项目,它包括权限管理(RBAC)、LDAP、日志审核、管理界面、自我注册、镜像复制和中文支持等功能。
本次演示环境,我是在虚拟机Linux Centos7上操作,以下是安装的软件及版本:
-
Docker:version 18.06
-
Docker-compose: version 1.22
-
Harbor: version 1.5.2
注意:Harbor的所有服务组件都是在Docker中部署的,所以官方安装使用Docker-compose快速部署,所以我们需要安装Docker、Docker-compose。由于Harbor是基于Docker Registry V2版本,所以就要求Docker版本不小于1.10.0,Docker-compose版本不小于1.6.0。
安装docker
略
安装docker-compose
安装docker-compose 有两种方式,一种通过python pip安装,一种直接下载二进制进行安装,本章以二进制安装展开。
从https://github.com/docker/compose/releases下载docker-coompose二进制编码或者直接使用curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-uname -s
-uname -m
-o /usr/local/bin/docker-compose进行下载。
将下载的文件重命名为docker-compose,放在/usr/local/bin目录下面,在文件进行赋权,
chmod +x /usr/local/bin/docker-compose
测试下docker-compose是否安装成功 docker-compose --version
docker-compose version 1.22.0, build 1719ceb
总步骤: 1、下载指定版本的docker-compose curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose 2、对二进制文件赋可执行权限 $ sudo chmod +x /usr/local/bin/docker-compose 3、测试下docker-compose是否安装成功 $ docker-compose --version docker-compose version 1.13.0, build 1719ceb
Harbor 服务搭建
下载Harbor二进制,可从github上直接下载,或者在线下载安装包。
Harbor提供两部分:一部分是离线包,一部分是在线包,选择下载一个。
下载安装包
1、在线安装包 $ wget https://github.com/vmware/harbor/releases/download/v1.5.2/harbor-online-installer-v1.5.2.tgz $ tar xvf harbor-online-installer-v1.5.2.tgz 2、离线安装包 $ wget https://github.com/vmware/harbor/releases/download/v1.5.2/harbor-offline-installer-v1.5.2.tgz $ tar xvf harbor-offline-installer-v1.5.2.tgz
修改配置文件
解压缩之后,目录下回生成harbor.cfg文件,该文件就是Harbor的配置文件。
## Configuration file of Harbor #This attribute is for migrator to detect the version of the .cfg file, DO NOT MODIFY! _version = 1.5.0 #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 = 10.0.81.36 #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 #Maximum number of job workers in job service max_job_workers = 50 #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 #Log files are rotated log_rotate_count times before being removed. If count is 0, old versions are removed rather than rotated. log_rotate_count = 50 #Log files are rotated only if they grow bigger than log_rotate_size bytes. If size is followed by k, the size is assumed to be in kilobytes. #If the M is used, the size is in megabytes, and if G is used, the size is in gigabytes. So size 100, size 100k, size 100M and size 100G #are all valid. log_rotate_size = 200M #Config http proxy for Clair, e.g. http://my.proxy.com:3128 #Clair doesn't need to connect to harbor ui container via http proxy. http_proxy = https_proxy = no_proxy = 127.0.0.1,localhost,ui #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 email_insecure = 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 = Harbor12345 ##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, 0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE ldap_scope = 2 #Timeout (in seconds) when connecting to an LDAP Server. The default value (and most reasonable) is 5 seconds. ldap_timeout = 5 #Verify certificate from LDAP server ldap_verify_cert = true #The base dn from which to lookup a group in LDAP/AD ldap_group_basedn = ou=group,dc=mydomain,dc=com #filter to search LDAP/AD group ldap_group_filter = objectclass=group #The attribute used to name a LDAP/AD group, it could be cn, name ldap_group_gid = cn #The scope to search for ldap groups. 0-LDAP_SCOPE_BASE, 1-LDAP_SCOPE_ONELEVEL, 2-LDAP_SCOPE_SUBTREE ldap_group_scope = 2 #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 #************************END INITIAL PROPERTIES************************ #######Harbor DB configuration section####### #The address of the Harbor database. Only need to change when using external db. db_host = mysql #The password for the root user of Harbor DB. Change this before any production use. db_password = root123 #The port of Harbor database host db_port = 3306 #The user name of Harbor database db_user = root ##### End of Harbor DB configuration####### #The redis server address. Only needed in HA installation. #address:port[,weight,password,db_index] redis_url = redis:6379 ##########Clair DB configuration############ #Clair DB host address. Only change it when using an exteral DB. clair_db_host = postgres #The password of the Clair's postgres database. Only effective when Harbor is deployed with Clair. #Please update it before deployment. Subsequent update will cause Clair's API server and Harbor unable to access Clair's database. clair_db_password = password #Clair DB connect port clair_db_port = 5432 #Clair DB username clair_db_username = postgres #Clair default database clair_db = postgres ##########End of Clair DB configuration############ #The following attributes only need to be set when auth mode is uaa_auth uaa_endpoint = uaa.mydomain.org uaa_clientid = id uaa_clientsecret = secret uaa_verify_cert = true uaa_ca_cert = /path/to/ca.pem ### Docker Registry setting ### #registry_storage_provider can be: filesystem, s3, gcs, azure, etc. registry_storage_provider_name = filesystem #registry_storage_provider_config is a comma separated "key: value" pairs, e.g. "key1: value, key2: value2". #Refer to https://docs.docker.com/registry/configuration/#storage for all available configuration. registry_storage_provider_config = #If reload_config=true, all settings which present in harbor.cfg take effect after prepare and restart harbor, it overwrites exsiting settings. #reload_config=true #Regular expression to match skipped environment variables #skip_reload_env_pattern=(^EMAIL.*)|(^LDAP.*)
需要注意:配置文件中hostname设置访问地址,可以使用ip、域名,不可以设置为127.0.0.1或localhost。
docker.service中增加 --insecure-registry rgs.unixfbi.com 即可。重启docker:
# systemctl daemon-reload # systemctl restart docker
或者
创建/etc/docker/daemon.json文件,在文件中指定仓库地址 # cat > /etc/docker/daemon.json << EOF { "insecure-registries":["rgs.unixfbi.com"] } EOF 然后重启docker就可以。 # systemctl restart docker
启动Harbor
修改完配置文件后,在的当前目录执行./install.sh。
Harbor服务就会根据当期目录下的docker-compose.yml
开始下载依赖的镜像,检测并按照顺序依次启动各个服务,Harbor依赖的镜像及启动服务如下:
执行harbor安装:
$ cd /harbor $ ./install.sh
install.sh包含四步:
-
loading Harbor images
-
preparing environment
-
checking existing instance of Harbor
-
starting Harbor 完成安装后提示:
✔ ----Harbor has been installed and started successfully.----1
通过docker ps 命令可以查看容器信息,通过docker-compose ps 可以查看编排信息:
$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3095a5bb81e4 vmware/harbor-jobservice:v1.2.0 "/harbor/harbor_jobse" 13 hours ago Up 13 hours harbor-jobservice ef7f8400663e vmware/nginx-photon:1.11.13 "nginx -g 'daemon off" 13 hours ago Up 13 hours 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp nginx 3c89da45d3f0 vmware/harbor-ui:v1.2.0 "/harbor/harbor_ui" 13 hours ago Up 13 hours harbor-ui d46ac994f6f7 vmware/harbor-adminserver:v1.2.0 "/harbor/harbor_admin" 13 hours ago Up 13 hours harbor-adminserver bde94644c2ce vmware/harbor-db:v1.2.0 "docker-entrypoint.sh" 13 hours ago Up 13 hours 3306/tcp harbor-db 8dc825c41d6f vmware/registry:2.6.2-photon "/entrypoint.sh serve" 13 hours ago Up 13 hours 5000/tcp registry 00033d665654 vmware/harbor-log:v1.2.0 "/bin/sh -c 'crond &&" 13 hours ago Up 13 hours 127.0.0.1:1514->514/tcp harbor-log $ sudo docker-compose ps Name Command State Ports ---------------------------------------------------------------------------------------------------------------------- harbor-adminserver /harbor/harbor_adminserver Up harbor-db docker-entrypoint.sh mysqld Up 3306/tcp harbor-jobservice /harbor/harbor_jobservice Up harbor-log /bin/sh -c crond && rm -f ... Up 127.0.0.1:1514->514/tcp harbor-ui /harbor/harbor_ui Up nginx nginx -g daemon off; Up 0.0.0.0:443->443/tcp, 0.0.0.0:4443->4443/tcp, 0.0.0.0:80->80/tcp registry /entrypoint.sh serve /etc/ ... Up 5000/tcp
登录Harbor
启动完成后,我们访问刚设置的hostname即可 http://10.0.81.36/,默认是80端口,如果端口占用,我们可以去修改docker-compose.yml文件中,对应服务的端口映射。
账号:admin 密码为默认:Harbor12345。密码在harbor.cfg文件中可以找到。
登录之后进入下面界面。
上传镜像到Harbor
# 查看当前的centos镜像 [root@urcar3 ~]# docker images|grep centos centos 6.7 000c5746fa52 6 months ago 191MB # 对现有的centos打tag,就tag成v1吧,这里的仓库地址在web界面项目里面的右侧的推送镜像即可知道。 [root@urcar3 ~]# docker tag centos:6.7 172.16.83.203/library/centos:v1 # 查看新tag的centos镜像 [root@urcar3 ~]# docker images|grep centos centos 6.7 000c5746fa52 6 months ago 191MB 10.0.81.36/library/centos v1 000c5746fa52 6 months ago 191MB
[root@urcar3 ~]# docker login 10.0.81.36 Username: admin #输入用户ing Password: #输入密码 WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Are you sure you want to proceed? y/N] y Login Succeeded #登录成功
3.Kubernetes集群上部署Harbor
1.环境说明
Kubernetes v1.10.0
Harbor v1.2.0
建议Kubernetes至少要准备v1.6.5及以后版本
harbor1.6.0版本,可从github上下载:https://github.com/goharbor/harbor.git
服务器地址:
master:192.168.72.2
node:192.168.72.6
master或者node机器上至少保证一台机器安装了nfs、rpc,因为集群上pv挂载使用的nfs。
harbor的目录结构
解压harbor的压缩包,直接计入harbor/make # cd harbor/make # tree kubernetes kubernetes ├── adminserver │ ├── adminserver.rc.yaml │ └── adminserver.svc.yaml ├── jobservice │ ├── jobservice.rc.yaml │ └── jobservice.svc.yaml ├── k8s-prepare ├── mysql │ ├── mysql.rc.yaml │ └── mysql.svc.yaml ├── pv │ ├── log.pvc.yaml │ ├── log.pv.yaml │ ├── registry.pvc.yaml │ ├── registry.pv.yaml ├── registry │ ├── registry.rc.yaml │ └── registry.svc.yaml ├── templates │ ├── adminserver.cm.yaml │ ├── jobservice.cm.yaml │ ├── mysql.cm.yaml │ ├── nginx.cm.yaml │ ├── registry.cm.yaml │ └── ui.cm.yaml └── ui ├── ui.rc.yaml └── ui.svc.yaml
2.安装nfs、rpc
具体可看分享的nfs安装记录。
3.创建pv、pvc
从刚刚的目录中可以看到,harbor已经提供了log和registry的pv,但是pv的挂载并不是nfs,所以用不上,log用于挂载harbor运行的日志信息,registry用于挂载上传的镜像,因为镜像上传到容器中,但是如果一旦容器被摧毁,存放在容器中的镜像都将无法恢复,所有需要通过pv挂载。
创建目录/home/pv,分别创建一下四个文件,具体示例如下:
log-pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: log-pv labels: type: log spec: capacity: storage: 1Gi accessModes: - ReadWriteMany nfs: server: 192.168.72.6 path: "/home/suzhan/nfs/data/zhou/log" persistentVolumeReclaimPolicy: Delete log-pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: log-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi selector: matchLabels: type: log registry-pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: registry-pv labels: type: registry spec: capacity: storage: 5Gi accessModes: - ReadWriteMany nfs: server: 192.168.72.6 path: "/home/suzhan/nfs/data/zhou/reg" persistentVolumeReclaimPolicy: Delete registry-pvc.yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: registry-pvc spec: accessModes: - ReadWriteMany resources: requests: storage: 5Gi selector: matchLabels: type: registry 说明: nfs服务搭建在72.6服务器上面,在72.6服务器上创建两个目录,一个存放日志信息,一个存放镜像信息 /home/suzhan/nfs/data/zhou/log /home/suzhan/nfs/data/zhou/reg
创建pv和pvc,如下:
# kubectl create -f log.pv.yaml persistentvolume "log-pv" created # kubectl create -f log.pvc.yaml persistentvolumeclaim "log-pvc" created # kubectl create -f registry.pv.yaml persistentvolume "registry-pv" created # kubectl create -f registry.pvc.yaml persistentvolumeclaim "registry-pvc" created # kubectl get pvc NAME STATUS VOLUME CAPACITY ACCESSMODES STORAGECLASS AGE log-pvc Bound log-pv 1Gi RWX 31s registry-pvc Bound registry-pv 5Gi RWX 2s # kubectl get pv NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM STORAGECLASS REASON AGE log-pv 1Gi RWX Retain Bound default/log-pvc 36s registry-pv 5Gi RWX Retain Bound default/registry-pvc 6s
4.创建harbor数据库
由于目前Harbor的kubernetes版本还不支持自动init数据库,因此我们需要为新建的registry_k8s数据库做初始化,目前使用的方法就是使用docker-compose工具在本地启动一个单机版harbor,然后获取harbor-db容器的id,进入容器之中将数据库dump出来,因为单机班的harbor会通过docker-compose.yaml创建数据库,然后需要将其中的数据信息导出到本地,建一个kubernetes中harbor能访问的数据库,具体操作如下:
注意:如何安装单机版harbor请看上文 找到harbor_db的container id: 77fde71390e7,进入容器,并将数据库registry dump出来: # docker exec -i -t 77fde71390e7 bash # mysqldump -u root -p --databases registry > registry.dump # 将数据库新建表和信息文件导出到容器的tmp/下面 离开容器,将容器内导出的registry.dump copy到本地home下面: # docker cp 77fde71390e7:/tmp/registry.dump ./ 然后自己启动一个数据库服务将信息导入到数据库中,但是要确保kubernetes中pod能访问,建议使用kubectl创建一个mysql容器。
这里有许多大坑需要注意:
这里需要注意的是:要确保单机版的harbor和集群中harbor中ui服务的版本必须一致,比如ui服务1.2.2,那么db服务的也是1.2.2,因为不同版本的harbor(目前v1.6.0)对db数据库的要求不一样,表结构和表都不一样。那修改各个镜像的版本只需要在/harbor/make/kubernetes/中各个服务的deployment文件中修改镜像版本。
版本问题
在github上下载的harbor各服务的镜像版本信息如下:
adminserver服务:vmware/harbor-adminserver:v1.2.0
jobserver服务:vmware/harbor-jobservice:v1.2.0
registry服务:vmware/registry:2.6.2-photon
ui服务:vmware/harbor-ui:v1.2.0
但是事实上github上给出的镜像版本(除了registry)和各个服务的镜像搭配是有问题的,启动时肯定会有问题的,从docker hub上查询发现除了registry镜像的版本是最新的,其他的版本都不是最新的,最新的目前已经更新到了v1.5.2,而且v1.5.2的版本相对1.2.0的区别非常大,具体的区别如下(只是根据我部署踩过的坑,不代表权威):
第一,如果使用registry:2.6.2版本搭配其他的1.2.0的镜像启动,regitstry会启动失败,需要将registry版本进行降级,使用2.6.1版本即可运行;
第二,ui:v1.5.2版本连接adminserver服务的时候默认是使用8080端口,但是1.2.0默认使用的80端口;
第三,ui:v1.2.0与ui:v1.5.2镜像,需要的数据库表数量和表结构不一样,例如:v1.5.2的ui需要的数据库一共有20张表,但是v1.2.0中只需要表15张表,而且表的结构也有差异,v1.5.2库中project表只要7个字段,而v1.2.0中project表有八个字段,多了一个public字段。
所以在复制数据库的时候必须保证各个服务的一致性。
数据库导入问题
第一:project project_metadata这两个表有外键,需要在最后导入。
第二:harbor_resource_label需要将resource_name字段长度改成200,不然会报错:specified key was too long
如果需要启动单机版的harbor麻烦,可以直接从这里下载:https://download.csdn.net/download/qq_24923725/10695167
可以下载两个版本的数据库脚本,但是个人建议全部使用v1.5.2的数据脚本,然后再project中插入public字段,字段类型TINYINT,长度:1,默认:0。
但是,当我们从github上下载了低版本的harbor,例如harbor1.5.2(看好是harbor的版本),所有的镜像都是最新的,这里就有点操蛋了。
5.配置harbor.cfg文件
harbor.cfg是整个配置生成的重要输入,我们在k8s-prepare执行之前,先要根据我们的需要和环境对harbor.cfg进行配置,配置如下:
// 进入harbor/make/harbor.cfg hostname = 192.168.72.2 //输入自己安装的宿主机ip地址 db_host = mysql //域名 db_password = password //数据库密码 db_port = 8000 //记住,这是pod对内暴露的端口,不是对外 db_user = root //数据库用户名
6.configmap配置模板
在/harbor/make/kubernetes/templates下面会有harbor所有需要启动的服务的configmap模板文件。
首先配置adminserver.cm.yaml文件,示例如下:
对于adminservier服务来说,具体还是配置数据库信息 MYSQL_HOST: 192.168.72.2 //数据库访问地址 MYSQL_USR: root //数据库用户 MYSQL_DATABASE: registry_k8s //数据库名称 RESET: "true"
其他的暂时可以无需调整。
7.执行k8s-prepare脚本
上面设置完毕之后需要执行脚本,进入/harbor/make/kubernetes目录,执行./k8s-prepare,就会根据tmplates中的模板生成各个服务的configmap脚本信息,具体操作:
./k8s-prepare 执行完毕,可以在各自的文件夹下面看到相应的configmap脚本信息,例如,在harbor/make/kubernetes/adminserver文件夹中可以看到adminserver.cm.yaml文件
8.启动Harbor组件
第一步启动configmap脚本,如下:
# kubectl apply -f jobservice/jobservice.cm.yaml configmap "harbor-jobservice-config" created # kubectl apply -f registry/registry.cm.yaml configmap "harbor-registry-config" created # kubectl apply -f ui/ui.cm.yaml configmap "harbor-ui-config" created # kubectl apply -f adminserver/adminserver.cm.yaml configmap "harbor-adminserver-config" created # kubectl get cm NAME DATA AGE harbor-adminserver-config 42 14s harbor-jobservice-config 8 16s harbor-registry-config 2 15s harbor-ui-config 9 15s
第二步启动server服务,如下:
# kubectl apply -f jobservice/jobservice.svc.yaml service "jobservice" created # kubectl apply -f registry/registry.svc.yaml service "registry" created # kubectl apply -f ui/ui.svc.yaml service "ui" created # kubectl apply -f adminserver/adminserver.svc.yaml service "adminserver" created # kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) adminserver 10.103.7.8 <none> 80/TCP jobservice 10.104.14.178 <none> 80/TCP registry 10.101.185.42 <none> 5000/TCP,5001/TCP ui 10.96.29.187 <none> 80/TCP
第三步启动deployment服务,如下:
# kubectl apply -f registry/registry.deploy.yaml replicationcontroller "registry-rc" created # kubectl apply -f jobservice/jobservice.deploy.yaml replicationcontroller "jobservice-deploy" created # kubectl apply -f ui/ui.deploy.yaml replicationcontroller "ui-deploy" created # kubectl apply -f adminserver/adminserver.deploy.yaml replicationcontroller "adminserver-deploy" created
启动后检查各个pod,可能会存在通过hostname连个某个服务无法连接的问题,比如说:ui服务连接adminserver:80连接不上,这个原因是因为dns问题,正常的kubernetes集群都安装了coredns或者flannel服务用于hostname映射,如果没有安装也可以解决这个问题,因为kubernetes1.7版本以后提供了hostnames这个属性。
k8s上不同服务之间可以通过service的域名来互相访问。域名的解析是一般是通过在集群中的kube-dns(主要是dnsmasq)或者coredns完成的。k8s的dns也可以向上级联dns服务器。有的时候,我们希望给运行在k8s上的Pod增加一些域名的解析(例如宿主机的主机名),但又不想对dns模块动太多,有没有什么比较方便的办法呢?容易想到的是,将域名记录到容器镜像的/etc/hosts
文件,这样容器运行时就可以正确解析了。然而这样是不行的。k8s会管理这个文件,打到镜像里的文件实际并不会起作用。可以通过yaml文件中hostnames进行设置,可用于deploy、sc、pod,示例如下:
apiVersion: v1 kind: Pod metadata: name: hostaliases-pod spec: restartPolicy: Never hostAliases: - ip: "127.0.0.1" hostnames: - "foo.local" - "bar.local" - ip: "10.1.2.3" hostnames: - "foo.remote" - "bar.remote" containers: - name: cat-hosts image: busybox command: - cat args: - "/etc/hosts" restartPolicy: Never hostAliases: - ip: "127.0.0.1" hostnames: - "foo.local" - "bar.local" - ip: "10.1.2.3" hostnames: - "foo.remote" - "bar.remote" 通过restartPolicy属性进行设置,这是在1.7版本开始,k8s支持了 HostAliases 特性: .spec.hostAliases。
启动后,如果需要访问harbor的图像界面,需要设置ui的server,对ui对外暴露一个接口,需要设置ui.svc.yaml,通过nodeport设置一个对外暴露的端口,就可以通过http://ip:端口镜像访问端口了。
还有一个端口需要设置,docker连接仓库服务也需要对registry服务设置一个对外暴露的端口,也是使用nodeport进行设置。
9.harbor校验
由于harbor默认使用了http访问,因此在docker login前先要将我们的仓库地址加到/etc/docker/daemon.json的insecure-registries中: 这里对registry服务设置对外暴露的端口是31277
///etc/docker/daemon.json { "insecure-registries": ["192.168.72.2:31277"] }
systemctl daemon-reload and restart后,我们就可以通过docker login登录新建的仓库了(初始密码:Harbor12345):
docker login hub.tonybai.com:31277 Username (admin): admin Password: Login Succeeded
在浏览器中打开http://192.168.72.2:31277,用admin/Harbor12345登录也可以访问ui界面。
更多推荐
所有评论(0)