Retrieving Kubernetes pod status with C#
Posted: (EET/GMT+2)
In my previous post around Midsummer, I gave an example of using C# to call the Docker CLI and check local container status. The same idea also works with Kubernetes and the kubectl tool.
This can be useful for internal diagnostics, developer tools, build agents, or simple status checks in lab environments.
The basic version calls kubectl get pods and prints the result.
using System.Diagnostics;
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "kubectl",
Arguments = "get pods --all-namespaces",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
};
using Process process = Process.Start(startInfo)!;
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0)
{
Console.WriteLine(error);
return;
}
Console.WriteLine(output);
The output is the same table you would normally see in the terminal.
NAMESPACE NAME READY STATUS RESTARTS AGE default web-api-7c9fd4d5c8-8xq2m 1/1 Running 0 12m default worker-6f458bb7d4-r9q2p 1/1 Running 1 30m kube-system coredns-787d4945fb-h7x28 1/1 Running 0 2d
For a quick status check, this may be enough.
You can also limit the query to one namespace:
kubectl get pods -n default
And update the C# arguments:
Arguments = "get pods -n default"
Note that this uses the current Kubernetes context. The result depends on the same configuration that kubectl uses in your normal terminal session.
You can check the current context with:
kubectl config current-context
This approach works well when you want a small .NET helper tool without introducing a full Kubernetes client library.
For production monitoring, use proper observability tooling. For local diagnostics and developer workflows, calling kubectl from C# can be a simple and practical option.
Tip: you can improve this by asking Docker and Kubernetes for JSON output and parsing it in C# instead of reading formatted text.