1
votes

kubernetes has DNS resolution for service or pod. For example, if I log into a pod, I can access the services or pods via DNS name, like mysvc.default.svc.

But how can I access them by DNS directly from Kubernetes host?

Currently, I have to get the IP address in order to access the service, which is inconvenient.

Thanks!

2
I dont think you can, These DNSs are visible only in the cluster. If you want host to be able to resolve that, you need to point to same DNS server - Ankit Bansal

2 Answers

1
votes

You can not use kubernetes cluster DNS (CoreDNS) from outside kubernetes i.e from the host machine. You should expose the pod via ingress or loadbalancer or NodePort type service and configure an external DNS provider to resolve a hostname to the IP.

1
votes

You not be able use CoreDNS from outside kubernetes. You should expose the pod via Ingress or LoadBalancer or NodePort Type service and configure an external DNS provider to resolve a hostname to the IP as Arghya Sadhu answers.

Other approach use external IP to your services and communicate with your Public IP directly.

Here some example :

  1. External IP
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: ClusterIP
  ports:
   - port: 5432
     targetPort: 5432
  selector:
   app: postgres
  externalIPs:
    - <YOUR_PUBLIC_IP_ADDRESS>
  1. NodePort
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
     targetPort: 5432
  selector:
   app: postgres