概述
本文介紹如何通過創(chuàng)建Secret來拉取私有鏡像倉庫的鏡像,從而完成Deployment的創(chuàng)建。
使用私有倉庫的問題
- 拉取鏡像時(shí)需要認(rèn)證
? 使用私有倉庫時(shí)需要通過用戶名和密碼進(jìn)行認(rèn)證。所以,若是直接配置鏡像倉庫的地址,無法拉取到鏡像文件,會報(bào)拉取鏡像的錯(cuò)誤。
- 可能由于網(wǎng)絡(luò)問題導(dǎo)致鏡像拉取失敗
? 若是通過公網(wǎng)來拉取鏡像,可能會由于網(wǎng)絡(luò)原因?qū)е麦@醒拉取失敗。
使用方式說明
在使用私有倉庫時(shí),需要進(jìn)行認(rèn)證??梢酝ㄟ^以下步驟來使用私有倉庫:
- 在對應(yīng)命名空間下創(chuàng)建一個(gè)Secret對象
創(chuàng)建secret的命令如下:
kubectl create secret docker-registry myregistrykey \
--docker-server=mycluster.icp:8500 \
--docker-username=<user_name> \
--docker-password=<user_password> \
--docker-email=<user_email> \
--namespace mynamespace
- 在創(chuàng)建pod或deployment的配置文件中引用該Secret對象
也就是在選項(xiàng)imagePullSecrets中指定myregistrykey。
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: nginx-demo
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
hostNetwork: false
containers:
- name: nginx
image: mycluster.icp:8500/developer/nginx
ports: []
resources:
limits: {}
imagePullSecrets:
- name: myregistrykey
這里一定要注意imagePullSecrets選項(xiàng)的位置,不要搞錯(cuò)了。其name,就是第1步我們創(chuàng)建的secret對象的名稱。文章來源:http://www.zghlxwxcb.cn/news/detail-581647.html
使用Secret的實(shí)戰(zhàn)步驟
創(chuàng)建secret
kubectl create secret docker-registry node-9005-pull-secret \
--docker-server=xxx-vpc.cn-beijing.cr.aliyuncs.com \
--docker-username=myuser \
--docker-password=mypass \
--docker-email=myuser@126.com \
--namespace mytest
創(chuàng)建Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-9005-dep
namespace: mytest
spec:
replicas: 2
selector:
matchLabels:
app: node-9005
template:
metadata:
name: node-9005
labels:
app: node-9005
spec:
containers:
- name: nodejs
image: xxx-vpc.cn-beijing.cr.aliyuncs.com/others/node-9005:20230505
imagePullPolicy: IfNotPresent
imagePullSecrets:
- name: node-9005-pull-secret
注意:我這里用的是阿里云的倉庫,所以使用了vpc的域名來拉取驚醒,這樣鏡像拉取會容易。文章來源地址http://www.zghlxwxcb.cn/news/detail-581647.html
參考文獻(xiàn)
- 為特定名稱空間創(chuàng)建 imagePullSecrets
到了這里,關(guān)于k8s實(shí)戰(zhàn)-如何使用私有鏡像倉庫的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!