Reputation: 787
I have several pods, for example a python web app and a redis(shared by other apps), so I need to place redis in a separate pod. But they are all use the same subnet from docker(172.17.0.0/16) or even the same ip address. how can app pods talk with redis pod?
Maybe what I want ask is what's the best way to setup multi-host conainer networking.
7 weeks later, I get more familiar with kubernetes. I know kubernetes has network assuming that pods can access each other. so if you app service pod need to access a shared service(redis) pod, you need expose the the shared service as a kubernetes service, then you can get the shared service endpoint from app pods' environment variables or hostname.
Upvotes: 2
Views: 2193
Reputation: 20808
I realize maybe the question was a bit vague:
if what you want is for your app to talk to the redis service, then set a service for redis (with a name of 'redis' for example) and then the redis service will be accessible simply by calling it by its hostname 'redis'
check the guestbook example that sets up a Redis master + replica slaves and get connected to the php app(s) that way
https://github.com/kubernetes/examples/tree/master/guestbook
Upvotes: 1
Reputation: 3558
Kubernetes provides a basic service discovery mechanism by providing DNS names to the kubernetes services (which are associated with pods). When a pod wants to talk to another pod, it should use the DNS name (e.g. svc1.namespace1.svc.cluster.local)
Upvotes: 0
Reputation: 20808
When you create a service, the service will proxy the connection to the different pods.
A service therefore maintains the list of IPs of the pods' containers.
You can then look those up in the API
they will be at
http(s)://${KUBERNETES_SERVICE_HOST}/api/v1/namespaces/${NAMESPACE}/endpoints/${SERVICE_NAME}
NAMESPACE is the name of the namespace. By default it is default, so if you didn't set a namespace in the pod replace with 'default' SERVICE_NAME is your service name KUBERNETES_SERVICE_HOST is an environment variable available in your container.
You will get a JSON object with containers and "ip" tags. You can then pipe the answer to a
grep '\"ip\"' | awk '{print $2}' | awk -F\" '{print $2}'
do isolate the IPs
You might also need credentials to reach the https (test it with curl) in Google Cloud, credentials can be found by looking up
gcloud cluster-info <your-cluster-name>
Note: even if you don't use the service to talk to your pods, it serves the purpose of gathering the IPs for your pods. However note that these may change if the pod get rescheduled somewhere else when a node fails,
the Service takes care of maintaining the up-to-date list, but your app needs to pull at intervals or set a watch on the endpoints to keep it's list up to date.
Upvotes: 2
Reputation: 13387
How did you set up Kubernetes? I'm not aware of any installation scripts that put pod IPs into a 172 subnet.
But in general, assuming Kubernetes has been set up properly (ideally using one of the provided scripts), using a service object to load balance across your 1 or more redis pods would be the standard approach.
Upvotes: 2