2021-03-31

dotnet core get k8s self POD name

markdown k8s 中的 dotnet core 應用取得 POD name 三種方式取得的結果相同 ``` class Program { static async Task Main(string[] args) { Console.WriteLine("MachineName:"+Environment.MachineName); Console.WriteLine("HostName:"+ System.Net.Dns.GetHostName()); Console.WriteLine("ENV K8S_POD_NAME:"+Environment.GetEnvironmentVariable("K8S_POD_NAME")); await Task.Run(() =〉 Thread.Sleep(Timeout.Infinite)); } } ``` result on dotnet 3.1.11-alpine3.12 ``` MachineName:mqsend-7d9579cc47-kld6v HostName:mqsend-7d9579cc47-kld6v ENV K8S_POD_NAME:mqsend-7d9579cc47-kld6v ``` ENV需要在deployment時額外加入參考 ``` spec: containers: - env: - name: K8S_POD_NAME valueFrom: fieldRef: fieldPath: metadata.name ``` ref - https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/#use-container-fields-as-values-for-environment-variablesƒ

沒有留言:

adsense