kubernetes 定制如何在Ingress配置中仅替换主机

zqdjd7g9  于 2023-06-21  发布在  Kubernetes
关注(0)|答案(3)|浏览(88)

我得到了这个ingress.yaml的基本配置:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    sia: aza
    app: asap-ingress-internal
  name: asap-ingress-internal
  annotations:
    kubernetes.io/ingress.class: "nginx-external"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: the-host-value
      http:
        paths:
          - path: /asap-srv-template/(.*)
            backend:
              serviceName: asap-srv-template
              servicePort: 8080

并且只想替换spec.rules.host值(并保持所有http. path不变)。
所以我创建了一个env-var.yaml,如下所示:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: asap-ingress-internal
spec:
  rules:
    - host: the.real.hostname

但结果如下:

$ kustomize build
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx-external
    nginx.ingress.kubernetes.io/use-regex: "true"
  labels:
    app: asap-ingress-internal
    env: dev
    sia: aza
  name: asap-ingress-internal
  namespace: aza-72461-dev
spec:
  rules:
  - host: the.real.hostname

我已经失去了所有的http.paths配置,我不能找到如何做。
我试着用补丁:或kustomization.yaml中的patchesStrategicMerge,但结果总是相同的。
任何帮助将不胜感激

a9wyjsp7

a9wyjsp71#

你可以使用json补丁,下面是一个例子。
下面是一个例子kustomization.yaml。它将在patches部分调用一个补丁:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- ../../base/app1

patches:
- target:
    kind: Ingress
    name: my-ingress
  path: ingress-patch.json

下面是一个示例ingress-patch.json

[
    { 
        "op": "replace", 
        "path": "/spec/rules/0/host", 
        "value": "the.real.hostname"
    }
]
ffx8fchx

ffx8fchx2#

另一个选择是做内联补丁。这与mroma提供的方法相同,但没有文件。我觉得更简单。

# kustomization.yaml

resources:
  - ingress.yaml
patches:
  - target:
      kind: Ingress
      name: asap-ingress-internal
    patch: |-
      - op: replace
        path: /spec/rules/0/host
        value: the.real.hostname
bjg7j2ky

bjg7j2ky3#

如果你使用的是kubernetes的最新版本(我认为从18年开始),Ingres API版本不再是beta版本apiVersion: extensions/v1beta1,现在是apiVersion: networking.k8s.io/v1
我已经测试了波纹管样品和它的工作:
ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: asap-ingress-internal
  annotations:
    kubernetes.io/ingress.class: "nginx-external"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: the-host-value
      http:
        paths:
          - path: /asap-srv-template/(.*)
            backend:
              serviceName: asap-srv-template
              servicePort: 8080

ingress-patch.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: asap-ingress-internal
spec:
  rules:
    - host: the.real.hostname

kustomization.yaml

resources:
- ingress.yaml
patchesStrategicMerge:
- ingress-patch.yaml

使用kubectl kustomize(kubectl版本为v1.19.7)和kustomize build(独立kustomize版本为v3.5.4)进行测试

相关问题