Skip to content

Commit 9ee8ef5

Browse files
author
Eduard Tomàs
committed
ejercicios modulos 1-3
1 parent 93a9783 commit 9ee8ef5

File tree

14 files changed

+485
-0
lines changed

14 files changed

+485
-0
lines changed

02-orquestacion/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
# Modulo 2 - Orquestacion de contenedores
2+
3+
## Entornos usados
4+
5+
1. Minikube
6+
2. KinD
7+
3. Vagrant box con kubeadm
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# REPASO
2+
# Ejecutar un pod con la imagen lemoncodersbc/go-hello-world
3+
kubectl run helloworld --image lemoncodersbc/go-hello-world --port=80
4+
# Probar el pod usando port-forward
5+
kubectl port-forward helloworld 8080:80
6+
7+
# INSTALAR UN WORDPRESS
8+
9+
# Escenario 1: Dos pods
10+
kubectl run mysql --image mysql:5 --env MYSQL_ROOT_PASSWORD=my-secret-pw --env MYSQL_DATABASE=lcwp --env MYSQL_USER=eiximenis --env MYSQL_PASSWORD=Pa+a+a1! --port 3306
11+
# Necesitamos obtener la IP. Podemos hacerlo con:
12+
# 1. kubectl get po -o wide
13+
# 2. kubectl get po mysql -o yaml | grep -i IP
14+
# 3. kubectl get po mysql -o jsonpath='{.status.podIP}'
15+
mysqlip=$(kubectl get po mysql -o jsonpath='{.status.podIP}')
16+
# Ahora ejecutamos el pod de wp y le pasamos la IP del pod de mysql
17+
kubectl run wp --image wordpress:php7.2 --env WORDPRESS_DB_HOST=$mysqlip --env WORDPRESS_DB_PASSWORD=Pa+a+a1! --env WORDPRESS_DB_USER=eiximenis --env WORDPRESS_DB_PASSWORD=Pa+a+a1! --env WORDPRESS_DB_NAME=lcwp --port 80
18+
19+
# Escenario 2: Un pod.
20+
# Creamos el YAML del primer contenedor
21+
kubectl run mysql --image mysql:5 --env MYSQL_ROOT_PASSWORD=my-secret-pw --env MYSQL_DATABASE=lcwp --env MYSQL_USER=eiximenis --env MYSQL_PASSWORD=Pa+a+a1! --port 3306 --dry-run=client -o yaml > mywp.yaml
22+
# Editar el fichero yaml para añadir el segundo contenedor:
23+
# - env:
24+
# - name: WORDPRESS_DB_HOST
25+
# value: localhost
26+
# - name: WORDPRESS_DB_NAME
27+
# value: lcwp
28+
# - name: WORDPRESS_DB_USER
29+
# value: eiximenis
30+
# - name: WORDPRESS_DB_PASSWORD
31+
# value: Pa+a+a1!
32+
# image: wordpress:php7.2
33+
# name: wp
34+
# ports:
35+
# - containerPort: 80
36+
# resources: {}
37+
38+
# NOTA: Tienes el fichero completo en wpall.yaml
39+
# Finalmente usa kubectl create -f
40+
41+
# kubectl create -f mywp.yaml
42+
43+
44+
# SERVICIOS
45+
# Exponer el pod anterior
46+
kubectl expose pod helloworld --name helloworld-svc --port 80
47+
kubectl port-forward svc/helloworld-svc 3000:80 # Ir a localhost:3000
48+
49+
kubectl delete svc helloworld-svc
50+
kubectl delete pod helloworld
51+
52+
# Asignar pods a servicios
53+
54+
# Crear el pod
55+
kubectl run helloweb --image lemoncodersbc/hello-world-web:v1 --port 3000
56+
# Crear el servicio
57+
kubectl expose pod helloweb --port 3000 --name helloweb-svc
58+
59+
# Probar el servicio usando un pod de busybox
60+
kubectl run bb --image busybox -it --rm -- /bin/sh
61+
# Teclear lo siguiente en el terminal que aparece:
62+
# wget -qO- http://helloweb-svc:3000
63+
# exit
64+
65+
# Quitar la label del pod
66+
kubectl label pod helloweb run-
67+
# Probar el servicio usando un pod de busybox
68+
kubectl run bb --image busybox -it --rm -- /bin/sh
69+
70+
# Actualizar el pod otra vez para que vuelva a conectar con el servicio
71+
kubectl label pod helloweb run=helloweb
72+
# Probar el servicio usando un pod de busybox
73+
kubectl run bb --image busybox -it --rm -- /bin/sh
74+
75+
# Borrar el pod y crear otro y ver qué pasa
76+
kubectl delete pod helloweb
77+
kubectl run helloweb --image lemoncodersbc/hello-world-web:v1 --port 3000
78+
# Probar el servicio usando un pod de busybox
79+
kubectl run bb --image busybox -it --rm -- /bin/sh
80+
81+
82+
83+
84+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
creationTimestamp: null
5+
labels:
6+
run: wpall
7+
name: wpall
8+
spec:
9+
containers:
10+
- env:
11+
- name: MYSQL_ROOT_PASSWORD
12+
value: my-secret-pw
13+
- name: MYSQL_DATABASE
14+
value: lcwp
15+
- name: MYSQL_USER
16+
value: eiximenis
17+
- name: MYSQL_PASSWORD
18+
value: Pa+a+a1!
19+
image: mysql:5
20+
name: mysql
21+
ports:
22+
- containerPort: 3306
23+
resources: {}
24+
- env:
25+
- name: WORDPRESS_DB_HOST
26+
value: "127.0.0.1"
27+
- name: WORDPRESS_DB_NAME
28+
value: lcwp
29+
- name: WORDPRESS_DB_USER
30+
value: eiximenis
31+
- name: WORDPRESS_DB_PASSWORD
32+
value: Pa+a+a1!
33+
image: wordpress:php7.2
34+
name: wp
35+
ports:
36+
- containerPort: 80
37+
resources: {}
38+
dnsPolicy: ClusterFirst
39+
restartPolicy: Always
40+
status: {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
labels:
5+
run: web1
6+
name: web1
7+
spec:
8+
containers:
9+
- image: lemoncodersbc/hello-world-web:latest
10+
name: web1
11+
dnsPolicy: ClusterFirst
12+
restartPolicy: Always
13+
---
14+
apiVersion: v1
15+
kind: Pod
16+
metadata:
17+
labels:
18+
run: web2
19+
name: web2
20+
spec:
21+
containers:
22+
- image: lemoncodersbc/hello-world-web:latest
23+
name: web2
24+
dnsPolicy: ClusterFirst
25+
restartPolicy: Always
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Escenario 1
2+
# ===========
3+
# Aplica el contenido del fichero deberes-1.yaml al cluster (kubectl create -f deberes-1.yaml)
4+
5+
# Alguien del equipo de desarrollo ha intentado desplegar dos réplicas de la web (lemoncodersbc/hello-world-web:v1) en el clúster pero NO funciona.
6+
# ¿Qué está ocurriendo?
7+
8+
# Arregla ese desaguisado: despliega dos réplicas de la web y asegúrate de que funcionan (pods corriendo, web accesible)
9+
10+
# Al cabo de un tiempo los desarrolladores liberan una nueva versión de la web (lemoncodersbc/hello-world-web:v2)
11+
# ¿Puedes desplegarla SIN CAIDA DE SERVICIO?
12+
13+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
labels:
5+
app: helloworld
6+
name: helloworld
7+
spec:
8+
containers:
9+
- image: lemoncodersbc/go-hello-world
10+
name: helloworld
11+
dnsPolicy: ClusterFirst
12+
restartPolicy: Always
13+
status: {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
apiVersion: apps/v1
2+
kind: ReplicaSet
3+
metadata:
4+
name: helloworld-rs
5+
labels:
6+
app: helloworld
7+
spec:
8+
replicas: 2
9+
selector:
10+
matchLabels:
11+
app: helloworld
12+
template:
13+
metadata:
14+
labels:
15+
app: helloworld
16+
spec:
17+
containers:
18+
- image: lemoncodersbc/go-hello-world
19+
name: helloworld
20+
dnsPolicy: ClusterFirst
21+
restartPolicy: Always
22+
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Parte 3: ReplicaSets y Deployments
2+
3+
# Empezamos creando un pod de la API go-hello-world
4+
kubectl run helloworld --image lemoncodersbc/go-hello-world
5+
# Etiquetamos el pod
6+
kubectl label pod helloworld app=helloworld
7+
# Vamos a crear un ReplicaSet. No hay manera de hacerlo imperativamente.
8+
kubectl get pod helloworld -o yaml > helloworld-pod
9+
# Borramos el pod
10+
kubectl delete pod helloworld
11+
# Limpiar "a mano" el YAML y convertirlo a un yaml de ReplicaSet
12+
# El fichero está en helloworld-rs.yaml
13+
kubectl create -f helloworld-rs.yaml
14+
# En este momento deben haber dos pods de helloworld
15+
# Vamos a crear un tercero.
16+
kubectl run helloworld --image lemoncodersbc/go-hello-world
17+
# Se ha creado? Se está ejecutando?
18+
# Etiquetamos el pod de nuevo
19+
kubectl label pod helloworld app=helloworld
20+
# Qué ha ocurrido al etiquetar el pod? Por qué?
21+
22+
# Recuerda: El ReplicaSet gestiona determinados pods y se basa en las etiquetas que tengan esos pods.
23+
# Para el ReplicaSet dos pods que tengan la misma etiqueta son "iguales".
24+
25+
# Vamos a exponer el RS usando un servicio. Puedes ver el yaml del servicio:
26+
kubectl expose rs/helloworld-rs --port 80 --name helloworld-svc --dry-run=client -o YAML
27+
# Vamos a aplicar dicho yaml
28+
# TIP de kubectl: Observa el create -f -. Eso significa -f <stdin>
29+
kubectl expose rs/helloworld-rs --port 80 --name helloworld-svc --dry-run=client -o YAML | kubectl create -f -
30+
31+
# El servicio es de tipo ClusterIP. Podemos probarlo con port-forward
32+
kubectl port-forward svc/helloworld-svc 9000:80
33+
wget -qO- localhost:9000
34+
35+
# Actualiza el servicio a tipo NodePort / LoadBalancer (depende de tu kubernetes)
36+
# k8s en MV: Usa NodePort
37+
# minikube: Usa LoadBalancer
38+
39+
kubectl delete svc helloworld-svc
40+
kubectl expose rs/helloworld-rs --port 80 --name helloworld-svc --type NodePort # O LoadBalancer
41+
kubectl get svc
42+
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
43+
# helloworld-svc LoadBalancer 10.108.173.114 <pending> 80:30430/TCP 27s
44+
45+
# En el caso de NodePort el segundo puerto (30430) es el puerto donde está escuchando el servicio en cualquiera
46+
# de los nodos del clúster. Haz un curl a http://<ip-nodo-cluster>:<puerto> y te responderá
47+
48+
# En el caso de LoadBalancer el valor de EXTERNAL-IP te indica la IP a la que debes apuntar. En este caso
49+
# el puerto es 80 (el balanceador hace la redirección interna al puerto del nodo del clúster).
50+
# Minikube es especial (ya que usa LoadBalancer sin existir un balanceador real)
51+
# Debes teclear
52+
# minikube service helloworld-svc
53+
# Eso crea un tunel y te da un puerto temporal sobre localhost:
54+
55+
#|-----------|----------------|-------------|---------------------------|
56+
#| NAMESPACE | NAME | TARGET PORT | URL |
57+
#|-----------|----------------|-------------|---------------------------|
58+
#| default | helloworld-svc | 80 | http://192.168.49.2:30430 |
59+
#|-----------|----------------|-------------|---------------------------|
60+
#🏃 Starting tunnel for service helloworld-svc.
61+
#|-----------|----------------|-------------|------------------------|
62+
#| NAMESPACE | NAME | TARGET PORT | URL |
63+
#|-----------|----------------|-------------|------------------------|
64+
#| default | helloworld-svc | | http://127.0.0.1:44319 |
65+
#|-----------|----------------|-------------|------------------------|
66+
#🎉 Opening service default/helloworld-svc in default browser...
67+
#👉 http://127.0.0.1:44319
68+
#❗ Because you are using a Docker driver on linux, the terminal needs to be open to run it.
69+
70+
# Escalamos la replica
71+
kubectl scale rs helloworld --replicas=4
72+
73+
# Deployments
74+
75+
# Empecemos por ver el YAML de un deployment:
76+
kubectl create deployment helloworld --image=lemoncodersbc/go-hello-world --port=80 --replicas=3 --dry-run=client -o yaml
77+
# Creamos el deployment
78+
kubectl create deployment helloworld --image=lemoncodersbc/go-hello-world --port=80 --replicas=3 --dry-run=client -o yaml | kubectl create -f -
79+
kubectl get deployment # Aparece un deployment
80+
kubectl get pods # Aparecen 3 pods
81+
kubectl get rs # Aparece 1 replica set
82+
# Podemos escalar el deployment direcamente. NO HAY NECESIDAD DE INTERACCIONAR CON EL RS
83+
kubectl scale deployment helloworld --replicas=4 # Eso escala el replicaset asociado al deployment.
84+
# Exponemos el deployment
85+
kubectl expose deployment helloworld --port 80 --name helloworld-svc --type LoadBalancer
86+
87+
# Limpieza
88+
kubectl delete svc helloworld-svc
89+
kubectl delete deploy helloworld
90+
91+
# ROLLOUTS
92+
# Empecemos por generar el escenario inicial: Un ReplicaSet (2 replicas) y un servicio
93+
kubectl create -f rollouts-begin.yaml
94+
95+
# Ahora actualizamos la imagen de los pods asociados al replicaset
96+
kubectl set image rs helloworld-rs *=lemoncodersbc/go-hello-world:invalidtag
97+
# No ocurre nada, ya que los pods siguen corriendo. Hay que forzar que el rs cree los pods nuevos.
98+
# Para ello escalamos a 0 y luego a 2
99+
kubectl scale rs helloworld-rs --replicas=0
100+
kubectl scale rs helloworld-rs --replicas=2
101+
# ¿Resultado? Los pods están en ImageErrPull
102+
kubectl delete -f rollouts-begin.yaml # Limpieza
103+
104+
# Vamos a verlo con deployments
105+
kubectl create -f rollouts2-begin.yaml
106+
107+
# Actualizar la imagen del deploy
108+
kubectl set image deployment helloworld *=lemoncodersbc/go-hello-world:invalidtag
109+
# Qué ha ocurrido?
110+
# Ahora tenemos 3 pods. Uno con ImageErrPull y los 2 iniciales
111+
kubectl get po # Muestra 3 pods
112+
kubectl get rs # Muestra 2 replicasets
113+
kubectl get deploy # Muestra 1 solo deployment
114+
115+
# Cual es el estado del rollout?
116+
kubectl rollout status deploy helloworld
117+
118+
# Cual es el histórico de cambios
119+
kubectl rollout history deploy helloworld
120+
121+
# Volvemos para atrás
122+
kubectl rollout undo deploy helloworld
123+
124+
# Cual es la nueva situación?
125+
kubectl get po # Muestra 3 pods
126+
kubectl get rs # Muestra 2 replicasets. 1 (el nuevo) con 0 replicas deseadas
127+
kubectl get deploy # Muestra 1 solo deployment
128+
129+
kubectl delete -f rollouts2-begin.yaml # Limpieza
130+
131+
# Vamos a ver ahora un rollout correcto
132+
133+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: apps/v1
2+
kind: ReplicaSet
3+
metadata:
4+
name: helloworld-rs
5+
labels:
6+
app: helloworld
7+
spec:
8+
replicas: 2
9+
selector:
10+
matchLabels:
11+
app: helloworld
12+
template:
13+
metadata:
14+
labels:
15+
app: helloworld
16+
spec:
17+
containers:
18+
- image: lemoncodersbc/go-hello-world
19+
name: helloworld
20+
dnsPolicy: ClusterFirst
21+
restartPolicy: Always
22+
---
23+
apiVersion: v1
24+
kind: Service
25+
metadata:
26+
labels:
27+
app: helloworld
28+
name: helloworld-svc
29+
spec:
30+
ports:
31+
- port: 80
32+
protocol: TCP
33+
targetPort: 80
34+
selector:
35+
app: helloworld
36+
type: LoadBalancer # Usar NodePort si se necesita

0 commit comments

Comments
 (0)