k8s-Harbor 高可用部署-02-Harbor 高可用

一、设置Nginx反向代理

创建Nginx目录:

[root@hombd03 ~]# mkdir nginx
cd nginx

在master 节点下拉取Nginx镜像:

[root@hombd03 ~]# docker pull nginx:1.17.9

新建 nginx.conf 配置文件:

[root@hombd03 nginx]# cat nginx.conf
user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;

pid /var/run/nginx.pid;

events {
  worker_connections 1024;
}

stream {
  upstream hub {
     server 192.168.1.125:5000;
  }
  server {
    listen 5000;
    proxy_pass hub;
    proxy_timeout 300s;
    proxy_connect_timeout 5s;
    }
}

配置启动脚本:

vi restart.sh

[root@hombd03 nginx]# cat restart.sh
#!/bin/bash
docker stop harbornginx

docker rm harbornginx

# 启动nginx
docker run -idt --net=host --name harbornginx -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf nginx:1.17.9

启动脚本:

[root@homaybd03 nginx]# sh restart.sh 
Error response from daemon: No such container: harbornginx
Error: No such container: harbornginx
9e79565bdcac237d2bb3d601dd5c3dc013c5c8d57430a94ca75b4d9cea2f49d3
[root@homaybd03 nginx]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS     NAMES
9e79565bdcac   nginx:1.17.9   "nginx -g 'daemon of…"   51 seconds ago   Up 49 seconds             harbornginx
[root@homaybd03 nginx]# 

然后访问:
通过代理,访问master 192.168.1.123:5000 直接 proxy 代理到了worker节点的: 192.168.1.125:5000;
http://192.168.1.123:5000/harbor/sign-in?redirect_url=%2Fharbor%2Fprojects

至此反向代理配置成功!

登录:admin/Harbor12345

二、推送镜像到镜像仓库

先在harbor管理台创建一个项目,名字叫k8s
file

Harbor 服务绑定 host

[root@hombd03 ~]# vi /etc/hosts

# harbor
192.168.1.123 hub.mooc.com

查看镜像:

