ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

KubeVela container-image 运维特征:动态替换 Pod 容器镜像与多容器管理实战

KubeVela container-image 运维特征:动态替换 Pod 容器镜像与多容器管理实战 云原生DevOps运维微服务【免费下载链接】kubevelaThe Modern Application Platform.项目地址https://gitcode.com/gh_mirrors/ku/kubevela点击查看免费下载container-image是 KubeVela 内置的运维特征Trait用于在应用部署时动态设置 Pod 内容器的镜像地址与镜像拉取策略imagePullPolicy并且支持通过containers字段一次性批量控制主容器与 sidecar 等全部容器。本文以官方示例 container-image.eg.md 为主体结合定义源码、Helm 模板与测试用例完整讲解单容器与多容器两种用法、全部参数语义及其底层 CUE 实现原理帮助你直接将其复用到自己的 Application 中。一、container-image 是什么在 KubeVela 的 OAM 模型里运维特征以trait类型定义挂载在 Application 的components[].traits下对组件生成的 workload 资源做追加修改。container-image正是其中专门负责改镜像的特征它通过 CUE 模板对 workload 的 Pod 模板spec.template.spec.containers打补丁把指定容器的image替换为新值可以顺带设置imagePullPolicy支持定位任意具名容器因此能同时修改主容器与 sidecar 容器定义位于 vela-templates/definitions/internal/trait/container-image.cueHelm 安装时会被渲染进 charts/vela-core/templates/defwithtemplate/container-image.yaml 中的TraitDefinition。从定义元数据可以看到它适用于四类常见工作负载且属于破坏性Pod 重建型操作container-image: { type: trait description: Set the image of the container. attributes: { podDisruptive: true appliesToWorkloads: [deployments.apps, statefulsets.apps, daemonsets.apps, jobs.batch] } }也就是说container-image可作用于 Deployment、StatefulSet、DaemonSet 与 Job相关说明可对照 appfile 特征解析 与内置定义目录 vela-templates/definitions/internal/trait。podDisruptive: true表示修改镜像会导致 Pod 滚动重建这在设计上线流程时需要留意。二、参数一览container-image的全部参数定义在 CUE 模板的#PatchParams中源码见 container-image.cue语义如下参数类型默认值说明containerNamestring空目标容器名不填时默认取组件名context.name作为容器名imagestring必填要设置的容器镜像地址imagePullPolicystring空镜像拉取策略可选IfNotPresent/Always/Never不填则不修改原策略containers[...#PatchParams]无多容器场景下逐容器指定的数组数组内每项必须带containerName#PatchParams: { // usageSpecify the name of the target container, if not set, use the component name containerName: * | string // usageSpecify the image of the container image: string // usageSpecify the image pull policy of the container imagePullPolicy: * | IfNotPresent | Always | Never }需要注意imagePullPolicy的默认值是空字符串模板中只有当其非空时才会写入补丁对应源码if _params.imagePullPolicy ! 分支因此它本质上是一个可选覆盖项。若你不设置Pod 将沿用 workload 原本的拉取策略例如 Kubernetes 依据镜像 tag 自动推导的IfNotPresent/Always规则。三、单容器用法替换主容器镜像最常见的场景是把组件生成的主容器容器名默认等于组件名替换为指定镜像。官方示例文件 single-container.yaml 给出了完整可运行的 ApplicationapiVersion: core.oam.dev/v1beta1 kind: Application metadata: name: busybox spec: components: - name: busybox type: webservice properties: image: busybox cmd: [sleep, 86400] traits: - type: sidecar properties: name: sidecar-nginx image: nginx - type: container-image properties: # by default, the following image will be applied to the main container - busybox in this case image: busybox-1.34.0 # if you want to set the imagePullPolicy as well, specify it # imagePullPolicy: IfNotPresent # if you want to patch the sidecar container, specify the container name explicitly # containerName: sidecar-nginx这里webservice组件会生成名为busybox的 DeploymentPod 内包含主容器busybox以及sidecar特征注入的sidecar-nginx容器。挂载container-image特征时不写containerName特征默认把镜像改为busybox-1.34.0作用于与组件同名的busybox主容器取消注释imagePullPolicy: IfNotPresent会同时把主容器的拉取策略改为IfNotPresent取消注释containerName: sidecar-nginx则本次补丁改为作用于 sidecar 容器。这条默认使用组件名作为容器名的逻辑在模板中有明确实现container-image.cueif parameter.containerName { containerName: context.name } if parameter.containerName ! { containerName: parameter.containerName }四、多容器用法通过 containers 批量控制当 Pod 内存在多个容器例如主容器 sidecar init-container且需要一次性修改多个容器的镜像时可以使用containers数组。这是官方文档 container-image.eg.md 演示的核心用法同样的示例也存放在 multiple-containers.yamlapiVersion: core.oam.dev/v1beta1 kind: Application metadata: name: busybox spec: components: - name: busybox type: webservice properties: image: busybox cmd: [sleep, 86400] traits: - type: sidecar properties: name: sidecar-nginx image: nginx - type: container-image properties: # you can use container-image to control multiple containers by filling containers # NOTE: in containers, you must set the container name for each container containers: - containerName: busybox image: busybox-1.34.0 imagePullPolicy: IfNotPresent - containerName: sidecar-nginx image: nginx-1.20要点数组中的每一项都是一个#PatchParams必须显式填写containerName指定目标容器每项可独立设置image与imagePullPolicy互不影响该写法可同时覆盖主容器与sidecar特征注入的sidecar-nginx容器实现一键升级整 Pod 镜像。若某个容器在数组里没有给出containerName模板会直接报错见下文的校验机制。五、底层实现CUE 补丁如何工作container-image的模板核心是PatchContainer辅助结构与patch块整体模式与 KubeVela 的 defkitPatchContainer生成能力一致可对照 pkg/definition/defkit/trait.go 中的UsePatchContainer配置及其单测 patch_container_test.go。5.1 按容器名匹配模板通过读取 workload 的 Pod 模板容器列表并按name精确匹配目标容器PatchContainer: { _params: #PatchParams name: _params.containerName _baseContainers: context.output.spec.template.spec.containers _matchContainers_: [for _container_ in _baseContainers if _container_.name name {_container_}] _baseContainer: *_|_ | {...} if len(_matchContainers_) 0 { err: container \(name) not found } if len(_matchContainers_) 0 { // patchStrategyretainKeys image: _params.image if _params.imagePullPolicy ! { // patchStrategyretainKeys imagePullPolicy: _params.imagePullPolicy } } }从中可以读出的行为找不到容器即报错如果containerName或默认的组件名与 workload 中任何容器名都不匹配模板产生err: container xxx not found应用会进入失败状态避免静默修改错误对象retainKeys补丁策略image与imagePullPolicy都带// patchStrategyretainKeys注释告知渲染引擎按 key 合并而非整体替换容器对象保证容器其他字段如env、resources不受影响条件写入拉取策略imagePullPolicy只有在非空时才补丁实现可选覆盖语义。5.2 patch 块的两种分支patch块根据是否提供containers字段走两个分支container-image.cue未提供containers按单容器处理// patchKeyname标注以容器名为补丁键生成一个包含目标容器的数组提供了containers用 CUE 列表推导[for c in parameter.containers {...}]逐项处理并对每一项做containerName非空校验if c.containerName { err: containerName must be set for containers } if c.containerName ! { PatchContainer {_params: c} }最后模板通过errs: [for c in patch.spec.template.spec.containers if c.err ! _|_ {c.err}]收集所有容器级错误并统一暴露任何一项容器匹配失败都会让特征渲染失败、阻断本次部署。参数整体定义在模板末尾container-image.cueparameter: #PatchParams | close({ // usageSpecify the container image for multiple containers containers: [...#PatchParams] })即顶层参数要么是单个#PatchParams单容器要么是{containers: [...#PatchParams]}多容器二者互斥、由字段存在性自动区分。六、安装与验证container-image是 KubeVela 内置定义。使用vela install安装控制平面后Helm Chart 会通过 defwithtemplate/container-image.yaml 在vela-system命名空间注册名为container-image的TraitDefinition其中包含与 CUE 源码一致的定义appliesToWorkloads、podDisruptive: true及完整模板。因此上述示例无需额外导入定义即可直接使用vela up -f docs/examples/traits/container-image/multiple-containers.yamlCLI 侧也有对应验证用例references/cli/traits_test.go 会在测试环境中创建container-image与configmap两个 TraitDefinition然后执行vela trait命令并断言输出中不出现 error从命令层面确认该特征可被正常识别与渲染。七、小结与实践建议优先用containers做整 Pod 镜像统一管理它允许在同一个特征实例里同时升级主容器与 sidecar避免为每个容器重复挂载特征多容器场景必须写全containerName漏写会直接触发containerName must be set for containers校验错误imagePullPolicy是可选覆盖项不填时保留原策略填IfNotPresent/Always/Never时以retainKeys策略安全合并注意podDisruptive: true镜像变更会引发 Pod 重建灰度发布时应配合 rollout 等机制控制变更节奏理解失败语义目标容器名不存在会导致应用渲染失败而不是默默不改这保证了镜像替换的准确性。若需进一步研究模板生成模式可阅读 defkit/trait.go 的PatchContainerConfig含DefaultToContextName、AllowMultiple、ContainersParam等配置项与 patch_container_test.go 中针对image/imagePullPolicy的retainKeys、NotEmpty生成断言它们正是本节所述容器补丁体系的可复现测试证据。赞分享云原生DevOps运维微服务【免费下载链接】kubevelaThe Modern Application Platform.项目地址https://gitcode.com/gh_mirrors/ku/kubevela点击查看免费下载相关推荐思源等宽字体高级应用在VS Code、Terminal等开发环境中的最佳配置思源等宽字体高级应用在VS Code、Terminal等开发环境中的最佳配置 思源等宽Source Han Mono是一款源自Source Han San设计系统Kustomize 镜像替换实战用 images 与 kustomize edit set image 修改容器镜像名称、标签与摘要Kustomize 镜像替换实战用 images 与 kustomize edit set image 修改容器镜像名称、标签与摘要 导读 本文以仓库中的官方CLI开发工具云原生Kubernetes实战在Pod中使用镜像卷(Image Volume)挂载OCI镜像内容Kubernetes实战在Pod中使用镜像卷 Image Volume 挂载OCI镜像内容 概述 在Kubernetes中Image Volume镜像卷文档教程云原生上一篇如何将AI-Infra-Guard接入CI/CD流水线SARIF输出与自动化安全评估完全指南下一篇Sunshine游戏串流终极指南从零开始打造完美远程游戏体验创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表