If you have, say a service and client in a single Pod, then you’re supplied a virtual ip address and the services etc. within the Pod are accessible via localhost, but you’re more likely to want to deploy services in their own Pods (unless they are tightly coupled) to allow scaling per service etc.
How do we communicate with a services in a different Pod to our application?
A common scenario here is, we have a a client application, for example the razordemo application in the post Deploying an ASP.NET core application into a Docker image within Kubernetes and it might be using the WeatherForecast service that is created using
dotnet new webapi -o weatherservice --no-https -f net5.0
We’re not going to go into the code of the actual services apart from show the pieces that matter for communication, so let’s assumed we’ve deployed weatherservice to k8s using a configuration such as
apiVersion: apps/v1 kind: Deployment metadata: name: weatherservice namespace: default spec: selector: matchLabels: run: weatherservice replicas: 1 template: metadata: labels: run: weatherservice spec: containers: - name: weatherservice image: localhost:32000/weatherservice imagePullPolicy: IfNotPresent ports: - containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: weatherservice namespace: default labels: run: weatherservice spec: ports: - port: 80 protocol: TCP selector: run: weatherservice
This service will exist in it’s own virtual network running on port 80, we may scale this service according to our needs and without affecting other services or clients.
If we then deploy our razordemo as per Deploying an ASP.NET core application into a Docker image within Kubernetes – it will also exist in it’s own virtual network, also running on port 80.
To communicate from razordemo to the weatherservice we simply use the service name (if we’re on the same cluster) for example http://weatherservice.
Here’s a simple example of razordemo code for getting weatherservice data…
var httpClient = new HttpClient(); httpClient.BaseAddress = new Uri("http://weatherservice"); var response = await httpClient.GetAsync("/weatherforecast"); var results = JsonConvert.DeserializeObject<WeatherForecast[]>(await response.Content.ReadAsStringAsync());
The first two lines will probably be set in your ASP.NET core Startup.cs file, although better still is for us to store the URL within configuration via an environment variable within k8s, so the client is agnostic to the service name that we deployed the weatherservice with.