Accessing Clusters

    When accessing the Kubernetes API for the first time, we suggest using the Kubernetes CLI, .

    To access a cluster, you need to know the location of the cluster and have credentials to access it. Typically, this is automatically set-up when you work through a Getting started guide, or someone else setup the cluster and provided you with credentials and a location.

    Check the location and credentials that kubectl knows about with this command:

    Many of the provide an introduction to using kubectl and complete documentation is found in the kubectl manual.

    Directly accessing the REST API

    Kubectl handles locating and authenticating to the apiserver. If you want to directly access the REST API with an http client like curl or wget, or a browser, there are several ways to locate and authenticate:

    • Run kubectl in proxy mode.
      • Recommended approach.
      • Uses stored apiserver location.
      • Verifies identity of apiserver using self-signed cert. No MITM possible.
      • Authenticates to apiserver.
      • In future, may do intelligent client-side load-balancing and failover.
    • Provide the location and credentials directly to the http client.
      • Alternate approach.
      • Works with some types of client code that are confused by using a proxy.
      • Need to import a root cert into your browser to protect against MITM.

    The following command runs kubectl in a mode where it acts as a reverse proxy. It handles locating the apiserver and authenticating. Run it like this:

    1. kubectl proxy --port=8080

    See kubectl proxy for more details.

    Then you can explore the API with curl, wget, or a browser, replacing localhost with [::1] for IPv6, like so:

    The output is similar to this:

    1. {
    2. "kind": "APIVersions",
    3. "versions": [
    4. "v1"
    5. ],
    6. "serverAddressByClientCIDRs": [
    7. {
    8. "serverAddress": "10.0.1.149:443"
    9. }
    10. ]
    11. }

    Use kubectl describe secret... to get the token for the default service account with grep/cut:

    The output is similar to this:

    1. {
    2. "kind": "APIVersions",
    3. "v1"
    4. ],
    5. "serverAddressByClientCIDRs": [
    6. {
    7. "clientCIDR": "0.0.0.0/0",
    8. "serverAddress": "10.0.1.149:443"
    9. }
    10. ]
    11. }

    The output is similar to this:

    1. {
    2. "kind": "APIVersions",
    3. "versions": [
    4. "v1"
    5. ],
    6. "serverAddressByClientCIDRs": [
    7. "clientCIDR": "0.0.0.0/0",
    8. "serverAddress": "10.0.1.149:443"
    9. }
    10. ]
    11. }

    The above examples use the --insecure flag. This leaves it subject to MITM attacks. When kubectl accesses the cluster it uses a stored root certificate and client certificates to access the server. (These are installed in the ~/.kube directory). Since cluster certificates are typically self-signed, it may take special configuration to get your http client to use root certificate.

    On some clusters, the apiserver does not require authentication; it may serve on localhost, or be protected by a firewall. There is not a standard for this. describes how a cluster admin can configure this.

    Kubernetes officially supports Go and client libraries.

    • To get the library, run the following command: go get k8s.io/client-go@kubernetes-<kubernetes-version-number>, see INSTALL.md for detailed installation instructions. See to see which versions are supported.
    • Write an application atop of the client-go clients. Note that client-go defines its own API objects, so if needed, please import API definitions from client-go rather than from the main repository, e.g., import "k8s.io/client-go/kubernetes" is correct.

    The Go client can use the same kubeconfig file as the kubectl CLI does to locate and authenticate to the apiserver. See this .

    If the application is deployed as a Pod in the cluster, please refer to the next section.

    To use , run the following command: pip install kubernetes. See Python Client Library page for more installation options.

    The Python client can use the same as the kubectl CLI does to locate and authenticate to the apiserver. See this example.

    There are for accessing the API from other languages. See documentation for other libraries for how they authenticate.

    Accessing the API from a Pod

    When accessing the API from a pod, locating and authenticating to the apiserver are somewhat different.

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

    If available, a certificate bundle is placed into the filesystem tree of each container at /var/run/secrets/kubernetes.io/serviceaccount/ca.crt, and should be used to verify the serving certificate of the apiserver.

    Finally, the default namespace to be used for namespaced API operations is placed in a file at /var/run/secrets/kubernetes.io/serviceaccount/namespace in each container.

    From within a pod the recommended ways to connect to API are:

    • Use the Go client library, and create a client using the rest.InClusterConfig() and functions. They handle locating and authenticating to the apiserver.

    In each case, the credentials of the pod are used to communicate securely with the apiserver.

    The previous section describes how to connect to the Kubernetes API server. For information about connecting to other services running on a Kubernetes cluster, see Access Cluster Services.

    Requesting redirects

    The redirect capabilities have been deprecated and removed. Please use a proxy (see below) instead.

    There are several different proxies you may encounter when using Kubernetes:

    1. The kubectl proxy:

      • runs on a user’s desktop or in a pod
      • proxies from a localhost address to the Kubernetes apiserver
      • client to proxy uses HTTP
      • proxy to apiserver uses HTTPS
      • locates apiserver
      • adds authentication headers
    2. The :

      • is a bastion built into the apiserver
      • connects a user outside of the cluster to cluster IPs which otherwise might not be reachable
      • runs in the apiserver processes
      • client to proxy uses HTTPS (or http if apiserver so configured)
      • proxy to target may use HTTP or HTTPS as chosen by proxy using available information
      • can be used to reach a Node, Pod, or Service
      • does load balancing when used to reach a Service
    3. The kube proxy:

      • runs on each node
      • proxies UDP and TCP
      • does not understand HTTP
      • provides load balancing
      • is only used to reach services
    4. A Proxy/Load-balancer in front of apiserver(s):

      • existence and implementation varies from cluster to cluster (e.g. nginx)
      • sits between all clients and one or more apiservers
      • acts as load balancer if there are several apiservers.
      • are provided by some cloud providers (e.g. AWS ELB, Google Cloud Load Balancer)
      • are created automatically when the Kubernetes service has type LoadBalancer
      • use UDP/TCP only

    Kubernetes users will typically not need to worry about anything other than the first two types. The cluster admin will typically ensure that the latter types are setup correctly.