├── packer
├── linuxtips
├── README.md
├── example.pkr.hcl
└── playbook.yml
├── README.md
├── output.tf
├── servers
├── output.tf
├── playbook-inicial.yml
├── terraform.aws_ec2.yml
├── variables.tf
└── ec2.tf
├── wordpress
├── kustomization.yml
├── mysql.yml
└── wordpress.yml
├── clusterip.yaml
├── nodeport.yaml
├── loadbalancer.yaml
├── deploy
├── loadbalancer.yml
└── kube.yaml
├── main.tf
├── nginx-deployment.yaml
├── playbook-inicial.yml
├── terraform.aws_ec2.yml
├── variables.tf
├── ec2.tf
├── terrafile.tf
├── codes
└── graphs
├── graph1.svg
├── graph.svg
└── graphtf.svg
/packer/linuxtips:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ##### TERRAFORM
2 |
3 | Exercícios e exemplos de fixação com Kubernetes e Terraform.
--------------------------------------------------------------------------------
/packer/README.md:
--------------------------------------------------------------------------------
1 | packer init .
2 | packer fmt .
3 | packer validate .
4 | packer build example.pkr.hcl
--------------------------------------------------------------------------------
/output.tf:
--------------------------------------------------------------------------------
1 | output "public_ip" {
2 | value = aws_instance.web[*].public_ip
3 | }
4 |
5 | output "environment" {
6 | value = var.environment
7 | }
--------------------------------------------------------------------------------
/servers/output.tf:
--------------------------------------------------------------------------------
1 | output "public_ip" {
2 | value = aws_instance.web[*].public_ip
3 | }
4 |
5 | output "environment" {
6 | value = var.environment
7 | }
--------------------------------------------------------------------------------
/wordpress/kustomization.yml:
--------------------------------------------------------------------------------
1 | secretGenerator:
2 | - name: mysql-pass
3 | literals:
4 | - password=linuxtips123
5 | resources:
6 | - mysql.yml
7 | - wordpress.yml
--------------------------------------------------------------------------------
/clusterip.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: v1
2 | kind: Service
3 | metadata:
4 | name: nginx-service-cluster-ip
5 | spec:
6 | type: ClusterIP
7 | selector:
8 | app: nginx
9 | ports:
10 | - protocol: TCP
11 | port: 80
12 | targetPort: 80
13 |
--------------------------------------------------------------------------------
/nodeport.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: v1
2 | kind: Service
3 | metadata:
4 | name: nginx-service-nodeport
5 | spec:
6 | type: NodePort
7 | selector:
8 | app: nginx
9 | ports:
10 | - protocol: TCP
11 | port: 80
12 | targetPort: 80
13 |
--------------------------------------------------------------------------------
/loadbalancer.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: v1
2 | kind: Service
3 | metadata:
4 | name: nginx-service-loadbalancer
5 | spec:
6 | type: LoadBalancer
7 | selector:
8 | app: nginx
9 | ports:
10 | - protocol: TCP
11 | port: 80
12 | targetPort: 80
13 |
--------------------------------------------------------------------------------
/deploy/loadbalancer.yml:
--------------------------------------------------------------------------------
1 | apiVersion: v1
2 | kind: Service
3 | metadata:
4 | name: dockercami-loadbalancer
5 | spec:
6 | type: LoadBalancer
7 | selector:
8 | app: dockercami
9 | ports:
10 | - protocol: TCP
11 | port: 8080
12 | targetPort: 8080
13 |
--------------------------------------------------------------------------------
/main.tf:
--------------------------------------------------------------------------------
1 | provider "aws" {
2 | region = "us-east-1"
3 | version = "~> 3.0"
4 | }
5 |
6 | terraform {
7 | backend "s3" {
8 | # Lembre de trocar o bucket para o seu, não pode ser o mesmo nome
9 | bucket = "descomplicando-terraform-punkdodevops-tfstates"
10 | key = "terraform-test.tfstate"
11 | region = "us-east-1"
12 | encrypt = true
13 | }
14 | }
--------------------------------------------------------------------------------
/deploy/kube.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: apps/v1
2 | kind: Deployment
3 | metadata:
4 | name: dockercami
5 | namespace: default
6 | spec:
7 | replicas: 1
8 | selector:
9 | matchLabels:
10 | dockercami: web
11 | template:
12 | metadata:
13 | labels:
14 | dockercami: web
15 | spec:
16 | containers:
17 | - name: dockercami
18 | image: camillamartins/dockernode
--------------------------------------------------------------------------------
/packer/example.pkr.hcl:
--------------------------------------------------------------------------------
1 | source "docker" "ubuntu" {
2 | commit = true
3 | image = "ubuntu:16.04"
4 | }
5 | build {
6 | sources = ["source.docker.ubuntu"]
7 |
8 | provisioner "ansible" {
9 | user = "ubuntu"
10 | playbook_file = "./playbook.yml"
11 | }
12 |
13 | post-processor "docker-tag" {
14 | repository = "camillamartins/dockerpacker"
15 | tags = ["1.0"]
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/nginx-deployment.yaml:
--------------------------------------------------------------------------------
1 | apiVersion: apps/v1
2 | kind: Deployment
3 | metadata:
4 | name: nginx-deployment
5 | labels:
6 | app: nginx
7 | spec:
8 | replicas: 2
9 | selector:
10 | matchLabels:
11 | app: nginx
12 | template:
13 | metadata:
14 | labels:
15 | app: nginx
16 | spec:
17 | containers:
18 | - name: nginx
19 | image: nginx:1.14.2
20 | ports:
21 | - containerPort: 80
22 |
--------------------------------------------------------------------------------
/playbook-inicial.yml:
--------------------------------------------------------------------------------
1 | - hosts: all
2 | become: yes
3 | become_user: ubuntu
4 |
5 | tasks:
6 | - name: Checking the user name
7 | shell: /usr/bin/whoami
8 | register: username
9 |
10 | - name: Debugging the whoami username
11 | debug: msg={{ username.stdout }}
12 |
13 | - name: Creates directory
14 | file:
15 | path: /home/ubuntu/linuxtips
16 | state: directory
17 | owner: ubuntu
18 | group: ubuntu
--------------------------------------------------------------------------------
/servers/playbook-inicial.yml:
--------------------------------------------------------------------------------
1 | - hosts: all
2 | become: yes
3 | become_user: ubuntu
4 |
5 | tasks:
6 | - name: Checking the user name
7 | shell: /usr/bin/whoami
8 | register: username
9 |
10 | - name: Debugging the whoami username
11 | debug: msg={{ username.stdout }}
12 |
13 | - name: Creates directory
14 | file:
15 | path: /home/ubuntu/linuxtips
16 | state: directory
17 | owner: ubuntu
18 | group: ubuntu
--------------------------------------------------------------------------------
/terraform.aws_ec2.yml:
--------------------------------------------------------------------------------
1 | plugin: amazon.aws.aws_ec2
2 | regions:
3 | - us-east-1
4 | keyed_groups:
5 | # add hosts to tag_Name_value groups for each aws_ec2 host's tags.Name variable
6 | - key: tags.Name
7 | prefix: tag_Name_
8 | separator: ""
9 | groups:
10 | # add hosts to the group development if any of the dictionary's keys or values is the word 'devel'
11 | development: "'devel' in (tags|list)"
12 | compose:
13 | ansible_host: public_ip_address
--------------------------------------------------------------------------------
/servers/terraform.aws_ec2.yml:
--------------------------------------------------------------------------------
1 | plugin: amazon.aws.aws_ec2
2 | regions:
3 | - us-east-1
4 | keyed_groups:
5 | # add hosts to tag_Name_value groups for each aws_ec2 host's tags.Name variable
6 | - key: tags.Name
7 | prefix: tag_Name_
8 | separator: ""
9 | groups:
10 | # add hosts to the group development if any of the dictionary's keys or values is the word 'devel'
11 | development: "'devel' in (tags|list)"
12 | compose:
13 | ansible_host: public_ip_address
--------------------------------------------------------------------------------
/packer/playbook.yml:
--------------------------------------------------------------------------------
1 | - hosts: 127.0.0.1
2 | connection: local
3 |
4 | tasks:
5 | - name: Checking the user name
6 | shell: /usr/bin/whoami
7 | register: username
8 |
9 | - name: Debugging the whoami username
10 | debug: msg={{ username.stdout }}
11 |
12 | - name: Touch a file
13 | file:
14 | path: ./linuxtips
15 | state: touch
16 | group: camillamartins
17 | owner: camillamartins
18 |
19 | - name: List file
20 | command: ls -la ./linuxtips
21 | register: filelinuxtips
22 |
23 | - name: Log file
24 | debug: msg={{ filelinuxtips.stdout }}
--------------------------------------------------------------------------------
/variables.tf:
--------------------------------------------------------------------------------
1 | variable "image_id" {
2 | default = "ami-042e8287309f5df03"
3 | type = string
4 | description = "id do AMI da AWS"
5 |
6 | validation {
7 | condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
8 | error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
9 | }
10 | }
11 |
12 | variable "servers" {
13 | type = number
14 | default = 4
15 | }
16 |
17 | variable "environment" {
18 | type = string
19 | default = "Development"
20 | }
21 |
22 | variable "message" {
23 | type = string
24 | default = "Olá, tudo bem?"
25 | }
--------------------------------------------------------------------------------
/servers/variables.tf:
--------------------------------------------------------------------------------
1 | variable "image_id" {
2 | default = "ami-042e8287309f5df03"
3 | type = string
4 | description = "id do AMI da AWS"
5 |
6 | validation {
7 | condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-"
8 | error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"."
9 | }
10 | }
11 |
12 | variable "servers" {
13 | type = number
14 | default = 4
15 | }
16 |
17 | variable "environment" {
18 | type = string
19 | default = "Development"
20 | }
21 |
22 | variable "message" {
23 | type = string
24 | default = "Olá, tudo bem?"
25 | }
--------------------------------------------------------------------------------
/ec2.tf:
--------------------------------------------------------------------------------
1 | provider "aws" {
2 | region = "us-east-1"
3 | }
4 |
5 | data "aws_ami" "ubuntu" {
6 | most_recent = true
7 |
8 | filter {
9 | name = "name"
10 | values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
11 | }
12 |
13 | owners = ["099720109477"] # Ubuntu
14 | }
15 |
16 | resource "aws_instance" "web" {
17 | count = 1
18 | ami = var.image_id
19 | instance_type = "t2.micro"
20 | key_name = "testecami"
21 |
22 | tags = {
23 | Name = "Welcome to ${var.environment}"
24 | }
25 |
26 | provisioner "local-exec" {
27 | command = "sleep 60 && ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook playbook-inicial.yml -i terraform.aws_ec2.yml -u ubuntu --key-file ~/.ssh/testecami.pem"
28 | }
29 | }
--------------------------------------------------------------------------------
/servers/ec2.tf:
--------------------------------------------------------------------------------
1 | provider "aws" {
2 | region = "us-east-1"
3 | }
4 |
5 | data "aws_ami" "ubuntu" {
6 | most_recent = true
7 |
8 | filter {
9 | name = "name"
10 | values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
11 | }
12 |
13 | owners = ["099720109477"] # Ubuntu
14 | }
15 |
16 | resource "aws_instance" "web" {
17 | count = 1
18 | ami = var.image_id
19 | instance_type = "t2.micro"
20 | key_name = "testecami"
21 |
22 | tags = {
23 | Name = "Welcome to ${var.environment}"
24 | }
25 |
26 | provisioner "local-exec" {
27 | command = "sleep 60 && ANSIBLE_HOST_KEY_CHECKING=False ansible-playbook playbook-inicial.yml -i terraform.aws_ec2.yml -u ubuntu --key-file ~/.ssh/testecami.pem"
28 | }
29 | }
--------------------------------------------------------------------------------
/terrafile.tf:
--------------------------------------------------------------------------------
1 | data "aws_ami" "ubuntu" {
2 | most_recent = true
3 |
4 | filter {
5 | name = "name"
6 | values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
7 | }
8 |
9 | owners = ["099720109477"] # Ubuntu
10 | }
11 |
12 | resource "aws_instance" "worker" {
13 | count = 2
14 | ami = data.aws_ami.ubuntu.id
15 | instance_type = "t3.medium"
16 | key_name = "camichan"
17 |
18 | tags = {
19 | Name = "k8s-Worker"
20 | }
21 | }
22 |
23 | resource "aws_instance" "main" {
24 | count = 1
25 | ami = data.aws_ami.ubuntu.id
26 | instance_type = "t3.medium"
27 | key_name = "camichan"
28 |
29 | tags = {
30 | Name = "k8s-Main"
31 | }
32 | }
33 |
34 | resource "aws_instance" "importado" {
35 | count = 1
36 | ami = "ami-087c17d1fe0178315"
37 | instance_type = "t2.micro"
38 | }
39 |
--------------------------------------------------------------------------------
/wordpress/mysql.yml:
--------------------------------------------------------------------------------
1 | apiVersion: v1
2 | kind: Service
3 | metadata:
4 | name: wordpress-mysql
5 | labels:
6 | app: wordpress
7 | spec:
8 | ports:
9 | - port: 3306
10 | selector:
11 | app: wordpress
12 | tier: mysql
13 | clusterIP: None
14 | ---
15 | apiVersion: v1
16 | kind: PersistentVolumeClaim
17 | metadata:
18 | name: mysql-pv-claim
19 | labels:
20 | app: wordpress
21 | spec:
22 | accessModes:
23 | - ReadWriteOnce
24 | resources:
25 | requests:
26 | storage: 20Gi
27 | ---
28 | apiVersion: apps/v1
29 | kind: Deployment
30 | metadata:
31 | name: wordpress-mysql
32 | labels:
33 | app: wordpress
34 | spec:
35 | selector:
36 | matchLabels:
37 | app: wordpress
38 | tier: mysql
39 | strategy:
40 | type: Recreate
41 | template:
42 | metadata:
43 | labels:
44 | app: wordpress
45 | tier: mysql
46 | spec:
47 | containers:
48 | - image: mysql:5.6
49 | name: mysql
50 | env:
51 | - name: MYSQL_ROOT_PASSWORD
52 | valueFrom:
53 | secretKeyRef:
54 | name: mysql-pass
55 | key: password
56 | ports:
57 | - containerPort: 3306
58 | name: mysql
59 | volumeMounts:
60 | - name: mysql-persistent-storage
61 | mountPath: /var/lib/mysql
62 | volumes:
63 | - name: mysql-persistent-storage
64 | persistentVolumeClaim:
65 | claimName: mysql-pv-claim
66 |
--------------------------------------------------------------------------------
/codes:
--------------------------------------------------------------------------------
1 | terraform plan -out bla
2 | terraform apply "bla"
3 | remover o state de nuvem
4 | terraform plan -destroy
5 | terraform state pull >> arquivo.tfstate ou push
6 | terraform plan -out plano -lock=false
7 | -refresh=false para infras grandes
8 | terraform refresh para atualizar o state
9 | -target=module.inquiry no apply e no plan pra pegar um módulo específico
10 | terraform state rm tira aquele estado da sua gerência automatica
11 | terraform state mv para mover estados
12 | terraform import aws_instance.web ami-ewer32432432432 importar coisas que criei na mão pro estado tfstate
13 | terraform taint (vai ser destruído e recriado)
14 | terraform workspace (criar staging e produção) - só o estado é diferente, o físico é o mesmo. por isso separar em regiões
15 | encrypt true
16 | terraform depends on (exemplo implícito com elastic ip e explicito com depends on)
17 | TF_LOG=TRACE antes dos comandos
18 | terraform taint através de terraform state list (untaint)
19 | terraform graph
20 | terraform fmt format -check -diff
21 | criar condicionais de criacao de maquinas com ternários condition ? true : false e com variáveis no variables.tf
22 | https://www.terraform.io/docs/language/values/variables.html
23 | https://learn.hashicorp.com/tutorials/terraform/for-each
24 | terraform ${var.name}
25 | groundwork - minimo pra aws funcionar https://github.com/gomex/terraform-module-groundwork
26 | make ## help
27 | mudar de versão fácil no cmd do terraform - https://tfswitch.warrensbox.com/Install/
28 | >>>>> TERRAFORM CLOUD
29 |
30 | importações simples e complexas
--------------------------------------------------------------------------------
/wordpress/wordpress.yml:
--------------------------------------------------------------------------------
1 | apiVersion: v1
2 | kind: Service
3 | metadata:
4 | name: wordpress
5 | labels:
6 | app: wordpress
7 | spec:
8 | ports:
9 | - port: 80
10 | selector:
11 | app: wordpress
12 | tier: frontend
13 | type: LoadBalancer
14 | ---
15 | apiVersion: v1
16 | kind: PersistentVolumeClaim
17 | metadata:
18 | name: wp-pv-claim
19 | labels:
20 | app: wordpress
21 | spec:
22 | accessModes:
23 | - ReadWriteOnce
24 | resources:
25 | requests:
26 | storage: 20Gi
27 | ---
28 | apiVersion: apps/v1
29 | kind: Deployment
30 | metadata:
31 | name: wordpress
32 | labels:
33 | app: wordpress
34 | spec:
35 | selector:
36 | matchLabels:
37 | app: wordpress
38 | tier: frontend
39 | strategy:
40 | type: Recreate
41 | template:
42 | metadata:
43 | labels:
44 | app: wordpress
45 | tier: frontend
46 | spec:
47 | containers:
48 | - image: wordpress:4.8-apache
49 | name: wordpress
50 | env:
51 | - name: WORDPRESS_DB_HOST
52 | value: wordpress-mysql
53 | - name: WORDPRESS_DB_PASSWORD
54 | valueFrom:
55 | secretKeyRef:
56 | name: mysql-pass
57 | key: password
58 | ports:
59 | - containerPort: 80
60 | name: wordpress
61 | volumeMounts:
62 | - name: wordpress-persistent-storage
63 | mountPath: /var/www/html
64 | volumes:
65 | - name: wordpress-persistent-storage
66 | persistentVolumeClaim:
67 | claimName: wp-pv-claim
68 |
--------------------------------------------------------------------------------
/graphs/graph1.svg:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
134 |
--------------------------------------------------------------------------------
/graphs/graph.svg:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
260 |
--------------------------------------------------------------------------------
/graphs/graphtf.svg:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
260 |
--------------------------------------------------------------------------------