k8sharbor——企业级容器镜像仓库

发布时间:2023-05-16

k8sharbor 使用指南

一、概述

k8sharbor 是一个企业级的容器镜像仓库,是 CNCF(Cloud Native Computing Foundation)的孵化项目。它支持存储、签名和扫描镜像。Harbor 提供了 CLI 和 API 来管理镜像仓库,同时也可以与 pre-commit、RBAC 等集成。 Harbor 是一个开源项目,旨在解决企业在使用容器时可能面临的安全和管理问题,提供了多租户、LDAP/AD、API、可扩展性等功能。它支持 Docker 和 Kubernetes,并允许管理员设置存储、调度和安全策略等等。

二、安装与配置

  1. 安装前的准备工作:
# 新建 harbor 用户和用户组
sudo useradd -r -s /bin/false harbor
# 新建配置文件目录
sudo mkdir -p /etc/harbor/
# 新建数据存储目录
sudo mkdir -p /data/harbor
# 将 harbor 用户添加到 docker 组中
sudo usermod -aG docker harbor
  1. 下载并解压 Harbor 离线安装包:
wget https://storage.googleapis.com/harbor-releases/release-2.1.2/harbor-offline-installer-v2.1.2.tgz
tar -zxvf harbor-offline-installer-v2.1.2.tgz
cd harbor
  1. 修改 harbor.cfg:
# 设置 Harbor 的 URI
hostname = hub.example.com
# 管理员密码
harbor_admin_password = Harbor12345
# 数据库密码
database_password = root123
# 海外用户访问速度可选配置
https:
  port: 443
  certificate: /your/certificate/path
  private_key: /your/private/key/path
# 使用 Docker Compose 进行安装
docker-compose up -d

三、镜像管理

  1. 镜像库的管理:
# 新增项目
curl -X POST -H "Content-Type:application/json" -H "Authorization: Basic YWRtaW46SGFyYm9yMTIzNDU=" http://hub.example.com/api/v2.0/projects -d '{"name": "test"}'
# 删除项目
curl -X DELETE -H "Authorization: Basic YWRtaW46SGFyYm9yMTIzNDU=" http://hub.example.com/api/v2.0/projects/1
  1. 镜像的上传和下载:
# 登录
docker login -u admin -p Harbor12345 hub.example.com
# 推送
docker tag nginx:latest hub.example.com/test/nginx:latest
docker push hub.example.com/test/nginx:latest
# 拉取
docker pull hub.example.com/test/nginx:latest
  1. 镜像扫描与漏洞修复:
# 安装和配置 Trivy 扫描器
wget https://github.com/aquasecurity/trivy/releases/download/v0.15.0/trivy_0.15.0_Linux-64bit.tar.gz
tar zxvf trivy_0.15.0_Linux-64bit.tar.gz
sudo mv trivy /usr/local/bin
# 扫描并列出漏洞
trivy -no-progress alpine:3.10
# 使用 Harbor 来扫描你的 Docker 镜像
curl -X POST http://hub.example.com/api/security/scan -H 'Content-Type: application/json' -H 'Authorization: Basic YWRtaW46SGFyYm9yMTIzNDU=' -d '{"registry":"https://index.docker.io","repo":"library/python","tag":"2.7"}'

四、RBAC权限管理

  1. 基本角色:
# 只读用户
docker login -u read-only-user -p password hub.example.com
# 项目管理员
docker login -u project-admin -p password hub.example.com
# 系统管理员
docker login -u admin -p Harbor12345 hub.example.com
  1. 镜像仓库的权限管理:
# 新增用户
curl -u admin:Harbor12345 -H "Content-Type: application/json" -X POST "http://hub.example.com/api/v2.0/users" -d '{"username": "testuser","password":"Testpassword1%","email":"test@example.com","realname":"testuser","comment":""}'
# 修改用户密码
curl -u admin:Harbor12345 -H "Content-Type: application/json" -X PUT "http://hub.example.com/api/users/password?username=testuser" -d '{"new_password": "Testpassword2%"}'
# 新增角色
curl -u admin:Harbor12345 -H "Content-Type: application/json" -X POST "http://hub.example.com/api/v2.0/projects/1/members" -d '{"member_user":{"username":"testuser"},"role_id":2}'

五、自动赋予权限的 pre-commit Hook

  1. 自动构建:
# 以 Nginx 为例,新建一个 Dockerfile
FROM nginx:latest
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 创建 pre-commit hook
touch .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
# 写入 shell 脚本
#!/bin/bash
if docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/work uumpa/harbor-precommit
then
    exit 0
else
    exit 1
fi
# 提交代码
git add .
git commit -m "feat: add Nginx server"
  1. 自动赋予项目管理员权限:
# 新建 push 事件的一个处理页面(push.php)
<?php
$payload = json_decode($_REQUEST['payload'], true);
$push_user = $payload['push_data']['pusher'];
$tag = $payload['push_data']['tag'];
$repo = $payload['repository']['name'];
$repo_owner = $payload['repository']['namespace'];
// Get the credentials for harbor admin user
$auth_token = base64_encode("admin:Harbor12345");
$url = 'http://hub.example.com/api/v2.0/users?q='.$push_user;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$auth_token));
$server_output = curl_exec($ch);
curl_close($ch);
// Check if the user exists
$json = json_decode($server_output, true);
if(count($json) == 0) {
    echo('The pushing user does not exist!');
    exit();
}
$user = $json[0]['username'];
$user_id = $json[0]['user_id'];
// Add the user to the project for push access
$project_id = $repo_owner.'/'.$repo;
$url = 'http://hub.example.com/api/v2.0/projects/'.$project_id.'/members';
$data = array(
    'member_user' => array(
        'user_id' => $user_id,
        'username' => $user
    ),
    'role_id' => 2
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic '.$auth_token));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$server_output = curl_exec($ch);
curl_close($ch);
?>

六、总结

本文介绍了 k8sharbor——一个企业级容器镜像仓库的安装与配置、镜像管理、RBAC权限管理和使用 pre-commit Hook 实现自动赋予权限等方面。它可以帮助企业在使用容器时提高安全性和管理效率,值得一试。