mirror of
https://github.com/farcasclaudiu/terraform-course.git
synced 2026-06-28 19:02:07 +03:00
eks-demo
This commit is contained in:
@@ -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
|
||||||
|
```
|
||||||
@@ -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",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
@@ -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 = <<USERDATA
|
||||||
|
#!/bin/bash -xe
|
||||||
|
|
||||||
|
CA_CERTIFICATE_DIRECTORY=/etc/kubernetes/pki
|
||||||
|
CA_CERTIFICATE_FILE_PATH=$CA_CERTIFICATE_DIRECTORY/ca.crt
|
||||||
|
mkdir -p $CA_CERTIFICATE_DIRECTORY
|
||||||
|
echo "${aws_eks_cluster.demo.certificate_authority.0.data}" | base64 -d > $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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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"
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
resource "aws_iam_role" "demo-node" {
|
||||||
|
name = "terraform-eks-demo-node"
|
||||||
|
|
||||||
|
assume_role_policy = <<POLICY
|
||||||
|
{
|
||||||
|
"Version": "2012-10-17",
|
||||||
|
"Statement": [
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Principal": {
|
||||||
|
"Service": "ec2.amazonaws.com"
|
||||||
|
},
|
||||||
|
"Action": "sts:AssumeRole"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
POLICY
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_iam_role_policy_attachment" "demo-node-AmazonEKSWorkerNodePolicy" {
|
||||||
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy"
|
||||||
|
role = "${aws_iam_role.demo-node.name}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_iam_role_policy_attachment" "demo-node-AmazonEKS_CNI_Policy" {
|
||||||
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"
|
||||||
|
role = "${aws_iam_role.demo-node.name}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_iam_role_policy_attachment" "demo-node-AmazonEC2ContainerRegistryReadOnly" {
|
||||||
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly"
|
||||||
|
role = "${aws_iam_role.demo-node.name}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_iam_instance_profile" "demo-node" {
|
||||||
|
name = "terraform-eks-demo"
|
||||||
|
role = "${aws_iam_role.demo-node.name}"
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
resource "aws_iam_role" "demo-cluster" {
|
||||||
|
name = "terraform-eks-demo-cluster"
|
||||||
|
|
||||||
|
assume_role_policy = <<POLICY
|
||||||
|
{
|
||||||
|
"Version": "2012-10-17",
|
||||||
|
"Statement": [
|
||||||
|
{
|
||||||
|
"Effect": "Allow",
|
||||||
|
"Principal": {
|
||||||
|
"Service": "eks.amazonaws.com"
|
||||||
|
},
|
||||||
|
"Action": "sts:AssumeRole"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
POLICY
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_iam_role_policy_attachment" "demo-cluster-AmazonEKSClusterPolicy" {
|
||||||
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
|
||||||
|
role = "${aws_iam_role.demo-cluster.name}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_iam_role_policy_attachment" "demo-cluster-AmazonEKSServicePolicy" {
|
||||||
|
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
|
||||||
|
role = "${aws_iam_role.demo-cluster.name}"
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
# outputs
|
||||||
|
locals {
|
||||||
|
kubeconfig = <<KUBECONFIG
|
||||||
|
|
||||||
|
|
||||||
|
apiVersion: v1
|
||||||
|
clusters:
|
||||||
|
- cluster:
|
||||||
|
server: ${aws_eks_cluster.demo.endpoint}
|
||||||
|
certificate-authority-data: ${aws_eks_cluster.demo.certificate_authority.0.data}
|
||||||
|
name: kubernetes
|
||||||
|
contexts:
|
||||||
|
- context:
|
||||||
|
cluster: kubernetes
|
||||||
|
user: aws
|
||||||
|
name: aws
|
||||||
|
current-context: aws
|
||||||
|
kind: Config
|
||||||
|
preferences: {}
|
||||||
|
users:
|
||||||
|
- name: aws
|
||||||
|
user:
|
||||||
|
exec:
|
||||||
|
apiVersion: client.authentication.k8s.io/v1alpha1
|
||||||
|
command: heptio-authenticator-aws
|
||||||
|
args:
|
||||||
|
- "token"
|
||||||
|
- "-i"
|
||||||
|
- "${var.cluster-name}"
|
||||||
|
KUBECONFIG
|
||||||
|
}
|
||||||
|
|
||||||
|
output "kubeconfig" {
|
||||||
|
value = "${local.kubeconfig}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Join configuration
|
||||||
|
|
||||||
|
locals {
|
||||||
|
config-map-aws-auth = <<CONFIGMAPAWSAUTH
|
||||||
|
|
||||||
|
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: aws-auth
|
||||||
|
namespace: kube-system
|
||||||
|
data:
|
||||||
|
mapRoles: |
|
||||||
|
- rolearn: ${aws_iam_role.demo-node.arn}
|
||||||
|
username: system:node:{{EC2PrivateDNSName}}
|
||||||
|
groups:
|
||||||
|
- system:bootstrappers
|
||||||
|
- system:nodes
|
||||||
|
CONFIGMAPAWSAUTH
|
||||||
|
}
|
||||||
|
|
||||||
|
output "config-map-aws-auth" {
|
||||||
|
value = "${local.config-map-aws-auth}"
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
provider "aws" {
|
||||||
|
region = "us-east-1"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
data "aws_region" "current" {}
|
||||||
|
|
||||||
|
data "aws_availability_zones" "available" {}
|
||||||
|
|
||||||
|
provider "http" {}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
# workers
|
||||||
|
resource "aws_security_group" "demo-node" {
|
||||||
|
name = "terraform-eks-demo-node"
|
||||||
|
description = "Security group for all nodes in the cluster"
|
||||||
|
vpc_id = "${module.vpc.vpc_id}"
|
||||||
|
|
||||||
|
egress {
|
||||||
|
from_port = 0
|
||||||
|
to_port = 0
|
||||||
|
protocol = "-1"
|
||||||
|
cidr_blocks = ["0.0.0.0/0"]
|
||||||
|
}
|
||||||
|
|
||||||
|
tags = "${
|
||||||
|
map(
|
||||||
|
"Name", "terraform-eks-demo-node",
|
||||||
|
"kubernetes.io/cluster/${var.cluster-name}", "owned",
|
||||||
|
)
|
||||||
|
}"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_security_group_rule" "demo-node-ingress-self" {
|
||||||
|
description = "Allow node to communicate with each other"
|
||||||
|
from_port = 0
|
||||||
|
protocol = "-1"
|
||||||
|
security_group_id = "${aws_security_group.demo-node.id}"
|
||||||
|
source_security_group_id = "${aws_security_group.demo-node.id}"
|
||||||
|
to_port = 65535
|
||||||
|
type = "ingress"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_security_group_rule" "demo-node-ingress-cluster" {
|
||||||
|
description = "Allow worker Kubelets and pods to receive communication from the cluster control plane"
|
||||||
|
from_port = 1025
|
||||||
|
protocol = "tcp"
|
||||||
|
security_group_id = "${aws_security_group.demo-node.id}"
|
||||||
|
source_security_group_id = "${aws_security_group.demo-cluster.id}"
|
||||||
|
to_port = 65535
|
||||||
|
type = "ingress"
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
resource "aws_security_group" "demo-cluster" {
|
||||||
|
name = "terraform-eks-demo-cluster"
|
||||||
|
description = "Cluster communication with worker nodes"
|
||||||
|
vpc_id = "${module.vpc.vpc_id}"
|
||||||
|
|
||||||
|
egress {
|
||||||
|
from_port = 0
|
||||||
|
to_port = 0
|
||||||
|
protocol = "-1"
|
||||||
|
cidr_blocks = ["0.0.0.0/0"]
|
||||||
|
}
|
||||||
|
|
||||||
|
tags {
|
||||||
|
Name = "terraform-eks-demo"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_security_group_rule" "demo-cluster-ingress-node-https" {
|
||||||
|
description = "Allow pods to communicate with the cluster API Server"
|
||||||
|
from_port = 443
|
||||||
|
protocol = "tcp"
|
||||||
|
security_group_id = "${aws_security_group.demo-cluster.id}"
|
||||||
|
source_security_group_id = "${aws_security_group.demo-node.id}"
|
||||||
|
to_port = 443
|
||||||
|
type = "ingress"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_security_group_rule" "demo-cluster-ingress-workstation-https" {
|
||||||
|
cidr_blocks = ["${local.workstation-external-cidr}"]
|
||||||
|
description = "Allow workstation to communicate with the cluster API Server"
|
||||||
|
from_port = 443
|
||||||
|
protocol = "tcp"
|
||||||
|
security_group_id = "${aws_security_group.demo-cluster.id}"
|
||||||
|
to_port = 443
|
||||||
|
type = "ingress"
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
variable "cluster-name" {
|
||||||
|
default = "terraform-eks-demo"
|
||||||
|
type = "string"
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
module "vpc" {
|
||||||
|
source = "terraform-aws-modules/vpc/aws"
|
||||||
|
|
||||||
|
name = "vpc-module-demo"
|
||||||
|
cidr = "10.0.0.0/16"
|
||||||
|
|
||||||
|
azs = ["${slice(data.aws_availability_zones.available.names, 0, 3)}"]
|
||||||
|
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
|
||||||
|
public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
|
||||||
|
|
||||||
|
enable_nat_gateway = false
|
||||||
|
enable_vpn_gateway = false
|
||||||
|
|
||||||
|
tags = "${
|
||||||
|
map(
|
||||||
|
"Name", "terraform-eks-demo-node",
|
||||||
|
"kubernetes.io/cluster/${var.cluster-name}", "shared",
|
||||||
|
)
|
||||||
|
}"
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user