forked from GonzaloROD/DevopsPinFinal
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Contenedores.sh
72 lines (58 loc) · 1.99 KB
/
Contenedores.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# Script de User Data para ejecutar al lanzar la instancia
# Actualizar paquetes
sudo apt-get update -y
# Instalar Docker
sudo apt-get install -y docker.io
# Iniciar y habilitar Docker
sudo systemctl start docker
sudo systemctl enable docker
# Crear red Docker 'monitoring'
docker network inspect monitoring >/dev/null 2>&1 || \
docker network create monitoring
# Eliminar contenedores existentes (si los hubiera) para evitar conflictos
docker stop prometheus grafana nginx_exporter cadvisor nginx >/dev/null 2>&1
docker rm prometheus grafana nginx_exporter cadvisor nginx >/dev/null 2>&1
# Crear volúmenes persistentes
docker volume create prometheus_data
docker volume create grafana_data
docker volume create nginx_data
# Ejecutar NGINX
docker run -d --name nginx --network monitoring \
-v nginx_data:/usr/share/nginx/html:ro \
-p 80:80 nginx
# Ejecutar NGINX Exporter
docker run -d --name nginx_exporter --network monitoring \
--link nginx -p 9113:9113 \
nginx/nginx-prometheus-exporter:latest \
-nginx.scrape-uri=http://nginx:80/stub_status
# Crear archivo prometheus.yml en /home/ubuntu (directorio por defecto)
cat <<EOF > /home/ubuntu/prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'nginx'
static_configs:
- targets: ['nginx_exporter:9113']
- job_name: 'cadvisor'
static_configs:
- targets: ['cadvisor:8080']
EOF
# Ejecutar cAdvisor para monitorear contenedores
docker run -d --name cadvisor --network monitoring \
-p 8080:8080 \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
-v /sys:/sys:ro \
-v /var/lib/docker/:/var/lib/docker:ro \
gcr.io/cadvisor/cadvisor:latest
# Ejecutar Prometheus
docker run -d --name prometheus --network monitoring \
-p 9090:9090 \
-v prometheus_data:/prometheus \
-v /home/ubuntu/prometheus.yml:/etc/prometheus/prometheus.yml \
prom/prometheus
# Ejecutar Grafana
docker run -d --name grafana --network monitoring \
-p 3000:3000 \
-v grafana_data:/var/lib/grafana \
grafana/grafana