软件包地址:https://github.com/goharbor/harbor/releases
一、harbor 仓库部署
先将包上传至服务器:
# 解压
[root@test ~]# tar -zxf harbor-offline-installer-v2.8.4.tgz
# 编辑配置
[root@test ~]# cd harbor/
[root@test harbor]# cp harbor.yml.tmpl harbor.yml
[root@test harbor]# vim harbor.yml
...
5 hostname: 192.168.121.111 # 填本机IP
...
12 # https related config # 若需要配置https,填写对应的证书存放路径,若不需要,则注释即可
13 #https:
14 # https port for harbor, default is 443
15 #port: 443
16 # The path of cert and key files for nginx
17 #certificate: /your/certificate/path
18 #private_key: /your/private/key/path
...
34 harbor_admin_password: Harbor12345 # 设置admin用户(默认用户)密码
# 保存退出编辑好的文件,执行安装脚本
[root@test harbor]# ./install.sh # 需要charts仓库可加参数:--with-chartmuseum
服务启动后,浏览器输入IP地址即可访问(账号默认为admin)
二、docker 服务配置
使用http访问的话,需要在docker里面配置一下策略:
# 编辑daemon.json
[root@test harbor]# vim /etc/docker/daemon.json
{
...
"insecure-registries": ["192.168.121.111"]
}
# 编辑好之后重载并重启docker
[root@test harbor]# systemctl daemon-reload
[root@test harbor]# systemctl restart docker.service
# 重新启动服务
[root@test harbor]# docker compose up -d
# 登录镜像仓库
[root@test harbor]# docker login 192.168.121.111
Username: admin
Password:
WARNING! Your credentials are stored unencrypted in '/root/.docker/config.json'.
Configure a credential helper to remove this warning. See
https://docs.docker.com/go/credential-store/
Login Succeeded
后续就直接可以上传、下载镜像了。
三、harbor 配置自启动
日常使用,在遇到机器重启或者docker重启的情况下,需要手动启动harbor服务,可以配置一个systemd服务:
# 参考以下内容
[root@harbor-test harbor]# vim /etc/systemd/system/harbor.service
[Unit]
Description=Harbor Docker Compose Service
Requires=docker.service
After=docker.service network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/docker compose -f /root/harbor/docker-compose.yml up -d
ExecStop=/usr/bin/docker compose -f /root/harbor/docker-compose.yml down
ExecReload=/usr/bin/docker compose -f /root/harbor/docker-compose.yml restart
RemainAfterExit=yes
WorkingDirectory=/root/harbor
[Install]
WantedBy=multi-user.target
# 设置开机自启
[root@harbor-test harbor]# systemctl enable --now harbor.service
[root@harbor-test harbor]# systemctl status harbor.service
四、k8s 使用示例
(1)创建镜像仓库、上传镜像
在搭建好harbor之后,在页面创建一个仓库,用于存放对应的镜像

然后将docker镜像打上标签,格式为:仓库地址/仓库名/镜像名:标签
[root@centos7-ii-64-basic-yfang-2066 harbor]# docker images | grep game
10.4.2.4/game/2048 latest 19299002fdbe 8 years ago 55.5MB
[root@centos7-ii-64-basic-yfang-2066 harbor]# docker push 10.4.2.4/game/2048
(2)配置secret
私有仓库需要配置这一项、而公有仓库可以直接拉取镜像。
[root@centos7-ii-64-basic-yfang-2066 k8s-test]# kubectl create secret docker-registry harbor-registry \
> --namespace=privatization \
> --docker-server=10.4.2.4 \
> --docker-username=admin \
> --docker-password=123
(3)创建Pod
[root@centos7-ii-64-basic-yfang-2066 k8s-test]# cat game2048.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: game2048
name: game2048
namespace: privatization # 指定命名空间为privatization,与Secret一致
spec:
replicas: 1
selector:
matchLabels:
app: game2048
template:
metadata:
labels:
app: game2048
spec:
imagePullSecrets: # 引用镜像拉取Secret
- name: harbor-registry
containers:
- image: 10.4.2.4/game/2048:latest
name: "2048"
---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: game2048
name: game2048-svc
namespace: privatization
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 9001
selector:
app: game2048
type: NodePort
# 启动服务
[root@centos7-ii-64-basic-yfang-2066 k8s-test]# kubectl apply -f game2048.yaml
(4)访问测试


136

被折叠的 条评论
为什么被折叠?



