5

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@********** ~]#
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
shuwenouwan
  • 51
  • 1
  • 2

2 Answers2

2

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

See here for more details

jaxxstorm
  • 12,422
  • 5
  • 57
  • 67
  • 5
    I would like to access the service through the cluster ip from the inside, for example I want to access the node from the primary node through the cluster ip,curl 10.99.237.89:2379,it fails, is the way I access it wrong? what should I do? can you tell me an sample? – shuwenouwan Jan 18 '17 at 02:31
  • Access to services through the cluster ip, the need for the same Host? – shuwenouwan Jan 18 '17 at 10:23
0

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)