Accessing the Yandex Cloud API from a Managed Service for Kubernetes cluster using a workload identity federation in Identity and Access Management
Managed Service for Kubernetes supports integration with Identity and Access Management workload identity federations.
Workload identity federations allow you to configure a link between external systems and Yandex Cloud via the OpenID Connect
When this option is enabled, Managed Service for Kubernetes automatically creates an OIDC provider for the specific cluster and provides the following parameters for integration with workload identity federations:
Issuer URL.JWKS key set URL.
This tutorial exemplifies getting the value of a Yandex Lockbox secret from a Managed Service for Kubernetes using an Identity and Access Management service account.
Similarly, you can perform any action via the Yandex Cloud CLI, Terraform, an SDK, or the API.
Note
This tutorial demonstrates an example of integrating a Managed Service for Kubernetes cluster with a workload identity federation. For a tutorial on integrating a custom Kubernetes installation, see Getting the Yandex Lockbox secret value on the custom Kubernetes installation side.
To configure access to a Yandex Lockbox secret from a Managed Service for Kubernetes cluster via the Yandex Cloud API using a workload identity federation:
- Get your cloud ready.
- Configure the Managed Service for Kubernetes cluster.
- Create a workload identity federation.
- Set up an IAM service account.
- Link the IAM service account to the federation.
- Create a Yandex Lockbox secret.
- Test the integration.
If you no longer need the resources you created, delete them.
Get your cloud ready
Sign up for Yandex Cloud and create a billing account:
- Navigate to the management console
and log in to Yandex Cloud or create a new account. - On the Yandex Cloud Billing
page, make sure you have a billing account linked and it has theACTIVEorTRIAL_ACTIVEstatus. If you do not have a billing account, create one and link a cloud to it.
If you have an active billing account, you can navigate to the cloud page
Learn more about clouds and folders here.
Required paid resources
The infrastructure support costs include:
- Fee for computing resources and disks for Managed Service for Kubernetes cluster nodes (see Yandex Compute Cloud pricing).
- Fee for using the Managed Service for Kubernetes master and outgoing traffic (see Managed Service for Kubernetes pricing).
- Fee for a Managed Service for Kubernetes cluster's public IP addresses (see Yandex Virtual Private Cloud pricing).
- Fee for storing the secret and requests to it (see Yandex Lockbox pricing).
Set up a Managed Service for Kubernetes cluster
Warning
Integration with a workload identity federation only works for Managed Service for Kubernetes clusters 1.30 or higher in the rapid release channel.
To use the Yandex Cloud API, your cluster nodes must have internet access.
-
If you do not have a Managed Service for Kubernetes cluster yet:
- Create a cluster.
- Create a node group.
- Set up security groups for the cluster and node group.
-
To use the Yandex Cloud API, your cluster nodes must have internet access. Make sure your cluster nodes have public IP addresses assigned to them or the subnet they reside in has a configured NAT gateway. Also make sure the security group rules allow unrestricted outgoing traffic for your cluster nodes.
-
Set up integration with a workload identity federation:
Management console- In the management console
, select the folder with your cluster. - In the list of services, select Managed Service for Kubernetes.
- Click
next to the cluster and select Edit. - In the Identity and access management field, enable workload identity federations.
- Click Save.
- Under Identity and access management on the cluster overview page, copy and save the Issuer URL and JWKS URL values. You will need them later.
- In the management console
-
Create a Kubernetes service account:
-
Connect to the Managed Service for Kubernetes cluster using
kubectl. -
Create a manifest named
service-account.yamlfor the Kubernetes service account with the following contents:apiVersion: v1 kind: ServiceAccount metadata: name: wlif -
Apply the manifest:
kubectl apply -f service-account.yaml -
To check the
ServiceAccountresource you created, run this command:kubectl describe serviceaccount wlifResult:
Name: wlif Namespace: default ... -
Save the values of the
NameandNamespacefields, as you will need them later.
-
Create a workload identity federation
- In the management console
, select the folder you want to access via the Yandex Cloud API. - In the list of services, select Identity and Access Management.
- In the left-hand panel, select
Workload identity federations. - Click Create federation.
- In the Issuer value (iss) field, enter the Issuer URL value you previously saved, e.g.,
https://storage.yandexcloud.net/mk8s-oidc/v1/clusters/c49i54tk66ob********. - In the Acceptable Audience values (AUD) field, enter the Issuer URL value as well.
- In the JWKS address field, enter the JWKS URL value you previously saved, e.g.,
https://storage.yandexcloud.net/mk8s-oidc/v1/clusters/c49i54tk66ob********/jwks.json. - In the Name field, enter a name for the federation, e.g.,
test-iam-federation. - Click Create.
Set up an IAM service account
-
Create a IAM service account:
Management console- In the management console
, select the folder with the Yandex Lockbox secret. - In the list of services, select Identity and Access Management.
- Click Create service account.
- Enter a name for the service account, e.g.,
sa-lockbox. - Click Create.
- Select the service account you created and save its ID, as you will need it later.
- In the management console
-
Assign the
lockbox.payloadViewerrole for the folder to the service account:Management console- In the management console
, select the folder where you created the service account. - Navigate to the Access bindings tab.
- Click Configure access.
- In the window that opens, select Service accounts.
- Select the service account you created earlier.
- Click
Add role and select thelockbox.payloadViewerrole. - Click Save.
- In the management console
Link the IAM service account to the federation
-
In the management console
, select the folder the service account was created in. -
In the list of services, select Identity and Access Management.
-
In the list of service accounts, select
sa-lockbox. -
Navigate to the Workload identity federations tab.
-
Click Link to federation.
-
In the Workload identity federation field, select the federation you created earlier.
-
In the Subject value (sub) field, specify the ID of the previously created Kubernetes service account in
system:serviceaccount:<namespace>:<account_name>format, where:namespace:Namespacefield value for the Kubernetes service account, e.g.,default.account_name:Namefield value for the Kubernetes service account, e.g.,wlif.
-
Click Link.
Create a Yandex Lockbox secret
- In the management console
, select the folder where you want to create your secret. - In the list of services, select Lockbox.
- Click Create secret.
- In the Name field, enter a name for the secret:
MY_SECRET. - Select
CustomSecret type. - In the Key field, enter a non-secret ID, e.g.,
test-secret. - In the Value field, enter the confidential data you want to store, e.g.,
hello-world. - Click Create.
- Save the ID of the secret. You will need it later.
Test the integration
-
Connect to the Managed Service for Kubernetes cluster using
kubectl. -
Create a test pod manifest named
pod.yamlwith the following contents:apiVersion: v1 kind: Pod metadata: name: test-wlif spec: containers: - image: nginx name: nginx volumeMounts: - mountPath: /var/run/secrets/tokens name: sa-token serviceAccountName: wlif volumes: - name: sa-token projected: sources: - serviceAccountToken: path: sa-token expirationSeconds: 7200 audience: https://storage.yandexcloud.net/mk8s-oidc/v1/clusters/c49i54tk66ob********Where:
spec:serviceAccountName: Name of the Kubernetes service account you created earlier.spec:volumes:projected:sources:serviceAccountToken:audience: Acceptable Audience values (AUD) value set when creating the federation.
-
Apply the manifest:
kubectl apply -f pod.yaml -
Make sure the status of the new pod switched to
Running:kubectl get pod test-wlifResult:
NAME READY STATUS RESTARTS AGE test-wlif 1/1 Running 0 1m -
Access the container:
kubectl exec test-wlif -it -- bash -
Set the required variables:
SA_ID="<IAM_service_account_ID>" && \ SECRET_ID="<Lockbox_secret_ID>" && \ SA_TOKEN="$(cat /var/run/secrets/tokens/sa-token)"Where:
SA_ID: IAM service account ID you got earlier.SECRET_ID: Yandex Lockbox secret ID you got earlier.SA_TOKEN: Kubernetes service account token.
-
Install
jq:apt-get update >/dev/null 2>&1 && apt-get install -y jq >/dev/null 2>&1 -
Get an IAM token:
IAMTOKEN=$(curl -sH "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=urn:ietf:params:oauth:grant-type:token-exchange&requested_token_type=urn:ietf:params:oauth:token-type:access_token&audience=${SA_ID}&subject_token=${SA_TOKEN}&subject_token_type=urn:ietf:params:oauth:token-type:id_token" \ -X POST https://auth.yandex.cloud/oauth/token | jq -r '.access_token') -
Get the Yandex Lockbox secret:
curl -sH "Authorization: Bearer ${IAMTOKEN}" \ "https://payload.lockbox.api.cloud.yandex.net/lockbox/v1/secrets/${SECRET_ID}/payload"Result:
{ "entries": [ { "key": "test-secret", "textValue": "hello-world" } ], "versionId": "e6qsqiadrsnhduubgijb" }
How to delete the resources you created
To stop paying for the resources you created:
- Delete the Yandex Lockbox secret.
- Delete the Managed Service for Kubernetes node group.
- Delete the Managed Service for Kubernetes cluster.