ACCESS CLUSTERIP FROM OUTSIDE CLUSTER:
Here is how to access ClusterIp service from outside cluster, by using 'kubectl port-forward...' command:
https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/
ACCESS CLUSTERIP FROM WITH IN CLUSTER:
And here is how you access it from with in cluster, by using the IP address which you can get with command 'kubectl get svc -n < ns_name >:
Create 2 API servers in same namespace using Flask in following manner, tried and tested on my Minikube. Host the 1st one in Kubernetes as a ClusterIp service. Host the 2nd one using NodePort to access outside Minikube cluster.
The 1st service is accessed using it's ClusterIp inside the 2nd service as shown in the Python code below, I tried to access using service name but did not work.
Here is 1st flask api server(.py file) which would be called from with in the 2nd flask api server:
import flask
from flask import Flask
import json
from flask import request
application = Flask(__name__)
@application.route('/api/v1/square', methods=['GET'])
def square():
# sample api call: http://0.0.0.0:8003/api/v1/square?nb1=11
nb1 = request.args.get('nb1')
sq_nb1 = int(nb1)*int(nb1)
return flask.jsonify(sq_nb1)
if __name__ == '__main__':
application.run(debug=True, host="0.0.0.0", port=8003)
2nd API server (.py file) which calls the 1st API server hosted as ClusterIP in Kubernetes
import flask
from flask import Flask
import json
from flask import request
import requests
application = Flask(__name__)
@application.route('/api/v1/sumsq', methods=['GET'])
def sumSq():
# sample api call: http://0.0.0.0:8002/api/v1/sumsq?nb1=111&nb2=222
nb1 = request.args.get('nb1')
nb2 = request.args.get('nb2')
sum_val = int(nb1) + int(nb2)
# call square micro-service (Svc: 3-Internal-API-Server)
api_url = "http://10.96.55.98/api/v1/square?nb1={v}".format(v=sum_val)
# get IP using 'kubectl get svc -n <ns_name>' command
res = requests.get(url=api_url)
sum_sq_val = res.json()
return flask.jsonify(sum_sq_val)
if __name__ == '__main__':
application.run(debug=True, host="0.0.0.0", port=8002)