github.com/Azure/aad-pod-identity@v1.8.17/website/content/en/docs/Demo/standard_walkthrough.md (about)

     1  ---
     2  title: "Standard Walkthrough"
     3  linkTitle: "Standard Walkthrough"
     4  weight: 1
     5  description: >
     6    You will need Azure CLI installed and a Kubernetes cluster running on Azure, either managed by AKS or provisioned with AKS Engine.
     7  ---
     8  
     9  Run the following commands to set Azure-related environment variables and login to Azure via `az login`:
    10  
    11  ```bash
    12  export SUBSCRIPTION_ID="<SubscriptionID>"
    13  
    14  # login as a user and set the appropriate subscription ID
    15  az login
    16  az account set -s "${SUBSCRIPTION_ID}"
    17  
    18  export RESOURCE_GROUP="<AKSResourceGroup>"
    19  export CLUSTER_NAME="<AKSClusterName>"
    20  
    21  # for this demo, we will be deploying a user-assigned identity to the AKS node resource group
    22  export IDENTITY_RESOURCE_GROUP="$(az aks show -g ${RESOURCE_GROUP} -n ${CLUSTER_NAME} --query nodeResourceGroup -otsv)"
    23  export IDENTITY_NAME="demo"
    24  ```
    25  
    26  > For AKS clusters, there are two resource groups that you need to be aware of - the resource group where you deploy your AKS cluster to (denoted by the environment variable `RESOURCE_GROUP`), and the node resource group (`MC_<AKSResourceGroup>_<AKSClusterName>_<AKSClusterLocation>`). The latter contains all of the infrastructure resources associated with the cluster like VM/VMSS and VNet. Depending on where you deploy your user-assigned identities, you might need additional role assignments. Please refer to [Role Assignment](../../getting-started/role-assignment/) for more information. For this demo, it is recommended to deploy the demo identity to your node resource group (the one with `MC_` prefix).
    27  
    28  ### 1. Deploy aad-pod-identity
    29  
    30  Deploy `aad-pod-identity` components to an RBAC-enabled cluster:
    31  
    32  ```bash
    33  kubectl apply -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment-rbac.yaml
    34  
    35  # For AKS clusters, deploy the MIC and AKS add-on exception by running -
    36  kubectl apply -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/mic-exception.yaml
    37  ```
    38  
    39  Deploy `aad-pod-identity` components to a non-RBAC cluster:
    40  
    41  ```bash
    42  kubectl apply -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/deployment.yaml
    43  
    44  # For AKS clusters, deploy the MIC and AKS add-on exception by running -
    45  kubectl apply -f https://raw.githubusercontent.com/Azure/aad-pod-identity/master/deploy/infra/mic-exception.yaml
    46  ```
    47  
    48  Deploy `aad-pod-identity` using [Helm 3](https://v3.helm.sh/):
    49  
    50  ```bash
    51  helm repo add aad-pod-identity https://raw.githubusercontent.com/Azure/aad-pod-identity/master/charts
    52  helm install aad-pod-identity aad-pod-identity/aad-pod-identity
    53  ```
    54  
    55  For a list of overwritable values when installing with Helm, please refer to [this section](https://github.com/Azure/aad-pod-identity/tree/master/charts/aad-pod-identity#configuration).
    56  
    57  > Important: For AKS clusters with [limited egress traffic](https://docs.microsoft.com/en-us/azure/aks/limit-egress-traffic), Please install aad-pod-identity in `kube-system` namespace using the helm charts.
    58  
    59  ```bash
    60  helm install aad-pod-identity aad-pod-identity/aad-pod-identity --namespace=kube-system
    61  ```
    62  
    63  ### 2. Create an identity on Azure
    64  
    65  Create an identity on Azure and store the client ID and resource ID of the identity as environment variables:
    66  
    67  ```bash
    68  az identity create -g ${IDENTITY_RESOURCE_GROUP} -n ${IDENTITY_NAME}
    69  export IDENTITY_CLIENT_ID="$(az identity show -g ${IDENTITY_RESOURCE_GROUP} -n ${IDENTITY_NAME} --query clientId -otsv)"
    70  export IDENTITY_RESOURCE_ID="$(az identity show -g ${IDENTITY_RESOURCE_GROUP} -n ${IDENTITY_NAME} --query id -otsv)"
    71  ```
    72  
    73  ### 3. Deploy `AzureIdentity`
    74  
    75  Create an `AzureIdentity` in your cluster that references the identity you created above:
    76  
    77  ```bash
    78  cat <<EOF | kubectl apply -f -
    79  apiVersion: "aadpodidentity.k8s.io/v1"
    80  kind: AzureIdentity
    81  metadata:
    82    name: ${IDENTITY_NAME}
    83  spec:
    84    type: 0
    85    resourceID: ${IDENTITY_RESOURCE_ID}
    86    clientID: ${IDENTITY_CLIENT_ID}
    87  EOF
    88  ```
    89  
    90  > Set `type: 0` for user-assigned MSI, `type: 1` for Service Principal with client secret, or `type: 2` for Service Principal with certificate. For more information, see [here](https://github.com/Azure/aad-pod-identity/tree/master/deploy/demo).
    91  
    92  ### 4. (Optional) Match pods in the namespace
    93  
    94  For matching pods in the namespace, please refer to the [namespaced documentation](../../configure/match_pods_in_namespace/).
    95  
    96  ### 5. Deploy `AzureIdentityBinding`
    97  
    98  Create an `AzureIdentityBinding` that reference the `AzureIdentity` you created above:
    99  
   100  ```bash
   101  cat <<EOF | kubectl apply -f -
   102  apiVersion: "aadpodidentity.k8s.io/v1"
   103  kind: AzureIdentityBinding
   104  metadata:
   105    name: ${IDENTITY_NAME}-binding
   106  spec:
   107    azureIdentity: ${IDENTITY_NAME}
   108    selector: ${IDENTITY_NAME}
   109  EOF
   110  ```
   111  
   112  ### 6. Deployment and Validation
   113  
   114  For a pod to match an identity binding, it needs a label with the key `aadpodidbinding` whose value is that of the `selector:` field in the `AzureIdentityBinding`. Deploy a pod that validates the functionality:
   115  
   116  ```bash
   117  cat << EOF | kubectl apply -f -
   118  apiVersion: v1
   119  kind: Pod
   120  metadata:
   121    name: demo
   122    labels:
   123      aadpodidbinding: $IDENTITY_NAME
   124  spec:
   125    containers:
   126    - name: demo
   127      image: mcr.microsoft.com/oss/azure/aad-pod-identity/demo:v1.8.17
   128      args:
   129        - --subscription-id=${SUBSCRIPTION_ID}
   130        - --resource-group=${IDENTITY_RESOURCE_GROUP}
   131        - --identity-client-id=${IDENTITY_CLIENT_ID}
   132    nodeSelector:
   133      kubernetes.io/os: linux
   134  EOF
   135  ```
   136  
   137  > `mcr.microsoft.com/oss/azure/aad-pod-identity/demo` is an image that demonstrates the use of AAD pod identity. The source code can be found [here](https://github.com/Azure/aad-pod-identity/blob/master/cmd/demo/main.go).
   138  
   139  To verify that the pod is indeed using the identity correctly:
   140  
   141  ```bash
   142  kubectl logs demo
   143  ```
   144  
   145  If successful, the log output would be similar to the following output:
   146  
   147  ```log
   148  I0510 18:16:53.042124       1 main.go:128] curl -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2017-08-01": {"compute":{"location":"westus2","name":"aks-nodepool1-17529566-vmss_1","offer":"aks","osType":"Linux","placementGroupId":"877d5750-2bed-43dd-bad6-62e4f3b58a3c","platformFaultDomain":"0","platformUpdateDomain":"1","publisher":"microsoft-aks","resourceGroupName":"MC_chuwon_chuwon_westus2","sku":"aks-ubuntu-1804-gen2-2021-q1","subscriptionId":"2d31b5ab-0ddc-4991-bf8d-61b6ad196f5a","tags":"aksEngineVersion:v0.47.0-aks-gomod-b4-aks;creationSource:aks-aks-nodepool1-17529566-vmss;orchestrator:Kubernetes:1.18.14;poolName:nodepool1;resourceNameSuffix:17529566","version":"2021.01.28","vmId":"4fc9f60c-170c-4e76-84ff-81c6c0cecea1","vmSize":"Standard_DS2_v2"},"network":{"interface":[{"ipv4":{"ipAddress":[{"privateIpAddress":"10.240.0.5","publicIpAddress":""}],"subnet":[{"address":"10.240.0.0","prefix":"16"}]},"ipv6":{"ipAddress":[]},"macAddress":"000D3AFE98BF"}]}}
   149  I0510 18:17:04.474588       1 main.go:100] successfully acquired a service principal token from http://169.254.169.254/metadata/identity/oauth2/token using a user-assigned identity (a9979fb6-6655-4612-95c9-7e4d0c83001b)
   150  I0510 18:17:04.474610       1 main.go:50] Try decoding your token <JWT token> at https://jwt.io
   151  ```
   152  
   153  Once you are done with the demo, clean up your resources:
   154  
   155  ```bash
   156  kubectl delete pod demo
   157  kubectl delete azureidentity ${IDENTITY_NAME}
   158  kubectl delete azureidentitybinding ${IDENTITY_NAME}-binding
   159  az role assignment delete --id ${IDENTITY_ASSIGNMENT_ID}
   160  az identity delete -g ${IDENTITY_RESOURCE_GROUP} -n ${IDENTITY_NAME}
   161  ```
   162  
   163  ## Uninstall Notes
   164  
   165  The NMI pods modify the nodes' [iptables] to intercept calls to IMDS endpoint within a node. This allows NMI to insert identities assigned to a pod before executing the request on behalf of the caller.
   166  
   167  These iptables entries will be cleaned up when the pod-identity pods are uninstalled. However, if the pods are terminated for unexpected reasons, the iptables entries can be removed with these commands on the node:
   168  
   169  ```bash
   170  # remove the custom chain reference
   171  iptables -t nat -D PREROUTING -j aad-metadata
   172  
   173  # flush the custom chain
   174  iptables -t nat -F aad-metadata
   175  
   176  # remove the custom chain
   177  iptables -t nat -X aad-metadata
   178  ```