在 CentOS 上部署的 Kubernetes(k8s)对接镜像仓库,常见场景有 Docker Hub、Harbor、私有 Registry(HTTP/HTTPS)。下面按最常用方式给你一个完整说明。
| 仓库类型 | 示例 |
|---|---|
| Docker Hub | docker.io/library/nginx |
| Harbor | harbor.example.com/project/image |
| 私有 Registry | registry.example.com/myapp |
| HTTP 仓库 | http://192.168.1.10:5000 |
✅ 最推荐:使用 imagePullSecrets
✅ 不推荐:改 container runtime(已逐步淘汰)
kubectl create secret docker-registry myreg-secret \
--docker-server=harbor.example.com \
--docker-username=admin \
--docker-password=Harbor12345 \
--docker-email=admin@example.com
如果是 Docker Hub:
--docker-server=https://index.docker.io/v1/
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
imagePullSecrets:
- name: myreg-secret
containers:
- name: nginx
image: harbor.example.com/library/nginx:1.25
✅ 这是生产环境标准做法
⚠️ Kubernetes 默认禁止 HTTP 仓库
编辑:
vim /etc/containerd/config.toml
找到或添加:
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."192.168.1.10:5000"]
endpoint = ["http://192.168.1.10:5000"]
重启:
systemctl restart containerd
vim /etc/docker/daemon.json
{
"insecure-registries": ["192.168.1.10:5000"]
}
systemctl restart docker
image: harbor.example.com/project/app:v1
拉取 / 推送让 所有 Pod 自动使用仓库
kubectl patch serviceaccount default \
-p '{"imagePullSecrets": [{"name": "myreg-secret"}]}'
✅ 以后 Pod 不用再写 imagePullSecrets
ErrImagePull / ImagePullBackOffkubectl describe pod xxx
常见原因:
cp harbor-ca.crt /etc/pki/ca-trust/source/anchors/
update-ca-trust
systemctl restart containerd
| 场景 | 做法 |
|---|---|
| Docker Hub | imagePullSecrets |
| Harbor | imagePullSecrets + HTTPS |
| HTTP 仓库 | containerd / docker insecure |
| 所有 Pod 默认 | patch serviceaccount |
如果你愿意,可以告诉我:
我可以直接给你 一份可复制的配置文件。