K8S集群范圍使用imagePullSecret示例詳解
imagePullSecrets 簡介
Kubernetes 在每個 Pod 或每個 Namespace 的基礎(chǔ)上使用 imagePullSecrets 對私有容器注冊表進(jìn)行身份驗證。要做到這一點,你需要創(chuàng)建一個秘密與憑據(jù):
{% note warning %} ?? 警告:
現(xiàn)在隨著公共鏡像倉庫(如:docker.io 等)開始對匿名用戶進(jìn)行限流,配置公共倉庫的身份認(rèn)證也變得有必要。 {% endnote %}
kubectl create secret docker-registry image-pull-secret \ -n <your-namespace> \ --docker-server=<your-registry-server> \ --docker-username=<your-name> \ --docker-password=<your-password> \ --docker-email=<your-email>
例如配置 docker.io 的 pull secret:
kubectl create secret docker-registry image-pull-secret-src \
-n imagepullsecret-patcher \
--docker-server=docker.io \
--docker-username=caseycui \
--docker-password=c874d654-xxxx-40c6-xxxx-xxxxxxxx89c2 \
--docker-email=cuikaidong@foxmail.com
{% note info %} ?? 信息:
如果 docker.io 啟用了「2 階段認(rèn)證」,可能需要創(chuàng)建 Access Token(對應(yīng)上面的 docker-password,創(chuàng)建鏈接在這里:賬號 -> 安全 {% endnote %}
現(xiàn)在我們可以在一個 pod 中使用這個 secret 來下載 docker 鏡像:
apiVersion: v1
kind: Pod
metadata:
name: busybox
namespace: private-registry-test
spec:
containers:
- name: my-app
image: my-private-registry.infra/busybox:v1
imagePullSecrets:
- name: image-pull-secret
另一種方法是將它添加到命名空間的默認(rèn) ServiceAccount 中:
kubectl patch serviceaccount default \
-p "{\"imagePullSecrets\": [{\"name\": \"image-pull-secret\"}]}" \
-n <your-namespace>
在 K8S 集群范圍使用 imagePullSecrets
我找到了一個叫做 imagepullsecret-patch 的工具,它可以在你所有的命名空間上做這個:
wget https://raw.githubusercontent.com/titansoft-pte-ltd/imagepullsecret-patcher/185aec934bd01fa9b6ade2c44624e5f2023e2784/deploy-example/kubernetes-manifest/1_rbac.yaml wget https://raw.githubusercontent.com/titansoft-pte-ltd/imagepullsecret-patcher/master/deploy-example/kubernetes-manifest/2_deployment.yaml kubectl create ns imagepullsecret-patcher
編輯下載的文件,一般需要修改image-pull-secret-src的內(nèi)容,這個 pull secret 就會應(yīng)用到 K8S 集群范圍。
nano 1_rbac.yaml nano 2_deployment.yaml kubectl apply -f 1_rbac.yaml kubectl apply -f 2_deployment.yaml
這里背后創(chuàng)建的資源有:
NameSpace
RBAC 權(quán)限相關(guān):
imagepullsecret-patcher ServiceAccount
imagepullsecret-patcher ClusterRole,具有對 service account 和 secret 的所有權(quán)限
imagepullsecret-patcher ClusterRoleBinding,為 imagepullsecret-patcher ServiceAccount 賦予 imagepullsecret-patcher ClusterRole 的權(quán)限。
- 全局 pull secret
image-pull-secret-src,里面是你的 K8S 全局包含的所有的鏡像庫地址和認(rèn)證信息。 - Deployment
imagepullsecret-patcher,指定 ServiceAccount 是imagepullsecret-patcher就有了操作 service account 和 secret 的所有權(quán)限,并將上面的 secret 掛載到 Deployment pod 內(nèi)。
可以包含多個鏡像庫地址和認(rèn)證信息,如:
{
"auths": {
"docker.io": {
"username": "caseycui",
"password": "c874xxxxxxxxxxxxxxxx1f89c2",
"email": "cuikaidong@foxmail.com",
"auth": "Y2FzxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxWMy"
},
"quay.io": {
"auth": "ZWFzdxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxlXWmpNPQ==",
"email": ""
}
}
}
base64 編碼后寫到 secret 的 .dockerconfigjson 字段即可:
apiVersion: v1
kind: Secret
metadata:
name: image-pull-secret-src
namespace: imagepullsecret-patcher
data:
.dockerconfigjson: >-
eyJhdXRocyI6eyJkb2NrZXIuaW8iOnsidXNlcm5hbWUiOiJjYXNleWN1aSIsInB.............................................IiwiZW1haWwiOiIifX19
type: kubernetes.io/dockerconfigjson
啟動后的 pod 會在所有 NameSpace 下創(chuàng)建 image-pull-secret secret(內(nèi)容來自于image-pull-secret-src) 并把它 patch 到 default service account 及該 K8S 集群的所有 ServiceAccount 里,日志如下:
time="2022-01-12T16:07:30Z" level=info msg="Application started" time="2022-01-12T16:07:30Z" level=info msg="[default] Created secret" time="2022-01-12T16:07:30Z" level=info msg="[default] Patched imagePullSecrets to service account [default]" time="2022-01-12T16:07:30Z" level=info msg="[kube-system] Created secret" time="2022-01-12T16:07:31Z" level=info msg="[kube-system] Patched imagePullSecrets to service account [node-controller]" ... time="2022-01-12T16:07:37Z" level=info msg="[kube-public] Created secret" time="2022-01-12T16:07:37Z" level=info msg="[kube-public] Patched imagePullSecrets to service account [default]" time="2022-01-12T16:07:38Z" level=info msg="[kube-node-lease] Created secret" time="2022-01-12T16:07:38Z" level=info msg="[kube-node-lease] Patched imagePullSecrets to service account [default]" time="2022-01-12T16:07:38Z" level=info msg="[prometheus] Created secret" time="2022-01-12T16:07:39Z" level=info msg="[prometheus] Patched imagePullSecrets to service account [default]" ... time="2022-01-12T16:07:41Z" level=info msg="[imagepullsecret-patcher] Created secret" time="2022-01-12T16:07:41Z" level=info msg="[imagepullsecret-patcher] Patched imagePullSecrets to service account [default]" time="2022-01-12T16:07:41Z" level=info msg="[imagepullsecret-patcher] Patched imagePullSecrets to service account [imagepullsecret-patcher]"
今后我們只需要更新 image-pull-secret-src 這一個即可了。?????????
Kyverno policy
Kyverno policy 可以實現(xiàn)同樣的效果:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: sync-secret
spec:
background: false
rules:
- name: sync-image-pull-secret
match:
resources:
kinds:
- Namespace
generate:
kind: Secret
name: image-pull-secret
namespace: "{{request.object.metadata.name}}"
synchronize: true
clone:
namespace: default
name: image-pull-secret
---
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: mutate-imagepullsecret
spec:
rules:
- name: mutate-imagepullsecret
match:
resources:
kinds:
- Pod
mutate:
patchStrategicMerge:
spec:
imagePullSecrets:
- name: image-pull-secret ## imagePullSecret that you created with docker hub pro account
(containers):
- (image): "*" ## match all container images以上就是K8S集群范圍使用 imagePullSecret示例詳解的詳細(xì)內(nèi)容,更多關(guān)于K8S使用 imagePullSecret的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Dashboard管理Kubernetes集群與API訪問配置
這篇文章介紹了Dashboard管理Kubernetes集群與API訪問配置的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
Kubernetes(K8S)入門基礎(chǔ)內(nèi)容介紹
這篇文章介紹了Kubernetes(K8S)的入門基礎(chǔ)內(nèi)容,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Kubernetes?controller?manager運行機制源碼解析
這篇文章主要為大家介紹了Kubernetes?controller?manager運行機制源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Kubernetes關(guān)鍵組件與結(jié)構(gòu)組成介紹
這篇文章介紹了Kubernetes的關(guān)鍵組件與結(jié)構(gòu)組成,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
kubernetes數(shù)據(jù)持久化PV?PVC深入分析詳解
這篇文章主要為大家介紹了kubernetes數(shù)據(jù)持久化PV?PVC分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
kubernetes數(shù)據(jù)持久化StorageClass動態(tài)供給實現(xiàn)詳解
這篇文章主要為大家介紹了kubernetes數(shù)據(jù)持久化StorageClass動態(tài)供給實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Kubernetes Informer數(shù)據(jù)存儲Index與Pod分配流程解析
這篇文章主要為大家介紹了Kubernetes Informer數(shù)據(jù)存儲Index與Pod分配流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

