Loading

kubernetes之镜像拉取策略ImagePullSecrets;

1.容器镜像是什么?

1.容器镜像(Container Image)是最终运行的软件;
2.容器镜像(最初为Docker镜像,现在叫OCI镜像更合适)是将软件打包的形式。但是容器镜像还可以携带额外的设置和命令: 环境变量、启动命令、用户名等。
3.容器镜像是由容器运行时解释和执行。Docker守护进程是最著名的,后来又出现了Runc和Containerd。当然现在还有其他实现,比如CRI-O、gVisor、Kata、Firecracker和Project Pacific,这些都是独立的实现,可以创建相同的运行时行为,通常还具有其他所需的功能。

2. 容器镜像拉取的两种策略;

2.1ImagePullPolicy

在指定容器镜像时,必须要提供镜像值(Image Value)这是一个供容器运行时(如Containerd)从镜像仓库拉取镜像的地址;
关键配置: ImagePullPolicy和ImagePullSecrets这两个都是供容器运行时使用的;
imagePullPolicy设置的是kubernetes节点拉取镜像策略,这个设置很重要。它有三个值可以设置,Always、Never、IfNotPresent;

2.2ImgaePullSecrets

1.ImagePullSecrets的设置是kubernetes机制的另一亮点,习惯于直接使用Docker Pull来拉取公共镜像,但非所有容器镜像都是公开的。此外,并不是所有的镜像仓库都允许匿名拉取,也就是说需要身份认证;
kubernetes有一个secret记录类型,可用于配置镜像登陆凭证。与所有kubernetes记录一样,Secrets通过名称被其他资源引用。此处是通过ImagePullSecrets来引用Secret的;
2.官方文档: https://kubernetes.io/zh-cn/docs/concepts/configuration/secret/#using-imagepullsecrets

3.ImagePullSecret引用;

3.1创建Secrets资源;

[root@kn-server-master01-13 knative]# kubectl create secret docker-registry \
> aliyun-haitang-registry \
> --docker-server=registry.cn-hangzhou.aliyuncs.com \
> --docker-username=xxxxxxx\
> --docker-password=xxxxxx
secret/aliyun-haitang-registry created

3.1.2查看Secrets

[root@kn-server-master01-13 knative]# kubectl get secrets
NAME                      TYPE                                  DATA   AGE
aliyun-haitang-registry   kubernetes.io/dockerconfigjson        1      2m46s

3.2Pod引用Secret资源;

3.2.1imagePullSecret在spec字段中;和container是平级的;

[root@kn-server-master01-13 knative]# kubectl explain pod.spec.imagePullSecrets
KIND:     Pod
VERSION:  v1

RESOURCE: imagePullSecrets <[]Object>

DESCRIPTION:
     ImagePullSecrets is an optional list of references to secrets in the same
     namespace to use for pulling any of the images used by this PodSpec. If
     specified, these secrets will be passed to individual puller
     implementations for them to use. For example, in the case of docker, only
     DockerConfig type secrets are honored. More info:
     https://kubernetes.io/docs/concepts/containers/images#specifying-imagepullsecrets-on-a-pod

     LocalObjectReference contains enough information to let you locate the
     referenced object inside the same namespace.

FIELDS:
   name	<string>
     Name of the referent. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names

3.2.引入imagePullSecrets

[root@kn-server-master01-13 knative]# vim knative-httpd.yaml
apiVersion: serving.knative.dev/v1
kind: Configuration
metadata:
  name: knative-httpd
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/lengyuye/httpd:alpine3.14
      imagePullSecrets:
      - name: aliyun-haitang-registry
[root@kn-server-master01-13 knative]# kubectl apply -f knative-httpd.yaml 
configuration.serving.knative.dev/knative-httpd created

3.2.3describe查看详情;
可以看到镜像确实来自于我们认证的阿里云仓库;

[root@kn-server-master01-13 knative]# kn revision describe knative-httpd-00001
Name:       knative-httpd-00001
Namespace:  default
Age:        3m
Image:      registry.cn-hangzhou.aliyuncs.com/lengyuye/httpd:alpine3.14 (at 0e0805)
posted @ 2022-08-23 09:16  梨花海棠  阅读(3604)  评论(0编辑  收藏  举报