文章目录
前言
- 此次实操,最后会验证K8S集群是否可以访问下载harbor仓库的镜像,所以我们需要使用k8s集群,k8s集群搭建不在赘述,可查看之前的博客:https://blog.csdn.net/CN_TangZheng/article/details/104318529
一:harbor原理解析
1.1:harbor概述
- Harbor是 VMware公司开源的企业级 DockerRegistry项目,项目地址为http:/ github. com/vmware/harbor 。其目标是帮助用户迅速搭建一个企业级的 Dockerregistry服务。它以 Docker公司开源的 registry为基础,提供了管理UI,基于角色的访问控制 (Role Based Access Control), AD/LDAP集成、以及审计日志( Auditlogging)等企业用户需求的功能,同时还原生支持中文。 Harbor的每个组件都是以 Docker容器的形式构建的,使用 Docker Compose来对它进行部署。用于部署 Harbor的 Docker Compose模板位于 /Deployer/docker-compose yml,由5个容器组成,这几个容器通过Docker link的形式连接在一起,在容器之间通过容器名字互相访问。对终端用户而言,只需要暴露 proxy(即Nginx)的服务端口
- Poxy:由Nginx服务器构成的反向代理。
- Registry:由 Docker官方的开源 regis try镜像构成的容器实例。
- UI:即架构中的 core services,构成此容器的代码是 Harbor项目的主体
- MySQL:由官方 MySQL镜像构成的数据库容器
- Log:运行着 rsvs logd的容器,通过 log-driver的形式收集其他容器的日志
1.2:habor特点
- 基于角色控制:用户和仓库都是基于项目进行组织的,而用户基于项目可以拥有不同的权限
- 基于镜像的复制策略:镜像可以在多个 Harbor实例之间进行复制
- 支持LDAP:Harbor的用户授权可以使用己经存在LDA用户
- 镜像删除&垃圾回收:Image可以被删除并且回收 Image占用的空间,绝大部分的用户操作API,方便用户对系统进行扩展
- 用户UI:用户可以轻松的浏览、搜索镜像仓库以及对项目进行管理
- 轻松的部署功能:Harbor提供了 online、 offline安装,除此之外还提供了 virtualappliance安装
- Harbor和 docker registry关系:Harbor实质上是对 docker registry做了封装,扩展了自己的业务模块
1.3:harbor认证过程
- docker daemon从 docker registry拉取镜像。
- 如果 dockerregistry需要进行授权时, registry将会返回401 Unauthorized响应,同时在响应中包含了 docker client如何进行认证的信息。
- dockerclien根据 regis try返回的信息,向 auth server发送请求获取认证 token
- auth server则根据自己的业务实现去验证提交的用户信息是否存符合业务要求。
- 用户数据仓库返回用户的相关信息
- auth server将会根据查询的用户信息,生成 token令牌,以及当前用户所具有的相关权限信息。上述就是完整的授权过程当用户完成上述过程以后便可以执行相关的 pullus操作。认证信息会每次都带在请求头中
1.4:harbor认证流程
- 首先,请求被代理容器监听拦截,并跳转到指定的认证服务器。
- 如果认证服务器配置了权限认证,则会返回401通知 dockerclient在特定的请求中需要带上一个合法的token。而认证的逻辑地址则指向架构图中的 core services
- 当 docker client接受到错误 code, client就会发送认证请求(带有用户名和密码)到 coreservices进行 basic auth认证
- 当上一步的请求发送给nginx以后,ngnix会根据配置的认证地址将带有用户名和密码的请求发送到core service
- core services获取用户名和密码以后对用户信息进行认证(自己的激据库或者介入LDAP都可以)。成功以后,返回认证成功的信息
二:harbor私有仓库搭建实操
2.1:harbor开局设置
-
挂载,升级系统内核,并设置开启自启
[root@192 ~]# hostnamectl set-hostname harbor '//修改主机名' [root@192 ~]# su [root@harbor ~]# mount.cifs //192.168.11.1/ccc /mnt '//挂载宿主机' Password for root@//192.168.11.1/ccc: [root@harbor ~]# cd /mnt/kubernetes/ [root@harbor kubernetes]# ls elrepo-release-7.0-3.el7.elrepo.noarch.rpm kube-flannel.yml kubeadm-basic.images.tar.gz [root@harbor kubernetes]# rpm -ivh elrepo-release-7.0-3.el7.elrepo.noarch.rpm '//安装内核rpm包' [root@harbor kubernetes]# yum --enablerepo=elrepo-kernel install -y kernel-lt '//安装内核相关组件' [root@harbor kubernetes]# grub2-set-default "CentOS Linux (4.4.213-1.el7.elrepo.x86_64) 7 (Core)" '//设置4.4内核开机自启' [root@harbor kubernetes]# init 6
-
安装docker并设置开启自启
[root@harbor ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 '//安装docker依赖' [root@harbor ~]# yum-config-manager \ > --add-repo \ > http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo '//加载阿里云docker镜像,yum仓库' [root@harbor ~]# yum update -y && yum install -y docker-ce '//更新并安装docker:时间较长' [root@harbor ~]# grub2-set-default "CentOS Linux (4.4.213-1.el7.elrepo.x86_64) 7 (Core)" [root@harbor ~]# init 6 [root@harbor ~]# systemctl start docker '//开启docker' [root@harbor ~]# cat > /etc/docker/daemon.json <<EOF > { > "exec-opts": ["native.cgroupdriver=systemd"], > "log-driver": "json-file", > "log-opts": { > "max-size": "100m" > } > } > EOF '//配置dockerdaemon' [root@harbor ~]# mkdir ‐p /etc/systemd/system/docker.service.d [root@harbor ~]# systemctl daemon-reload && systemctl restart docker && systemctl enable docker
2.2:配置局域网证书,让k8s集群能相互访问
-
证书
[root@harbor ~]# vim /etc/docker/daemon.json '//harbor和k8s主从节点都要做' { "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, '//注意逗号' "insecure-registries": ["https://hub.atguigu.com"] '//添加了局域网证书地址,镜像仓库地址' } [root@harbor ~]# systemctl restart docker '//主从节点重启docker服务'
-
配置docker-compose并解压harbor软件包
[root@harbor ~]# mount.cifs //192.168.11.1/ccc /mnt '//挂载宿主机' Password for root@//192.168.11.1/ccc: [root@harbor ~]# cd /mnt/kubernetes/ [root@harbor kubernetes]# mv docker-compose /usr/local/bin/ '//移动docker-compose' [root@harbor kubernetes]# chmod a+x /usr/local/bin/docker-compose '//给权限' [root@harbor kubernetes]# tar zxvf harbor-offline-installer-v1.2.0.tgz -C /usr/local '//解压harbor软件包' [root@harbor kubernetes]# cd /usr/local/harbor/ [root@harbor harbor]# vim harbor.cfg '//编辑配置文件' hostname = hub.atguigu.com '//修改主机名' ui_url_protocol = https '//协议修改为https'
-
创建https证书以及配置相关目录权限
[root@harbor harbor]# mkdir -p /data/cert '//创建存储https证书目录' [root@harbor harbor]# cd /data/cert/ [root@harbor cert]# openssl genrsa -des3 -out server.key 2048 '//创建服务器证书密钥文件 server.key' Generating RSA private key, 2048 bit long modulus ..............................+++ .................................................................+++ e is 65537 (0x10001) Enter pass phrase for server.key: '//输入私钥密码。如123123' Verifying - Enter pass phrase for server.key: '//重复输入私钥密码' [root@harbor cert]# openssl req -new -key server.key -out server.csr '//创建服务器证书的申请文件 server.csr' Enter pass phrase for server.key: '/输入前面创建的密码/' 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 '/国家代号,中国输入CN/' State or Province Name (full name) []:JS '//省名称' Locality Name (eg, city) [Default City]:NJ '//市名称' Organization Name (eg, company) [Default Company Ltd]:atguigu '//公司英文名' Organizational Unit Name (eg, section) []:atguigu '//组织名称' Common Name (eg, your name or your server's hostname) []:hub.atguigu.com ''//常用名称/服务器主机名' Email Address []:[email protected] '//邮箱地址' Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: '//可不填' An optional company name []: '//可不填' [root@harbor cert]# cp server.key server.key.org '//备份一份服务器密钥文件' [root@harbor cert]# openssl rsa -in server.key.org -out server.key '//去除文件口令' Enter pass phrase for server.key.org: writing RSA key [root@harbor cert]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt '//生成证书文件server.crt' Signature ok subject=/C=CN/ST=NJ/L=NJ/O=atguigu/OU=atguigu/CN=hub.atguigu.com/[email protected] Getting Private key [root@harbor cert]# chmod a+x * '//给所有文件权限'
-
运行脚本安装
[root@harbor cert]# cd - /usr/local/harbor [root@harbor harbor]# ./install.sh
-
修改k8s集群节点,harbor和宿主机本地域名解析文件
[root@k8s-master ~]# echo "192.168.233.134 hub.atguigu.com" >> /etc/hosts '//三个节点添加本地解析' [root@k8s-master ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.233.128 k8s-master 192.168.233.129 k8s-node01 192.168.233.130 k8s-node02 192.168.233.134 hub.atguigu.com [root@harbor harbor]# vim /etc/hosts '//harbor节点也要添加' 添加 192.168.233.128 k8s-master 192.168.233.129 k8s-node01 192.168.233.130 k8s-node02 192.168.233.134 hub.atguigu.com [root@harbor harbor]# docker ps -a '//查看容器状态是否都是up'
-
修改Windows本地域名解析文件,使用管理员身份登陆系统打开并保存
扫描二维码关注公众号,回复: 9284580 查看本文章
2.3:harbor访问测试
-
宿主机访问harbor网页
-
尝试使用docker登陆harbor
[root@k8s-node01 ~]# docker login https://hub.atguigu.com Username: admin 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 Login Succeeded
2.4:harbor上传镜像并测试
-
上传镜像
[root@harbor harbor]# docker images '//查看docker镜像' [root@harbor harbor]# docker pull photon '//拉取镜像' [root@harbor harbor]# docker tag photon:latest hub.atguigu.com/library/photon:latest '//更改镜像名称' [root@harbor harbor]# docker push hub.atguigu.com/library/photon:latest '//上传镜像到harbor仓库'
-
刷新harbor仓库,发现上传成功
-
在k8s集群中创建pod,查看镜像下载情况
[root@k8s-master ~]# kubectl run nginx-deployment --image=hub.atguigu.com/library/photon:latest --port=80 --replicas=1
-
测试完成,harbor私有仓库搭建成功