之前在我的專欄《Harbor寶典》中分享了Harbor系列的文章,
本期將繼續分享在k8s的容器內如何拉取Harbor中的私有鏡像,
Harbor中公有的鏡像我們可以隨便拉取,但是一些私有的鏡像無法直接拉取到,我們可以使用Secret資源物件來拉取私有鏡像,以下為詳細的操作步驟,
Harbor地址: https://192.168.2.250:443
Harbor用戶:admin
Harbor密碼:Harbor12345
文末記錄遇到的問題及解決辦法!
1、登錄Harbor
登錄成功后會在~/.docker/config.json檔案中記錄登錄資訊,然后基于該資訊創建Secret,容器中通過imagePullSecret指定該Secret來實作認證,從而拉取私有鏡像,
如果登錄Harbor失敗,請查看專欄的問題解決,
# docker login -u admin -p Harbor12345 192.168.2.250:443
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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 //登錄成功
# cat ~/.docker/config.json
{
"auths": {
"192.168.2.250:443": {
"auth": "YWRtaW46SGFyYm9yMTIzNDU="
}
},
"HttpHeaders": {
"User-Agent": "Docker-Client/19.03.8 (linux)"
}
}
2、用BASH64編碼決議密鑰資料
一下創建Secret的時候需要用到決議出來的結果;-w 0 表示生成秘鑰不轉行,默認轉行不是正確的格式會出錯,
# cat ~/.docker/config.json | base64 -w 0
ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjIuMjUwOjQ0MyI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZTR0Z5WW05eU1USXpORFU9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOS4wMy44IChsaW51eCkiCgl9Cn0=

3、創建Secret鏡像拉取憑證
.dockerconfigjson的值就是第2步決議的結果(將結果復制到該處)
# vim harbor-image-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: harbor-pull
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: ewoJImF1dGhzIjogewoJCSIxOTIuMTY4LjIuMjUwOjQ0MyI6IHsKCQkJImF1dGgiOiAiWVdSdGFXNDZTR0Z5WW05eU1USXpORFU9IgoJCX0KCX0sCgkiSHR0cEhlYWRlcnMiOiB7CgkJIlVzZXItQWdlbnQiOiAiRG9ja2VyLUNsaWVudC8xOS4wMy44IChsaW51eCkiCgl9Cn0=
# kubectl apply -f harbor-image-secret.yaml
secret/harbor-pull created
# kubectl get secret
NAME TYPE DATA AGE
default-token-qqjxn kubernetes.io/service-account-token 3 13d
harbor-pull kubernetes.io/dockerconfigjson 1 52s

命令列創建secret的方法見kubectl create secret -h來創建,本處不詳細說明了,
4、容器中使用鏡像拉取憑證來拉取私有鏡像
本處以拉取私有鏡像192.168.2.250:443/muli/tomcat:8.5.34-jre8-alpine為例,
# cat tomcat-pod1.yaml
kind: Pod
apiVersion: v1
metadata:
name: tomcat-v2.3.1
namespace: test
spec:
imagePullSecrets:
- name: image-secret
containers:
- name: tomcat-po
image: 192.168.2.250:443/muli/tomcat:8.5.34-jre8-alpine
imagePullPolicy: IfNotPresent
# kubectl apply -f tomcat-pod1.yaml
pod/tomcat-v2.3.1 created
# kubectl get pods
NAME READY STATUS RESTARTS AGE
tomcat-v2.3.1 1/1 Running 0 20h

5、遇到的問題
創建Pod后,鏡像一直拉取失敗,
問題排查:
因為是在master上操作的,查看Pod是調度到node節點,但是node節點未登錄過Harbor,故node節點沒有~/.docker/config.json檔案,導致node節點拉取鏡像時無法獲取到登錄的資訊,
解決辦法:
到Pod調度到的節點執行:
# docker login -u admin -p Harbor12345 192.168.2.250:443
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
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 //登錄成功
則會自動生成~/.docker/config.json檔案,其內容與master生成的一致,
在生產環境中,事先不知道Pod會調度到哪個節點,可以在每個節點都執行登錄操作,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394619.html
標籤:其他
