构建Harbor私有镜像库

1.项目说明

1.1概述

搭建并配置一个Harbor私有镜像库,用于安全地存储和管理Docker镜像。

项目范围:

环境准备:配置Linux服务器,安装Docker和Docker Compose。

Harbor安装:下载并解压Harbor安装包,修改配置文件,执行安装脚本。

配置管理:设置主机名、端口、HTTPS等,确保Harbor正常运行。

用户权限:创建用户和项目,配置访问权限。

预期成果:成功部署Harbor私有镜像库,实现镜像的安全存储、推送和拉取。

1.2项目背景知识

1.2.1项目需求

由于国外由于docker镜像源无法使用,科学上网隐患太大,各大开源厂商的镜像加速器都又都进行收费使用,为了保证docker今后能顺利拉取镜像,k8s集群顺利拉取镜像,现企业内部需要部署一台私有的docker镜像源服务器

要求使用开源镜像库构建软件Harbor来搭建

通过Web页管理镜像库

1.2.2Harbor概述

  • 官网:https://goharbor.io/

  • Harbor是VMware公司开源的企业级DockerRegistry项目,其目标是帮助用户迅速搭建一个企业级的Docker registry服务。

  • 它以Docker公司开源的registry为基础,提供了管理UI,基于角色的访问控制(Role Based Access Control),AD/LDAP集成、以及审计日志(Auditlogging) 等企业用户需求的功能,同时还原生支持中文。

  • 简单说来,Harbor封装了Docker的registry v2,帮用户提供了许多便捷管理的特性,方便用户操作。

2.项目环境

2.1项目拓扑结构

使用单节点实现

2.2软硬件环境清单

主机名IP地址硬件软件
Harbor192.168.172.128

cpu:1颗2核

内 存:2GB

HDD:20GB

网 络:NAT

VmWare17.6

OpenEuler22.03 SP4

docker26.1.4 docker-compose1.22.0 Harbor2.12.2

3.任务清单

3.1项目环境搭建

安装OpenEuler22.03 SP4系统

按照规划,最小化部署系统,关闭防火墙,禁用SELinux,确保能够联网。

3.2Harbor服务器部署

3.2.1docker部署

设置docker源。

安装docker-ce

配置镜像加速器

安装docker-compose

3.2.2Harbor部署

下载Harbor2.12.2 latest版本软件

配置软件

安装软件

Web测试

3.2.3 k8s集群所需镜像上传

导入镜像

重新标记镜像

上传镜像

4.实施步骤

4.1环境搭建

部署Euler22.0系统,连接xshell,过程略

关闭防火墙和selinux,设置主机名

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# vi /etc/selinux/config
SELINUX=disabled
[root@localhost ~]# hostnamectl set-hostname harbor
[root@localhost ~]# reboot

设置静态ip

[root@web01 ~]#vi  /etc/sysconfig/network-scripts/ifcfg-ens32
# 以下为示例
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=ens32
UUID=0a99b9c8-e82d-4382-a948-c0bbdfa23ed4
DEVICE=ens32
ONBOOT=yes
IPADDR=192.168.88.100
PREFIX=24
GATEWAY=192.168.88.2
DNS1=223.5.5.5

[root@harbor ~]# systemctl restart network

# 安装并升级所需软件
[root@harbor ~]# yum install vim  tree tar net-tools -y
[root@harbor ~]# yum  update  -y

制作快照

4.2安装docker

添加docker-ce 源信息

