How do i configure kubectl for aws eks?



  • To install kubectl on linux also see: https://www.lightnetics.com/post/10389
    To install the Heptio AWS authenticator: https://www.lightnetics.com/post/10391
    To create a eks cluster using the cli: https://www.lightnetics.com/post/10410

    Kubernetes uses the kubectl tool to communicate with the Kubernetes API Server.

    The kubectl configuration needs certain information to authenticate with the API Server. The configuration is kept under .kube of the use home directory.

    For the demo various things need to be obtained from our previously created eks cluster.

    • The endpoint URL for the eks cluster.
    • The certificate authority data, base64 encoded.
    • The cluster name/cluster ID.
    • Optionally the role and the AWS environment profile

    To obtain each one of these non-optional values run the following commands.

    Endpoint URL

    $ aws eks describe-cluster --name trainingeks --query cluster.endpoint
    

    Certificate authority base64 encoded.

    $ aws eks describe-cluster --name trainingeks  --query cluster.certificateAuthority.data
    

    Cluster ID/name is the name you used for the aws eks cluster.

    An example taken from the AWS website getting started documentation, substitute the values obtained into the configuration file: $HOME/.kube/trainingeks.config, Create the .kube directory if this a new kubernetes environment.

    apiVersion: v1
    clusters:
    - cluster:
        server: <endpoint-url>
        certificate-authority-data: <base64-encoded-ca-cert>
      name: kubernetes
    contexts:
    - context:
        cluster: kubernetes
        user: aws
      name: aws
    current-context: aws
    kind: Config
    preferences: {}
    users:
    - name: aws
      user:
        exec:
          apiVersion: client.authentication.k8s.io/v1alpha1
          command: aws-iam-authenticator
          args:
            - "token"
            - "-i"
            - "<cluster-name>"
            # - "-r"
            # - "<role-arn>"
          # env:
            # - name: AWS_PROFILE
            #   value: "<aws-profile>"
    

    Point kubectl to the configuration file. You can add it to your .bashrc for convenience.

    $ export KUBECONFIG=$KUBECONFIG:~/.kube/trainingeks.config
    

    Test you can communicate with the eks cluster.

    $ kubectl get svc                                                   
    NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
    kubernetes   ClusterIP   10.100.0.1   <none>        443/TCP   1d
    

Log in to reply
 

© Lightnetics 2024