Access to metric-server from a pod

10/31/2019

I have deployed the metric-server on my kubernetes cluster and it's just working fine as I run the following command:

kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes

I want to access the metric-server from a pod. For that, I use the service/metric-server's IP and it is in the same namespace that the metric-server is. The way I'm trying to access to the metric is like this:

    myurl := fmt.Sprintf("https://%s:%s/apis/metrics.k8s.io/v1beta1/nodes/", serviceHost, servicePort)
    u, err := url.Parse(myurl)
    if err != nil {
        panic(err)
    }
    req, err := http.NewRequest(httpMethod, u.String(), nil)
    if err != nil {
        log.Printf("Cant sned req: %s", err)
    }
    caToken, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/token")
    if err != nil {
        panic(err) // cannot find token file
    }

    req.Header.Set("Content-Type", "application/json")
    req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", string(caToken)))

    caCertPool := x509.NewCertPool()
    caCert, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/ca.crt")
    if err != nil {
        panic(err)
    }
    caCertPool.AppendCertsFromPEM(caCert)

    client := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                RootCAs: caCertPool,
            },
        },
    }

    resp, err := client.Do(req)
    if err != nil {
        log.Printf("sending helm deploy payload failed: %s", err.Error())
        panic(err)
    }

This is not working nad the logs result for the pod is:

Get https://METRIC-SERVER-SERVICE-IP/apis/metrics.k8s.io/v1beta1/nodes: x509: certificate is valid for 127.0.0.1, not METRIC-SERVER-SERVICE-IP

Is this the right way to access the metric-server from a pod?

-- mR.aTA
kubernetes

1 Answer

11/1/2019

what i did so i can access the metrics-server on a local deployment:

  1. set proper rbac
  2. inside the pod : export CURL_CA_BUNDLE=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt curl -H "Authorization: Bearer $TOKEN" -k https://10.100.123.57/apis/metrics.k8s.io/v1beta1/nodes
-- iliefa
Source: StackOverflow