Targetting external REST API using openshift external service

6/15/2018

I would like my application hosted in an Openshift cluster to target an external REST API without hardcoding IP/PORT in the client application, and also to be able to change IP/PORT without redelivering the application.

I managed to do it through ConfigMap, but I saw it may also be possible to do through Service in OpenShift doc.

However I did not manage to understand how it is working. I did the following:

Creating a service

sylvain@HP:~$ oc export svc example-external-service
apiVersion: v1
kind: Service
metadata:
   creationTimestamp: null
   name: example-external-service
 spec:
   ports:
   - name: http
     port: 80
     protocol: TCP
     targetPort: 80
   sessionAffinity: None
   type: ClusterIP
 status:
   loadBalancer: {}

Creating an endpoint

sylvain@HP:~$ oc export endpoints example-external-service
apiVersion: v1
kind: Endpoints
metadata:
  creationTimestamp: null
  name: example-external-service
subsets:
- addresses:
  - ip: 216.58.198.195
  ports:
  - name: "80"
    port: 80
    protocol: TCP

Doing a curl to my service from the POD where my app is running

sylvain@HP:~$ oc get pods
NAME                     READY     STATUS    RESTARTS   AGE
nodejs-example-1-qnq46   1/1       Running   0          36m
sylvain@HP:~$ oc rsh nodejs-example-1-qnq46
sh-4.2$ env | grep "EXAMPLE_EXTERNAL"
EXAMPLE_EXTERNAL_SERVICE_SERVICE_PORT_HTTP=80
EXAMPLE_EXTERNAL_SERVICE_SERVICE_PORT=80
EXAMPLE_EXTERNAL_SERVICE_PORT_80_TCP_PORT=80
EXAMPLE_EXTERNAL_SERVICE_SERVICE_HOST=172.30.51.168
EXAMPLE_EXTERNAL_SERVICE_PORT_80_TCP_ADDR=172.30.51.168
EXAMPLE_EXTERNAL_SERVICE_PORT_80_TCP_PROTO=tcp
EXAMPLE_EXTERNAL_SERVICE_PORT=tcp://172.30.51.168:80
EXAMPLE_EXTERNAL_SERVICE_PORT_80_TCP=tcp://172.30.51.168:80
sh-4.2$ curl 172.30.51.168
curl: (7) Failed connect to 172.30.51.168:80; No route to host
sh-4.2$ curl 216.58.198.195
<HTML><HEAD><meta http-equiv="content-type" content="text/html;     charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>. 
</BODY></HTML>
sh-4.2$ 

In the first curl using the address defnined in the endpoint is working, however using the one in the environment variable:

EXAMPLE_EXTERNAL_SERVICE_SERVICE_HOST=172.30.51.168

It is failing, so the routing is not correcty done.

What am I doing wrong? What did I miss?

Cheers

-- scoulomb
docker
kubernetes
openshift
openshift-origin

1 Answer

6/16/2018

In your endpoint configuration, change the name of port 80 ("80") to http. It needs to be the same as in the service configuration.

-- Sabrina Jutz
Source: StackOverflow