I have two services (client and server) both of types of load balancer. I would like to make the client service talking to server service. How do I do programmatically (via yaml file etc).
More explicitly client runs a python code in which it needs access to server's external ip. However I only know server service name not external ip.
of course one way to solve this issue is to launch server first and get the external ip and add it to client's yaml file. I am looking for solution like accessing external ip by just knowing the service name.
Client and server resides in the same kubernetes cluster.
Client.yaml Client runs a web server and if someone hits the url advertised by web server demo.py
should make a call to the server's service. I was thinking of a way to get the server's external-ip
programmatically here, however I am open for suggestions.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name:client-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: client
spec:
containers:
- name: client-container
image: client_image:latest
command:
- "python"
- "/root/demo.py"
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
labels:
run: client-service
name: client-service
spec:
ports:
- port: 5000
targetPort: 5000
selector:
app: inception-client
type: LoadBalancer
Server.yaml: It exposes a service at port 9000, which client should call.
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: server-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: server
spec:
containers:
- name: server-container
image: server_image:latest
command:
- <Command to start a server which listens on port 9000>
ports:
- containerPort: 9000
---
apiVersion: v1
kind: Service
metadata:
labels:
run: server-service
name: server-service
spec:
ports:
- port: 9000
targetPort: 9000
selector:
app: server
type: LoadBalancer
As you have not specified on where the servers reside, lets consider both cases.
Case 1: Services are within the same cluster
Use service name in place of host ip address. kube-proxy converts the service name to ip address(internal).
Case 2: Services are not in the same cluster.
Establishing a connection between them requires an external IP address. One alternative approach is to write a bash script. Write a bash script to get the ip address programatically and store it in a variable. Substitute this variable within the server yaml file.
The client only needs to know the kubernetes Service name linked to the server. Kubernetes will handle the routing to the desired server pod. Specifying a load balancer also implicitly creates a clusterip. Which your client python application can use.
You can provide in your client code a readiness probe that checks if the server is up, that way you can be sure that when the server is ready, the client can also start taking requests.