Skip to content

Commit ff2809e

Browse files
authored
add AKS with kubelogin + MSI example (kubernetes-client#1523)
* Add AKS Kubelogin example code * Update kubelogin path and add instructions in README
1 parent 6cc9c21 commit ff2809e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

examples/aks-kubelogin/Program.cs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using k8s;
2+
using System;
3+
using System.IO;
4+
using System.Text;
5+
6+
var server = "https://example.hcp.eastus.azmk8s.io"; // the server url of your aks
7+
var clientid = "00000000-0000-0000-0000-000000000000"; // the client id of the your msi
8+
var kubelogin = @"C:\bin\kubelogin.exe"; // the path to the kubelogin.exe
9+
10+
using var configstream = new MemoryStream(Encoding.ASCII.GetBytes($"""
11+
apiVersion: v1
12+
clusters:
13+
- cluster:
14+
insecure-skip-tls-verify: true
15+
server: {server}
16+
name: aks
17+
contexts:
18+
- context:
19+
cluster: aks
20+
user: msi
21+
name: aks
22+
current-context: aks
23+
kind: Config
24+
users:
25+
- name: msi
26+
user:
27+
exec:
28+
apiVersion: client.authentication.k8s.io/v1beta1
29+
args:
30+
- get-token
31+
- --login
32+
- msi
33+
- --server-id
34+
- 6dae42f8-4368-4678-94ff-3960e28e3630
35+
- --client-id
36+
- {clientid}
37+
command: {kubelogin}
38+
env: null
39+
"""));
40+
41+
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(configstream);
42+
IKubernetes client = new Kubernetes(config);
43+
Console.WriteLine("Starting Request!");
44+
45+
var list = client.CoreV1.ListNamespacedPod("default");
46+
foreach (var item in list.Items)
47+
{
48+
Console.WriteLine(item.Metadata.Name);
49+
}

examples/aks-kubelogin/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# AKS C# example using kubelogin + MSI
2+
3+
This example shows how to use the [kubelogin](https://github.com/Azure/kubelogin) to authenticate using [managed identities](https://learn.microsoft.com/en-us/entra/identity/managed-identities-azure-resources/overview) with Azure Kubernetes Service (AKS) using the C# SDK.
4+
5+
6+
## Prerequisites
7+
8+
- turn on AAD support for AKS, see [here](https://docs.microsoft.com/en-us/azure/aks/managed-aad)
9+
- create a managed identity for the AKS cluster
10+
- assign the managed identity the `Azure Kubernetes Service RBAC Cluster Admin` (or other RBAC permission) on the AKS cluster
11+
- assign the managed identity to the VM, see [here](https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm)
12+
- install the [kubelogin](https://github.com/Azure/kubelogin) to your machine
13+
14+
## Running the code
15+
16+
*You must the the code on VM with MSI*
17+
18+
- Replace `server` with the address of your AKS cluster
19+
- Replace `clientid` with the client id of the managed identity
20+
- Replace `kubelogin` with the path to the kubelogin executable
21+
22+
```
23+
dotnet run
24+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
</PropertyGroup>
5+
</Project>

0 commit comments

Comments
 (0)