Kubernetes入口返回404

时间:2019-06-23 06:36:21

标签: kubernetes docker-for-windows nginx-ingress

我有在Windows的Docker中运行的Kubernetes。

我正在使用NGINX入口控制器,已经应用了mandatory.yamlcloud-generic.yaml

> kubectl get services -n ingress-nginx
NAME            TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
ingress-nginx   LoadBalancer   10.110.198.161   localhost     80:30037/TCP,443:31218/TCP   5h

我的本​​地注册表test-app/mock-service:1.0.0中有一个映像,该映像公开了运行具有单个端点GET /mocks的服务器的端口8080。使用docker run -d -p 8080:8080 test-app/mock-service:1.0.0在Docker中运行它可以正常工作,我可以看到预期的结果。

我的意图是使用Kubernetes部署它。

我创建了部署和服务资源:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-app-mock-service-deployment
  namespace: test-app
spec:
  selector:
    matchLabels:
      app: test-app-mock-service
  replicas: 1
  template:
    metadata:
      labels:
        app: test-app-mock-service
    spec:
      containers:
      - name: test-app-mock-service-container
        image: "test-app/mock-service:1.0.0"
---
apiVersion: v1
kind: Service
metadata:
  name: test-app-mock-service
  namespace: test-app
spec:
  type: ClusterIP
  selector:
    app: test-app-mock-service
  ports:
  - port: 8080
    targetPort: 8080
    protocol: TCP

还有一个入口资源:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-app-ingress
  namespace: test-app
  annotations:
    ingress.kubernetes.io/ssl-redirect: "true"
spec:
  rules:
  - http:
      paths:
      - path: /mocks
        backend:
          serviceName: test-app-mock-service
          servicePort: 8080

我可以看到该服务:

> kubectl get services -n test-app
NAME                    TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
test-app-mock-service   ClusterIP   10.108.10.246   <none>        8080/TCP   1h

还有吊舱:

> kubectl get pods -n test-app
NAME                                                READY     STATUS    RESTARTS   AGE
test-app-mock-service-deployment-6b9b58fcb9-mdskn   1/1       Running   0          59m

我可以看到入口:

> kubectl describe ingress test-app-ingress -n test-app
Name:             test-app-ingress
Namespace:        test-app
Address:          localhost
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host  Path  Backends
  ----  ----  --------
  *
        /mocks  test-app-mock-service:8080 (<none>)
Annotations:
  ingress.kubernetes.io/ssl-redirect:                true
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"ingress.kubernetes.io/ssl-redirect":"true"},"name":"test-app-ingress","namespace":"test-app"},"spec":{"rules":[{"http":{"paths":[{"backend":{"serviceName":"test-app-mock-service","servicePort":8080},"path":"/mocks"}]}}]}}

Events:
  Type    Reason  Age               From                      Message
  ----    ------  ----              ----                      -------
  Normal  CREATE  1h                nginx-ingress-controller  Ingress test-app/test-app-ingress
  Normal  UPDATE  53m (x2 over 1h)  nginx-ingress-controller  Ingress test-app/test-app-ingress

但是,当我浏览到http://localhost/mocks时,我得到了308到https://localhost/mocks,在那里我得到了404。

我不确定入口是否配置错误或是否是另一个问题。

1 个答案:

答案 0 :(得分:0)

可能发生的情况是SSL部分正在使用SNI,并且由于没有主机或默认的后端集,nginx不知道将请求发送到哪里。您可以通过两件事来解决此问题。

默认后端

您可以添加注释以为没有主机或路由的请求设置默认后端。有关此文档,请参见https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/#default-backend。在您的示例中为:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-app-ingress
  namespace: test-app
  annotations:
    ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/default-backend: test-app-mock-service
spec:
  rules:
  - http:
      paths:
      - path: /mocks
        backend:
          serviceName: test-app-mock-service
          servicePort: 8080

添加主机

您的规范不包含TLS主机名。添加一个应该使SNI知道指定的路由规则需要使用什么名称:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-app-ingress
  namespace: test-app
  annotations:
    ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/default-backend: test-app-mock-service
spec:
  tls:
  - hosts:
    - localhost
    secretName: tls-secret
  rules:
  - http:
      paths:
      - path: /mocks
        backend:
          serviceName: test-app-mock-service
          servicePort: 8080

但是,如果您使用的是自签名证书,则可能需要找到它的名称。您可能需要生成自己的。有关更多信息,请参见https://kubernetes.github.io/ingress-nginx/user-guide/tls/