Question
I used to be able to curl
https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1beta3/namespaces/default/
as my base URL, but in kubernetes 0.18.0 it gives me "unauthorized". The
strange thing is that if I used the external IP address of the API machine
(http://172.17.8.101:8080/api/v1beta3/namespaces/default/
), it works just
fine.
Answer
In the official documentation I found this:
<https://kubernetes.io/docs/tasks/access-application-cluster/access- cluster/#accessing-the-api-from-a-pod>
Apparently I was missing a security token that I didn't need in a previous version of Kubernetes. From that, I devised what I think is a simpler solution than running a proxy or installing golang on my container. See this example that gets the information, from the api, for the current container:
KUBE_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
curl -sSk -H "Authorization: Bearer $KUBE_TOKEN" \
https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/default/pods/$HOSTNAME
I also use include a simple binary, jq (http://stedolan.github.io/jq/download/), to parse the json for use in bash scripts.