Create a Kubernetes cluster in AWS using Terraform and install MKE
Prerequisites
In addition to the MKE dependencies, you need to do the following:
- Install Terraform (required for creating VMs in AWS)
- Create an AWS account
- Set the environment variables for the AWS CLI:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
Create virtual machines on AWS
To create virtual machines on AWS using the example Terraform scripts:
Copy the example Terraform folder to your local machine.
Create a
terraform.tfvars
file with content similar to:cluster_name = "k0s-cluster" controller_count = 1 worker_count = 1 cluster_flavor = "m5.large" region = "us-east-1"
Run
terraform init
.Run
terraform apply -auto-approve
.Run
terraform output --raw k0s_cluster > VMs.yaml
.
To get detailed information on the virtual machines using the AWS CLI, run:
aws ec2 describe-instances --region $(grep "region" terraform.tfvars | awk -F' *= *' '{print $2}' | tr -d '"')
Alternatively, you can get a visual overview of the virtual machines at the AWS EC2 page by selecting the desired region from the dropdown menu in the top-right corner.
Install MKE on k0s
Generate a sample
mke4.yaml
file:mkectl init > mke4.yaml
Edit the
hosts
section inmke4.yaml
using the values from theVMs.yaml
file. Example configuration of thehosts
section:hosts: - role: controller+worker ssh: address: 54.91.231.190 keyPath: <path_to_terraform_folder>/aws_private.pem port: 22 user: ubuntu - role: worker ssh: address: 18.206.202.16 keyPath: <path_to_terraform_folder>/aws_private.pem port: 22 user: ubuntu
Edit the
apiServer.externalAddress
in the configuration fileterraform output -raw lb_dns_name | { read lb; yq -i ".apiServer.externalAddress = \"$lb\"" mke4.yaml; }
If you do not have the
yq
tool installed, edit themke4.yaml
file manually settingapiServer.externalAddress
to the output of theterraform output -raw lb_dns_name
command.Create the MKE cluster:
mkectl apply -f mke4.yaml
ℹ️Upon successful completion of the MKE 4 installation, a username and password will be automatically generated and displayed once for you to use.
To explicitly set a password value, run
mkectl apply -f mke4.yaml --admin-password <password>
.
Clean up infrastructure
To clean up and tear down infrastructure that is no longer needed, ensuring that all resources managed by Terraform are properly deleted, navigate to the Terraform folder and run:
terraform destroy --auto-approve
After successfully destroying the resources, Terraform will update the state file to reflect that the resources no longer exist.