Skip to content

Commit 59fdeca

Browse files
committed
ejemplo de Bridge to Kubernetes
1 parent 2f95460 commit 59fdeca

17 files changed

+548
-0
lines changed

04-cloud/00-aks/01-brigde-to-kubernetes/01-brigde-to-kubernetes.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,40 @@
44
#Variables
55
RESOURCE_GROUP="Bridge-To-Kubernetes"
66
AKS_NAME="lemoncode-dev"
7+
LOCATION="northeurope"
8+
9+
#Puedes usar Bridge to Kubernetes para redirigir el tráfico entre tu clúster en la nube
10+
#y tu entorno de desarrollo.
11+
12+
#Lo primero que vamos a hacer es crear un clúster
13+
az group create \
14+
--name $RESOURCE_GROUP \
15+
--location $LOCATION
16+
17+
az aks create \
18+
--resource-group $RESOURCE_GROUP \
19+
--name $AKS_NAME \
20+
--location $LOCATION \
21+
--node-count 1 \
22+
--generate-ssh-keys
23+
24+
#Recuperamos el contexto del cluster
25+
az aks get-credentials -g ${RESOURCE_GROUP} -n ${AKS_NAME}
26+
27+
#Para ver claramente cómo podemos interactuar con el clúster en local,
28+
#vamos a desplegar en él un MongoDB, el cual es el que utilizará mi web api en la carpeta webapi
29+
kubectl apply -f 04-cloud/00-aks/01-brigde-to-kubernetes/manifests/mongodb.yml
30+
kubectl get po --watch
31+
32+
#Desplegamos la webapi que hace uso del mongodb
33+
kubectl apply -f 04-cloud/00-aks/01-brigde-to-kubernetes/manifests/webapi.yml
34+
kubectl get po,svc
35+
36+
#Instalamos la extensión Bridge to Kubernetes en Visual Studio code instalada.
37+
# Hacemos la configuración para la depuración de la web api (puerto 5001)
38+
39+
#Comprueba el archivo hosts
40+
cat /private/etc/hosts
41+
42+
#Ahora puedo depurar desde mi local mis servicios desplegados en Kubernetes
743

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: mongo-express
5+
labels:
6+
app: web
7+
name: mongo-express
8+
spec:
9+
replicas: 2
10+
selector:
11+
matchLabels:
12+
app: web
13+
name: mongo-express
14+
template:
15+
metadata:
16+
labels:
17+
app: web
18+
name: mongo-express
19+
spec:
20+
containers:
21+
- name: mongo-express
22+
image: mongo-express
23+
env:
24+
- name: ME_CONFIG_MONGODB_SERVER
25+
value: mongodb://mongodb-svc
26+
ports:
27+
- containerPort: 8081
28+
29+
---
30+
31+
apiVersion: v1
32+
kind: Service
33+
metadata:
34+
name: mongo-express-svc
35+
spec:
36+
type: LoadBalancer
37+
selector:
38+
app: web
39+
name: mongo-express
40+
ports:
41+
- protocol: TCP
42+
port: 80
43+
targetPort: 8081
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: mongodb
5+
spec:
6+
selector:
7+
matchLabels:
8+
app: mongodb
9+
template:
10+
metadata:
11+
labels:
12+
app: mongodb
13+
spec:
14+
containers:
15+
- name: mongodb
16+
image: mongo
17+
ports:
18+
- name: mongodbport
19+
containerPort: 27017
20+
protocol: TCP
21+
22+
---
23+
24+
apiVersion: v1
25+
kind: Service
26+
metadata:
27+
name: mongodb-svc
28+
labels:
29+
app: mongodb
30+
spec:
31+
type: ClusterIP
32+
selector:
33+
app: mongodb
34+
ports:
35+
- port: 27017
36+
targetPort: 27017
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: webapi
5+
labels:
6+
app: web
7+
name: webapi
8+
spec:
9+
replicas: 1
10+
selector:
11+
matchLabels:
12+
app: web
13+
name: webapi
14+
template:
15+
metadata:
16+
labels:
17+
app: web
18+
name: webapi
19+
spec:
20+
containers:
21+
- name: webapi
22+
image: 0gis0/webapi:v1
23+
ports:
24+
- containerPort: 80
25+
26+
---
27+
28+
apiVersion: v1
29+
kind: Service
30+
metadata:
31+
name: webapi-svc
32+
spec:
33+
type: LoadBalancer
34+
selector:
35+
app: web
36+
name: webapi
37+
ports:
38+
- protocol: TCP
39+
port: 80
40+
targetPort: 80
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
**/.classpath
2+
**/.dockerignore
3+
**/.env
4+
**/.git
5+
**/.gitignore
6+
**/.project
7+
**/.settings
8+
**/.toolstarget
9+
**/.vs
10+
**/.vscode
11+
**/*.*proj.user
12+
**/*.dbmdl
13+
**/*.jfm
14+
**/azds.yaml
15+
**/bin
16+
**/charts
17+
**/docker-compose*
18+
**/Dockerfile*
19+
**/node_modules
20+
**/npm-debug.log
21+
**/obj
22+
**/secrets.dev.yaml
23+
**/values.dev.yaml
24+
README.md
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
*.swp
2+
*.*~
3+
project.lock.json
4+
.DS_Store
5+
*.pyc
6+
nupkg/
7+
8+
# Visual Studio Code
9+
.vscode
10+
11+
# Rider
12+
.idea
13+
14+
# User-specific files
15+
*.suo
16+
*.user
17+
*.userosscache
18+
*.sln.docstates
19+
20+
# Build results
21+
[Dd]ebug/
22+
[Dd]ebugPublic/
23+
[Rr]elease/
24+
[Rr]eleases/
25+
x64/
26+
x86/
27+
build/
28+
bld/
29+
[Bb]in/
30+
[Oo]bj/
31+
[Oo]ut/
32+
msbuild.log
33+
msbuild.err
34+
msbuild.wrn
35+
36+
# Visual Studio 2015
37+
.vs/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using webapi.Models;
2+
using MongoDB.Driver;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace webapi.Services
7+
{
8+
public class BookService
9+
{
10+
private readonly IMongoCollection<Book> _books;
11+
12+
public BookService(IBookstoreDatabaseSettings settings)
13+
{
14+
var client = new MongoClient(settings.ConnectionString);
15+
var database = client.GetDatabase(settings.DatabaseName);
16+
17+
_books = database.GetCollection<Book>(settings.BooksCollectionName);
18+
}
19+
20+
public List<Book> Get() =>
21+
_books.Find(book => true).ToList();
22+
23+
public Book Get(string id) =>
24+
_books.Find<Book>(book => book.Id == id).FirstOrDefault();
25+
26+
public Book Create(Book book)
27+
{
28+
_books.InsertOne(book);
29+
return book;
30+
}
31+
32+
public void Update(string id, Book bookIn) =>
33+
_books.ReplaceOne(book => book.Id == id, bookIn);
34+
35+
public void Remove(Book bookIn) =>
36+
_books.DeleteOne(book => book.Id == bookIn.Id);
37+
38+
public void Remove(string id) =>
39+
_books.DeleteOne(book => book.Id == id);
40+
}
41+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using webapi.Models;
2+
using webapi.Services;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System.Collections.Generic;
5+
6+
namespace webapi.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class BooksController : ControllerBase
11+
{
12+
private readonly BookService _bookService;
13+
14+
public BooksController(BookService bookService)
15+
{
16+
_bookService = bookService;
17+
}
18+
19+
[HttpGet]
20+
public ActionResult<List<Book>> Get() =>
21+
_bookService.Get();
22+
23+
[HttpGet("{id:length(24)}", Name = "GetBook")]
24+
public ActionResult<Book> Get(string id)
25+
{
26+
var book = _bookService.Get(id);
27+
28+
if (book == null)
29+
{
30+
return NotFound();
31+
}
32+
33+
return book;
34+
}
35+
36+
[HttpPost]
37+
public ActionResult<Book> Create(Book book)
38+
{
39+
_bookService.Create(book);
40+
41+
return CreatedAtRoute("GetBook", new { id = book.Id.ToString() }, book);
42+
}
43+
44+
[HttpPut("{id:length(24)}")]
45+
public IActionResult Update(string id, Book bookIn)
46+
{
47+
var book = _bookService.Get(id);
48+
49+
if (book == null)
50+
{
51+
return NotFound();
52+
}
53+
54+
_bookService.Update(id, bookIn);
55+
56+
return NoContent();
57+
}
58+
59+
[HttpDelete("{id:length(24)}")]
60+
public IActionResult Delete(string id)
61+
{
62+
var book = _bookService.Get(id);
63+
64+
if (book == null)
65+
{
66+
return NotFound();
67+
}
68+
69+
_bookService.Remove(book.Id);
70+
71+
return NoContent();
72+
}
73+
}
74+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base
2+
WORKDIR /app
3+
EXPOSE 80
4+
EXPOSE 443
5+
6+
FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build
7+
WORKDIR /src
8+
COPY ["webapi.csproj", "./"]
9+
RUN dotnet restore "webapi.csproj"
10+
COPY . .
11+
WORKDIR "/src/."
12+
RUN dotnet build "webapi.csproj" -c Release -o /app/build
13+
14+
FROM build AS publish
15+
RUN dotnet publish "webapi.csproj" -c Release -o /app/publish
16+
17+
FROM base AS final
18+
WORKDIR /app
19+
COPY --from=publish /app/publish .
20+
ENTRYPOINT ["dotnet", "webapi.dll"]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using MongoDB.Bson;
2+
using MongoDB.Bson.Serialization.Attributes;
3+
4+
namespace webapi.Models
5+
{
6+
public class Book
7+
{
8+
[BsonId]
9+
[BsonRepresentation(BsonType.ObjectId)]
10+
public string Id { get; set; }
11+
12+
[BsonElement("Name")]
13+
public string BookName { get; set; }
14+
15+
public decimal Price { get; set; }
16+
17+
public string Category { get; set; }
18+
19+
public string Author { get; set; }
20+
}
21+
}

0 commit comments

Comments
 (0)