KubernetesClient を使って AKS で動くPod のログを取得する

そのものズバリ KubernetesClient. ReadNamespacedPodLogAsync メソッドで、Pod が標準出力に出力したログを取得できた。

using k8s;
using k8s.Models;

var config = KubernetesClientConfiguration.BuildDefaultConfig();

// AKS が HTTP/2 をサポートしていないのか、ハンドシェイクで失敗する。
// HTTP/2 を使わないようにしておく。
config.DisableHttp2 = true;

var client = new Kubernetes(config);

var namespaceName = "your namespace";

var pods = await client.ListNamespacedPodAsync(namespaceName);

foreach (var pod in pods)
{
    var logs = await client.ReadNamespacedPodLogAsync(pod.Name(), namespaceName);
    using (var reader = new StreamReader(logs))
    {
        while (!reader.EndOfStream)
        {
            var line = await reader.ReadLineAsync();
            Console.WriteLine(line);
        }
    }
}

Console.ReadLine();