I am currently working on a application which will be running in a kubernetes pod. It is supposed to connect to a postgressql pod, that is ran within the same cluster.
but I can for some reason not deduce what the connection string should be
I for now defined the postgressql deployment as such:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:10.4
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgredb
volumes:
- name: postgredb
persistentVolumeClaim:
claimName: postgres-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: postgres-service
labels:
app: postgres
spec:
ports:
- port: 5432
targetPort: 5432
selector:
app: postgres
but for a connection string
x.UseNpgsql("Host=postgres-service:5432;Database=postgres;Username=postgres;Password=postgres"));
Which does not seem to work?
Something as simple as
using System;
using System.Net.NetworkInformation;
namespace pingMe
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Ping ping = new Ping();
PingReply pingresult = ping.Send("postgres-service.default.svc.cluster.local");
if (pingresult.Status.ToString() == "Success")
{
Console.WriteLine("I can reach");
}
}
}
}
resolve into this:
within the cluster triggers an error
System.Net.NetworkInformation.PingException: An exception occurred during a Ping request.
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name or service not known
at System.Net.Dns.InternalGetHostByName(String hostName)
at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
--- End of inner exception stack trace ---
at System.Net.NetworkInformation.Ping.GetAddressAndSend(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress, Int32 timeout, Byte[] buffer, PingOptions options)
at System.Net.NetworkInformation.Ping.Send(String hostNameOrAddress)
at API.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, SchemaContext schemaContext) in /src/API/Startup.cs:line 42
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.b__2(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
Unhandled exception. System.Net.NetworkInformation.PingException: An exception occurred during a Ping request.
Kubernetes service
kubectl get svc postgres-service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
postgres-service ClusterIP 10.106.91.9 <none> 5432/TCP 74m
Dockerfile:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["pingMe/pingMe.csproj", "pingMe/"]
RUN dotnet restore "pingMe/pingMe.csproj"
COPY . .
WORKDIR "/src/pingMe"
RUN dotnet build "pingMe.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "pingMe.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "pingMe.dll"]
Local pod:
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: local-deployment
spec:
replicas: 1
revisionHistoryLimit: 3
template:
metadata:
labels:
app: local-pod
spec:
containers:
- name: local-deployment
image: api:dev5
imagePullPolicy: Never
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /WeatherForecast
port: 80
initialDelaySeconds: 3
periodSeconds: 3
I am not sure I understand why.. But all my asp. Net core containerized application was not able to resolve the service name..
These had to be resolved in a separate step using https://learn.microsoft.com/en-us/dotnet/api/system.net.dns.gethostname?view=netframework-4.8 , and then passed as an ip address, resolved from the prior step..
I based this upon how nslookup responded, it initially fails as the dns record is not cached, and then resolves the host name as this can be found via broadcast.
I guess since the initial dns lookup fails, it triggers and exception which mine kept failing at...