From ea4e0ed6e8d9f0254790db2c21a5f8d19deb7303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Tom=C3=A0s?= Date: Thu, 11 Jan 2018 18:18:16 +0100 Subject: [PATCH] Deploying ocelot to k8s configuration to ocelot passed through a k8s volume --- docker-compose.override.yml | 4 +- docker-compose.yml | 5 +- k8s/deploy.ps1 | 10 +++- k8s/ingress.yaml | 10 ++-- k8s/ocelot/configuration.json | 58 +++++++++++++++++++ k8s/ocelot/deployment.yaml | 31 ++++++++++ k8s/ocelot/service.yaml | 13 +++++ src/Apigw/OcelotApiGw/Dockerfile | 3 +- src/Apigw/OcelotApiGw/OcelotApiGw.csproj | 1 + src/Apigw/OcelotApiGw/Program.cs | 1 + src/Apigw/OcelotApiGw/Startup.cs | 22 ++++--- .../configuration/configuration.json | 58 +++++++++++++++++++ xglobal.json | 5 ++ 13 files changed, 201 insertions(+), 20 deletions(-) create mode 100644 k8s/ocelot/configuration.json create mode 100644 k8s/ocelot/deployment.yaml create mode 100644 k8s/ocelot/service.yaml create mode 100644 src/Apigw/OcelotApiGw/configuration/configuration.json create mode 100644 xglobal.json diff --git a/docker-compose.override.yml b/docker-compose.override.yml index a4f1eff0c..c8b952af5 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -221,9 +221,9 @@ services: - "15672:15672" - "5672:5672" - ocelotapigw: + apigw: environment: - ASPNETCORE_ENVIRONMENT=Development ports: - - "80" + - "5200:80" diff --git a/docker-compose.yml b/docker-compose.yml index 82b9b1670..bda0acded 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -108,9 +108,8 @@ services: rabbitmq: image: rabbitmq:3-management-alpine - ocelotapigw: - image: ocelotapigw + apigw: + image: eshop/ocelotapigw build: context: . dockerfile: src/Apigw/OcelotApiGw/Dockerfile - diff --git a/k8s/deploy.ps1 b/k8s/deploy.ps1 index 42202f637..3372a5937 100644 --- a/k8s/deploy.ps1 +++ b/k8s/deploy.ps1 @@ -105,6 +105,7 @@ ExecKube -cmd 'delete deployments --all' ExecKube -cmd 'delete services --all' ExecKube -cmd 'delete configmap urls' ExecKube -cmd 'delete configmap externalcfg' +ExecKube -cmd 'delete configmap ocelot' # start sql, rabbitmq, frontend deployments if ($deployInfrastructure) { @@ -113,6 +114,12 @@ if ($deployInfrastructure) { } +Write-Host 'Deploying ocelot APIGW' -ForegroundColor Yellow + +ExecKube "create configmap ocelot --from-file=ocelot/configuration.json" +ExecKube -cmd "apply -f ocelot/deployment.yaml" +ExecKube -cmd "apply -f ocelot/service.yaml" + Write-Host 'Deploying code deployments (Web APIs, Web apps, ...)' -ForegroundColor Yellow ExecKube -cmd 'create -f services.yaml' @@ -154,7 +161,6 @@ ExecKube -cmd 'create configmap urls ` ExecKube -cmd 'label configmap urls app=eshop' Write-Host "Deploying configuration from $configFile" -ForegroundColor Yellow - ExecKube -cmd "create -f $configFile" Write-Host "Creating deployments..." -ForegroundColor Yellow @@ -178,6 +184,7 @@ ExecKube -cmd 'set image deployments/payment payment=${registryPath}${dockerOrg} ExecKube -cmd 'set image deployments/webmvc webmvc=${registryPath}${dockerOrg}/webmvc:$imageTag' ExecKube -cmd 'set image deployments/webstatus webstatus=${registryPath}${dockerOrg}/webstatus:$imageTag' ExecKube -cmd 'set image deployments/webspa webspa=${registryPath}${dockerOrg}/webspa:$imageTag' +ExecKube -cmd 'set image deployments/ocelot ocelot=${registryPath}${dockerOrg}/ocelotapigw:$imageTag' Write-Host "Execute rollout..." -ForegroundColor Yellow ExecKube -cmd 'rollout resume deployments/basket' @@ -190,6 +197,7 @@ ExecKube -cmd 'rollout resume deployments/payment' ExecKube -cmd 'rollout resume deployments/webmvc' ExecKube -cmd 'rollout resume deployments/webstatus' ExecKube -cmd 'rollout resume deployments/webspa' +ExecKube -cmd 'rollout resume deployments/ocelot' Write-Host "WebSPA is exposed at http://$externalDns, WebMVC at http://$externalDns/webmvc, WebStatus at http://$externalDns/webstatus" -ForegroundColor Yellow diff --git a/k8s/ingress.yaml b/k8s/ingress.yaml index 3c2fe8bad..813a88a36 100644 --- a/k8s/ingress.yaml +++ b/k8s/ingress.yaml @@ -13,11 +13,11 @@ spec: paths: - path: /basket-api backend: - serviceName: basket + serviceName: ocelot servicePort: 80 - path: /catalog-api backend: - serviceName: catalog + serviceName: ocelot servicePort: 80 - path: /identity backend: @@ -25,7 +25,7 @@ spec: servicePort: 80 - path: /ordering-api backend: - serviceName: ordering + serviceName: ocelot servicePort: 80 - path: /webmvc backend: @@ -41,11 +41,11 @@ spec: servicePort: 80 - path: /payment-api backend: - serviceName: payment + serviceName: ocelot servicePort: 80 - path: /locations-api backend: - serviceName: locations + serviceName: ocelot servicePort: 80 - path: / backend: diff --git a/k8s/ocelot/configuration.json b/k8s/ocelot/configuration.json new file mode 100644 index 000000000..7a94504c3 --- /dev/null +++ b/k8s/ocelot/configuration.json @@ -0,0 +1,58 @@ +{ + "ReRoutes": [ + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "ordering", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/ordering-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "basket", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/basket-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "catalog", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/catalog-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "marketing", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/marketing-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "payment", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/payment-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "locations", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/location-api/{everything}", + "UpstreamHttpMethod": [] + } + + ], + "GlobalConfiguration": { + "RequestIdKey": "OcRequestId", + "AdministrationPath": "/administration" + } + } + \ No newline at end of file diff --git a/k8s/ocelot/deployment.yaml b/k8s/ocelot/deployment.yaml new file mode 100644 index 000000000..4800172e6 --- /dev/null +++ b/k8s/ocelot/deployment.yaml @@ -0,0 +1,31 @@ +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: ocelot +spec: + paused: true + template: + metadata: + labels: + app: eshop + component: ocelot + spec: + containers: + - name: ocelot + image: eshop/ocelotapigw + imagePullPolicy: Always + ports: + - containerPort: 80 + volumeMounts: + - name: config + mountPath: /app/configuration + volumes: + - name: config + configMap: + name: ocelot + items: + - key: configuration.json + path: configuration.json + imagePullSecrets: + - name: registry-key + diff --git a/k8s/ocelot/service.yaml b/k8s/ocelot/service.yaml new file mode 100644 index 000000000..6b376ca78 --- /dev/null +++ b/k8s/ocelot/service.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: eshop + component: ocelot + name: ocelot +spec: + ports: + - port: 80 + selector: + app: eshop + component: ocelot diff --git a/src/Apigw/OcelotApiGw/Dockerfile b/src/Apigw/OcelotApiGw/Dockerfile index 06b842479..aec2dc4e3 100644 --- a/src/Apigw/OcelotApiGw/Dockerfile +++ b/src/Apigw/OcelotApiGw/Dockerfile @@ -4,9 +4,8 @@ EXPOSE 80 FROM microsoft/aspnetcore-build:2.0 AS build WORKDIR /src -COPY *.sln ./ COPY src/Apigw/OcelotApiGw/OcelotApiGw.csproj src/Apigw/OcelotApiGw/ -RUN dotnet restore +RUN dotnet restore src/Apigw/OcelotApiGw/ COPY . . WORKDIR /src/src/Apigw/OcelotApiGw RUN dotnet build -c Release -o /app diff --git a/src/Apigw/OcelotApiGw/OcelotApiGw.csproj b/src/Apigw/OcelotApiGw/OcelotApiGw.csproj index 2d176adde..4f6ce6e77 100644 --- a/src/Apigw/OcelotApiGw/OcelotApiGw.csproj +++ b/src/Apigw/OcelotApiGw/OcelotApiGw.csproj @@ -10,6 +10,7 @@ + diff --git a/src/Apigw/OcelotApiGw/Program.cs b/src/Apigw/OcelotApiGw/Program.cs index 43be3c57c..bd5230b96 100644 --- a/src/Apigw/OcelotApiGw/Program.cs +++ b/src/Apigw/OcelotApiGw/Program.cs @@ -19,6 +19,7 @@ namespace OcelotApiGw public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) + .ConfigureAppConfiguration(ic => ic.AddJsonFile(Path.Combine("configuration", "configuration.json"))) .UseStartup() .Build(); } diff --git a/src/Apigw/OcelotApiGw/Startup.cs b/src/Apigw/OcelotApiGw/Startup.cs index 671bb72aa..8e99afaf2 100644 --- a/src/Apigw/OcelotApiGw/Startup.cs +++ b/src/Apigw/OcelotApiGw/Startup.cs @@ -2,22 +2,33 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using CacheManager.Core; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; namespace OcelotApiGw { public class Startup { - // This method gets called by the runtime. Use this method to add services to the container. - // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + + private readonly IConfiguration _cfg; + + public Startup(IConfiguration configuration) + { + _cfg = configuration; + } + public void ConfigureServices(IServiceCollection services) { + services.AddOcelot(_cfg); } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) @@ -25,10 +36,7 @@ namespace OcelotApiGw app.UseDeveloperExceptionPage(); } - app.Run(async (context) => - { - await context.Response.WriteAsync("Hello World!"); - }); + app.UseOcelot(); } } } diff --git a/src/Apigw/OcelotApiGw/configuration/configuration.json b/src/Apigw/OcelotApiGw/configuration/configuration.json new file mode 100644 index 000000000..6e9513993 --- /dev/null +++ b/src/Apigw/OcelotApiGw/configuration/configuration.json @@ -0,0 +1,58 @@ +{ + "ReRoutes": [ + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "ordering.api", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/orders-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "basket.api", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/basket-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "catalog.api", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/catalog-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "marketing.api", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/marketing-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "payment.api", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/payment-api/{everything}", + "UpstreamHttpMethod": [] + }, + { + "DownstreamPathTemplate": "/{everything}", + "DownstreamScheme": "http", + "DownstreamHost": "locations.api", + "DownstreamPort": 80, + "UpstreamPathTemplate": "/location-api/{everything}", + "UpstreamHttpMethod": [] + } + + ], + "GlobalConfiguration": { + "RequestIdKey": "OcRequestId", + "AdministrationPath": "/administration" + } + } + \ No newline at end of file diff --git a/xglobal.json b/xglobal.json new file mode 100644 index 000000000..2ab18dceb --- /dev/null +++ b/xglobal.json @@ -0,0 +1,5 @@ +{ + "sdk": { + "version":"2.1.2" + } +} \ No newline at end of file