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!
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.
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>
apiVersion: v1
kind: Service
metadata:
name: postgres
labels:
app: postgres
spec:
type: NodePort
ports:
- port: 5432
targetPort: 5432
selector:
app: postgres