kubernetes 自定义修补期间删除字段

bjg7j2ky  于 2023-04-05  发布在  Kubernetes
关注(0)|答案(1)|浏览(156)

我试图通过使用kustomize路径部署我只需要改变环境在spec.template.spec.containers.但在kustomisation其他字段被删除太多
树:

k8s
|_base
  |_ deployment.yaml
  |_ kustomisation.yaml
  |_ svc.yaml
|_overlays
  |prod
    |_ kustomisation.yaml
    |_ patches.yaml
  |_stage
  |_dev

base.deployment.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: __NAME__
  name: __NAME__
  namespace: __NAME__
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/name: __NAME__
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: __NAME__
        app.kubernetes.io/name: __NAME__
    spec:
      containers:
      - name: __NAME__
        image: __IMAGE__ 
        env:
          - name: name_
            value: value_
        imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 15
          periodSeconds: 20
          successThreshold: 1
          timeoutSeconds: 1
        ports:
        - containerPort: 8081
          protocol: TCP
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 8081
            scheme: HTTP
          initialDelaySeconds: 5
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        resources:
          limits:
            cpu: 1500m
            memory: 1Gi
          requests:
            cpu: 250m
            memory: 128Mi
        securityContext:
          runAsGroup: 1001
          runAsUser: 1001
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

overlays.prod.kustomization.yaml

resources:
- ../../base/
patches:
- target:
    kind: Deployment
    name: __NAME__
  path: patches.yaml

overlays.prod.patches.yaml

kind: Deployment
metadata:
  name: __NAME__
spec:
  template:
    spec:
      containers:
      - name:  __NAME__
        env:
        - name: ENV_VAR_NAME_1
          value: new_value_1
        - name: ENV_VAR_NAME_2
          value: new_value_2

在命令kubectl kustomize overlays/prod/ -o stage_test.yaml输出为

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: __NAME__
  name: __NAME__
  namespace: __NAME__
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app.kubernetes.io/name: __NAME__
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: __NAME__
        app.kubernetes.io/name: __NAME__
    spec:
      containers:
      - env:
        - name: ENV_VAR_NAME_1
          value: new_value_1
        - name: ENV_VAR_NAME_2
          value: new_value_2
        name: __NAME__
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30

正如你所看到的kustomise -删除探针,端口和其他东西
如何避免删除?如果我把所有其他的东西放到补丁中,我只需要修改envs. ofc。yaml kustomize会把它添加到输出中
尝试设置注解注解:kustomize.kubernetes.io/ignore-patches:“true”并使用它运行kustomize(不工作-相同的输出)
patchesStrategicMerge的路径(工作方式相同)
版本如果有问题:

  • 定制版本:版本4.5.7
  • 客户端版本:1.26
  • 平台:达尔文/arm 64
brvekthn

brvekthn1#

这里的根本原因是您将Deployment定义为:

apiVersion: extensions/v1beta1
kind: Deployment

但这不是Deployment的正确apiVersion;它应该是:

apiVersion: apps/v1
kind: Deployment

extensions/v1beta1 API的支持是removed in Kubernetes 1.16
Kustomize“知道”常规Kubernetes Deployment,并将正确合并您的配置;有了这一个变化(并使用Kustomize v5.0.1),您的补丁工作正常。
更改前比较:

$ kustomize build overlays/prod | yq .spec.template.spec.containers
[
  {
    "env": [
      {
        "name": "ENV_VAR_NAME_1",
        "value": "new_value_1"
      },
      {
        "name": "ENV_VAR_NAME_2",
        "value": "new_value_2"
      }
    ],
    "name": "__NAME__"
  }
]

收件人:

$ sed -i '/^apiVersion:/ s|apiVersion:.*|apiVersion: apps/v1|' base/deployment.yaml
$ kustomize build overlays/prod | yq .spec.template.spec.containers
[
  {
    "env": [
      {
        "name": "ENV_VAR_NAME_1",
        "value": "new_value_1"
      },
      {
        "name": "ENV_VAR_NAME_2",
        "value": "new_value_2"
      },
      {
        "name": "name_",
        "value": "value_"
      }
    ],
    "image": "__IMAGE__",
    "imagePullPolicy": "IfNotPresent",
    "livenessProbe": {
      "failureThreshold": 3,
      "httpGet": {
        "path": "/healthz",
        "port": 8081,
        "scheme": "HTTP"
      },
      "initialDelaySeconds": 15,
      "periodSeconds": 20,
      "successThreshold": 1,
      "timeoutSeconds": 1
    },
    "name": "__NAME__",
    "ports": [
      {
        "containerPort": 8081,
        "protocol": "TCP"
      }
    ],
    "readinessProbe": {
      "failureThreshold": 3,
      "httpGet": {
        "path": "/healthz",
        "port": 8081,
        "scheme": "HTTP"
      },
      "initialDelaySeconds": 5,
      "periodSeconds": 10,
      "successThreshold": 1,
      "timeoutSeconds": 1
    },
    "resources": {
      "limits": {
        "cpu": "1500m",
        "memory": "1Gi"
      },
      "requests": {
        "cpu": "250m",
        "memory": "128Mi"
      }
    },
    "securityContext": {
      "runAsGroup": 1001,
      "runAsUser": 1001
    },
    "terminationMessagePath": "/dev/termination-log",
    "terminationMessagePolicy": "File"
  }
]

如果您被迫使用extensions/v1beta1 API,您可能需要查看this document,了解如何为Kustomize提供有关如何处理自定义资源类型合并的信息。
This article更详细地探讨了这个过程。

相关问题