mirror of
https://github.com/marcel-dempers/docker-development-youtube-series.git
synced 2025-06-06 17:01:30 +00:00
Merge pull request #35 from marcel-dempers/terraform-aws-eks
terraform-aws-eks
This commit is contained in:
commit
4093b374a7
@ -134,9 +134,9 @@ eksctl create cluster --name getting-started-eks \
|
|||||||
--managed \
|
--managed \
|
||||||
--node-type t2.small \
|
--node-type t2.small \
|
||||||
--nodes 1 \
|
--nodes 1 \
|
||||||
|
--node-volume-size 200 \
|
||||||
--ssh-access \
|
--ssh-access \
|
||||||
--ssh-public-key=~/.ssh/id_rsa.pub \
|
--ssh-public-key=~/.ssh/id_rsa.pub \
|
||||||
--node-volume-size 200
|
|
||||||
|
|
||||||
```
|
```
|
||||||
## Create some sample containers
|
## Create some sample containers
|
||||||
|
174
kubernetes/cloud/amazon/terraform/main.tf
Normal file
174
kubernetes/cloud/amazon/terraform/main.tf
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
terraform {
|
||||||
|
required_version = ">= 0.12.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
provider "aws" {
|
||||||
|
version = ">= 2.28.1"
|
||||||
|
region = var.region
|
||||||
|
}
|
||||||
|
|
||||||
|
data "aws_eks_cluster" "cluster" {
|
||||||
|
name = module.eks.cluster_id
|
||||||
|
}
|
||||||
|
|
||||||
|
data "aws_eks_cluster_auth" "cluster" {
|
||||||
|
name = module.eks.cluster_id
|
||||||
|
}
|
||||||
|
|
||||||
|
data "aws_availability_zones" "available" {
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_security_group" "worker_group_mgmt_one" {
|
||||||
|
name_prefix = "worker_group_mgmt_one"
|
||||||
|
vpc_id = module.vpc.vpc_id
|
||||||
|
|
||||||
|
ingress {
|
||||||
|
from_port = 22
|
||||||
|
to_port = 22
|
||||||
|
protocol = "tcp"
|
||||||
|
|
||||||
|
cidr_blocks = [
|
||||||
|
"10.0.0.0/8",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "aws_security_group" "all_worker_mgmt" {
|
||||||
|
name_prefix = "all_worker_management"
|
||||||
|
vpc_id = module.vpc.vpc_id
|
||||||
|
|
||||||
|
ingress {
|
||||||
|
from_port = 22
|
||||||
|
to_port = 22
|
||||||
|
protocol = "tcp"
|
||||||
|
|
||||||
|
cidr_blocks = [
|
||||||
|
"10.0.0.0/8",
|
||||||
|
"172.16.0.0/12",
|
||||||
|
"192.168.0.0/16",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
|
||||||
|
public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
|
||||||
|
enable_nat_gateway = true
|
||||||
|
single_nat_gateway = true
|
||||||
|
enable_dns_hostnames = true
|
||||||
|
|
||||||
|
public_subnet_tags = {
|
||||||
|
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
|
||||||
|
"kubernetes.io/role/elb" = "1"
|
||||||
|
}
|
||||||
|
|
||||||
|
private_subnet_tags = {
|
||||||
|
"kubernetes.io/cluster/${var.cluster_name}" = "shared"
|
||||||
|
"kubernetes.io/role/internal-elb" = "1"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module "eks" {
|
||||||
|
source = "terraform-aws-modules/eks/aws"
|
||||||
|
cluster_name = var.cluster_name
|
||||||
|
cluster_version = "1.17"
|
||||||
|
subnets = module.vpc.private_subnets
|
||||||
|
version = "12.2.0"
|
||||||
|
cluster_create_timeout = "1h"
|
||||||
|
cluster_endpoint_private_access = true
|
||||||
|
|
||||||
|
vpc_id = module.vpc.vpc_id
|
||||||
|
|
||||||
|
worker_groups = [
|
||||||
|
{
|
||||||
|
name = "worker-group-1"
|
||||||
|
instance_type = "t2.small"
|
||||||
|
additional_userdata = "echo foo bar"
|
||||||
|
asg_desired_capacity = 1
|
||||||
|
additional_security_group_ids = [aws_security_group.worker_group_mgmt_one.id]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
worker_additional_security_group_ids = [aws_security_group.all_worker_mgmt.id]
|
||||||
|
map_roles = var.map_roles
|
||||||
|
map_users = var.map_users
|
||||||
|
map_accounts = var.map_accounts
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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.11"
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "kubernetes_deployment" "example" {
|
||||||
|
metadata {
|
||||||
|
name = "terraform-example"
|
||||||
|
labels = {
|
||||||
|
test = "MyExampleApp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spec {
|
||||||
|
replicas = 2
|
||||||
|
|
||||||
|
selector {
|
||||||
|
match_labels = {
|
||||||
|
test = "MyExampleApp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template {
|
||||||
|
metadata {
|
||||||
|
labels = {
|
||||||
|
test = "MyExampleApp"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
spec {
|
||||||
|
container {
|
||||||
|
image = "nginx:1.7.8"
|
||||||
|
name = "example"
|
||||||
|
|
||||||
|
resources {
|
||||||
|
limits {
|
||||||
|
cpu = "0.5"
|
||||||
|
memory = "512Mi"
|
||||||
|
}
|
||||||
|
requests {
|
||||||
|
cpu = "250m"
|
||||||
|
memory = "50Mi"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
resource "kubernetes_service" "example" {
|
||||||
|
metadata {
|
||||||
|
name = "terraform-example"
|
||||||
|
}
|
||||||
|
spec {
|
||||||
|
selector = {
|
||||||
|
test = "MyExampleApp"
|
||||||
|
}
|
||||||
|
port {
|
||||||
|
port = 80
|
||||||
|
target_port = 80
|
||||||
|
}
|
||||||
|
|
||||||
|
type = "LoadBalancer"
|
||||||
|
}
|
||||||
|
}
|
24
kubernetes/cloud/amazon/terraform/outputs.tf
Normal file
24
kubernetes/cloud/amazon/terraform/outputs.tf
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
output "cluster_endpoint" {
|
||||||
|
description = "Endpoint for EKS control plane."
|
||||||
|
value = module.eks.cluster_endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
output "cluster_security_group_id" {
|
||||||
|
description = "Security group ids attached to the cluster control plane."
|
||||||
|
value = module.eks.cluster_security_group_id
|
||||||
|
}
|
||||||
|
|
||||||
|
output "kubectl_config" {
|
||||||
|
description = "kubectl config as generated by the module."
|
||||||
|
value = module.eks.kubeconfig
|
||||||
|
}
|
||||||
|
|
||||||
|
output "config_map_aws_auth" {
|
||||||
|
description = "A kubernetes configuration to authenticate to this EKS cluster."
|
||||||
|
value = module.eks.config_map_aws_auth
|
||||||
|
}
|
||||||
|
|
||||||
|
output "region" {
|
||||||
|
description = "AWS region"
|
||||||
|
value = var.region
|
||||||
|
}
|
82
kubernetes/cloud/amazon/terraform/readme.md
Normal file
82
kubernetes/cloud/amazon/terraform/readme.md
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
# Getting Started with Amazon EKS using Terraform
|
||||||
|
|
||||||
|
More resources:
|
||||||
|
|
||||||
|
Terraform provider for AWS [here](https://www.terraform.io/docs/providers/aws/index.html) <br/>
|
||||||
|
|
||||||
|
## Amazon CLI
|
||||||
|
|
||||||
|
You can get the Amazon CLI on [Docker-Hub](https://hub.docker.com/r/amazon/aws-cli) <br/>
|
||||||
|
We'll need the Amazon CLI to gather information so we can build our Terraform file.
|
||||||
|
|
||||||
|
```
|
||||||
|
# Run Amazon CLI
|
||||||
|
docker run -it --rm -v ${PWD}:/work -w /work --entrypoint /bin/sh amazon/aws-cli:2.0.43
|
||||||
|
|
||||||
|
# some handy tools :)
|
||||||
|
yum install -y jq gzip nano tar git unzip wget
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
## Login to Amazon
|
||||||
|
|
||||||
|
```
|
||||||
|
# Access your "My Security Credentials" section in your profile.
|
||||||
|
# Create an access key
|
||||||
|
|
||||||
|
aws configure
|
||||||
|
|
||||||
|
Default region name: ap-southeast-2
|
||||||
|
Default output format: json
|
||||||
|
```
|
||||||
|
|
||||||
|
# Terraform CLI
|
||||||
|
|
||||||
|
```
|
||||||
|
# Get Terraform
|
||||||
|
|
||||||
|
curl -o /tmp/terraform.zip -LO https://releases.hashicorp.com/terraform/0.13.1/terraform_0.13.1_linux_amd64.zip
|
||||||
|
unzip /tmp/terraform.zip
|
||||||
|
chmod +x terraform && mv terraform /usr/local/bin/
|
||||||
|
terraform
|
||||||
|
```
|
||||||
|
|
||||||
|
## Terraform Amazon Kubernetes Provider
|
||||||
|
|
||||||
|
Documentation on all the Kubernetes fields for terraform [here](https://www.terraform.io/docs/providers/aws/r/eks_cluster.html)
|
||||||
|
|
||||||
|
```
|
||||||
|
cd kubernetes/cloud/amazon/terraform
|
||||||
|
|
||||||
|
terraform init
|
||||||
|
|
||||||
|
terraform plan
|
||||||
|
terraform apply
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
# Lets see what we deployed
|
||||||
|
|
||||||
|
```
|
||||||
|
# grab our EKS config
|
||||||
|
aws eks update-kubeconfig --name getting-started-eks --region ap-southeast-2
|
||||||
|
|
||||||
|
# Get kubectl
|
||||||
|
|
||||||
|
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
|
||||||
|
chmod +x ./kubectl
|
||||||
|
mv ./kubectl /usr/local/bin/kubectl
|
||||||
|
|
||||||
|
kubectl get nodes
|
||||||
|
kubectl get deploy
|
||||||
|
kubectl get pods
|
||||||
|
kubectl get svc
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
# Clean up
|
||||||
|
|
||||||
|
```
|
||||||
|
terraform destroy
|
||||||
|
```
|
57
kubernetes/cloud/amazon/terraform/variables.tf
Normal file
57
kubernetes/cloud/amazon/terraform/variables.tf
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
variable "region" {
|
||||||
|
default = "ap-southeast-2"
|
||||||
|
description = "AWS region"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "cluster_name" {
|
||||||
|
default = "getting-started-eks"
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "map_accounts" {
|
||||||
|
description = "Additional AWS account numbers to add to the aws-auth configmap."
|
||||||
|
type = list(string)
|
||||||
|
|
||||||
|
default = [
|
||||||
|
"777777777777",
|
||||||
|
"888888888888",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "map_roles" {
|
||||||
|
description = "Additional IAM roles to add to the aws-auth configmap."
|
||||||
|
type = list(object({
|
||||||
|
rolearn = string
|
||||||
|
username = string
|
||||||
|
groups = list(string)
|
||||||
|
}))
|
||||||
|
|
||||||
|
default = [
|
||||||
|
{
|
||||||
|
rolearn = "arn:aws:iam::66666666666:role/role1"
|
||||||
|
username = "role1"
|
||||||
|
groups = ["system:masters"]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "map_users" {
|
||||||
|
description = "Additional IAM users to add to the aws-auth configmap."
|
||||||
|
type = list(object({
|
||||||
|
userarn = string
|
||||||
|
username = string
|
||||||
|
groups = list(string)
|
||||||
|
}))
|
||||||
|
|
||||||
|
default = [
|
||||||
|
{
|
||||||
|
userarn = "arn:aws:iam::66666666666:user/user1"
|
||||||
|
username = "user1"
|
||||||
|
groups = ["system:masters"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
userarn = "arn:aws:iam::66666666666:user/user2"
|
||||||
|
username = "user2"
|
||||||
|
groups = ["system:masters"]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
@ -58,6 +58,7 @@ az role assignment create --assignee $SERVICE_PRINCIPAL \
|
|||||||
For extra reference you can also take a look at the Microsoft Docs: [here](https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/aks/kubernetes-service-principal.md) </br>
|
For extra reference you can also take a look at the Microsoft Docs: [here](https://github.com/MicrosoftDocs/azure-docs/blob/master/articles/aks/kubernetes-service-principal.md) </br>
|
||||||
|
|
||||||
|
|
||||||
|
# Terraform CLI
|
||||||
```
|
```
|
||||||
# Get Terraform
|
# Get Terraform
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user