天天看點

kubernetes 拉取私有鏡像 imagepullsecrets

1. kubernetes 拉取私有鏡像的測試

建立secret(建立方式有兩鐘,一種使用指令,第二種使用檔案)

下面我的私有倉庫如下:

  • reg.k8s.test.com
  • ureg.k8s.test.com

a. 修改

docker

/etc/docker/daemon.json

檔案

在所有的

node

節點中修改

docker

/etc/docker/daemon.json

檔案修改

insecure-registries

參數。必須包含上面上面私有倉庫的位址:

{
"registry-mirrors": [ "https://registry.docker-cn.com"],
"insecure-registries":["reg.k8s.test.com","ureg.k8s.test.com","uhub.service.ucloud.cn"]
}           

重新開機

docker

服務

systemctl restart docker

### 方法1. 使用檔案生成secret

生成

~/.docker/config.json

配置檔案

[root@ip-172-31-10-110 ~]# docker login reg.k8s.test.com
Username: lvnian
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@ip-172-31-10-110 ~]# 
[root@ip-172-31-10-110 ~]# docker login ureg.k8s.test.com
Username: lvnian
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@ip-172-31-10-110 ~]# ll ~/.docker/config.json 
-rw------- 1 root root 261 Nov  8 13:21 /root/.docker/config.json           

測試密碼是否成功,往私有倉庫

push images

[root@ip-172-31-10-110 ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
f17d81b4b692: Pull complete 
82dca86e04c3: Pull complete 
046ccb106982: Pull complete 
Digest: sha256:d59a1aa7866258751a261bae525a1842c7ff0662d4f34a355d5f36826abc0341
Status: Downloaded newer image for nginx:latest
[root@ip-172-31-10-110 ~]# docker tag nginx ureg.k8s.test.com/test/nginx
[root@ip-172-31-10-110 ~]# docker push ureg.k8s.test.com/test/nginx
The push refers to repository [ureg.k8s.test.com/test/nginx]
ad9ac0e6043b: Pushed 
6ccbee34dd10: Pushed 
237472299760: Pushed 
latest: digest: sha256:427498d66ad8a3437939bb7ef613fe76458b550f6c43b915d8d4471c7d34a544 size: 948
[root@ip-172-31-10-110 ~]# docker tag nginx reg.k8s.test.com/test/nginx
[root@ip-172-31-10-110 ~]# docker push reg.k8s.test.com/test/nginx
The push refers to repository [reg.k8s.test.com/test/nginx]
ad9ac0e6043b: Layer already exists 
6ccbee34dd10: Layer already exists 
237472299760: Layer already exists 
latest: digest: sha256:427498d66ad8a3437939bb7ef613fe76458b550f6c43b915d8d4471c7d34a544 size: 948           

密碼沒問題

擷取

base64 -w 0 ~/.docker/config.json

密文

[root@ip-172-31-10-110 ~]# base64 -w 0 ~/.docker/config.json
ewoJImF1dGhjNWdlpHVnVaenB5Wld4aFFFeFdUa2xCVGtBeU1ERTMiCgkJfSwKCQkidXJlZy5rOHMueXVud2VpLnJlbGEubWUiOiB7CgkJCSJhdXRoIjogIloyRnZaM1Z2WkdWdVp6cHlaV3hoUUV4V1RrbEJUa0F5TURFMyIKCQl9Cgl9LAoJIkh0dHBIZWFkZXJzIjogewoJCSJVc2VyLUFnZW50IjogIkRvY2tlci1DbGllbnQvMTguMDYuMS1jZSAobGludXgpIgoJfQp9[root@ip-172-31-10-110 ~]#            

建立Secret

### vim secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: regsecret
  namespace: default
data:
    .dockerconfigjson: ewoJImF1dGhjNWdlpHVnVaenB5Wld4aFFFeFdUa2xCVGtBeU1ERTMiCgkJfSwKCQkidXJlZy5rOHMueXVud2VpLnJlbGEubWUiOiB7CgkJCSJhdXRoIjogIloyRnZaM1Z2WkdWdVp6cHlaV3hoUUV4V1RrbEJUa0F5TURFMyIKCQl9Cgl9LAoJIkh0dHBIZWFkZXJzIjogewoJCSJVc2VyLUFnZW50IjogIkRvY2tlci1DbGllbnQvMTguMDYuMS1jZSAobGludXgpIgoJfQp9
type: kubernetes.io/dockerconfigjson           

kubectl create -f secret.yaml \

kubectl describe Secret regsecret

建立deployment測試是否可以拉私有倉庫的鏡像

[root@ip-172-31-10-110 ~]#  vim test.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: dentestreplce
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: dentestreplace
    spec:
      containers:
      - name: dentestreplace 
        imagePullPolicy: Always
        image: ureg.k8s.test.com/rela_dev/logreport:latest
      imagePullSecrets:
      - name: regsecret           
[root@ip-172-31-10-110 ~]# kubectl create -f test.yaml
[root@ip-172-31-10-110 ~]# kubectl describe po/dentestreplce-6f788968fb-dr768 
...
Volumes:
  default-token-tfmc8:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-tfmc8
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     <none>
Events:
  Type    Reason                 Age   From                    Message
  ----    ------                 ----  ----                    -------
  Normal  Scheduled              57s   default-scheduler       Successfully assigned dentestreplce-6f788968fb-dr768 to 172.31.40.120
  Normal  SuccessfulMountVolume  57s   kubelet, 172.31.40.120  MountVolume.SetUp succeeded for volume "default-token-tfmc8"
  Normal  Pulling                57s   kubelet, 172.31.40.120  pulling image "ureg.k8s.test.com/rela_dev/logreport:latest"
  Normal  Pulled                 15s   kubelet, 172.31.40.120  Successfully pulled image "ureg.k8s.test.com/rela_dev/logreport:latest"
  Normal  Created                15s   kubelet, 172.31.40.120  Created container
  Normal  Started                15s   kubelet, 172.31.40.120  Started container
[root@ip-172-31-10-110 ~]# 
           

檢視結果,成功。上面是使用第一個私有倉庫,第二個的測試也是一樣。

注意,必須要確定私有倉庫中本來就有

ureg.k8s.test.com/rela_dev/logreport:latest

這個image哦

另外一個私有參考也是一樣這樣測試即可。

方法2:

kubectl create secret docker-registry regsecret --docker-server=ureg.k8s.test.com --docker-username=lvnian --docker-password=LVNIAN@2017 [email protected] 
           
regsecret: 指定密鑰的鍵名稱, 可自行定義
--docker-server: 指定docker倉庫位址
--docker-username: 指定docker倉庫賬号
--docker-password: 指定docker倉庫密碼
--docker-email: 指定郵件位址
-n : 命名空間,在那個命名空間建立,就隻能在那個命名空間使用這個secret           

繼續閱讀