You are on page 1of 8

MEMORANDUM

De: Joao Tiago


Aluno n.: ist178058
Disciplina: ACRONYM 2017/2018
Assunto: Laboratorio 10
Data: 7 de Dezembro de 2017

Para cumprir a tarefa de implementar um servico replicado e balanciado por varios


servidores web. Foi necessarario alterar o conteudo do ficheiro terraform-webserver.tf.
Desta forma o plano inclui a criacao de um servidor que serviria de front-end e de
load balancer. O plano considera tambem a criacao de outras 3 maquinas que servem
o proposito de webservers. O loadbalancer corre o HAproxy que e instalado atraves
do playbook de Ansible. Os webservers servem um ficheiro index.html atraves de um
servidor Nginx. Software tambem instalado atraves do ansible.
A sequencia de passos seguida foi muito semelhate a indicada no enunciado do
lab10. Apos intanciadas as maquinas, anotados os IPs e geradas os pares de chaves
restou instalar o software necessario atraves do playbook abaixo. O inventorio Ansible
foi atualizado para acomodar devidamente as novas maquinas.
O conteudo do playbook ansible apos a alteracoes devidas:
- name: Configure a webserver instance
hosts: webs
become: yes
become_method: sudo

tasks:
- name: install apps
apt: name={{ item }} update_cache=yes state=latest allow_unauthenticated=yes
with_items:
- nginx
register: conf
- debug:
var: conf

- name: write nginx.conf


template: src=templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: restart nginx

- name: write /etc/nginx/sites-available/default


template: src=templates/default-site.j2 dest=/etc/nginx/sites-available/default
notify: restart nginx

- name: deploy website content

1
template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html

# The handler is common to the nginx installation steps


handlers:
- name: restart nginx
service: name=nginx state=restarted

# This Play installs the required software in the Load Balancer


- hosts: lbal
gather_facts: True
remote_user: vagrant
become: yes
become_method: sudo
tasks:
# as there is no internal DNS service, we need to populate theb /etc/hosts
- name: update /etc/hosts file for name resolution of self
lineinfile: dest=/etc/hosts regexp=.*{{ item }}$ line="127.0.0.1 {{item}}" state
with_items: lbal
# as there is no internal DNS service, we need to populate theb /etc/hosts
- name: update /etc/hosts file for name resolution
lineinfile: dest=/etc/hosts regexp=.*{{ item }}$ line="{{ hostvars[item][ansibl
when: hostvars[item][ansible_default_ipv4][address] is defined
with_items: {{groups.webs}}

- name: install haproxy and socat


apt: pkg={{ item }} state=latest
with_items:
- haproxy
- socat

- name: enable haproxy


lineinfile: dest=/etc/default/haproxy regexp="ENABLED" line="ENABLED=1"
notify: restart haproxy

- name: deploy haproxy config


template: src=templates/haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg
notify: restart haproxy

# This task may be required for some updates due to packages installed
- name: reboot machine
shell: sleep 2 && shutdown -r now "Ansible reboot"
async: 1
poll: 0
ignore_errors: true
# Wait for the server to come alive again
- name: wait for server to come back
local_action: wait_for
args:
host: vagrant
port: 22
state: started
delay: 30
timeout: 300

# The handler is common to the haproxy installation steps


handlers:
- name: restart haproxy
service: name=haproxy state=restarted

2
O ficheiro terraform que foi utilizado para instanciar as maquinas:

# Elemets of the cloud such as virtual servers,


# networks, firewall rules are created as resources
# syntax is: resource RESOURCE_TYPE RESOURCE_NAME
# https://www.terraform.io/docs/configuration/resources.html

resource "google_compute_firewall" "frontend_rules" {


name = "default"
network = "default"

allow {
protocol = "tcp"
ports = ["80", "443"]
}

source_ranges = ["0.0.0.0/0"]
target_tags = ["web"]
}

# create the lbal


