今天在集群中创建yaml,使用create就创建成功,apply就创建失败原因分析。

本文涉及的产品
可观测监控 Prometheus 版,每月50GB免费额度
简介: 今天在集群中创建yaml,使用create就创建成功,apply就创建失败原因分析。
1. 背景:今天在集群中搭建 Prometheus 监控,在使用 kubectl apply -f [文件名] 时,出现了报错,我对集群资源、命名空间、权限等进行一系列排查,甚至在没部署任何服务新集群部署该服务依旧显示部署失败。

第一次使用 kubectl apply -f 文件命 显示报错。

#kubectl apply -f setup/
customresourcedefinition.apiextensions.k8s.io/alertmanagerconfigs.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/podmonitors.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/probes.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/prometheusrules.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/scrapeconfigs.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/servicemonitors.monitoring.coreos.com created
namespace/monitoring created
Error from server (Invalid): error when creating "setup/0alertmanagerCustomResourceDefinition.yaml": CustomResourceDefinition.apiextensions.k8s.io "alertmanagers.monitoring.coreos.com" is invalid: metadata.annotations: Too long: must have at most 262144 bytes
Error from server (Invalid): error when creating "setup/0prometheusCustomResourceDefinition.yaml": CustomResourceDefinition.apiextensions.k8s.io "prometheuses.monitoring.coreos.com" is invalid: metadata.annotations: Too long: must have at most 262144 bytes
Error from server (Invalid): error when creating "setup/0prometheusagentCustomResourceDefinition.yaml": CustomResourceDefinition.apiextensions.k8s.io "prometheusagents.monitoring.coreos.com" is invalid: metadata.annotations: Too long: must have at most 262144 bytes
Error from server (Invalid): error when creating "setup/0thanosrulerCustomResourceDefinition.yaml": CustomResourceDefinition.apiextensions.k8s.io "thanosrulers.monitoring.coreos.com" is invalid: metadata.annotations: Too long: must have at most 262144 bytes

使用 kubectl delete -f setup/ --grace-period=0 --force 强制删除创建yaml。

kubectl delete -f setup/ --grace-period=0 --force
Warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
customresourcedefinition.apiextensions.k8s.io "alertmanagerconfigs.monitoring.coreos.com" force deleted
customresourcedefinition.apiextensions.k8s.io "podmonitors.monitoring.coreos.com" force deleted
customresourcedefinition.apiextensions.k8s.io "probes.monitoring.coreos.com" force deleted
customresourcedefinition.apiextensions.k8s.io "prometheusrules.monitoring.coreos.com" force deleted
customresourcedefinition.apiextensions.k8s.io "scrapeconfigs.monitoring.coreos.com" force deleted
customresourcedefinition.apiextensions.k8s.io "servicemonitors.monitoring.coreos.com" force deleted
namespace "monitoring" force deleted
Error from server (NotFound): error when deleting "setup/0alertmanagerCustomResourceDefinition.yaml": customresourcedefinitions.apiextensions.k8s.io "alertmanagers.monitoring.coreos.com" not found
Error from server (NotFound): error when deleting "setup/0prometheusCustomResourceDefinition.yaml": customresourcedefinitions.apiextensions.k8s.io "prometheuses.monitoring.coreos.com" not found
Error from server (NotFound): error when deleting "setup/0prometheusagentCustomResourceDefinition.yaml": customresourcedefinitions.apiextensions.k8s.io "prometheusagents.monitoring.coreos.com" not found
Error from server (NotFound): error when deleting "setup/0thanosrulerCustomResourceDefinition.yaml": customresourcedefinitions.apiextensions.k8s.io "thanosrulers.monitoring.coreos.com" not found

重新使用 kubectl create -f setup/ 显示成功。

