Creating an Amazon EC2 Cluster

In this section, you’ll learn how to use Rancher to install an RKE Kubernetes cluster in Amazon EC2.

First, you will set up your EC2 cloud credentials in Rancher. Then you will use your cloud credentials to create a node template, which Rancher will use to provision new nodes in EC2.

Then you will create an EC2 cluster in Rancher, and when configuring the new cluster, you will define node pools for it. Each node pool will have a Kubernetes role of etcd, controlplane, or worker. Rancher will install RKE Kubernetes on the new nodes, and it will set up each node with the Kubernetes role defined by the node pool.

Prerequisites

Creating an EC2 Cluster

The steps to create a cluster differ based on your Rancher version.

  • RKE
  • RKE2
  1. Create your cloud credentials
  2. Create a node template with your cloud credentials and information from EC2
  3. Create a cluster with node pools using the node template

1. Create your cloud credentials

  1. Click ☰ > Cluster Management.
  2. Click Cloud Credentials.
  3. Click Create.
  4. Click Amazon.
  5. Enter a name for the cloud credential.
  6. In the Default Region field, select the AWS region where your cluster nodes will be located.
  7. Enter your AWS EC2 Access Key and Secret Key.
  8. Click Create.

Result: You have created the cloud credentials that will be used to provision nodes in your cluster. You can reuse these credentials for other node templates, or in other clusters.

2. Create a node template with your cloud credentials and information from EC2

Creating a node template for EC2 will allow Rancher to provision new nodes in EC2. Node templates can be reused for other clusters.

  1. Click ☰ > Cluster Management.

  2. Click RKE1 Configuration > Node Templates

  3. Click Add Template.

  4. Fill out a node template for EC2. For help filling out the form, refer to EC2 Node Template Configuration.

  5. Click Create.

    Creating an Amazon EC2 Cluster - 图1note

    If you want to use the dual-stack feature, there are additional requirements that must be taken into consideration.

3. Create a cluster with node pools using the node template

Add one or more node pools to your cluster. For more information about node pools, see this section.

  1. Click ☰ > Cluster Management.

  2. On the Clusters page, click Create.

  3. Click Amazon EC2.

  4. Create a node pool for each Kubernetes role. For each node pool, choose a node template that you created. For more information about node pools, including best practices for assigning Kubernetes roles to them, see this section.

  5. Click Add Member to add users that can access the cluster. Use the Role drop-down to set permissions for each user.

  6. Use Cluster Options to choose the version of Kubernetes that will be installed, what network provider will be used and if you want to enable project network isolation. Refer to Selecting Cloud Providers to configure the Kubernetes Cloud Provider. For help configuring the cluster, refer to the RKE cluster configuration reference.

    Creating an Amazon EC2 Cluster - 图2note

    If you want to use the dual-stack feature, there are additional requirements that must be taken into consideration.

  7. Click Create.

1. Create your cloud credentials

If you already have a set of cloud credentials to use, skip this section.

  1. Click ☰ > Cluster Management.
  2. Click Cloud Credentials.
  3. Click Create.
  4. Click Amazon.
  5. Enter a name for the cloud credential.
  6. In the Default Region field, select the AWS region where your cluster nodes will be located.
  7. Enter your AWS EC2 Access Key and Secret Key.
  8. Click Create.

Result: You have created the cloud credentials that will be used to provision nodes in your cluster. You can reuse these credentials for other node templates, or in other clusters.

2. Create your cluster

  1. Click ☰ > Cluster Management.
  2. On the Clusters page, click Create.
  3. Toggle the switch to RKE2/K3s.
  4. Click Amazon EC2.
  5. Select a Cloud Credential, if more than one exists. Otherwise, it’s preselected.
  6. Enter a Cluster Name.
  7. Create a machine pool for each Kubernetes role. Refer to the best practices for recommendations on role assignments and counts.
    1. For each machine pool, define the machine configuration. Refer to the EC2 machine configuration reference for information on configuration options.
  8. Use the Cluster Configuration to choose the version of Kubernetes that will be installed, what network provider will be used and if you want to enable project network isolation. For help configuring the cluster, refer to the RKE2 cluster configuration reference.
  9. Use Member Roles to configure user authorization for the cluster. Click Add Member to add users that can access the cluster. Use the Role drop-down to set permissions for each user.
  10. Click Create.

Result:

Your cluster is created and assigned a state of Provisioning. Rancher is standing up your cluster.

You can access your cluster after its state is updated to Active.

Active clusters are assigned two Projects:

  • Default, containing the default namespace
  • System, containing the cattle-system, ingress-nginx, kube-public, and kube-system namespaces

Optional Next Steps

