First of all it is worth mentioning that typically you won't connect to individual Pods
using their domain names. One good reason for that is their ephemeral nature. Note that typically you don't create plain Pods
but controller such as Deployment which manages your Pods
and ensures that specific number of Pods
of a certain kind is constantly up and running. Pods
may be often deleted and recreated hence you should never rely on their domain names in your applications. Typically you will expose them to another apps e.g. running in other Pods
via Service.
Although using invididual Pod
's domain name is not recommended, it is still possible. You can do it just for fun or learning/experimenting purposes.
As @David already mentioned you would help us much more in providing you a comprehensive answer if you EDIT your question and provide a few important details, showing what you've tried already such as your Pods
and Services
definitions in yaml
format.
Answering literally to your question posted in the title:
minikube how to connect from one pod to another using hostnames?
You won't be able to connect to a Pod
using simply its hostname. You can e.g. ping your backend Pods
exposed via ClusterIP Service
by simply pinging the <service-name>
(provided it is in the same namespace
as the Pod
your pinging from).
Keep in mind however that it doesn't work for Pods
- neither Pods
names nor their hostnames are resolvable by cluster DNS.
You should be able to connect to an individual Pod
using its fully quallified domain name (FQDN) provided you have configured everything properly. Just make sure you didn't overlook any of the steps described here:
Make sure you've created a simple Headless Service which may look like this:
apiVersion: v1
kind: Service
metadata:
name: default-subdomain
spec:
selector:
name: busybox
clusterIP: None
Make sure that your Pods
definitions didn't lack any important details:
apiVersion: v1
kind: Pod
metadata:
name: busybox1
labels:
name: busybox
spec:
hostname: busybox-1
subdomain: default-subdomain
containers:
- image: busybox:1.28
command:
- sleep
- "3600"
name: busybox
---
apiVersion: v1
kind: Pod
metadata:
name: busybox2
labels:
name: busybox
spec:
hostname: busybox-2
subdomain: default-subdomain
containers:
- image: busybox:1.28
command:
- sleep
- "3600"
name: busybox
Speaking about important details, pay special attention that you correctly defined hostname
and subdomain
in Pod
specification and that labels
used by Pods
match the labels used by Service
's selector
.
Once everything is configured properly you will be able to attach to Pod
busybox1 and ping
Pod
busybox2 by using its FQDN like in the example below:
$ kubectl exec -ti busybox1 -- /bin/sh
/ # ping busybox-2.default-subdomain.default.svc.cluster.local
PING busybox-2.default-subdomain.default.svc.cluster.local (10.16.0.109): 56 data bytes
64 bytes from 10.16.0.109: seq=0 ttl=64 time=0.051 ms
64 bytes from 10.16.0.109: seq=1 ttl=64 time=0.082 ms
64 bytes from 10.16.0.109: seq=2 ttl=64 time=0.081 ms
I hope this helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…