Kubernetes Ingress and Services 故障排查

流量进入:Internet → Ingress 控制器规则(根据你的 Ingress YAML)→ Service → Pods

调试流程:Pods → Service → Ingress → Ingress controller → Internet

检查 Deployment & Pods

  1. 确保 Pod 已启动并运行(Pod 的“Status”为“Running”)。如果没有,请检查Deployment/Pod 资源事件和log日志以解决问题。
  2. 如果您使用的是 HTTP GET livenessProbe,请确保您的 Service 和 Ingress 已预先部署。
$ kubectl get deployment -n # logs of deployment
$ kubectl logs deployment/ -n # follow logs of deployment, ctrl+c to quit
$ kubectl logs -f deployment/ -n # check "Events" at bottom of the output
$ kubectl describe deployment  -n 
$ kubectl get pods -n $ kubectl logs pod/ -n $ kubectl describe pod  -o wide -n 

检查端口映射

Kubernetes Ingress and Services 故障排查
image.png

Ingress

Ingress 可以配置为提供服务外部可访问的 URL、负载平衡流量、终止 SSL / TLS 并提供基于名称的虚拟主机。Ingress 控制器负责实现 Ingress,通常使用负载均衡器,但它也可以配置边缘路由器或其他前端来帮助处理流量。

Ingress 不会暴露任意端口或协议。向 Internet 公开 HTTP 和 HTTPS 以外的服务通常使用 Service.Type=NodePort 或 Service.Type=LoadBalancer 类型的服务。

检查  Service

  1. 检查 Service 中的“Endpoints”字段,该字段应与 Pod 的“IP”匹配。
  2. 如果您使用 GKE、AKS…等公共云,您可以将 Service Types 修改为 LoadBalancer 从而不经过ingress对外暴漏Service 。如果能公网成功访问,这意味着您的 Pod 和服务正常工作,那么问题是由其他人造成的。
$ kubectl get service -n $ kubectl describe service  -n 

检查 the Ingress & Ingress Controller 日志和资源时间

Ingress Controllers

为了使 Ingress 资源正常工作,集群必须运行一个入口控制器。

与作为 kube-controller-manager 二进制文件的一部分运行的其他类型的控制器不同,Ingress 控制器不会通过 GKE、AKS 等公共云中的集群自动启动。您需要选择最适合您的集群的入口控制器。

AKS 中的 Ingress Controller 是 AKS Application Gateway Ingress Controller,下面是 ingress-appgw-deployment。入口控制器作为 AKS 群集中的 pod 运行。它使用 kubernetes Ingress 资源并将它们转换为 Azure 应用程序网关配置,该配置允许网关对 Kubernetes pod 的流量进行负载平衡。

  1. 检查您的 Ingress 是否有任何事件或错误日志
$ kubectl get ingress -n <namespace>
$ kubectl describe ingress <name-of-ingress> -n <namespace>
  1. 检查您的Ingress Controller 配置,看看它的规则是否与您刚刚应用的入口匹配。
λ kubectl get deployment  -n kube-system
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
ingress-appgw-deployment   1/1     1            1           48d
...
$ kubectl get pods -n kube-system
NAME                                        READY   STATUS    RESTARTS   AGE
azure-cni-networkmonitor-2fmfk              1/1     Running   0          7d5h
...
azure-ip-masq-agent-6k4rm                   1/1     Running   0          3d5h
...
coredns-84d976c568-pjt8q                    1/1     Running   1          86d
...
ingress-appgw-deployment-7b8b687b46-scvs7   1/1     Running   315        3d5h
kube-proxy-4c6qw                            1/1     Running   0          7d5h
...
metrics-server-569f6547dd-j2wjz             1/1     Running   5          86d
...
$ kubectl describe pod ingress-appgw-deployment-7b8b687b46-scvs7 -n kube-system

最后找出问题是由 Ingress 控制器在更新 Ingress YAML 或 Pod 服务端点(包括 livenessProbe)时无法将 Ingress YAML 转换为 Azure 应用程序网关配置引起的。

$ kubectl logs -n kube-system ingress-appgw-deployment-7b8b687b46-scvs7 | grep --color=always -i error
E0110 03:19:02.123462       1 requestroutingrules.go:386] A path-rule with path '/merchant/*' already exists in config for BackendPool '/subscriptions/49fc9d19-f517-4ca5-a93e-76ed0fbd0ab1/resourceGroups/xxx/providers/Microsoft.Network/applicationGateways/aks-gw/backendAddressPools/defaultaddresspool'. Duplicate path-rule with BackendPool '/subscriptions/49fc9d19-f517-4ca5-a93e-76ed0fbd0ab1/resourceGroups/xxx/providers/Microsoft.Network/applicationGateways/aks-gw/backendAddressPools/pool-payment-payment-svc-80-bp-80' will not be applied

重启 Ingress Controller

$ kubectl rollout restart deployment <your-ingress-controller-deployment>

Application Gateway 失败原因

可能导致 Application Gateway 无法监控和应用 Ingress 配置的原因。

  1. 使用没有包含 TLS 私钥和证书的 Secret 的 HTTPS。如果您使用 let’s encrypt 自动生成 tls.crt 和 tls.key 类型为 kubernates.io/tls 的机密文件,请确保它没有被您的 AKS 防火墙阻止,这将导致错误类型的 Secret(Opaque )。
apiVersion: v1
kind: Secret
metadata:
  name: testsecret-tls
  namespacedefault
data:
  tls.crt: base64 encoded cert
  tls.key: base64 encoded key
type: kubernetes.io/tls
  1. 重定向问题

Ingress gives 502 error注意到应用程序必须在“/”处返回 200 状态码。如果您的应用程序返回 302(重定向到登录),这将导致运行状况失败。当健康检查失败时,入口资源返回502。

  1. 后端路径规则冲突。

如果您在 Ingress 中指定 backend-path-prefix,请确保它不会与您的后端资源 Deployment livenessProbe 路径前缀冲突。

Deployment

...       
        livenessProbe:
          httpGet:
            path: /payment_resource/healthcheck.jsp
            port8080
          initialDelaySeconds180
          periodSeconds10
          timeoutSeconds3
          failureThreshold3
          successThreshold1

Ingress

kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
  name: ingress-payment
  namespace: payment
  annotations:
    appgw.ingress.kubernetes.io/backend-path-prefix: /payment_resource/
    appgw.ingress.kubernetes.io/connection-draining: 'true'
    appgw.ingress.kubernetes.io/connection-draining-timeout: '30'
    appgw.ingress.kubernetes.io/cookie-based-affinity: 'true'
    appgw.ingress.kubernetes.io/ssl-redirect: 'true'
    cert-manager.io/cluster-issuer: letsencrypt-production
    kubernetes.io/ingress.allow-http: 'false'
    kubernetes.io/ingress.class: azure/application-gateway
spec:
  tls:
    - hosts:
        - xxx.xxx.xxx.azure.com
      secretName: aks-ingress-cert
  rules:
    - host: xxx.xxx.xxx.azure.com
      http:
        paths:
          - path: /payment/*
            pathType: ImplementationSpecific
            backend:
              service:
                name: payment-svc
                port:
                  number: 80

访问 https://xxx.xxx.xxx.azure.com/payment/healthcheck.jsp ,它将重定向到后端资源端点https://xxx.xxx.xxx.azure.com/payment_resource/healthcheck.jsp 。


原文始发于微信公众号(程序员石磊):Kubernetes Ingress and Services 故障排查

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/251464.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!