After creating your cluster, you can access it through the Rancher UI. As a best practice, we recommend setting up these alternate ways of accessing your cluster:

  • Access your cluster with the kubectl CLI: Follow these steps to access clusters with kubectl on your workstation. In this case, you will be authenticated through the Rancher server’s authentication proxy, then Rancher will connect you to the downstream cluster. This method lets you manage the cluster without the Rancher UI.
  • Access your cluster with the kubectl CLI, using the authorized cluster endpoint: Follow these steps to access your cluster with kubectl directly, without authenticating through Rancher. We recommend setting up this alternative method to access your cluster so that in case you can’t connect to Rancher, you can still access the cluster.

IAM Policies

Example IAM Policy

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Sid": "VisualEditor0",
  6. "Effect": "Allow",
  7. "Action": [
  8. "ec2:AuthorizeSecurityGroupIngress",
  9. "ec2:Describe*",
  10. "ec2:ImportKeyPair",
  11. "ec2:CreateKeyPair",
  12. "ec2:CreateSecurityGroup",
  13. "ec2:CreateTags",
  14. "ec2:DeleteKeyPair",
  15. "ec2:ModifyInstanceMetadataOptions"
  16. ],
  17. "Resource": "*"
  18. },
  19. {
  20. "Sid": "VisualEditor1",
  21. "Effect": "Allow",
  22. "Action": [
  23. "ec2:RunInstances"
  24. ],
  25. "Resource": [
  26. "arn:aws:ec2:REGION::image/ami-*",
  27. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:instance/*",
  28. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:placement-group/*",
  29. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:volume/*",
  30. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:subnet/*",
  31. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:key-pair/*",
  32. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:network-interface/*",
  33. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:security-group/*"
  34. ]
  35. },
  36. {
  37. "Sid": "VisualEditor2",
  38. "Effect": "Allow",
  39. "Action": [
  40. "ec2:RebootInstances",
  41. "ec2:TerminateInstances",
  42. "ec2:StartInstances",
  43. "ec2:StopInstances"
  44. ],
  45. "Resource": "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:instance/*"
  46. }
  47. ]
  48. }

Example IAM Policy with PassRole

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Sid": "VisualEditor0",
  6. "Effect": "Allow",
  7. "Action": [
  8. "ec2:AuthorizeSecurityGroupIngress",
  9. "ec2:Describe*",
  10. "ec2:ImportKeyPair",
  11. "ec2:CreateKeyPair",
  12. "ec2:CreateSecurityGroup",
  13. "ec2:CreateTags",
  14. "ec2:DeleteKeyPair",
  15. "ec2:ModifyInstanceMetadataOptions"
  16. ],
  17. "Resource": "*"
  18. },
  19. {
  20. "Sid": "VisualEditor1",
  21. "Effect": "Allow",
  22. "Action": [
  23. "iam:PassRole",
  24. "ec2:RunInstances"
  25. ],
  26. "Resource": [
  27. "arn:aws:ec2:REGION::image/ami-*",
  28. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:instance/*",
  29. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:placement-group/*",
  30. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:volume/*",
  31. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:subnet/*",
  32. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:key-pair/*",
  33. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:network-interface/*",
  34. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:security-group/*",
  35. "arn:aws:iam::AWS_ACCOUNT_ID:role/YOUR_ROLE_NAME"
  36. ]
  37. },
  38. {
  39. "Sid": "VisualEditor2",
  40. "Effect": "Allow",
  41. "Action": [
  42. "ec2:RebootInstances",
  43. "ec2:TerminateInstances",
  44. "ec2:StartInstances",
  45. "ec2:StopInstances"
  46. ],
  47. "Resource": "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:instance/*"
  48. }
  49. ]
  50. }

Example IAM Policy to allow encrypted EBS volumes

  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Effect": "Allow",
  6. "Action": [
  7. "kms:Decrypt",
  8. "kms:GenerateDataKeyWithoutPlaintext",
  9. "kms:Encrypt",
  10. "kms:DescribeKey",
  11. "kms:CreateGrant",
  12. "ec2:DetachVolume",
  13. "ec2:AttachVolume",
  14. "ec2:DeleteSnapshot",
  15. "ec2:DeleteTags",
  16. "ec2:CreateTags",
  17. "ec2:CreateVolume",
  18. "ec2:DeleteVolume",
  19. "ec2:CreateSnapshot"
  20. ],
  21. "Resource": [
  22. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:volume/*",
  23. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:instance/*",
  24. "arn:aws:ec2:REGION:AWS_ACCOUNT_ID:snapshot/*",
  25. "arn:aws:kms:REGION:AWS_ACCOUNT_ID:key/KMS_KEY_ID"
  26. ]
  27. },
  28. {
  29. "Effect": "Allow",
  30. "Action": [
  31. "ec2:DescribeInstances",
  32. "ec2:DescribeTags",
  33. "ec2:DescribeVolumes",
  34. "ec2:DescribeSnapshots"
  35. ],
  36. "Resource": "*"
  37. }
  38. ]
  39. }