#kubectl create -f setup/
customresourcedefinition.apiextensions.k8s.io/alertmanagerconfigs.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/alertmanagers.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/podmonitors.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/probes.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/prometheuses.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/prometheusagents.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/prometheusrules.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/scrapeconfigs.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/servicemonitors.monitoring.coreos.com created
customresourcedefinition.apiextensions.k8s.io/thanosrulers.monitoring.coreos.com created
namespace/monitoring created
2. 经过查询各种相关资讯,在知识星球有篇文章可以解释create与apply在创建yaml有很大区别。
1. kubectl create 命令只是简单地创建资源对象,它不会去检查或者处理对象的注解数据,所以它不会因为注解数据过大而报错。
2. kubectl apply 命令则会对资源对象进行更复杂的处理。在创建或者更新资源对象时,它会将整个资源对象的配置数据(包括注解数据)保存在 kubectl.kubernetes.io/last-applied-configuration 注解中。这样在以后的 apply 操作中,kubectl 就可以通过比较这个注解中的配置数据和当前的配置数据,来决定哪些字段需要更新,哪些字段不需要更新。由于这个原因,如果你的注解数据过大,超过了 Kubernetes 对注解数据大小的限制,那么 kubectl apply 就会报错。

总结,如果你的注解数据过大,你可以选择使用 kubectl create 命令来创建资源对象,但是你需要注意,这样做之后你就不能再使用 kubectl apply 命令来更新这个资源对象了,因为 apply 命令仍然会因为注解数据过大而报错。你需要使用其他的命令,比如 kubectl edit 或者 kubectl patch 来更新资源对象。

3. create与apply更详细区别请参考知识星球"云原生运维圈 ":https://wx.zsxq.com/dweb2/index/topic_detail/588548181528454


相关实践学习
深入解析Docker容器化技术
Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
相关文章
|
Kubernetes 应用服务中间件 nginx
kubernetes集群命令行工具kubectl介绍&yaml文件介绍-20230208
kubernetes集群命令行工具kubectl介绍&yaml文件介绍-20230208
348 0
|
Kubernetes Cloud Native 流计算
Flink-12 Flink Java 3分钟上手 Kubernetes云原生下的Flink集群 Rancher Stateful Set yaml详细 扩容缩容部署 Docker容器编排
Flink-12 Flink Java 3分钟上手 Kubernetes云原生下的Flink集群 Rancher Stateful Set yaml详细 扩容缩容部署 Docker容器编排
350 3
|
Kubernetes 大数据 容器
三、kubernetes 集群 YAML 文件详解
是一个可读性高,用来表大数据列的格式。Yaml的意思其实是:仍是一种标记语言,但是为了强调这种语言以数据为中心。而不是以标记语言为重点。
1403 0
三、kubernetes 集群 YAML 文件详解
|
算法 Go vr&ar
YOLOv8模型yaml结构图理解(逐层分析)
YOLOv8模型yaml结构图理解(逐层分析)
18425 0
|
JSON Kubernetes 应用服务中间件
K8S集群-(YAML.YML)文件详解
K8S集群-(YAML.YML)文件详解
697 0
K8S集群-(YAML.YML)文件详解
|
存储 Kubernetes 应用服务中间件
K8S 集群资源清单(YAML)文件书写方法_资源清单描述方法 | 学习笔记
快速学习 K8S 集群资源清单(YAML)文件书写方法_资源清单描述方法
315 0
K8S 集群资源清单(YAML)文件书写方法_资源清单描述方法 | 学习笔记
|
Kubernetes Ubuntu Shell
shell 脚本实现 k8s 集群环境下指定 ns 资源的 yaml 文件备份
在基于 `k8s` 平台的容器化部署环境中,有时候需要快速的实现部署文件的迁移备份,当 `k8s` 平台部署一个 `app` 时,都会相应的产生一堆 `yaml` 文件,如果 `yaml` 文件数量较少,我们可以人工手动的方式进行拷贝,但是当 `yaml` 文件数量多,并且该 `k8s` 平台部署了多个 `app` 时,如果在采用...
762 0
shell 脚本实现 k8s 集群环境下指定 ns 资源的 yaml 文件备份
|
XML 运维 Kubernetes
K8S 集群资源清单(YAML)文件书写方法 | 学习笔记
快速学习 K8S 集群资源清单(YAML)文件书写方法
411 0
|
JSON Kubernetes 应用服务中间件
【Docker】Kubernetes集群 yaml部署应用样例
非常简单的yaml配置~,对配置参数标注了说明
1730 0
【Docker】Kubernetes集群 yaml部署应用样例
|
2月前
|
Java 测试技术 数据安全/隐私保护
通过yaml文件配置自动化测试程序
通过yaml文件可以将自动化测试环境,测试数据和测试行为分开,请看一下案例
100 4