Home > database >  How to use an ExternalName service to access an internal service that is exposed with ingress
How to use an ExternalName service to access an internal service that is exposed with ingress

Time:12-16

I am trying out a possible kubernetes scenario in the local machine minikube cluster. It is to access an internal service that is exposed with ingress in one cluster from another cluster using an ExternalName service. I understand that using an ingress the service will already be accessible within the cluster. As I am trying this out locally using minikube, I am unable to use simultaneously running clusters. Since I just wanted to verify whether it is possible to access an ingress exposed service using ExternName service.

I started the minikube tunnel using minikube tunnel.

I can access the service using http://k8s-yaml-hello.info.

But when I tryout curl k8s-yaml-hello-internal within a running POD, the error that I that is curl: (7) Failed to connect to k8s-yaml-hello-internal port 80 after 1161 ms: Connection refused

Can anyone point me out the issue here? Thanks in advance.

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: k8s-yaml-hello
spec:
  selector:
    app: k8s-yaml-hello
  ports:
  - port: 3000
    targetPort: 3000

ingress.yaml

kind: Ingress
metadata:
  name: k8s-yaml-hello-ingress
  labels:
    name: k8s-yaml-hello-ingress
spec:
  rules:
  - host: k8s-yaml-hello.info
    http:
      paths:
      - pathType: Prefix
        path: "/"
        backend:
          service:
            name: k8s-yaml-hello
            port: 
              number: 3000

externalName.yaml

apiVersion: v1
kind: Service
metadata:
  name: k8s-yaml-hello-internal
spec:
  ports:
  - name: ''
    appProtocol: http
    protocol: TCP
    port: 3000
  type: ExternalName
  externalName: k8s-yaml-hello.info

etc/hosts

    127.0.0.1   k8s-yaml-hello.info

CodePudding user response:

As You are getting the error curl: (7) Failed to connect :

The above error message means that no web-server is running on the specified IP and Port and the specified (or implied) port.

Check using nano /etc/hosts whether the IP and port is pointing to the correct domain or not. If it's not pointing, provide the correct IP and Port.

Refer to this SO for more information.

In Ingress.Yaml use Port 80 and also in service.yaml port should be 80. The service port and Target port should be different As per your yaml it is the same. Change it to 80 and have a try , If you get any errors, post here.

CodePudding user response:

The problem is that minikube tunnel by default binds to the localhost address 127.0.0.1. Every node, machine, vm, container etc. has its own and the same localhost address. It is to reach local services without having to know the ip address of the network interface (the service is running on "myself"). So when k8s-yaml-hello.info resolves to 127.0.0.1 then it points to different service depending on which container you are (just to myself).

To make it work like you want, you first have to find out the ip address of your hosts network interface e.g. with ifconfig. Its name is something like eth0 or en0, depending on your system.

Then you can use the bind-address option of minikube tunnel to bind to that address instead:

minikube tunnel --bind-address=192.168.1.10

With this your service should be reachable from within the container. Please check first with the ip address:

curl http://192.168.1.10

Then make sure name resolution with /etc/hosts works in your container with dig, nslookup, getent hosts or something similar that is available in your container.

  • Related