[root@homaybd03 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        1.17.9    5a8dfb2ca731   2 years ago   127MB

示例:

[root@localhost ~]# docker images
REPOSITORY                                  TAG                 IMAGE ID            CREATED             SIZE
mycentos                                    0.0.1               ce509ec398c1        2 days ago          231MB

标记镜像:
docker tag : 标记本地镜像,将其归入某一仓库。

格式:

docker tag 镜像名:版本 you-ip:端口/镜像名:版本

# 示例
docker tag mycentos:0.0.1 127.0.0.1:5000/mycentos:v1

镜像打标签:

docker tag nginx:1.17.9  hub.mooc.com:5000/k8s/nginx:1.17.9

查看本地镜像:

[root@hombd03 ~]# docker images
REPOSITORY                    TAG       IMAGE ID       CREATED       SIZE
nginx                         1.17.9    5a8dfb2ca731   2 years ago   127MB
hub.mooc.com:5000/k8s/nginx   1.17.9    5a8dfb2ca731   2 years ago   127MB

将镜像推送到镜像仓库:

[root@hombd03 ~]# docker push hub.mooc.com:5000/k8s/nginx:1.17.9
The push refers to repository [hub.mooc.com:5000/k8s/nginx]
Get "https://hub.mooc.com:5000/v2/": http: server gave HTTP response to HTTPS client
[root@hombd03 ~]# 

出现上述错误,配置仓库可直接通过http方式访问:

docker默认是传输方式使用https协议,我们手头上没有https证书,所以此处不配置https证书,直接设置可信源,使我们内网可以通过http方式访问。

1、修改 vim /etc/docker/daemon.json,添加以下内容
没有 daemon.json 文件的话,新建一个就好。

    "insecure-registries" :[ 
         "your-server-ip:5000"
    ]

因为我这是已经有了哪个文件,而且里面还有别的,所以,到时候一定要看好格式;

[root@hombd03 ~]# vi /etc/docker/daemon.json
{
  "registry-mirrors": ["https://22amdajy.mirror.aliyuncs.com"],
  "insecure-registries": [ 
         "hub.mooc.com:5000"
    ]
}

重新加载、重启docker、启动镜像

#重新加载
systemctl daemon-reload

#重启docker
systemctl restart docker

# 或者
# service docker restart

然后启动nginx服务:

[root@homaybd03 ~]# cd nginx
[root@homaybd03 nginx]# ls -l
total 8
-rw-r--r--. 1 root root 311 Jun 18 00:16 nginx.conf
-rw-r--r--. 1 root root 181 Jun 18 00:59 restart.sh
[root@homaybd03 nginx]# sh restart.sh
harbornginx
harbornginx
a4ac53c966bed9a37d5ac871dcf54a1f13ec2779137cc3142c919753af43c408

然后再尝试 push 一下:

[root@hombd03 nginx]#  docker push hub.mooc.com:5000/k8s/nginx:1.17.9
The push refers to repository [hub.mooc.com:5000/k8s/nginx]
351816b95c49: Preparing 
0e07021aa61a: Preparing 
b60e5c3bcef2: Preparing 
unauthorized: unauthorized to access repository: k8s/nginx, action: push: unauthorized to access repository: k8s/nginx, action: push
[root@hombd03 nginx]# 

报了未授权的错误,在管理后台新创建一个用户:
file

用户密码为:pusher/Pusher2022

然后再项目k8s成员中,加入刚刚创建的用户:
file

file

接下来使用新创建的用户,向这个项目中推送镜像了:

[root@hombd03 nginx]# docker login hub.mooc.com:5000
Username: pusher
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
[root@homaybd03 nginx]#  docker push hub.mooc.com:5000/k8s/nginx:1.17.9
The push refers to repository [hub.mooc.com:5000/k8s/nginx]
351816b95c49: Pushed 
0e07021aa61a: Pushed 
b60e5c3bcef2: Pushed 
1.17.9: digest: sha256:30d9dde0c4cb5ab4989a92bc2c235b995dfa88ff86c09232f309b6ad27f1c7cd size: 948
[root@hombd03 nginx]# 

可以看到已经推送成功了。

file

然后再到其他节点pull 一下,不过在拉取之前需要在该节点进行 hosts 和镜像配置文件修改,修改好之后,就可以pull一下刚才推送到 harbor 镜像仓库的镜像了:

在 worker(192.168.1.124)进行测试:

docker pull hub.mooc.com:5000/k8s/nginx:1.17.9

执行打印:

[root@hombd04 ~]# docker pull hub.mooc.com:5000/k8s/nginx:1.17.9
1.17.9: Pulling from k8s/nginx
123275d6e508: Pull complete 
9a5d769f04f8: Pull complete 
faad4f49180d: Pull complete 
Digest: sha256:30d9dde0c4cb5ab4989a92bc2c235b995dfa88ff86c09232f309b6ad27f1c7cd
Status: Downloaded newer image for hub.mooc.com:5000/k8s/nginx:1.17.9
hub.mooc.com:5000/k8s/nginx:1.17.9

可以看到,已经可以成功拉取下来了。

三、配置双主复制

双主复制需要分别访问两个地址:

http://192.168.1.124:5000/harbor/

# 改地址由192.168.1.123:5000 通过nginx代理到 192.168.1.125:5000
http://192.168.1.125:5000/harbor/

在 125 镜像管理后台创建仓库目标,用于复制到 124镜像仓库:
file

然后进入到项目 k8s 下边,写入复制:
file

同样的,也需要在 124 上添加复制规则,用于复制镜像到 125 镜像仓库,这里就不再赘述了。


相关文章:
Nginx 反向代理详解
Nginx配置-反向代理
Docker搭建harbor私有镜像仓库(命令行模式)
发布镜像到 Harbor 仓库

为者常成,行者常至