resource "google_compute_instance" "lbal" {
name = "lbal"
machine_type = "${var.GCP_MACHINE_TYPE}"
zone = "${var.GCP_REGION}"

boot_disk {
initialize_params {
# image list can be found at:
# https://cloud.google.com/compute/docs/images
image = "ubuntu-os-cloud/ubuntu-1604-lts"
}
}

network_interface {
network = "default"
access_config {
}
}
tags = ["web"]
}

# create the frontend server


resource "google_compute_instance" "webserver" {
count = 3
name = "web${count.index + 1}"
machine_type = "${var.GCP_MACHINE_TYPE}"
zone = "${var.GCP_REGION}"

boot_disk {
initialize_params {
# image list can be found at:
# https://cloud.google.com/compute/docs/images
image = "ubuntu-os-cloud/ubuntu-1604-lts"
}
}

3
network_interface {
network = "default"
access_config {
}
}
tags = ["web"]
}

output "web" {
value = "${join(" ", google_compute_instance.webserver.*.network_interface.0.access_
}
output "lbal" {
value = "${join(" ", google_compute_instance.lbal.*.network_interface.0.access_confi
}
O conteudo do ficheiro terraform.tfstate apos montada a infraestrutura:
{
"version": 3,
"terraform_version": "0.11.0",
"serial": 5,
"lineage": "67baa84e-d85c-45ce-9ce1-e4be5c8a9382",
"modules": [
{
"path": [
"root"
],
"outputs": {
"lbal": {
"sensitive": false,
"type": "string",
"value": "35.224.139.68"
},
"web": {
"sensitive": false,
"type": "string",
"value": "35.225.152.174 35.225.50.176 35.226.199.189"
}
},
"resources": {
"google_compute_instance.lbal": {
"type": "google_compute_instance",
"depends_on": [],
"primary": {
"id": "lbal",
"attributes": {
"attached_disk.#": "0",
"boot_disk.#": "1",
"boot_disk.0.auto_delete": "true",
"boot_disk.0.device_name": "persistent-disk-0",
"boot_disk.0.disk_encryption_key_raw": "",
"boot_disk.0.disk_encryption_key_sha256": "",
"boot_disk.0.initialize_params.#": "1",
"boot_disk.0.initialize_params.0.image": "ubuntu-os-cloud/ub
"boot_disk.0.initialize_params.0.size": "0",
"boot_disk.0.initialize_params.0.type": "",
"boot_disk.0.source": "https://www.googleapis.com/compute/v1
"can_ip_forward": "false",
"cpu_platform": "Intel Sandy Bridge",

4
"create_timeout": "4",
"guest_accelerator.#": "0",
"id": "lbal",
"instance_id": "4562942350909083262",
"label_fingerprint": "42WmSpB8rSM=",
"machine_type": "f1-micro",
"metadata.%": "0",
"metadata_fingerprint": "hUswLMpmJrE=",
"min_cpu_platform": "",
"name": "lbal",
"network_interface.#": "1",
"network_interface.0.access_config.#": "1",
"network_interface.0.access_config.0.assigned_nat_ip": "35.2
"network_interface.0.access_config.0.nat_ip": "35.224.139.68
"network_interface.0.address": "10.128.0.3",
"network_interface.0.alias_ip_range.#": "0",
"network_interface.0.name": "nic0",
"network_interface.0.network": "https://www.googleapis.com/c
"network_interface.0.network_ip": "10.128.0.3",
"network_interface.0.subnetwork": "https://www.googleapis.co
"network_interface.0.subnetwork_project": "agiit1718-42",
"project": "agiit1718-42",
"scheduling.#": "1",
"scheduling.0.automatic_restart": "true",
"scheduling.0.on_host_maintenance": "MIGRATE",
"scheduling.0.preemptible": "false",
"scratch_disk.#": "0",
"self_link": "https://www.googleapis.com/compute/v1/projects
"service_account.#": "0",
"tags.#": "1",
"tags.365508689": "web",
"tags_fingerprint": "3d9fNIIa_10=",
"zone": "us-central1-a"
},
"meta": {
"schema_version": "6"
},
"tainted": false
},
"deposed": [],
"provider": "provider.google"
},
"google_compute_instance.webserver.0": {
"type": "google_compute_instance",
"depends_on": [],
"primary": {
"id": "web1",
"attributes": {
"attached_disk.#": "0",
"boot_disk.#": "1",
"boot_disk.0.auto_delete": "true",
"boot_disk.0.device_name": "persistent-disk-0",
"boot_disk.0.disk_encryption_key_raw": "",
"boot_disk.0.disk_encryption_key_sha256": "",
"boot_disk.0.initialize_params.#": "1",
"boot_disk.0.initialize_params.0.image": "ubuntu-os-cloud/ub
"boot_disk.0.initialize_params.0.size": "0",
"boot_disk.0.initialize_params.0.type": "",

5
"boot_disk.0.source": "https://www.googleapis.com/compute/v1
"can_ip_forward": "false",
"cpu_platform": "Intel Sandy Bridge",
"create_timeout": "4",
"guest_accelerator.#": "0",
"id": "web1",
"instance_id": "4846001951508945342",
"label_fingerprint": "42WmSpB8rSM=",
"machine_type": "f1-micro",
"metadata.%": "0",
"metadata_fingerprint": "hUswLMpmJrE=",
"min_cpu_platform": "",
"name": "web1",
"network_interface.#": "1",
"network_interface.0.access_config.#": "1",
"network_interface.0.access_config.0.assigned_nat_ip": "35.2
"network_interface.0.access_config.0.nat_ip": "35.225.152.17
"network_interface.0.address": "10.128.0.2",
"network_interface.0.alias_ip_range.#": "0",
"network_interface.0.name": "nic0",
"network_interface.0.network": "https://www.googleapis.com/c
"network_interface.0.network_ip": "10.128.0.2",
"network_interface.0.subnetwork": "https://www.googleapis.co
"network_interface.0.subnetwork_project": "agiit1718-42",
"project": "agiit1718-42",
"scheduling.#": "1",
"scheduling.0.automatic_restart": "true",
"scheduling.0.on_host_maintenance": "MIGRATE",
"scheduling.0.preemptible": "false",
"scratch_disk.#": "0",
"self_link": "https://www.googleapis.com/compute/v1/projects
"service_account.#": "0",
"tags.#": "1",
"tags.365508689": "web",
"tags_fingerprint": "3d9fNIIa_10=",
"zone": "us-central1-a"
},
"meta": {
"schema_version": "6"
},
"tainted": false
},
"deposed": [],
"provider": "provider.google"
},
"google_compute_instance.webserver.1": {
"type": "google_compute_instance",
"depends_on": [],
"primary": {
"id": "web2",
"attributes": {
"attached_disk.#": "0",
"boot_disk.#": "1",
"boot_disk.0.auto_delete": "true",
"boot_disk.0.device_name": "persistent-disk-0",
"boot_disk.0.disk_encryption_key_raw": "",
"boot_disk.0.disk_encryption_key_sha256": "",
"boot_disk.0.initialize_params.#": "1",

6
"boot_disk.0.initialize_params.0.image": "ubuntu-os-cloud/ub
"boot_disk.0.initialize_params.0.size": "0",
"boot_disk.0.initialize_params.0.type": "",
"boot_disk.0.source": "https://www.googleapis.com/compute/v1
"can_ip_forward": "false",
"cpu_platform": "Intel Sandy Bridge",
"create_timeout": "4",
"guest_accelerator.#": "0",
"id": "web2",
"instance_id": "7321754558441819774",
"label_fingerprint": "42WmSpB8rSM=",
"machine_type": "f1-micro",
"metadata.%": "0",
"metadata_fingerprint": "hUswLMpmJrE=",
"min_cpu_platform": "",
"name": "web2",
"network_interface.#": "1",
"network_interface.0.access_config.#": "1",
"network_interface.0.access_config.0.assigned_nat_ip": "35.2
"network_interface.0.access_config.0.nat_ip": "35.225.50.176
"network_interface.0.address": "10.128.0.4",
"network_interface.0.alias_ip_range.#": "0",
"network_interface.0.name": "nic0",
"network_interface.0.network": "https://www.googleapis.com/c
"network_interface.0.network_ip": "10.128.0.4",
"network_interface.0.subnetwork": "https://www.googleapis.co
"network_interface.0.subnetwork_project": "agiit1718-42",
"project": "agiit1718-42",
"scheduling.#": "1",
"scheduling.0.automatic_restart": "true",
"scheduling.0.on_host_maintenance": "MIGRATE",
"scheduling.0.preemptible": "false",
"scratch_disk.#": "0",
"self_link": "https://www.googleapis.com/compute/v1/projects
"service_account.#": "0",
"tags.#": "1",
"tags.365508689": "web",
"tags_fingerprint": "3d9fNIIa_10=",
"zone": "us-central1-a"
},
"meta": {
"schema_version": "6"
},
"tainted": false
},
"deposed": [],
"provider": "provider.google"
},
"google_compute_instance.webserver.2": {
"type": "google_compute_instance",
"depends_on": [],
"primary": {
"id": "web3",
"attributes": {
"attached_disk.#": "0",
"boot_disk.#": "1",
"boot_disk.0.auto_delete": "true",
"boot_disk.0.device_name": "persistent-disk-0",

7
"boot_disk.0.disk_encryption_key_raw": "",
"boot_disk.0.disk_encryption_key_sha256": "",
"boot_disk.0.initialize_params.#": "1",
"boot_disk.0.initialize_params.0.image": "ubuntu-os-cloud/ub
"boot_disk.0.initialize_params.0.size": "0",
"boot_disk.0.initialize_params.0.type": "",
"boot_disk.0.source": "https://www.googleapis.com/compute/v1
"can_ip_forward": "false",
"cpu_platform": "Intel Sandy Bridge",
"create_timeout": "4",
"guest_accelerator.#": "0",
"id": "web3",
"instance_id": "6202403158170635902",
"label_fingerprint": "42WmSpB8rSM=",
"machine_type": "f1-micro",
"metadata.%": "0",
"metadata_fingerprint": "hUswLMpmJrE=",
"min_cpu_platform": "",
"name": "web3",
"network_interface.#": "1",
"network_interface.0.access_config.#": "1",
"network_interface.0.access_config.0.assigned_nat_ip": "35.2
"network_interface.0.access_config.0.nat_ip": "35.226.199.18
"network_interface.0.address": "10.128.0.5",
"network_interface.0.alias_ip_range.#": "0",
"network_interface.0.name": "nic0",
"network_interface.0.network": "https://www.googleapis.com/c
"network_interface.0.network_ip": "10.128.0.5",
"network_interface.0.subnetwork": "https://www.googleapis.co
"network_interface.0.subnetwork_project": "agiit1718-42",
"project": "agiit1718-42",
"scheduling.#": "1",
"scheduling.0.automatic_restart": "true",
"scheduling.0.on_host_maintenance": "MIGRATE",
"scheduling.0.preemptible": "false",
"scratch_disk.#": "0",
"self_link": "https://www.googleapis.com/compute/v1/projects
"service_account.#": "0",
"tags.#": "1",
"tags.365508689": "web",
"tags_fingerprint": "3d9fNIIa_10=",
"zone": "us-central1-a"
},
"meta": {
"schema_version": "6"
},
"tainted": false
},
"deposed": [],
"provider": "provider.google"
}
},
"depends_on": []
}
]
}

You might also like