Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README IAW 2 #2

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
40 changes: 40 additions & 0 deletions Practica-14.1/EC2/ scripts /install_docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

# Script de instalación de Docker y Docker Compose
# Referencia: https://docs.docker.com/engine/install/ubuntu/

set -x

# Actualizamos los repositorios
sudo apt-get update

# Instalamos los paquetes necesarios para que `apt` pueda usar repositorios sobre HTTPS
sudo apt-get install -y \
ca-certificates \
curl \
gnupg \
lsb-release

# Añadimos la clave GPG oficial de Docker
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Añadimos el repositorio oficial de Docker a nuestro sistema
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# Actualizamos la lista de paquetes
sudo apt-get update

# Instalamos la última versión de Docker y Docker Compose
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

# Añadimos el usuario actual al grupo docker
sudo usermod -aG docker $USER

# Habilitamos el servicio de Docker para que se inicie automáticamente al arrancar el sistema
sudo systemctl enable docker

# Iniciamos el servicio de Docker
sudo systemctl start docker
57 changes: 57 additions & 0 deletions Practica-14.1/EC2/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Configuramos el proveedor de AWS
provider "aws" {
region = var.region
}

# Creamos el grupo de seguridad
resource "aws_security_group" "sg_wp_prac14" {
name = var.sg_wp_prac14
description = var.sg_description
}

