Fork me on GitHub

版权声明 本站原创文章 由 萌叔 发表
转载请注明 萌叔 | https://vearne.cc

1. 前言

警告:本文仅用于萌叔自己总结之用,对其它人而言可能毫无营养,没有阅读价值。

在k8s中要使用自己搭建的私有仓库,还需要一些额外的配置。
萌叔使用的harbor搭建的私有仓库 goharbor/harbor, 假定地址为
https://docker-harbor.vearne.cc

2. 配置过程

要能够成功拉去image,需要解决2个问题

2.1 TLS证书验证

由于我们的私有仓库,使用的自签名的证书,所以需要能够通过TLS的握手阶段对证书。
如果证书认证失败,你可能收到如下错误

[root@xx ~]# docker pull docker-harbor.vearne.cc/ut/helloworld:0.2.6
Error response from daemon: Get https://docker-harbor.vearne.cc/ut/helloworld:0.2.6: x509: certificate signed by unknown authority

一种做法是直接添加CA根证书到操作系统获得信任。但是萌叔尝试后,发现无效。

这里验证有效的做法是,使用dockerd的--insecure-registry参数

2.1.1 修改dockerd的配置文件

默认路径为/etc/docker/daemon.json

{
  "insecure-registries" : ["docker-harbor.vearne.cc"]
}
2.1.2 重启dockerd
systemctl restart docker

2.2 登录

要想在宿主机拉取镜像,可以用

docker login docker-harbor.vearne.cc

在k8s集群中拉取镜像可以使用

kubectl create secret docker-registry regcred \
  --docker-server=<你的镜像仓库服务器> \
  --docker-username=<你的用户名> \
  --docker-password=<你的密码> \
  --docker-email=<你的邮箱地址>

创建一个密钥,用于拉取镜像

检查 Secret regcred
kubectl get secret regcred --output="jsonpath={.data.\.dockerconfigjson}" | base64 --decode

输出和下面类似:

{"auths":{"yourprivateregistry.com":{"username":"janedoe","password":"xxxxxxxxxxx","email":"jdoe@example.com","auth":"c3R...zE2"}}}

读者应该已经发现用户名和密码只是简单的用base64编码以后保存成字符串文本。实际上docker client和harbor的交互用的就是使用Basic Auth进行鉴权。

2.3 创建使用你的 Secret 的 Pod

注意: Secret regcred所在的Namespace要和Pod所在的Namespace保持一致

apiVersion: v1
kind: Pod
metadata:
  name: private-reg
spec:
  containers:
  - name: private-reg-container
    image: docker-harbor.vearne.cc/ut/helloworld:0.2.6
    imagePullPolicy: IfNotPresent
  imagePullSecrets:
  - name: regcred

3. 总结

经过上面的操作,就可以方便的从私有仓库拉取image了。

参考资料

  1. 从私有仓库拉取镜像
  2. insecure registry

请我喝瓶饮料

微信支付码

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据