美文网首页Docker容器
私有仓库之 用户认证

私有仓库之 用户认证

作者: 刘远鑫 | 来源:发表于2017-11-15 10:14 被阅读26次

通常在生产场景中,对私有仓库还需要进行访问代理和提供认证和用户管理。

配置Nginx 代理

使用Nginx 来代理registry 服务原理简单,我们让registry服务监听在 127.0.0.1:15000,这意味着只允许本机才能通过15000端口访问到,其他主机是无法访问到的。

为了让其他主机访问到,通过Nginx 监听在对外地址的5000端口,当外部访问请求到达5000端口时,内部再将请求转发到本地的15000端口。

首先,安装Nginx

# sudo apt-get-y install nginx

在/etc/nginx/sites-available/目录下,创建新的站配置文件/etc/nginx/sites-available/docker-registry.conf,代理本地的5000端口转发到15000端口。

配置文件内容为:

# 本地的registry 服务监听在15000 端口
upstream docker-registry{
server localhost:15000;
}
# 代理服务器监听在5000端口
server{
listen 5000;
server_name private-registry-server.com;
# ssl on;
# ssl_certificate /etc/ssl/certs/docker-registry;
proxy_set_header Host $http_host;# required for Docker client sake
proxy_set_header X_Real-IP $remote_addr; # pass on real client IP
client_max_body_size 0; disable any limits to avoid HTTP 413 for large image uploads
chunked_transfer_encoding on;
location/{
    # 配置转发对于/的访问请求到registry 服务
    proxy_pass http://docker-registry;
}
location/_ping{
    # 配置转发对于/ping 的访问请求到registry 服务
    auth_basic off;
    proxy_pass http://docker-registry;
}
location /v1/_ping{
    # 配置转发对于/v1/ping的访问请求到registry 服务
    auth_basic off;
    proxy_pass http://docker-registry;
}
}

建立配置文件软连接,放到/etc/nginx/sites-enabled/下面,让Nginx 启用它,最后重启Nginx 服务。

# sudo ln -s /etc/nginx/sites-available/docker-registry.conf /etc/nginx/sites-enable/docker-registry.conf
# server nginx restart

之后,通过上传镜像来测试服务器是否正常。测 试上传本地的ubuntu:latest镜像

# sudo docker tag ububtu:14.04 127.0.0.1:5000/ubuntu:latest
# sudo docker push 127.0.0.1:5000/ubuntu:latest

添加用户认证

公共仓库DockerHub 是通过注册索引(index)服务来实现的。由于index 服务并没有完善的开源实现,在这里介绍基于Nginx 代理的用户访问管理方案。
Nginx 支持基于用户名和密码的访问管理。
首先,在配置文件的location/字段中添加两行。

location/{
    #let Nginx know about our auth file
    auth_basic"Please Input username/password";
    auth_basic_user_file docker-registry-htpasswd;
    proxy_pass http://docker-registry;
}

auth_basic "Please Input username/password"; 行说明启用认证服务,不通过的请求无法转发。
auth_basic_user_file docker-registry-htpasswd; 指定了验证的用户名密码存储文件为本地(/etc/nginx/下)的docker-registry-htpasswd文件。
docker-registry-htpasswd 文件存储用户名密码的格式为每行放一个用户名、密码对:

user1:password1
user2:password2

需要注意的是,密码字段存储的并不是明文,使用crypt 函数加密过的字符串。
要生成加密后的字符串,可以通过htpasswd 工具,首先安装apache2-utils:

# sudo aptitude install apache2-utils -y

创建用户user1,并添加密码。
例如,如下的操作创建/etc/nginx/docker-registry-htp asswd 文件来保存用户名和对应密码。

# sudo htpasswd -c /etc/nginx/docker-registry-htpasswd user1
# New password:
# Re-type new password:
# Adding password for user user1

添加更多用户,可以重复上面的命令(密码文件存在后,不需要使用-c选项重新创建)。
最后,重新启动Nginx 服务。

# sudo service nginx restart

此时,通过访问本地的服务 http://127.0.0.1:5000/v1/search,会弹出对话框,提示需要输入用户名和密码。
通过命令行访问,需要在地址前面带上用户名和密码才能正常返回:

# curl USERNAEM:PASSWORD@localhost:5000/v1/search

相关文章

网友评论

    本文标题:私有仓库之 用户认证

    本文链接:https://www.haomeiwen.com/subject/omccvxtx.html