# Creamos las reglas de entrada del grupo de seguridad.
# Utilizamos un bucle para recorrer la lista de puertos definida como variable
resource "aws_security_group_rule" "ingress" {
security_group_id = aws_security_group.sg_wp_prac14.id
type = "ingress"

count = length(var.allowed_ingress_ports)
from_port = var.allowed_ingress_ports[count.index]
to_port = var.allowed_ingress_ports[count.index]
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

resource "aws_security_group_rule" "egress" {
security_group_id = aws_security_group.sg_wp_prac14.id
type = "egress"

from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}

# Creamos una instancia EC2
resource "aws_instance" "instancia_ec2_parac_14_1" {
ami = var.ami_id
instance_type = var.instance_type
key_name = var.key_name
security_groups = [aws_security_group.sg_wp_prac14.name]

# user_data = file("scripts/install_docker.sh")

tags = {
Name = var.instance_name
}
}

# Creamos una IP elástica y la asociamos a la instancia
resource "aws_eip" "ip_elastica" {
instance = aws_instance.instancia_ec2_parac_14_1.id
}

# Mostramos la IP pública de la instancia
output "elastic_ip" {
value = aws_eip.ip_elastica.public_ip
}
47 changes: 47 additions & 0 deletions Practica-14.1/EC2/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
variable "region" {
description = "Región de AWS donde se creará la instancia"
type = string
default = "us-east-1"
}

variable "allowed_ingress_ports" {
description = "Puertos de entrada del grupo de seguridad"
type = list(number)
default = [22, 80, 443, 8080]
}

variable "sg_wp_prac14" {
description = "Nombre del grupo de seguridad"
type = string
default = "sg_wp_prac14"
}

variable "sg_description" {
description = "Descripción del grupo de seguridad"
type = string
default = "Grupo de seguridad para la instancia de la practica 14.1"
}

variable "ami_id" {
description = "Identificador de la AMI"
type = string
default = "ami-00874d747dde814fa"
}

variable "instance_type" {
description = "Tipo de instancia"
type = string
default = "t2.small"
}

variable "key_name" {
description = "Nombre de la clave pública"
type = string
default = "vockey"
}

variable "instance_name" {
description = "Nombre de la instancia"
type = string
default = "EC2-PRAC_14.1-WP"
}
10 changes: 10 additions & 0 deletions Practica-14.1/Install WP/Docker/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# WordPress environment variables
WORDPRESS_DB_NAME=wp_database
WORDPRESS_DB_USER=wp_user
WORDPRESS_DB_PASSWORD=wp_password

# MySQL Server environment variables
MYSQL_ROOT_PASSWORD=root

# Domain WP
DOMAIN=alejandroalsa.ddns.net
74 changes: 74 additions & 0 deletions Practica-14.1/Install WP/Docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
version: '3'

services:
wordpress:
image: wordpress
# ports:
# - 80:80
environment:
- WORDPRESS_DB_HOST=mysql
- WORDPRESS_DB_NAME=${WORDPRESS_DB_NAME}
- WORDPRESS_DB_USER=${WORDPRESS_DB_USER}
- WORDPRESS_DB_PASSWORD=${WORDPRESS_DB_PASSWORD}
volumes:
- wordpress_data:/var/www/html
depends_on:
- mysql
restart: always
networks:
- nt-frontend
- nt-backend

mysql:
image: mysql:8.0
# ports:
# - 3306:3306
environment:
- MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
- MYSQL_DATABASE=${WORDPRESS_DB_NAME}
- MYSQL_USER=${WORDPRESS_DB_USER}
- MYSQL_PASSWORD=${WORDPRESS_DB_PASSWORD}
volumes:
- mysql_data:/var/lib/mysql
restart: always
networks:
- nt-backend
healthcheck:
test: mysql --user=${WORDPRESS_DB_USER} --password=${WORDPRESS_DB_PASSWORD} -e "SHOW DATABASES;"
interval: 3s
timeout: 3s
retries: 4

phpmyadmin:
image: phpmyadmin
ports:
- 8080:80
environment:
- PMA_ARBITRARY=1
restart: always
networks:
- nt-frontend
- nt-backend

https:
image: steveltn/https-portal:1
ports:
- 80:80
- 443:443
environment:
DOMAINS: '${DOMAIN} -> http://wordpress:80'
STAGE: 'production'
restart: always
networks:
- nt-frontend
volumes:
- ssl_certs_data:/var/lib/https-portal

volumes:
mysql_data:
wordpress_data:
ssl_certs_data:

networks:
nt-backend:
nt-frontend:
7 changes: 7 additions & 0 deletions Practica-14.1/Install WP/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[EC2]
3.212.175.226

[all:vars]
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/alejandro/Documentos/AWS/vockey.pem
ansible_ssh_common_args='-o StrictHostKeyChecking=accept-new'
3 changes: 3 additions & 0 deletions Practica-14.1/Install WP/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- import_playbook: ./playbooks/install_docker.yml
- import_playbook: ./playbooks/deploy_wp.yml
17 changes: 17 additions & 0 deletions Practica-14.1/Install WP/playbooks/deploy_wp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Playbook para instalar DOCKER
hosts: EC2
become: true

tasks:

- name: Variables de Configuracion
ansible.builtin.include_vars:
../variables/variables.yml

- name: Iniciando el contenedor de WP
community.docker.docker_compose:
project_src: "{{ User.Directory_WP }}"
state: present
files:
- "{{ Docker.File }}"
69 changes: 69 additions & 0 deletions Practica-14.1/Install WP/playbooks/install_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
- name: Playbook para instalar DOCKER
hosts: EC2
become: true

tasks:

- name: Variables de Configuracion
ansible.builtin.include_vars:
../variables/variables.yml

- name: Actualizando repositorios
ansible.builtin.apt:
update_cache: true

- name: Instalando Docker
ansible.builtin.apt:
name: "{{ Docker.Package }}"
state: present

- name: Creando un directorio para Docker Compose
ansible.builtin.file:
path: "{{ Type_Install.User_Home }}"
mode: 0775
owner: "{{ User.Name_Group }}"
group: "{{ User.Name_Group }}"
state: directory

- name: Descargando Docker Compose
ansible.builtin.get_url:
url: https://github.com/docker/compose/releases/download/v2.15.1/docker-compose-linux-x86_64
dest: "{{ Type_Install.User_Home }}/{{ Compose_File.New_Name }}"
mode: 0775
owner: "{{ User.Name_Group }}"
group: "{{ User.Name_Group }}"

- name: Asignando e grupo Docker al usuario
ansible.builtin.user:
name: "{{ User.Name_Group }}"
groups: "{{ Docker.Group }}"
append: true

- name: Instalando el gestor de paquetes de Python
ansible.builtin.apt:
name: python3-pip
state: present

- name: Instalando Docker para Python
ansible.builtin.pip:
name: docker
state: present

- name: Instalando Docker Compose para Python
ansible.builtin.pip:
name: docker-compose
state: present

- name: Copiando los archivos del Docker Compose al HOST
ansible.builtin.copy:
src: "{{ Compose_File.Local_Files }}"
dest: "{{ User.Directory }}"
owner: ubuntu
group: ubuntu
mode: 0644

- name: Reiniciando Docker
ansible.builtin.service:
name: docker
state: restarted
18 changes: 18 additions & 0 deletions Practica-14.1/Install WP/variables/variables.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
User:
Directory: /home/ubuntu/WP-DOCKER-COMPOSE
Directory_WP: /home/ubuntu/WP-DOCKER-COMPOSE/Docker
Name_Group: ubuntu

Type_Install:
User_Home: /home/ubuntu/.docker/cli-plugins
Multi_User_Directory: /usr/local/lib/docker/cli-plugins

Compose_File:
New_Name: docker-compose
Local_Files: ../Docker

Docker:
Package: docker.io
Group: docker
Service: docker.service
File: docker-compose.yml
Loading