【k8s】通过命令行删除字段

环境

  1. kubernetes 1.20.6
  2. Spring Boot 2.5.1

目标

在 shell 中,通过命令将已有的字段属性删除。

示例

deploy.yaml

配置了一个存活探针,接下来会将其删除。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
spec:
  selector:
    matchLabels:
      app: busybox
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
        - name: busybox
          image: busybox:1.31.0
          command: ["/bin/sh", "-c", "sleep 3600"]
          livenessProbe:
            exec:
              command: ["sh", "-c", "date"]

修改前查看

[root@master ~]# kubectl get deployments.apps busybox -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: busybox
  namespace: default
  resourceVersion: "1933810"
  uid: cdf95c81-469f-4d40-99e5-baf1ed2d2187
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: busybox
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: busybox
    spec:
      containers:
      - command:
        - /bin/sh
        - -c
        - sleep 3600
        image: busybox:1.31.0
        imagePullPolicy: IfNotPresent
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - date
          failureThreshold: 3
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
...

使用命令删除字段

[root@master ~]# kubectl patch deployment busybox  --type json   \
-p='[{"op": "remove", "path": "/spec/template/spec/containers/0/livenessProbe"}]'
deployment.apps/busybox patched

修改后查看

...
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: busybox
    spec:
      containers:
      - command:
        - /bin/sh
        - -c
        - sleep 3600
        image: busybox:1.31.0
        imagePullPolicy: IfNotPresent
        name: busybox
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
...

总结

介绍了通过命令的方式,删除 k8s 资源中存在的字段。

附录

posted @   jiangbo4444  阅读(254)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2019-11-30 spring-boot 环境搭建(一)
点击右上角即可分享
微信分享提示