如何使用 Kustomize 配置 Traefik 2.x IngressRoute (metadata.name, spec.routes[0].services[0].name & spec.ro



我们有一个运行的EKS集群,Traefik以CRD风格部署(在GitHub上完全设置(,并且不部署我们的应用程序https://gitlab.com/jonashackt/microservice-api-spring-boot使用Kubernetes对象Deployment、Service和IngressRoute(请参阅此处的配置存储库(。清单如下:

deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: microservice-api-spring-boot
spec:
replicas: 3
revisionHistoryLimit: 3
selector:
matchLabels:
app: microservice-api-spring-boot
branch: main
template:
metadata:
labels:
app: microservice-api-spring-boot
branch: main
spec:
containers:
- image: registry.gitlab.com/jonashackt/microservice-api-spring-boot:c25a74c8f919a72e3f00928917dc4ab2944ab061
name: microservice-api-spring-boot
ports:
- containerPort: 8098
imagePullSecrets:
- name: gitlab-container-registry

service.yml:

apiVersion: v1
kind: Service
metadata:
name: microservice-api-spring-boot
spec:
ports:
- port: 80
targetPort: 8098
selector:
app: microservice-api-spring-boot
branch: main

traefik-ingress-route.yml:

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: microservice-api-spring-boot-ingressroute
namespace: default
spec:
entryPoints:
- web
routes:
- match: Host(`microservice-api-spring-boot-BRANCHNAME.tekton-argocd.de`)
kind: Rule
services:
- name: microservice-api-spring-boot
port: 80

我们已经使用Kustoize,尤其是kustomizeCLI(在Mac上或在与brew install kustomize一起安装的GitHub Actions中(,具有以下文件夹结构:

├── deployment.yml
├── kustomization.yaml
├── service.yml
└── traefik-ingress-route.yml

我们的kustomization.yaml看起来像这样:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yml
- service.yml
- traefik-ingress-route.yml
images:
- name: registry.gitlab.com/jonashackt/microservice-api-spring-boot
newTag: foobar
commonLabels:
branch: foobar
nameSuffix: foobar

现在,在我们的GitHub Actions工作流中动态更改metadata.name,为Deployment、Service和IngressRoute的.metadata.name添加后缀,使用kustomizeCLI很容易(因为我们希望后缀使用前缀-,所以我们需要在此处使用-- -barfoo语法(:

kustomize edit set namesuffix -- -barfoo

使用检查结果

kustomize build .

同样,在部署和服务中更改.spec.selector.matchLabels.branch.spec.template.metadata.labels.branch.spec.selector.branch也没有问题:

kustomize edit set label branch:barfoo

更改我们部署的.spec.template.spec.containers[0].image适用于:

kustomize edit set image registry.gitlab.com/jonashackt/microservice-api-spring-boot:barfoo

但看看我们的IngressRoute.spec.routes[0].services[0].name.spec.routes[0].match = Host()似乎无法用Kustomize开箱即用来更改?!那么,我们如何在不需要像yq甚至sed/envsubst这样的替换工具的情况下更改这两个字段呢

1。用Kustomize更改IngressRoute.spec.routes[0].services[0].name

Kustoize使用NameReference转换器可以更改IngressRoute.spec.routes[0].services[0].name(请参阅此处的文档(-幸运的是,我在本期中找到了灵感。因此,我们需要在kustomize.yaml:中包含configurations关键字

nameSuffix: foobar
configurations:
# Tie target Service metadata.name to IngressRoute's spec.routes.services.name
# Once Service name is changed, the IngressRoute referrerd service name will be changed as well.
- nameReference.yml

我们还需要添加名为nameReference.yml:的文件

nameReference:
- kind: Service
fieldSpecs:
- kind: IngressRoute
path: spec/routes/services/name

如您所见,我们将服务的name与入口路由spec/routes/services/name联系起来。正在运行

kustomize edit set namesuffix barfoo

不仅会更改Deployment、Service和IngressRoute的metadata.name标签,还会更改IngressRoute的.spec.routes[0].services[0].name,因为它现在已链接到Service的metadata.name。请注意,这只是在引用者和目标都有name标签的情况下。

2.更改部分入口路由.spec.routes[0].match = Host()

问题的第二部分询问如何更改入口路由.spec.routes[0].match = Host()的一部分。Kustomize GitHub项目中存在一个悬而未决的问题。现在Kustoize不支持这个用例——只为Kustoize编写了一个自定义生成器插件。由于这可能不是一个首选的选择,还有另一种方式受到了这篇博客文章的启发。由于我们可以使用语法cat > ./myyamlfile.yml <<EOF ... EOF在控制台中内联创建yaml文件,因此我们也可以使用内联变量替换。

因此,首先将分支名称定义为变量:

RULE_HOST_BRANCHNAME=foobar

然后使用所描述的语法以内联方式创建ingressroute-patch.yml文件:

cat > ./ingressroute-patch.yml <<EOF
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: microservice-api-spring-boot-ingressroute
namespace: default
spec:
entryPoints:
- web
routes:
- match: Host(`microservice-api-spring-boot-$RULE_HOST_BRANCHNAME.tekton-argocd.de`)
kind: Rule
services:
- name: microservice-api-spring-boot
port: 80
EOF

最后一步是在我们的kustomization.yaml中使用ingressroute-patch.yml文件作为patchesStrategicMerge,如下所示:

patchesStrategicMerge:
- ingressroute-patch.yml

现在运行kustomize build .应该为我们的设置输出正确的部署、服务和入口路由:

apiVersion: v1
kind: Service
metadata:
labels:
branch: barfoo
name: microservice-api-spring-boot-barfoo
spec:
ports:
- port: 80
targetPort: 8098
selector:
app: microservice-api-spring-boot
branch: barfoo
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
branch: barfoo
name: microservice-api-spring-boot-barfoo
spec:
replicas: 3
revisionHistoryLimit: 3
selector:
matchLabels:
app: microservice-api-spring-boot
branch: barfoo
template:
metadata:
labels:
app: microservice-api-spring-boot
branch: barfoo
spec:
containers:
- image: registry.gitlab.com/jonashackt/microservice-api-spring-boot:barfoo
name: microservice-api-spring-boot
ports:
- containerPort: 8098
imagePullSecrets:
- name: gitlab-container-registry
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
labels:
branch: barfoo
name: microservice-api-spring-boot-ingressroute-barfoo
namespace: default
spec:
entryPoints:
- web
routes:
- kind: Rule
match: Host(`microservice-api-spring-boot-barfoo.tekton-argocd.de`)
services:
- name: microservice-api-spring-boot-barfoo
port: 80

相关内容

  • 没有找到相关文章

最新更新