-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add example for IRSA and cluster-autoscaler #710
Merged
max-rocket-internet
merged 7 commits into
terraform-aws-modules:master
from
max-rocket-internet:irsa_example
Jan 30, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
63f9939
Add example for IRSA
max-rocket-internet e16dda6
remove unused SGs
max-rocket-internet 597d288
remove helm setup part
max-rocket-internet ee840c4
simplification and use module
max-rocket-internet 083a1a0
fix fmt error
max-rocket-internet f0fd34d
remove unused resource
max-rocket-internet df1ef07
Merge branch 'master' into irsa_example
max-rocket-internet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# IAM Roles for Service Accounts | ||
|
||
This example shows how to create an IAM role to be used for a Kubernetes `ServiceAccount`. It will create a policy and role to be used by the [cluster-autoscaler](https://github.com/kubernetes/autoscaler/tree/master/cluster-autoscaler) using the [public Helm chart](https://github.com/helm/charts/tree/master/stable/cluster-autoscaler). | ||
|
||
The AWS documentation for IRSA is here: https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html | ||
|
||
## Setup | ||
|
||
Run Terraform: | ||
|
||
``` | ||
terraform init | ||
terraform apply | ||
``` | ||
|
||
Set kubectl context to the new cluster: `export KUBECONFIG=kubeconfig_test-eks-irsa` | ||
|
||
Check that there is a node that is `Ready`: | ||
|
||
``` | ||
$ kubectl get nodes | ||
NAME STATUS ROLES AGE VERSION | ||
ip-10-0-2-190.us-west-2.compute.internal Ready <none> 6m39s v1.14.8-eks-b8860f | ||
``` | ||
|
||
Replace `<ACCOUNT ID>` with your AWS account ID in `cluster-autoscaler-chart-values.yaml`. There is output from terraform for this. | ||
|
||
Install the chart using the provided values file: | ||
|
||
``` | ||
helm install --name cluster-autoscaler --namespace kube-system stable/cluster-autoscaler --values=cluster-autoscaler-chart-values.yaml | ||
``` | ||
|
||
## Verify | ||
|
||
Ensure the cluster-autoscaler pod is running: | ||
|
||
``` | ||
$ kubectl --namespace=kube-system get pods -l "app.kubernetes.io/name=aws-cluster-autoscaler" | ||
NAME READY STATUS RESTARTS AGE | ||
cluster-autoscaler-aws-cluster-autoscaler-5545d4b97-9ztpm 1/1 Running 0 3m | ||
``` | ||
|
||
Observe the `AWS_*` environment variables that were added to the pod automatically by EKS: | ||
|
||
``` | ||
kubectl --namespace=kube-system get pods -l "app.kubernetes.io/name=aws-cluster-autoscaler" -o yaml | grep -A3 AWS_ROLE_ARN | ||
|
||
- name: AWS_ROLE_ARN | ||
value: arn:aws:iam::xxxxxxxxx:role/cluster-autoscaler | ||
- name: AWS_WEB_IDENTITY_TOKEN_FILE | ||
value: /var/run/secrets/eks.amazonaws.com/serviceaccount/token | ||
``` | ||
|
||
Verify it is working by checking the logs, you should see that it has discovered the autoscaling group successfully: | ||
|
||
``` | ||
kubectl --namespace=kube-system logs -l "app.kubernetes.io/name=aws-cluster-autoscaler" | ||
|
||
I0128 14:59:00.901513 1 auto_scaling_groups.go:354] Regenerating instance to ASG map for ASGs: [test-eks-irsa-worker-group-12020012814125354700000000e] | ||
I0128 14:59:00.969875 1 auto_scaling_groups.go:138] Registering ASG test-eks-irsa-worker-group-12020012814125354700000000e | ||
I0128 14:59:00.969906 1 aws_manager.go:263] Refreshed ASG list, next refresh after 2020-01-28 15:00:00.969901767 +0000 UTC m=+61.310501783 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
awsRegion: us-west-2 | ||
|
||
rbac: | ||
create: true | ||
serviceAccountAnnotations: | ||
eks.amazonaws.com/role-arn: "arn:aws:iam::<ACCOUNT ID>:role/cluster-autoscaler" | ||
|
||
autoDiscovery: | ||
clusterName: test-eks-irsa | ||
enabled: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module "iam_assumable_role_admin" { | ||
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role-with-oidc" | ||
version = "~> v2.6.0" | ||
create_role = true | ||
role_name = "cluster-autoscaler" | ||
provider_url = replace(module.eks.cluster_oidc_issuer_url, "https://", "") | ||
role_policy_arns = [aws_iam_policy.cluster_autoscaler.arn] | ||
oidc_fully_qualified_subjects = ["system:serviceaccount:${local.k8s_service_account_namespace}:${local.k8s_service_account_name}"] | ||
} | ||
|
||
resource "aws_iam_policy" "cluster_autoscaler" { | ||
name_prefix = "cluster-autoscaler" | ||
description = "EKS cluster-autoscaler policy for cluster ${module.eks.cluster_id}" | ||
policy = data.aws_iam_policy_document.cluster_autoscaler.json | ||
} | ||
|
||
data "aws_iam_policy_document" "cluster_autoscaler" { | ||
statement { | ||
sid = "clusterAutoscalerAll" | ||
effect = "Allow" | ||
|
||
actions = [ | ||
"autoscaling:DescribeAutoScalingGroups", | ||
"autoscaling:DescribeAutoScalingInstances", | ||
"autoscaling:DescribeLaunchConfigurations", | ||
"autoscaling:DescribeTags", | ||
"ec2:DescribeLaunchTemplateVersions", | ||
] | ||
|
||
resources = ["*"] | ||
} | ||
|
||
statement { | ||
sid = "clusterAutoscalerOwn" | ||
effect = "Allow" | ||
|
||
actions = [ | ||
"autoscaling:SetDesiredCapacity", | ||
"autoscaling:TerminateInstanceInAutoScalingGroup", | ||
"autoscaling:UpdateAutoScalingGroup", | ||
] | ||
|
||
resources = ["*"] | ||
|
||
condition { | ||
test = "StringEquals" | ||
variable = "autoscaling:ResourceTag/kubernetes.io/cluster/${module.eks.cluster_id}" | ||
values = ["owned"] | ||
} | ||
|
||
condition { | ||
test = "StringEquals" | ||
variable = "autoscaling:ResourceTag/k8s.io/cluster-autoscaler/enabled" | ||
values = ["true"] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
locals { | ||
cluster_name = "test-eks-irsa" | ||
k8s_service_account_namespace = "kube-system" | ||
k8s_service_account_name = "cluster-autoscaler-aws-cluster-autoscaler" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
terraform { | ||
required_version = ">= 0.12.0" | ||
} | ||
|
||
provider "aws" { | ||
version = ">= 2.28.1" | ||
region = var.region | ||
} | ||
|
||
provider "local" { | ||
version = "~> 1.2" | ||
} | ||
|
||
provider "null" { | ||
version = "~> 2.1" | ||
} | ||
|
||
provider "template" { | ||
version = "~> 2.1" | ||
} | ||
|
||
data "aws_eks_cluster" "cluster" { | ||
name = module.eks.cluster_id | ||
} | ||
|
||
data "aws_eks_cluster_auth" "cluster" { | ||
name = module.eks.cluster_id | ||
} | ||
|
||
provider "kubernetes" { | ||
host = data.aws_eks_cluster.cluster.endpoint | ||
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data) | ||
token = data.aws_eks_cluster_auth.cluster.token | ||
load_config_file = false | ||
version = "~> 1.10" | ||
} | ||
|
||
data "aws_availability_zones" "available" {} | ||
|
||
data "aws_caller_identity" "current" {} | ||
|
||
module "vpc" { | ||
source = "terraform-aws-modules/vpc/aws" | ||
version = "2.6.0" | ||
name = "test-vpc" | ||
cidr = "10.0.0.0/16" | ||
azs = data.aws_availability_zones.available.names | ||
public_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] | ||
enable_dns_hostnames = true | ||
|
||
tags = { | ||
"kubernetes.io/cluster/${local.cluster_name}" = "shared" | ||
} | ||
|
||
public_subnet_tags = { | ||
"kubernetes.io/cluster/${local.cluster_name}" = "shared" | ||
"kubernetes.io/role/elb" = "1" | ||
} | ||
} | ||
|
||
module "eks" { | ||
source = "../.." | ||
cluster_name = local.cluster_name | ||
subnets = module.vpc.public_subnets | ||
vpc_id = module.vpc.vpc_id | ||
enable_irsa = true | ||
|
||
worker_groups = [ | ||
{ | ||
name = "worker-group-1" | ||
instance_type = "t2.medium" | ||
asg_desired_capacity = 1 | ||
tags = [ | ||
{ | ||
"key" = "k8s.io/cluster-autoscaler/enabled" | ||
"propagate_at_launch" = "false" | ||
"value" = "true" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
output "aws_account_id" { | ||
value = data.aws_caller_identity.current.account_id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
variable "region" { | ||
default = "us-west-2" | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm not mistaken, using IRSA on cluster-autoscaler, it would be good to add these flags to the cluster variables:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not mistake but I think we just remove the autoscaling policy stuff completely and let users manage that themselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I suggest: Leave the policy stuff in but change default enabled to
false
in next release (and ofc. mention in release notes). Then remove in subsequent release.This would allow users to somewhat seamlessly transition to IRSA, without needing to temporary bolt on additional policies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TBeijen actually we already have
var.attach_worker_autoscaling_policy
/var.manage_worker_autoscaling_policy
. I'll just make a PR to set these to default offalse
.