diff --git a/eks-demo/README.md b/eks-demo/README.md new file mode 100644 index 0000000..ec3b902 --- /dev/null +++ b/eks-demo/README.md @@ -0,0 +1,37 @@ +# Setting up AWS EKS (Hosted Kubernetes) + +See https://www.terraform.io/docs/providers/aws/guides/eks-getting-started.html for full guide + +## Download the aws-iam-authenticator +``` +wget https://github.com/kubernetes-sigs/aws-iam-authenticator/releases/download/v0.3.0/heptio-authenticator-aws_0.3.0_linux_amd64 +chmod +x heptio-authenticator-aws_0.3.0_linux_amd64 +sudo mv heptio-authenticator-aws_0.3.0_linux_amd64 /usr/local/bin/heptio-authenticator-aws +``` + +## Modify providers.tf + +Choose your region. EKS is not available in every region, use the Region Table to check whether your region is supported: https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/ + +Make changes in providers.tf accordingly (region, optionally profile) + +## Terraform apply +``` +terraform apply +``` + +## Configure kubectl +``` +terraform output kubeconfig # save output in ~/.kube/config +``` + +## Configure config-map-auth-aws +``` +terraform output config-map-aws-auth # save output in config-map-aws-auth.yaml +kubectl apply -f config-map-aws-auth.yaml +``` + +## See nodes coming up +``` +kubectl get nodes +``` diff --git a/eks-demo/eks-cluster.tf b/eks-demo/eks-cluster.tf new file mode 100644 index 0000000..218965d --- /dev/null +++ b/eks-demo/eks-cluster.tf @@ -0,0 +1,15 @@ +resource "aws_eks_cluster" "demo" { + name = "${var.cluster-name}" + role_arn = "${aws_iam_role.demo-cluster.arn}" + + vpc_config { + security_group_ids = ["${aws_security_group.demo-cluster.id}"] + subnet_ids = ["${module.vpc.public_subnets}"] + } + + depends_on = [ + "aws_iam_role_policy_attachment.demo-cluster-AmazonEKSClusterPolicy", + "aws_iam_role_policy_attachment.demo-cluster-AmazonEKSServicePolicy", + ] +} + diff --git a/eks-demo/eks-workers.tf b/eks-demo/eks-workers.tf new file mode 100644 index 0000000..7e05fcb --- /dev/null +++ b/eks-demo/eks-workers.tf @@ -0,0 +1,74 @@ +data "aws_ami" "eks-worker" { + filter { + name = "name" + values = ["eks-worker-*"] + } + + most_recent = true + owners = ["602401143452"] # Amazon +} + +# EKS currently documents this required userdata for EKS worker nodes to +# properly configure Kubernetes applications on the EC2 instance. +# We utilize a Terraform local here to simplify Base64 encoding this +# information into the AutoScaling Launch Configuration. +# More information: https://amazon-eks.s3-us-west-2.amazonaws.com/1.10.3/2018-06-05/amazon-eks-nodegroup.yaml +locals { + demo-node-userdata = < $CA_CERTIFICATE_FILE_PATH +INTERNAL_IP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4) +sed -i s,MASTER_ENDPOINT,${aws_eks_cluster.demo.endpoint},g /var/lib/kubelet/kubeconfig +sed -i s,CLUSTER_NAME,${var.cluster-name},g /var/lib/kubelet/kubeconfig +sed -i s,REGION,${data.aws_region.current.name},g /etc/systemd/system/kubelet.service +sed -i s,MAX_PODS,20,g /etc/systemd/system/kubelet.service +sed -i s,MASTER_ENDPOINT,${aws_eks_cluster.demo.endpoint},g /etc/systemd/system/kubelet.service +sed -i s,INTERNAL_IP,$INTERNAL_IP,g /etc/systemd/system/kubelet.service +DNS_CLUSTER_IP=10.100.0.10 +if [[ $INTERNAL_IP == 10.* ]] ; then DNS_CLUSTER_IP=172.20.0.10; fi +sed -i s,DNS_CLUSTER_IP,$DNS_CLUSTER_IP,g /etc/systemd/system/kubelet.service +sed -i s,CERTIFICATE_AUTHORITY_FILE,$CA_CERTIFICATE_FILE_PATH,g /var/lib/kubelet/kubeconfig +sed -i s,CLIENT_CA_FILE,$CA_CERTIFICATE_FILE_PATH,g /etc/systemd/system/kubelet.service +systemctl daemon-reload +systemctl restart kubelet +USERDATA +} + +resource "aws_launch_configuration" "demo" { + associate_public_ip_address = true + iam_instance_profile = "${aws_iam_instance_profile.demo-node.name}" + image_id = "${data.aws_ami.eks-worker.id}" + instance_type = "m4.large" + name_prefix = "terraform-eks-demo" + security_groups = ["${aws_security_group.demo-node.id}"] + user_data_base64 = "${base64encode(local.demo-node-userdata)}" + + lifecycle { + create_before_destroy = true + } +} + +resource "aws_autoscaling_group" "demo" { + desired_capacity = 2 + launch_configuration = "${aws_launch_configuration.demo.id}" + max_size = 2 + min_size = 1 + name = "terraform-eks-demo" + vpc_zone_identifier = ["${module.vpc.public_subnets}"] + + tag { + key = "Name" + value = "terraform-eks-demo" + propagate_at_launch = true + } + + tag { + key = "kubernetes.io/cluster/${var.cluster-name}" + value = "owned" + propagate_at_launch = true + } +} diff --git a/eks-demo/external-ip.tf b/eks-demo/external-ip.tf new file mode 100644 index 0000000..c0fc52c --- /dev/null +++ b/eks-demo/external-ip.tf @@ -0,0 +1,7 @@ +data "http" "workstation-external-ip" { + url = "http://ipv4.icanhazip.com" +} + +locals { + workstation-external-cidr = "${chomp(data.http.workstation-external-ip.body)}/32" +} diff --git a/eks-demo/iam-workers.tf b/eks-demo/iam-workers.tf new file mode 100644 index 0000000..9649735 --- /dev/null +++ b/eks-demo/iam-workers.tf @@ -0,0 +1,38 @@ +resource "aws_iam_role" "demo-node" { + name = "terraform-eks-demo-node" + + assume_role_policy = <