简体   繁体   中英

Kubernetes Ingress gets Error while it works in minikube ssh

I am testing kubernetes with minikube on centOS 7, following the follow progress:

  1. Build 2 docker images running simple backend with express.js
  2. Running deployment to serve totally 4 pods, 2 of each image respectively
  3. Serving service with the pods
  4. Using ingress to access the services

When I gets into the minikube by minikube ssh , the APIs all work properly. However, outside of it, it always get error. So guess it must be error in 3 or 4? But it doesn't get connection error (as it once was before setup properly). Can it be security settings or what?


Settings & results as below:

  • deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: abc
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: abc
    spec:
      containers:
        - name: nginx
          image: image/abc
          imagePullPolicy: Never
          ports:
            - containerPort: 3000
  selector:
    matchLabels:
      app: abc
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: efg
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: efg
    spec:
      containers:
        - name: nginx
          image: image/efg
          imagePullPolicy: Never
          ports:
            - containerPort: 3000
  selector:
    matchLabels:
      app: efg
  • service.yaml
apiVersion: v1
kind: Service
metadata:
  name: abc-service
spec:
  type: NodePort
  selector:
    app: abc
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: efg-service
spec:
  type: NodePort
  selector:
    app: efg
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  • ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web
spec:
  rules:
    - host: my.host.com
      http:
        paths:
          - path: /abc
            backend:
              serviceName: abc-service
              servicePort: 80
          - path: /efg
            backend:
              serviceName: efg-service
              servicePort: 80
  • kubectl describe result:
Name:             web
Namespace:        default
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host         Path    Backends
  ----         ----    --------
  my.host.com
               /abc    abc-service:80 (172.18.0.4:3000,172.18.0.6:3000)
               /efg    efg-service:80 (172.18.0.5:3000,172.18.0.7:3000)
Annotations:   Events
  Type         Reason  Age               From                      Message
  ----         ------  ----              ----                      -------
  Normal       CREATE  24m               nginx-ingress-controller  Ingress default/web
  Normal       UPDATE  3s (x4 over 23m)  nginx-ingress-controller  Ingress default/web
  • minikube ip gets 172.17.0.4

  • /etc/hosts

172.17.0.4    my.host.com
  • Running curl 172.18.0.4:3000 gets the expected result (json) successfully
  • Running curl my.host.com/abc , it gets
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /abc</pre>
</body>
</html>

Finally my kubernetes works. Here to sum up the progress:

  1. As suggestion in comments above, add nginx.ingress.kubernetes.io/rewrite-target: / in annotations of ingress.yaml
  2. But then the new issue was that, every route maps samely to the root api of each service. Here's the solution: ingress routing api prefix issue

Finally my new ingress.yaml:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: web
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: my.host.com
      http:
        paths:
          - path: /abc/?(.*)
            backend:
              serviceName: abc-service
              servicePort: 80
          - path: /efg/?(.*)
            backend:
              serviceName: efg-service
              servicePort: 80

Then I can now access my APIs:

  • my.host.com/abc/ (one thing to highlight is that the final / is required here, otherwise it gets 404 Error)
  • my.host.com/abc/child
  • my.host.com/efg/
  • my.host.com/efg/child

May be somehow helpful for others also new to K8s now or after.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM