Create a kubeconfig
for
Amazon EKS
In this section, you create a kubeconfig
file for your cluster (or
update an existing one).
This section offers two procedures to create or update your
file. You can quickly create or update a
kubeconfig
kubeconfig
file with the AWS CLI update-kubeconfig
command
automatically by using the AWS CLI, or you can create a kubeconfig
file manually
using the AWS CLI or the aws-iam-authenticator
.
Amazon EKS uses the aws eks get-token
command, available in version
1.16.156
or later of the AWS CLI or the AWS IAM Authenticator
for Kuberneteskubectl
for cluster authentication. If you have
installed the AWS CLI on your system, then by default the AWS IAM Authenticator for
Kubernetes uses the same credentials that are returned with the following command:
aws sts get-caller-identity
For more information, see Configuring the AWS CLI in the AWS Command Line Interface User Guide.
Create kubeconfig
file
automatically
To create your kubeconfig
file with the AWS CLI
-
Make sure that you have version
1.25.46
or2.7.21
or later installed. To install or upgrade the AWS CLI, see Installing the AWS CLI in the AWS Command Line Interface User Guide.Note Your system's Python version must be
2.7.9
or later. Otherwise, you receivehostname doesn't match
errors with AWS CLI calls to Amazon EKS.You can check your AWS CLI version with the following command:
aws --version
Important Package managers such
yum
,apt-get
, or Homebrew for macOS are often behind several versions of the AWS CLI. To ensure that you have the latest version, see Installing the AWS CLI in the AWS Command Line Interface User Guide. -
Create or update a
kubeconfig
file for your cluster. Replace the
with your own.example values
-
By default, the resulting configuration file is created at the default
kubeconfig
path (.kube
) in your home directory or merged with an existingconfig
file at that location. You can specify another path with the--kubeconfig
option. -
You can specify an IAM role ARN with the
--role-arn
option to use for authentication when you issuekubectl
commands. Otherwise, the IAM entity in your default AWS CLI or SDK credential chain is used. You can view your default AWS CLI or SDK identity by running theaws sts get-caller-identity
command. -
For more information, see the help page with the
aws eks update-kubeconfig help
command or see update-kubeconfig in the AWS CLI Command Reference.
Note To run the following command, you must have permission to use the
eks:DescribeCluster
API action with the cluster that you specify. For more information, see Amazon EKS identity-based policy examples.aws eks update-kubeconfig --region
region-code
--namemy-cluster
-
-
Test your configuration.
kubectl get svc
Note If you receive any authorization or resource type errors, see Unauthorized or access denied (kubectl) in the troubleshooting section.
The example output is as follows.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes ClusterIP 10.100.0.1 <none> 443/TCP 1m
Create kubeconfig
file
manually
To create your kubeconfig
file manually
-
Set values for a few variables by replacing the
with your own and then running the modified commands.example values
export region_code=
region-code
export cluster_name=my-cluster
export account_id=111122223333
-
Retrieve the endpoint for your cluster and store the value in a variable.
cluster_endpoint=$(aws eks describe-cluster \ --region $region_code \ --name $cluster_name \ --query "cluster.endpoint" \ --output text)
-
Retrieve the Base64-encoded certificate data required to communicate with your cluster and store the value in a variable.
certificate_data=$(aws eks describe-cluster \ --region $region_code \ --name $cluster_name \ --query "cluster.certificateAuthority.data" \ --output text)
-
Create the default
~/.kube
directory if it doesn't already exist.mkdir -p ~/.kube
-
Run the command for your preferred client token method (AWS CLI or AWS IAM authenticator for Kubernetes) to create the
config
file in the~/.kube
directory. You can specify the following before running one of the commands by modifying the command to include the following:-
An IAM role – Remove the
#
at the start of the lines underargs:
. Replace
with the name of the IAM role that you want to perform cluster operations with instead of the default AWS credential provider chain. For more information, see Set upmy-role
kubectl
to use authentication tokens provided by AWS IAM Authenticator for Kuberneteson GitHub. -
An AWS CLI named profile – Remove the
#
at the start of theenv:
line, and remove#
at the start of the lines under it. Replace
with the name of the profile to use. If you don't specify a profile, then the default profile is used. For additional information, see Specifying Credentials & Using AWS Profilesaws-profile
on GitHub.
-
-
Add the file path to your
KUBECONFIG
environment variable so thatkubectl
knows where to look for your cluster configuration.-
For Bash shells on macOS or Linux:
export KUBECONFIG=$KUBECONFIG:~/.kube/config
-
For PowerShell on Windows:
$ENV:KUBECONFIG="{0};{1}" -f $ENV:KUBECONFIG, "$ENV:userprofile\.kube\config"
-
-
(Optional) Add the configuration to your shell initialization file so that it is configured when you open a shell.
-
For Bash shells on macOS:
echo 'export KUBECONFIG=$KUBECONFIG:~/.kube/config' >> ~/.bash_profile
-
For Bash shells on Linux:
echo 'export KUBECONFIG=$KUBECONFIG:~/.kube/config' >> ~/.bashrc
-
For PowerShell on Windows:
[System.Environment]::SetEnvironmentVariable('KUBECONFIG', $ENV:KUBECONFIG, 'Machine')
-
-
Test your configuration.
kubectl get svc
Note If you receive any authorization or resource type errors, see Unauthorized or access denied (kubectl) in the troubleshooting section.
The example output is as follows.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes ClusterIP 10.100.0.1 <none> 443/TCP 1m