Traefik IngressRoute规则解析报错:非法字符U+005C求助
Traefik IngressRoute规则解析错误解决
问题现象
配置Traefik Dashboard的IngressRoute和BasicAuth中间件后,Traefik Pod抛出规则解析错误:
time="2022-11-05T11:45:52Z" level=error msg="error while parsing rule Host(\`traefik.domain.com\`) && (PathPrefix(\`/api\`) || PathPrefix(\`/dashboard\`)): 1:6: illegal character U+005C '\' (and 2 more errors)" entryPointName=websecure routerName=traefik-traefik-dashboard-1cd33424d5fb33e10e2e@kubernetescrd time="2022-11-05T11:45:52Z" level=error msg="invalid rule Host(\`traefik.domain.com\`) && (PathPrefix(\`/api\`) || PathPrefix(\`/dashboard\`)), error: 1:6: illegal character U+005C '\' (and 2 more errors)" routerName=traefik-traefik-dashboard-1cd33424d5fb33e10e2e@kubernetescrd entryPointName=websecure
当前使用的YAML配置如下:
apiVersion: traefik.containo.us/v1alpha1 kind: IngressRoute metadata: name: traefik-dashboard namespace: traefik spec: entryPoints: - websecure routes: - kind: Rule match: Host(\`traefik.domain.com\`) && (PathPrefix(\`/api\`) || PathPrefix(\`/dashboard\`)) services: - name: api@internal kind: TraefikService middlewares: - name: traefik-dashboard-auth # Referencing the BasicAuth middleware namespace: traefik tls: secretName: domain-com-cert-secret --- apiVersion: traefik.containo.us/v1alpha1 kind: Middleware metadata: name: traefik-dashboard-auth namespace: traefik spec: basicAuth: secret: traefik-dashboard-auth-secret
问题原因
错误根源是YAML配置中match字段里的转义符\完全多余,Traefik规则语法不需要对反引号做转义处理,YAML解析时会将\识别为非法字符。
解决方法
修正match字段的规则写法,两种可行方式任选其一:
方式一:直接移除转义符
match: Host(`traefik.domain.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))
方式二:用单引号包裹整个规则(避免YAML解析歧义,更稳妥)
match: 'Host(`traefik.domain.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))'
修正后的完整IngressRoute配置如下:
apiVersion: traefik.containo.us/v1alpha1 kind: IngressRoute metadata: name: traefik-dashboard namespace: traefik spec: entryPoints: - websecure routes: - kind: Rule match: 'Host(`traefik.domain.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))' services: - name: api@internal kind: TraefikService middlewares: - name: traefik-dashboard-auth namespace: traefik tls: secretName: domain-com-cert-secret
应用修正后的配置即可解决规则解析错误。
内容的提问来源于stack exchange,提问作者Ross
相关产品推荐
相关产品推荐