[root@harbor ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

修改docker-ce源

[root@harbor ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo   # 更换为阿里开源镜像站源

[root@server ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/docker-ce.repo
# 由于阿里开源中没有Euler系统版本,但其源自Centos则改为7即可

更新源

[root@harbor ~]# yum  makecache

安装docker-ce:默认安装docker-ce是最新版本

[root@harbor ~]# yum install docker-ce  -y

启动服务,查看信息

[root@harbor ~]# systemctl enable --now docker   # 启动服务
[root@harbor ~]# systemctl status docker  # 查看状态
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)
     Active: active (running) since Fri 2023-12-22 19:49:16 CST; 4s ago
……  ……

[root@harbor ~]# docker  version         # 查看版本详细信息
Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.45
 Go version:        go1.21.11
 Git commit:        5650f9b
 Built:             Wed Jun  5 11:32:04 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          26.1.4
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.11
  Git commit:       de5c9cf
  Built:            Wed Jun  5 11:31:02 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.33
  GitCommit:        d2d58213f83a351ca8f528a95fbd145f5654e957
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

配置镜像加速器

[root@harbor ~]# vim /etc/docker/daemon.json
{
  "registry-mirrors":     [ 
            "https://448070254bba46a593086759c591a95f.mirror.swr.myhuaweicloud.com",  
            "https://docker.1ms.run",
            "https://docker.1panel.live/"
            ]
}

[root@harbor ~]# systemctl daemon-reload
[root@harbor ~]# systemctl restart docker
[root@harbor ~]# docker info 

5.部署Harbor服务

5.1下载Harbor

[root@harbor ~]# cd  /
[root@harbor /]# wget  https://github.com/goharbor/harbor/releases/download/v2.12.2/harbor-offline-installer-v2.12.2.tgz

# 注意:若超时,可以将链接粘贴到迅雷中下载之后使用xftp上传到/目录

5.1.1安装编排工具

[root@harbor ~]# yum  install docker-compose  -y

5.1.2配置Harbor

[root@harbor /]# tar -xvf harbor-offline-installer-v2.12.0.tgz  # 解压缩

[root@harbor /]# cd harbor
[root@harbor harbor]# ls
common  common.sh  docker-compose.yml  harbor.v2.12.0.tar.gz  harbor.yml  install.sh  LICENSE  prepare

# 修改配置文件
[root@harbor harbor]# mv harbor.yml.tmpl harbor.yml
[root@harbor harbor]# vim 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: 192.168.88.100              # 修改本机IP,若改为主机名需要做映射

# http related config
http:
  # port for http, default is 80. If https enabled, this port will redirect to https port
  port: 1021                         # 改为其它端口,防止80端口占用

# https related config               # https的内容全部注释掉
# https:
  # https port for harbor, default is 443
  # port: 443
  # The path of cert and key files for nginx
  # certificate: /your/certificate/path
  # private_key: /your/private/key/path
  # enable strong ssl ciphers (default: false)
  # strong_ssl_ciphers: false
……………………………………
# 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: 123         # 登录密码
………………………………

5.1.3安装Harbor

# 安装并启动
[root@harbor harbor]# ./install.sh  

[root@harbor harbor]# docker ps          # 共9个容器 
CONTAINER ID   IMAGE                                 COMMAND                   CREATED          STATUS                    PORTS                                       NAMES
ac1a5c4b979f   goharbor/harbor-jobservice:v2.12.0    "/harbor/entrypoint.…"   10 minutes ago   Up 32 seconds (healthy)                                               harbor-jobservice
faa530cd75f5   goharbor/nginx-photon:v2.12.0         "nginx -g 'daemon of…"   10 minutes ago   Up 33 seconds (healthy)   0.0.0.0:1021->8080/tcp, :::1021->8080/tcp   nginx
6d2c4bac7b07   goharbor/harbor-core:v2.12.0          "/harbor/entrypoint.…"   10 minutes ago   Up 33 seconds (healthy)                                               harbor-core
854f42aac077   goharbor/redis-photon:v2.12.0         "redis-server /etc/r…"   10 minutes ago   Up 33 seconds (healthy)                                               redis
7b57185d5b3b   goharbor/registry-photon:v2.12.0      "/home/harbor/entryp…"   10 minutes ago   Up 33 seconds (healthy)                                               registry
a974bf73479f   goharbor/harbor-registryctl:v2.12.0   "/home/harbor/start.…"   10 minutes ago   Up 33 seconds (healthy)                                               registryctl
d227b76d163a   goharbor/harbor-db:v2.12.0            "/docker-entrypoint.…"   10 minutes ago   Up 33 seconds (healthy)                                               harbor-db
89433c9e5af3   goharbor/harbor-portal:v2.12.0        "nginx -g 'daemon of…"   10 minutes ago   Up 33 seconds (healthy)                                               harbor-portal
e726d19c7b75   goharbor/harbor-log:v2.12.0           "/bin/sh -c /usr/loc…"   10 minutes ago   Up 33 seconds (healthy)   127.0.0.1:1514->10514/tcp

6.测试

windows浏览器中输入服务端IP:192.168.172.128:1021 来访问管理页面

用户面:admin

密码:123

如下图:

6.1管理服务

 # 进入harbor目录
[root@harbor ~]# cd  /harbor
[root@harbor harbor]# 
[root@harbor harbor]# docker compose stop      # 停止服务
[root@harbor harbor]# docker compose start     # 启动服务
[root@harbor harbor]# docker compose restart   # 重启服务 

 6.2设置私有镜像库

Harbor服务器部署好之后,需要在客户端的仓库配置文件进行私有镜像库的配置

本机既是服务器也是客户端

[root@harbor ~]# vim  /etc/docker/daemon.json
{
  "registry-mirrors":   [ 
                        "https://448070254bba46a593086759c591a95f.mirror.swr.myhuaweicloud.com",  
                        "https://docker.1ms.run",
                        "https://docker.1panel.live/"
                        ],
  "insecure-registries": ["http://192.168.172.128:1021"]
}

[root@harbor ~]# systemctl restart docker

7.实战

7.1目的

将k8s集群所需的镜像上传到Harbor仓库中

学习docker镜像下拉、导出、上传镜像

为之后k8s集群项目打下基础

7.2Harbor新建项目

注意:上述项目名称重复,是本实现多次操作的结果,只是样例

7.2.1镜像处理

k8s集群默认下需要10个镜像另加可视化面板镜像共11个

常用语法

docker  save -o  新镜像文件名.tar   镜像名          # 导出
docker  load -i  镜像包名.tar                     # 导入
docker  tag      源镜像名:版本   新标签名:版本      # 镜像重新打标签

7.2.2将下载好的镜像导入

 

[root@harbor ~]# mkdir  /images
[root@harbor /]# cd  /images
[root@harbor images]# docker load -i cni.tar
[root@harbor images]# docker load -i coredns.tar
[root@harbor images]# docker load -i etcd.tar
[root@harbor images]# docker load -i kube-apiserver.tar
[root@harbor images]# docker load -i kube-controller-manager.tar
[root@harbor images]# docker load -i kube-controllers.tar
[root@harbor images]# docker load -i kube-proxy.tar
[root@harbor images]# docker load -i kube-scheduler.tar
[root@harbor images]# docker load -i kuboard.tar
[root@harbor images]# docker load -i node.tar
[root@harbor images]# docker load -i pause.tar

[root@harbor images]# docker images

7.2.3镜像重新打标签

[root@harbor images]# docker tag eipwork/kuboard:v3  192.168.172.128:1021/k8s/kuboard:v3
[root@harbor images]# docker tag registry.aliyuncs.com/google_containers/kube-apiserver:v1.28.0  192.168.172.128:1021/k8s/kube-apiserver:v1.28.0
[root@harbor images]#  docker tag registry.aliyuncs.com/google_containers/kube-controller-manager:v1.28.0  192.168.172.128:1021/k8s/kube-controller-manager:v1.28.0
[root@harbor images]# docker tag registry.aliyuncs.com/google_containers/kube-scheduler:v1.28.0  192.168.172.128:1021/k8s/kube-scheduler:v1.28.0
[root@harbor images]# docker tag registry.aliyuncs.com/google_containers/kube-proxy:v1.28.0  192.168.172.128:1021/k8s/kube-proxy:v1.28.0
[root@harbor images]# docker tag registry.aliyuncs.com/google_containers/etcd:3.5.9-0  192.168.172.128:1021/k8s/etcd:3.5.9-0
[root@harbor images]#  docker tag registry.aliyuncs.com/google_containers/coredns:v1.10.1  192.168.172.128:1021/k8s/coredns:v1.10.1
[root@harbor images]# docker tag calico/kube-controllers:v3.25.0  192.168.172.128:1021/k8s/calico/kube-controllers:v3.25.0
[root@harbor images]# docker tag calico/cni:v3.25.0  192.168.172.128:1021/k8s/calico/cni:v3.25.0
[root@harbor images]# docker tag calico/node:v3.25.0  192.168.172.128:1021/k8s/calico/node:v3.25.0
[root@harbor images]# docker tag registry.aliyuncs.com/google_containers/pause:3.9  192.168.172.128:1021/k8s/pause:3.9
 

7.2.4镜像推送到Harbor

查看配置是否支持私服

[root@harbor iamges]# cat  /etc/docker/daemon.json
{
  "registry-mirrors":     [ 
            "https://448070254bba46a593086759c591a95f.mirror.swr.myhuaweicloud.com",  
            "https://docker.1ms.run",
            "https://docker.1panel.live/"
            ],
  "insecure-registries": ["http://192.168.172.128:1021"]      # 这里
}

7.3登录Harbor

[root@harbor iamges]# docker login 192.168.172.128:1021
Username: admin
Password:                                   # 密码为123
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

7.4推送镜像到Harbor

[root@harbor iamges]# docker  push  192.168.172.128:1021/k8s/kuboard:v3

[root@harbor iamges]# docker  push  192.168.172.128:1021/k8s/kube-apiserver:v1.28.0

[root@harbor iamges]# docker  push  192.168.172.128:1021/k8s/kube-controller-manager:v1.28.0

[root@harbor iamges]# docker  push 192.168.172.128:1021/k8s/kube-scheduler:v1.28.0

[root@harbor iamges]# docker  push  192.168.172.128:1021/k8s/kube-proxy:v1.28.0

[root@harbor iamges]# docker  push 192.168.172.128:1021/k8s/etcd:3.5.9-0

[root@harbor iamges]# docker  push 192.168.172.128:1021/k8s/coredns:v1.10.1

[root@harbor iamges]# docker  push  192.168.172.128:1021/k8s/calico/kube-controllers:v3.25.0

[root@harbor iamges]# docker  push  192.168.172.128:1021/k8s/calico/cni:v3.25.0 

[root@harbor iamges]# docker  push 192.168.172.128:1021/k8s/calico/node:v3.25.0

[root@harbor iamges]# docker  push 192.168.172.128:1021/k8s/pause:3.9

7.5Web管理页面查看结果

注意:机器重启后需要执行下列操作,harbor才会就绪

[root@harbor ~]# cd  /harbor

[root@harbor harbor]#  systemc    restart docker

[root@harbor harbor]#  docker compose restart

8.总结

构建Harbor私有镜像库的过程包括几个关键步骤,以确保你能够成功搭建并使用Harbor来管理你的私有Docker镜像。以下是详细的总结:

1. 环境准备:
   确保有一台运行Linux操作系统的虚拟机,分配至少4GB内存和20GB磁盘空间,并选择NAT网络模式以确保虚拟机可以访问外部网络。
   安装Docker与Docker Compose工具,这两个工具是Harbor运行的基础。

2. 安装Harbor:
   下载Harbor安装包,通常为 tgz 格式。
   解压安装包并根据需要修改配置文件harbor.yml,例如设置主机名、端口、HTTPS配置等。
   使用`./install.sh`命令安装Harbor,并检查是否成功启动了所有相关容器。

3.配置Harbor:
   修改配置文件以适应你的网络环境和安全需求,例如,如果你的Harbor服务器仅支持HTTP,你可能需要在Docker的配置文件中添加该服务器地址到`insecure-registries`列表中。
  如果需要使用HTTPS,确保正确配置SSL证书和私钥,并在Harbor配置文件中指定这些文件的路径。

4. 创建和管理项目:
   在Harbor UI中创建项目,这些项目将用于组织你的镜像。
   可以设置项目为公开或私有,私有项目需要用户权限才能访问。

5. 镜像管理:
   将本地镜像标记为指向Harbor项目的格式:<harbor_hostname>/<project_name>/<image_name>:<tag>。
   登录Harbor后,你可以推送(Push)和拉取(Pull)镜像。

6. 用户权限管理:
   利用Harbor提供的基于角色的访问控制(Role Based Access Control),管理不同用户对镜像库的访问权限。

7. 镜像复制和日志审计:
   用Harbor的镜像复制功能在不同仓库之间复制镜像。
   利用日志审计功能记录所有操作日志,便于追踪与审计。

8. 注意事项:
   确保虚拟机的防火墙已关闭,避免影响Harbor的正常运行。
   在 /etc/hosts文件中配置主机名与IP地址的映射。
   为Harbor分配足够的磁盘空间,避免因空间不足导致镜像上传失败。

通过遵循上述步骤,可以成功构建并使用Harbor私有镜像库来管理你的Docker镜像,从而提高镜像管理的安全性和便利性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值