简体   繁体   中英

Ingress object doesn't redirect to service

I'm trying to setup an Ingress in my kubernetes cluster, with no success. I followed the instructions specified here . Basically I applied the following objects:

First, set the RBAC infrastructure:

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-ingress-controller
rules:
  - apiGroups:
      - ""
    resources:
      - services
      - endpoints
      - secrets
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - extensions
    resources:
      - ingresses
    verbs:
      - get
      - list
      - watch
  - apiGroups:
    - extensions
    resources:
    - ingresses/status
    verbs:
    - update
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: traefik-ingress-controller
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: traefik-ingress-controller
subjects:
- kind: ServiceAccount
  name: traefik-ingress-controller
  namespace: kube-system

**Versions**: <br>
Kubectl, kubeadm, kubelet: 1.21.00<br>
Traefik 1.17

Then, created a DaemonSet with the Pods running the Ingress Controller:

---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
---
kind: DaemonSet
apiVersion: apps/v1
metadata:
  name: traefik-ingress-controller
  namespace: kube-system
  labels:
    k8s-app: traefik-ingress-lb
spec:
  selector:
    matchLabels:
      k8s-app: traefik-ingress-lb
      name: traefik-ingress-lb
  template:
    metadata:
      labels:
        k8s-app: traefik-ingress-lb
        name: traefik-ingress-lb
    spec:
      serviceAccountName: traefik-ingress-controller
      terminationGracePeriodSeconds: 60
      containers:
      - image: traefik:v1.7
        name: traefik-ingress-lb
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: admin
          containerPort: 8080
          hostPort: 8080
        securityContext:
          capabilities:
            drop:
            - ALL
            add:
            - NET_BIND_SERVICE
        args:
        - --api
        - --kubernetes
        - --logLevel=INFO
---
kind: Service
apiVersion: v1
metadata:
  name: traefik-ingress-service
  namespace: kube-system
spec:
  selector:
    k8s-app: traefik-ingress-lb
  ports:
    - protocol: TCP
      port: 80
      name: web
    - protocol: TCP
      port: 8080
      name: admin

Now, I can access the Ingress web UI using port 8080 on one of my nodes, and I also know that port 80 directs traffic to the Ingress Controller, since when I run

$ curl localhost:80
404 page not found

The problem is, that when trying to create a Ingress object, for some reason, the Ingress Controller doesn't redirect it to the backend service. Created the following objects:

apiVersion: v1
kind: Service
metadata:
  name: svc-myserver
spec:
  type: ClusterIP
  selector: 
    app: my-server
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000

This works, when running:

$ curl 10.100.127.255
Hello world ! Version 1

Now, all there's to do is to create an Ingress that will forward traffic to the service:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-server-ingress
  namespace: kube-system
spec:
  rules:
  - host: myapp.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: svc-myserver
            port:
                   number: 80

Now when I run:

$ kubectl get ingress -A
NAMESPACE     NAME                CLASS    HOSTS                 ADDRESS   PORTS   AGE
kube-system   my-server-ingress   <none>   myapp.com                       80      13h

You can see that the ADDRESS field is empty, and indeed when I try to browse to this myapp.com ( Updated it in the /etc/hosts ) - I get 404.

What am I missing?

Versions
kubeadm, kubectl, kubelet - 1.21.00
traefik 1.17

Ok I understood what the problem was: My Ingress object wasn't in the same namespace as of the Service. When examining the logs of the Ingress Controller's Pod, I got:

time="2021-12-21T19:16:33Z" level=error msg="Service not found for kube-system/svc-myserver"
time="2021-12-21T19:16:33Z" level=error msg="Service not found for kube-system/traefik-web-ui"
time="2021-12-21T19:16:33Z" level=error msg="Service not found for kube-system/traefik-web-ui"
time="2021-12-21T19:16:33Z" level=error msg="Service not found for kube-system/svc-myserver"
time="2021-12-21T19:16:33Z" level=error msg="Service not found for kube-system/traefik-web-ui"

Once I moved it to the correct namespace, it worked like charm.

Thanks!

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