K8S集群配置使用私有镜像仓库Harbor

当我们需要搭建私有化集群时,由于业务需要可能需要搭建私有镜像仓库,以下是k8s部署pod时使用私有harbor的配置方法,亲测有效!

harbor参考 https://blog.csdn.net/weixin_43909790/article/details/128671695

1、在每台服务器上配置harbor的地址,修改或创建**/etc/docker/daemon.json**文件,加入以下配置

[root@k8s-master-01 ~]# cat /etc/docker/daemon.json
{
 "insecure-registries":["192.168.10.206"]  #192.168.10.206是harbor地址

}
[root@k8s-master-01 ~]#

#配置完成后重启docker服务
[root@k8s-master-01 ~]# systemctl restart docker

#为了不影响其他容器正常运行,可以使用热启动
[root@k8s-master-01 ~]# systemctl daemon-reload
[root@k8s-master-01 ~]# sudo kill -SIGHUP $(pidof dockerd) #热启动docker

2、登录harbor

harbor开启https后,即使通过http地址访问,它也会重定向到https的地址,所以这里直接用https地址登录,步骤1中的地址也配置成https的地址 。登录时根据提示输入harbor的用户名和密码,由于我之前登录过,再登录就不用输入了。

[root@k8s-master-01 ~]# docker login 192.168.10.206
Authenticating with existing credentials...
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@k8s-master-01 ~]#

3、查看秘钥

[root@k8s-master-01 ~]# cat /root/.docker/config.json
{
        "auths": {
                "192.168.10.206": {
                        "auth": "YWRtaW46YWRtaW4="
                }
        }
}
[root@k8s-master-01 ~]#

4、将密钥进行base64加密

[root@k8s-master-01 ~]# cat /root/.docker/config.json | base64 -w 0
ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEwLjIwNiI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZZV1J0YVc0PSIKCQl9Cgl9Cn0=[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]#

5、创建 harbor-image-secret.yaml文件

# vim  harbor-image-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: harbor-pull
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEwLjIwNiI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZZV1J0YVc0PSIKCQl9Cgl9Cn0=

6、创建secret

[root@k8s-master-01 ~]# kubectl apply  -f harbor-image-secret.yaml
secret/harbor-pull created
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]#
[root@k8s-master-01 ~]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-n6d56   kubernetes.io/service-account-token   3      43h
harbor-pull           kubernetes.io/dockerconfigjson        1      122m
[root@k8s-master-01 ~]#

7、容器中使用镜像拉取凭证来拉取私有镜像

本处以拉取私有镜像192.168.10.206/nginx/nginx:latest为例。

cat >  nginx-service.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      imagePullSecrets:    #  如过使用harbor仓库得镜像,需要设置从secret处拉取镜像  查看 kubectl get secret
      - name: harbor-pull  #  kubectl get secret  名称NAME
      containers:
      - name: nginx
        image: 192.168.10.206/nginx/nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80

---
kind: Service
apiVersion: v1
metadata:
  name: my-service1
spec:
  type: NodePort #此处有变化
  selector:
    app: nginx
  ports:
  - protocol: TCP
    nodePort: 32222 #此处为新添加
    port: 80
    targetPort: 80
EOF
# kubectl apply -f nginx-service.yaml
# kubectl get pod -o wide
# kubectl get svc
# kubectl get endpoints

curl 192.168.10.200:32222

游览器访问URL: http://192.168.10.200:32222

kubectl delete -f nginx-service.yaml

Secret详解:

secret用来保存小片敏感数据的k8s资源,例如密码,token,或者秘钥。
这类数据当然也可以存放在Pod或者镜像中,但是放在Secret中是为了更方便的控制如何使用数据,并减少暴露的风险。

用户可以创建自己的secret,系统也会有自己的secret。
Pod需要先引用才能使用某个secret

如果deployment部署在namespace中,则需要在namespace中创建secret

 创建namespace: harbor-image-secret.yaml文件


# vim  harbor-image-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: harbor-pull
  namespace: dev
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjEwLjIwNiI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZZV1J0YVc0PSIKCQl9Cgl9Cn0=

容器中使用镜像拉取凭证来拉取私有镜像

本处以拉取私有镜像192.168.10.206/nginx/nginx:latest为例。

cat >  nginx-service.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: dev
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      imagePullSecrets:    #  如过使用harbor仓库得镜像,需要设置从secret处拉取镜像  查看 kubectl get secret
      - name: harbor-pull  #  kubectl get secret  名称NAME
      containers:
      - name: nginx
        image: 192.168.10.206/nginx/nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80

---
kind: Service
apiVersion: v1
metadata:
  name: my-service1
spec:
  type: NodePort #此处有变化
  selector:
    app: nginx
  ports:
  - protocol: TCP
    nodePort: 32222 #此处为新添加
    port: 80
    targetPort: 80
EOF
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