What's the recommended way to locate the apiserver from an openshift pod?

4/17/2018

From the Kubernetes docs (Accessing the API from a Pod):

The recommended way to locate the apiserver within the pod is with the kubernetes DNS name, which resolves to a Service IP which in turn will be routed to an apiserver.

However, this 'kubernetes' dns name does not appear to exist when I was in the shell of an OpenShift pod. I expected it to exist by default due the Kubernetes running underneath, but am I mistaken? This was using OpenShift Container Platform version 3.7.

Is there a standard way to access the apiserver short of passing it in as an environment variable or something?

-- csp713
kubernetes
openshift

1 Answer

4/17/2018

In OpenShift, you can use:

https://openshift.default.svc.cluster.local

You could also use the values from the environment variables:

KUBERNETES_SERVICE_PORT
KUBERNETES_SERVICE_HOST

as in:

#!/bin/sh

SERVER=`https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT`
TOKEN=`cat /var/run/secrets/kubernetes.io/serviceaccount/token`

URL="$SERVER/oapi/v1/users/~"

curl -k -H "Authorization: Bearer $TOKEN" $URL

Note that the default service account that containers are run as will not have REST API access. Best thing to do is to create a new service account in the project and grant that the rights to use the REST API endpoint for the actions it needs.

-- Graham Dumpleton
Source: StackOverflow