Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Cross Namespace Ingress Network

I have a simple ingress network, I want to access services at different namespaces, from this ingress network.

How I can do this? My ingress network yaml file:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
 - host: api.myhost.com
 http:
 paths:
  - backend:
      serviceName: bookapi-2
      servicePort: 8080
    path: /booking-service/

I've set the ExternalNames service type to the yaml file:

 apiVersion: v1
 kind: Service
 metadata:
   name: bookapi-2
   namespace: booking-namespace
 spec:
   type: ExternalName
   externalName: bookapi-2
   ports:
     - name: app
     protocol: TCP
      port: 8080
      targetPort: 8080
   selector:
      app: bookapi-2
      tier: backend-2
like image 859
ColossusMark1 Avatar asked Aug 16 '18 13:08

ColossusMark1


People also ask

Can ingress access service in another namespace?

Now, Ingress Controller can be deployed in any namespace and is, in fact, usually deployed in a namespace separate from your app services.

Does ingress need to be in same namespace?

You can spread the Ingress configuration for a common host across multiple Ingress resources using Mergeable Ingress resources. Such resources can belong to the same or different namespaces.

Which namespace is the ingress controller deployed in?

The Kube-system namespace is used for objects created by the Kubernetes System itself. Basically the stuff you need to keep Kubernetes up and running. Additional services you add (such as monitoring with Azure Monitor, service meshes, ingress controllers) will be deployed into the Kube-system namespace as well.


2 Answers

An ExternalName service is a special case of service that does not have selectors and uses DNS names instead.

You can find out more about ExternalName service from the official Kubernetes documentation:

When you want to access a service from a different namespace, your yaml could, for example, look like this:

kind: Service
apiVersion: v1
metadata:
  name: test-service-1
  namespace: namespace-a
spec:
  type: ExternalName
  externalName: test-service-2.namespace-b.svc.cluster.local
  ports:
  - port: 80

As to your Ingress yaml file, please recheck it and make sure it is compliant with the official examples, for example this one as it contains some inconsistency:

apiVersion: extensions/v1beta1  
kind: Ingress  
metadata:  
  name: my-ingress  
spec:  
  rules:  
  - host: www.mysite.com  
    http:  
      paths:  
      - backend:  
          serviceName: website  
          servicePort: 80  
  - host: forums.mysite.com  
    http:  
      paths:  
      - path:  
        backend:  
          serviceName: forums  
          servicePort: 80

Please also recheck ExternalName yaml as it has TargetPorts and selectors which are not used in this type of Service and make sure that:

ExternalName Services are available only with kube-dns version 1.7 and later.

In case you will not succeed, please share the kind of problem you have meet.

like image 99
aurelius Avatar answered Sep 21 '22 10:09

aurelius


  1. create namespace service-ns
  2. create a service of type ClusterIP ( which is default ) named nginx-service listening on port 80 in namespace service-ns
  3. create nginx deployment in service-ns
  4. create namespace ingress-ns
  5. create a service in ingress-ns of type ExternalName and pointing to FQDN of nginx-service pointing it as nginx-internal.service-ns.svc.cluster.local
  6. create ingress rules

NOTE: Demo code not to be running in production. Just wanted to give an idea of how it would work cross-namespaces

---
#1
apiVersion: v1
kind: Namespace
metadata:
  name: service-ns
---
#2
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx-internal
  namespace: service-ns
spec:
  ports:
  - name: "80"
    port: 80
    targetPort: 80  
  selector:
    app: nginx
---
#3
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
  namespace: service-ns
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        image: nginx
        name: nginx
        ports:
        - containerPort: 80
      restartPolicy: Always
---
#4
apiVersion: v1
kind: Namespace
metadata:
  name: ingress-ns
---
#5
kind: Service
apiVersion: v1
metadata:
  name: nginx
  namespace: ingress-ns
spec:
  type: ExternalName
  externalName: nginx-internal.service-ns.svc.cluster.local
  ports:
  - port: 80
---
#6
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: main-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
  namespace: ingress-ns    
spec:
  rules:
    - host: whatever.domain.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: nginx
                port:
                  number: 80
like image 39
Rohit Salecha Avatar answered Sep 17 '22 10:09

Rohit Salecha