代码之家  ›  专栏  ›  技术社区  ›  jonashackt

如何使用Kustomize配置Traefik 2。x IngressRoute(metadata.name,spec.routes[0]。服务[0]。名称和spec.routes[0]。match=Host()

  •  0
  • jonashackt  · 技术社区  · 3 年前

    我们有 a EKS cluster running with Traefik deployed in CRD style (full setup on GitHub )不想部署我们的应用程序 https://gitlab.com/jonashackt/microservice-api-spring-boot 使用Kubernetes对象部署、服务和入口路由(请参阅 configuration repository here ).清单如下所示:

    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
    

    我们已经使用了 Kustomize 尤其是 kustomize CLI(在Mac或GitHub上安装) brew install kustomize )使用以下文件夹结构:

    ├── 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
    

    现在改变 metadata.name 动态地为部署、服务和入口路由添加后缀 .metadata.name 在我们的GitHub操作工作流中,使用 库斯托米兹 CLI(因为我们希望后缀使用前缀 - ,我们需要使用 -- -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 回复  |  直到 3 年前
        1
  •  0
  •   jonashackt    3 年前

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

    改变 入口路线 s .spec.routes[0]。服务[0]。名称 使用Kustomize是可能的 a NameReference transformer (see docs here) -幸运的是我找到了灵感 in this issue .因此,我们需要包括 configurations 关键字在我们的 kustomize.yaml :

    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
    

    不仅会改变 metadata.name 部署、服务和入口路线的标签,以及 .spec.routes[0]。服务[0]。名称 入口路线,因为它现在链接到 元数据。名称 这项服务的一部分。请注意,只有当推荐人和目标都有 名称 标签

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

    问题的第二部分询问如何更改入口路线的一部分 .spec.routes[0]。match=Host() .有 an open issue in the Kustomize GitHub project .目前Kustomize不支持此用例-只为Kustomize编写自定义生成器插件。由于这可能不是一个首选选项,所以有另一种方法是从 this blog post 。因为我们可以使用以下语法在控制台内联创建yaml文件 cat > ./myyamlfile.yml <<EOF ... EOF 我们也可以使用内联变量替换。

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

    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
    

    最后一步是使用 入口路线补丁。yml 归档为 patchesStrategicMerge 在我们的 kustomization.yaml 这样地:

    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