How to access services through kubernetes cluster ip?

1/15/2017

I built a kubernetes cluster through kubeadm and created several services. These services can be accessed externally through the node ip: port, but when I try to access the service through cluster ip, it fails, it displays the error message curl: (7) Failed connect to 10.99.237.89:2379; Connection timed out, this How to solve the problem?

Cluster-related information

enter image description here enter image description here

[root@********** ~]# kubectl describe svc etcd-torus-internal --namespace=default
Name:                   etcd-torus-internal
Namespace:              default
Labels:                 name=etcd-torus-internal
Selector:               name=etcd-torus
Type:                   ClusterIP
IP:                     10.99.237.89
Port:                   etcd-client     2379/TCP
Endpoints:              10.244.1.10:2379
Session Affinity:       None
No events.
[root@********** ~]# curl 10.99.237.89:2379
curl: (7) Failed connect to 10.99.237.89:2379; Connection timed out
[root@********** ~]#
-- shuwenouwan
kubernetes

2 Answers

4/11/2020

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)
-- Anurag Trivedi
Source: StackOverflow

1/16/2017

Cluster IPs are not accessible from outside the cluster, they are internal only.

See here for more details

-- jaxxstorm
Source: StackOverflow