最近看到一篇文章获取在 Kuberbetes 中的 namespace 通过 kubectl 来获取,并且还要配置 token,觉得太复杂了,我们也有在应用中获取当前所在的 Kubernetes 的 namespace,在 kubernetes 中会有一个默认 in cluster 的配置,不过没有那么多的权限,如果要获取更多 kubernetes 中的信息需要配置 service account 配置 rbac 角色以及权限呢,但是获取当前 namespace 信息默认的权限就完全足够了
Sample这里我先在 k8s 里跑一个 pod 来做一个测试
kubectl run dotnet-exec-playground --image=weihanli/dotnet-exec:0.23.0-web -- "WebApplication.Create().Run();"借助 dotnet-exec 的容器镜像来在 k8s 里跑一个 pod,执行之后可以通过 kubectl logs/kubectl describe 来查看 pod 状态,这里直接查看 pod log kubectl logs dotnet-exec-playground
> kubectl logs dotnet-exec-playgroundinfo: Microsoft.Hosting.Lifetime[14] Now listening on: http://[::]:8080info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down.info: Microsoft.Hosting.Lifetime[0] Hosting environment: Productioninfo: Microsoft.Hosting.Lifetime[0] Content root path: /app看到这样的 log 就证明我们的 pod 跑起来了,接着可以进入到 pod 里执行命令来测试了
这里展示两种实现方式,一种是使用 k8s 的 C# SDK KubernetesClient 可以比较方便地直接获取 InCluster 的 config
Console.WriteLine(KubernetesClientConfiguration.InClusterConfig().Namespace);可以在 pod 里执行命令:
dotnet-exec 'Console.WriteLine(KubernetesClientConfiguration.InClusterConfig().Namespace);' -r "nuget:KubernetesClient" -u k8s这里代表引用 KubernetesClient nuget 包并添加 k8s 命名空间的引用,执行上面的代码,打印 k8s in-clusterf 默认配置的 namespace
可以看到此时已经打印出来了当前的 namespace,我们执行的时候没有指定 namespace 所以是默认的命名空间 default,你也可以指定一个 namespace 来测试
如果不想引用 KubernetesClient 这个包的依赖也是可以的,如果查看下 KubernetesClient 的实现会发现其实 namespace 就是读了一个文件的内容,我们也可以直接读取这个文件,如果想加上前面的判断也是可以的,看自己需要
internal static string ServiceAccountPath = Path.Combine(new string[] {$"{Path.DirectorySeparatorChar}var", "run", "secrets", "kubernetes.io", "serviceaccount", });internal const string ServiceAccountTokenKeyFileName = "token";internal const string ServiceAccountRootCAKeyFileName = "ca.crt";internal const string ServiceAccountNamespaceFileName = "namespace";public static KubernetesClientConfiguration InClusterConfig(){if (!IsInCluster()) {throw new KubeConfigException("Unable to load in-cluster configuration. Missing environment variables KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT or service account token. Hint: consider using option \"automountServiceAccountToken: true\" in deployment declaration."); }var rootCAFile = Path.Combine(ServiceAccountPath, ServiceAccountRootCAKeyFileName);var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST");var port = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_PORT");if (string.IsOrEmpty(host)) { host = "kubernetes.default.svc"; }if (string.IsOrEmpty(port)) { port = "443"; }var result = new KubernetesClientConfiguration { Host = new UriBuilder("https", host, Convert.ToInt32(port)).ToString(), TokenProvider = new TokenFileAuth(Path.Combine(ServiceAccountPath, ServiceAccountTokenKeyFileName)), SslCaCerts = CertUtils.LoadPemFileCert(rootCAFile), };var namespaceFile = Path.Combine(ServiceAccountPath, ServiceAccountNamespaceFileName);if (FileSystem.Current.Exists(namespaceFile)) { result.Namespace = FileSystem.Current.ReadAllText(namespaceFile); }return result;}可以看到 namespace 就是读了 namespaceFile 文件的内容,所以我们也可以直接读取这个文件的内容来获取 namespace 就不需要引用依赖了
Console.WriteLine(File.ReadAllText("/var/run/secrets/kubernetes.io/serviceaccount/namespace"));更新下 dotnet-exec 命令
dotnet-exec 'Console.WriteLine(File.ReadAllText("/var/run/secrets/kubernetes.io/serviceaccount/namespace"));'从结果可以看出两次的结果是一样的
More顺便打个广告 dotnet-exec 从 0.23.0 开始也支持了打印当前 kubernetes namespace 了
可以通过 dotnet-exec info/dotnet --info 来查看