Kubernetes 入口不工作,默认后端 404

Kul*_*esu 3 kubernetes google-kubernetes-engine nginx-ingress

我是 Kubernetes 新手。我正在使用 K8S 的 GKE 托管服务。有 2 个部署 nginx、httpd,并为这 2 个部署创建了 NodePort 服务。

我正在尝试为服务创建入口规则。nginx入口控制器是通过helm安装的。我有一个来自 freenom 的域,并将 Google 云 DNS 设置为使用静态公共 IP。当我尝试访问入口 URL(域/nginx)时,它给出:

“默认后端 - 404”


部署:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
  replicas: 1
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: httpd
  labels:
    name: httpd
spec:
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
        - name: httpd
          image: httpd
  replicas: 1
Run Code Online (Sandbox Code Playgroud)

服务:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: nginx
  name: nginx-service
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx
  type: NodePort
Run Code Online (Sandbox Code Playgroud)

与 httpd 服务相同

入口:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    kubernetes.io/ingress.global-static-ip-name: testingk8s
spec:
  rules:
    - host: xyz.tk
      http: 
        paths:
          - path: /nginx
            backend:
              serviceName: nginx-service
              servicePort: 80
          - path: /httpd
            backend:
              serviceName: httpd-service
              servicePort: 80
Run Code Online (Sandbox Code Playgroud)

入口描述:

Default backend:  default-http-backend:80 (10.48.0.7:8080)
Rules:
  Host           Path  Backends
  ----           ----  --------
  xyz.tk
                 /nginx   nginx-service:80 (10.48.0.25:80)
                 /httpd   httpd-service:80 (10.48.0.26:80)
Annotations:     ingress.kubernetes.io/backends:
                   {"k8s-be-30916--413d33a91e61ca5d":"HEALTHY","k8s-be-31376--413d33a91e61ca5d":"HEALTHY","k8s-be-32702--413d33a91e61ca5d":"HEALTHY"}

Run Code Online (Sandbox Code Playgroud)

入口控制器 Pod 日志:

I0812 09:38:34.405188       6 event.go:278] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"nginx", Name:"test", UID:"61991dbd-a361-47d2-88cc-548a7c43e743", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"316030", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress nginx/test
I0812 09:38:34.405815       6 controller.go:139] Configuration changes detected, backend reload required.
I0812 09:38:34.532163       6 controller.go:155] Backend successfully reloaded.
I0812 09:38:41.369315       6 status.go:275] updating Ingress nginx/test status from [] to [{35.192.136.218 }]
I0812 09:38:41.374080       6 event.go:278] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"nginx", Name:"test", UID:"61991dbd-a361-47d2-88cc-548a7c43e743", APIVersion:"networking.k8s.io/v1beta1", ResourceVersion:"316057", FieldPath:""}): type: 'Normal' reason: 'UPDATE' Ingress nginx/test

Run Code Online (Sandbox Code Playgroud)

Arg*_*dhu 5

添加注释kubernetes.io/ingress.class: nginxnginx.ingress.kubernetes.io/rewrite-target: /. 所以入口如下所示

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: test
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: xyz.tk
      http: 
        paths:
          - path: /nginx
            backend:
              serviceName: nginx-service
              servicePort: 80
          - path: /httpd
            backend:
              serviceName: httpd-service
              servicePort: 80
Run Code Online (Sandbox Code Playgroud)