如何引用k8s yaml文件中的label值
How to reference the label value in k8s yaml file
我想在 k8s yaml 文件中的 VirtualService 规范部分引用标签值。我使用 ${metadata.labels[component]} 来表示下面的位置。有没有办法实现我的想法?
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: istio-ingress-version
namespace: netops
labels:
component: version
spec:
hosts:
- "service.api.com"
gateways:
- public-inbound-gateway
http:
- match:
- uri:
prefix: /${metadata.labels[component]}/
headers:
referer:
regex: ^https://[^\s/]*a.api.com[^\s]*
rewrite:
uri: "/"
route:
- destination:
host: ${metadata.labels[component]}.3da.svc.cluster.local
- match:
- uri:
prefix: /${metadata.labels[component]}/
headers:
referer:
regex: ^https://[^\s/]*b.api.com[^\s]*
rewrite:
uri: "/"
route:
- destination:
host: ${metadata.labels[component]}.3db.svc.cluster.local
- match:
- uri:
prefix: /${metadata.labels[component]}/
rewrite:
uri: "/"
route:
- destination:
host: ${metadata.labels[component]}.3db.svc.cluster.local
这不是 Kubernetes 本身的功能,但是存在其他工具可以帮助您处理这种情况。
其中最主要的是Helm。它允许您创建可以在多个不同的 YAML 文件之间共享的变量,允许您共享值甚至完全参数化您的部署。
查看 downwardAPI 以在运行时将标签和注释等 pod 元数据注入 pods。
apiVersion: v1
kind: Pod
metadata:
name: kubernetes-downwardapi-volume-example
labels:
zone: us-est-coast
cluster: test-cluster1
rack: rack-22
annotations:
build: two
builder: john-doe
spec:
containers:
- name: client-container
image: gcr.io/google_containers/busybox
command: ["sh", "-c", "while true; do if [[ -e /etc/labels ]]; then cat /etc/labels; fi; if [[ -e /etc/annotations ]]; then cat /etc/annotations; fi; sleep 5; done"]
volumeMounts:
- name: podinfo
mountPath: /etc
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
- path: "annotations"
fieldRef:
fieldPath: metadata.annotations
我想在 k8s yaml 文件中的 VirtualService 规范部分引用标签值。我使用 ${metadata.labels[component]} 来表示下面的位置。有没有办法实现我的想法?
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: istio-ingress-version
namespace: netops
labels:
component: version
spec:
hosts:
- "service.api.com"
gateways:
- public-inbound-gateway
http:
- match:
- uri:
prefix: /${metadata.labels[component]}/
headers:
referer:
regex: ^https://[^\s/]*a.api.com[^\s]*
rewrite:
uri: "/"
route:
- destination:
host: ${metadata.labels[component]}.3da.svc.cluster.local
- match:
- uri:
prefix: /${metadata.labels[component]}/
headers:
referer:
regex: ^https://[^\s/]*b.api.com[^\s]*
rewrite:
uri: "/"
route:
- destination:
host: ${metadata.labels[component]}.3db.svc.cluster.local
- match:
- uri:
prefix: /${metadata.labels[component]}/
rewrite:
uri: "/"
route:
- destination:
host: ${metadata.labels[component]}.3db.svc.cluster.local
这不是 Kubernetes 本身的功能,但是存在其他工具可以帮助您处理这种情况。
其中最主要的是Helm。它允许您创建可以在多个不同的 YAML 文件之间共享的变量,允许您共享值甚至完全参数化您的部署。
查看 downwardAPI 以在运行时将标签和注释等 pod 元数据注入 pods。
apiVersion: v1
kind: Pod
metadata:
name: kubernetes-downwardapi-volume-example
labels:
zone: us-est-coast
cluster: test-cluster1
rack: rack-22
annotations:
build: two
builder: john-doe
spec:
containers:
- name: client-container
image: gcr.io/google_containers/busybox
command: ["sh", "-c", "while true; do if [[ -e /etc/labels ]]; then cat /etc/labels; fi; if [[ -e /etc/annotations ]]; then cat /etc/annotations; fi; sleep 5; done"]
volumeMounts:
- name: podinfo
mountPath: /etc
readOnly: false
volumes:
- name: podinfo
downwardAPI:
items:
- path: "labels"
fieldRef:
fieldPath: metadata.labels
- path: "annotations"
fieldRef:
fieldPath: metadata.annotations