新增learn-kubernetes(https://github.com/yyong-brs/learn-kubernetes)相关文件
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
version: "3.7"
|
||||
|
||||
services:
|
||||
ch07-timecheck:
|
||||
image: kiamol/ch07-timecheck:latest-linux-amd64
|
||||
|
||||
ch07-simple-proxy:
|
||||
image: kiamol/ch07-simple-proxy:latest-linux-amd64
|
||||
@@ -0,0 +1,8 @@
|
||||
version: "3.7"
|
||||
|
||||
services:
|
||||
ch07-timecheck:
|
||||
image: kiamol/ch07-timecheck:latest-linux-arm64
|
||||
|
||||
ch07-simple-proxy:
|
||||
image: kiamol/ch07-simple-proxy:latest-linux-arm64
|
||||
@@ -0,0 +1,12 @@
|
||||
version: "3.6"
|
||||
|
||||
services:
|
||||
ch07-timecheck:
|
||||
image: kiamol/ch07-timecheck:latest
|
||||
build:
|
||||
context: ./timecheck
|
||||
|
||||
ch07-simple-proxy:
|
||||
image: kiamol/ch07-simple-proxy:latest
|
||||
build:
|
||||
context: ./simple-proxy
|
||||
@@ -0,0 +1,10 @@
|
||||
$images=$(yq e '.services.[].image' docker-compose.yml)
|
||||
|
||||
foreach ($image in $images)
|
||||
{
|
||||
docker manifest create --amend $image `
|
||||
"$($image)-linux-arm64" `
|
||||
"$($image)-linux-amd64"
|
||||
|
||||
docker manifest push $image
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS builder
|
||||
|
||||
WORKDIR /src
|
||||
COPY src/SimpleProxy.csproj .
|
||||
RUN dotnet restore
|
||||
|
||||
COPY src /src
|
||||
RUN dotnet publish -c Release -o /out SimpleProxy.csproj
|
||||
|
||||
# app image
|
||||
FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine
|
||||
|
||||
EXPOSE 1080
|
||||
WORKDIR /app
|
||||
ENTRYPOINT ["dotnet", "SimpleProxy.dll"]
|
||||
|
||||
COPY --from=builder /out/ .
|
||||
@@ -0,0 +1,65 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Titanium.Web.Proxy;
|
||||
using Titanium.Web.Proxy.EventArguments;
|
||||
using Titanium.Web.Proxy.Models;
|
||||
|
||||
namespace SimpleProxy
|
||||
{
|
||||
class Program
|
||||
{
|
||||
private static ManualResetEvent _ResetEvent = new ManualResetEvent(false);
|
||||
private static IConfiguration _Config;
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
_Config = GetConfig();
|
||||
|
||||
var port = _Config.GetValue<int>("Proxy:Port");
|
||||
var proxyServer = new ProxyServer();
|
||||
proxyServer.BeforeRequest += OnRequest;
|
||||
|
||||
var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, port, false);
|
||||
proxyServer.AddEndPoint(explicitEndPoint);
|
||||
proxyServer.Start();
|
||||
|
||||
Console.WriteLine($"** Logging proxy listening on port: {port} **");
|
||||
_ResetEvent.WaitOne();
|
||||
|
||||
proxyServer.BeforeRequest -= OnRequest;
|
||||
proxyServer.Stop();
|
||||
}
|
||||
|
||||
private static IConfiguration GetConfig()
|
||||
{
|
||||
return new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
}
|
||||
|
||||
public static Task OnRequest(object sender, SessionEventArgs e)
|
||||
{
|
||||
var sourceUri = e.HttpClient.Request.RequestUri.AbsoluteUri;
|
||||
if (sourceUri == _Config.GetValue<string>("Proxy:Request:UriMap:Source"))
|
||||
{
|
||||
var targetUri = _Config.GetValue<string>("Proxy:Request:UriMap:Target");
|
||||
Console.WriteLine($"{e.HttpClient.Request.Method} {sourceUri} -> {targetUri}");
|
||||
e.HttpClient.Request.RequestUri = new Uri(targetUri);
|
||||
}
|
||||
else if (_Config.GetValue<bool>("Proxy:Request:RejectUnknown"))
|
||||
{
|
||||
Console.WriteLine($"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url} [BLOCKED]");
|
||||
e.Ok("");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"{e.HttpClient.Request.Method} {e.HttpClient.Request.Url}");
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||
<PackageReference Include="Titanium.Web.Proxy" Version="3.1.1301" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings.json">
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"Proxy": {
|
||||
"Port": 1080,
|
||||
"Request": {
|
||||
"RejectUnknown": true,
|
||||
"UriMap": {
|
||||
"Source": "http://localhost/api",
|
||||
"Target": "http://blog.sixeyed.com"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS builder
|
||||
|
||||
WORKDIR /src
|
||||
COPY src/TimeCheck.csproj .
|
||||
RUN dotnet restore
|
||||
|
||||
COPY src /src
|
||||
RUN dotnet publish -c Release -o /out TimeCheck.csproj
|
||||
|
||||
# app image
|
||||
FROM mcr.microsoft.com/dotnet/runtime:6.0-alpine
|
||||
|
||||
COPY src/appsettings.json /config/appsettings.json
|
||||
|
||||
WORKDIR /app
|
||||
ENTRYPOINT ["dotnet", "TimeCheck.dll"]
|
||||
|
||||
COPY --from=builder /out/ .
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Timers;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Prometheus;
|
||||
using Serilog;
|
||||
|
||||
namespace Kiamol.Ch07.TimeCheck
|
||||
{
|
||||
class Program
|
||||
{
|
||||
private static readonly ManualResetEvent _ResetEvent = new ManualResetEvent(false);
|
||||
private static Counter _CheckCounter;
|
||||
private static string _Version;
|
||||
private static string _Env;
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
var config = new ConfigurationBuilder()
|
||||
.AddJsonFile("appsettings.json")
|
||||
.AddJsonFile("/config/appsettings.json", optional: true)
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
_Version = config["Application:Version"];
|
||||
_Env = config["Application:Environment"];
|
||||
var intervalSeconds = config.GetValue<int>("Timer:IntervalSeconds") * 1000;
|
||||
var metricsEnabled = config.GetValue<bool>("Metrics:Enabled");
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Information()
|
||||
.WriteTo.File("/logs/timecheck.log", shared: true, flushToDiskInterval: TimeSpan.FromSeconds(intervalSeconds))
|
||||
.CreateLogger();
|
||||
|
||||
if (metricsEnabled)
|
||||
{
|
||||
_CheckCounter = Metrics.CreateCounter("timecheck_total", "Number of timechecks");
|
||||
StartMetricServer(config);
|
||||
}
|
||||
|
||||
using (var timer = new System.Timers.Timer(intervalSeconds))
|
||||
{
|
||||
timer.Elapsed += WriteTimeCheck;
|
||||
timer.Enabled = true;
|
||||
_ResetEvent.WaitOne();
|
||||
}
|
||||
}
|
||||
|
||||
private static void WriteTimeCheck(Object source, ElapsedEventArgs e)
|
||||
{
|
||||
Log.Information("Environment: {environment}; version: {version}; time check: {timestamp}",
|
||||
_Env, _Version, e.SignalTime.ToString("HH:mm.ss"));
|
||||
|
||||
if (_CheckCounter != null)
|
||||
{
|
||||
_CheckCounter.Inc();
|
||||
}
|
||||
}
|
||||
|
||||
private static void StartMetricServer(IConfiguration config)
|
||||
{
|
||||
var metricsPort = config.GetValue<int>("Metrics:Port");
|
||||
var server = new MetricServer(metricsPort);
|
||||
server.Start();
|
||||
Log.Information("Metrics server listening on port: {metricsPort}", metricsPort);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
|
||||
<PackageReference Include="prometheus-net" Version="3.6.0" />
|
||||
<PackageReference Include="Serilog" Version="2.9.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="appsettings.json">
|
||||
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
|
||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||
</Content>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.30204.135
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimeCheck", "TimeCheck.csproj", "{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FC0AF23D-2E33-4AE5-8ED8-E8F228AA00B0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {502A678D-1443-4853-A777-00FA08C2BF53}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"Application": {
|
||||
"Version": "1.0",
|
||||
"Environment": "DEV"
|
||||
},
|
||||
"Timer": {
|
||||
"IntervalSeconds": "5"
|
||||
},
|
||||
"Metrics": {
|
||||
"Enabled": false,
|
||||
"Port" : 8080
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user