Before you begin
Before you start, make sure that you have performed the following tasks:
- Enable the Google Kubernetes Engine API. Enable Google Kubernetes Engine API
- If you want to use the Google Cloud CLI for this task,
install and then
initialize the
gcloud CLI. If you previously installed the gcloud CLI, get the latest
version by running the
gcloud components updatecommand. Earlier gcloud CLI versions might not support running the commands in this document.
Required roles
To get the permissions that you need to manage IAM service accounts and roles, ask your administrator to grant you the following IAM roles:
-
Security Admin (
roles/iam.securityAdmin) on the cluster project -
Configure service accounts in a separate project:
Security Admin (
roles/iam.securityAdmin) on the service account project
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Grant the minimum required role for GKE
GKE uses IAM service accounts that are attached to your nodes to
run system tasks like logging and monitoring. At a minimum, these node service accounts
must have the
Kubernetes Engine Default Node Service Account
(roles/container.defaultNodeServiceAccount) role on your project. By default,
GKE uses the
Compute Engine default service account,
which is automatically created in your project, as the node service account.
If your organization enforces the
iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint, the default Compute Engine service account in your project might
not automatically get the required permissions for GKE.
The following sections show you how to grant the
roles/container.defaultNodeServiceAccount role to the default
Compute Engine service account or to a new custom service account that
you create.
Configure the default Compute Engine service account
To grant the roles/container.defaultNodeServiceAccount role to the
Compute Engine default service account, complete the following steps:
Console
- Go to the Welcome page:
- In the Project number field, click Copy to clipboard.
- Go to the IAM page:
- Click Grant access.
- In the New principals field, specify the following value:
ReplacePROJECT_NUMBER-compute@developer.gserviceaccount.comPROJECT_NUMBERwith the project number that you copied. - In the Select a role menu, select the Kubernetes Engine Default Node Service Account role.
- Click Save.
gcloud
- Find your Google Cloud project number:
gcloud projects describe PROJECT_ID \ --format="value(projectNumber)"
Replace
PROJECT_IDwith your project ID.The output is similar to the following:
12345678901
- Grant the
roles/container.defaultNodeServiceAccountrole to the Compute Engine default service account:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com" \ --role="roles/container.defaultNodeServiceAccount"
Replace
PROJECT_NUMBERwith the project number from the previous step.
Configure a custom node service account
To create a custom service account and grant it the required role for GKE, complete the following steps:
Console
- Go to the Service accounts page:
- Click Create service account.
- Enter a name for the service account. The Service account ID field automatically generates a unique ID for the service account based on the name.
- Click Create and continue.
- In the Select a role menu, select the Kubernetes Engine Default Node Service Account role.
- Click Done.
gcloud
- Create the service account:
gcloud iam service-accounts create SA_NAME
Replace
SA_NAMEwith a unique name that identifies the service account. - Grant the
Kubernetes Engine Default Node Service Account
(
roles/container.defaultNodeServiceAccount) role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SA_NAME@PROJECT_ID.iam.gserviceaccount.com" \ --role=roles/container.defaultNodeServiceAccount
Replace the following:
PROJECT_ID: your Google Cloud project ID.SA_NAME: the name of the service account that you created.
Terraform
Create an IAM service account and grant it the
roles/container.defaultNodeServiceAccount role on the project:
Config Connector
Note: This step requires Config Connector. Follow the installation instructions to install Config Connector on your cluster.
- To create the service account, download the following resource as
service-account.yaml:Replace the following:
[SA_NAME]: the name of the new service account.[DISPLAY_NAME]: a display name for the service account.
- Create the service account:
kubectl apply -f service-account.yaml
- Apply the
roles/logging.logWriterrole to the service account:- Download the
following resource as
policy-logging.yaml.Replace the following:
[SA_NAME]: the name of the service account.[PROJECT_ID]: your Google Cloud project ID.
- Apply the role to the service account:
kubectl apply -f policy-logging.yaml
- Download the
following resource as
- Apply the
roles/monitoring.metricWriterrole to the service account:- Download the following resource as
policy-metrics-writer.yaml. Replace[SA_NAME]and[PROJECT_ID]with your own information.Replace the following:
[SA_NAME]: the name of the service account.[PROJECT_ID]: your Google Cloud project ID.
- Apply the role to the service account:
kubectl apply -f policy-metrics-writer.yaml
- Download the following resource as
- Apply the
roles/monitoring.viewerrole to the service account:- Download the following resource as
policy-monitoring.yaml.Replace the following:
[SA_NAME]: the name of the service account.[PROJECT_ID]: your Google Cloud project ID.
- Apply the role to the service account:
kubectl apply -f policy-monitoring.yaml
- Download the following resource as
- Apply the
roles/autoscaling.metricsWriterrole to the service account:- Download the following resource as
policy-autoscaling-metrics-writer.yaml.Replace the following:
[SA_NAME]: the name of the service account.[PROJECT_ID]: your Google Cloud project ID.
- Apply the role to the service account:
kubectl apply -f policy-autoscaling-metrics-writer.yaml
- Download the following resource as
You can also use this service account for resources in other projects. For instructions, see Enabling service account impersonation across projects.
Allow principals to attach custom service accounts
You can attach a custom service account when you create a cluster or a node
pool. To let a principal (such as a platform administrator) use a custom service
account to create GKE resources, grant the
Service Account User
(roles/iam.serviceAccountUser) role on the custom service account to that
principal. To grant this role, select one of the following options:
Console
In the Google Cloud console, go to the Service accounts page:
In the resource selector, choose the project that contains your custom service account.
Select the checkbox for the custom service account that you created to use with GKE nodes.
Click Manage access. The Manage Access pane opens.
Click Add principal. The Grant access pane opens.
In the New principals field, specify the principal, such as an administrator group.
In the Select a role drop-down menu, select the Service Account User role.
Click Save. The Grant access pane closes.
Close the Manage Access pane.
gcloud
Grant the roles/iam.serviceAccountUser role:
gcloud iam service-accounts add-iam-policy-binding \
SA_NAME@SERVICE_ACCOUNT_PROJECT_ID.iam.gserviceaccount.com \
--member=PRINCIPAL \
--role=roles/iam.serviceAccountUser
Replace the following:
SA_NAME: the name of the custom service account.SERVICE_ACCOUNT_PROJECT_ID: the project ID that contains the custom service account.PRINCIPAL: the principal identifier, such asuser:baklavainthebalkans@example.com.
Config Connector
Note: This step requires Config Connector. Follow the installation instructions to install Config Connector on your cluster.
Apply the iam.serviceAccountUser role to your service account. Download the
following resource as policy-service-account-user.yaml. Replace [SA_NAME]
and [PROJECT_ID] with your own information.
kubectl apply -f policy-service-account-user.yaml
After you grant the role on the service account, those principals can use that service account to create clusters and node pools. For more information, see the following documents:
Configure service account usage across projects
If your node service account isn't in the same project as your cluster, service agents in the cluster project need additional permissions on the service account. For more information, see Node service accounts and project service agents.
To grant the required roles on node service accounts that aren't in your cluster project, follow these steps:
- To enable cross-project service account attachment, update your organization policies.
To grant the required roles on the custom service account to the service agents in your cluster project, select one of the following options:
Console
In the Google Cloud console, go to the Service accounts page.
Select the checkbox for the custom service account that you created to use with GKE nodes.
Click Manage access. The Manage Access pane opens.
Grant the Service Account Token Creator role to the Compute Engine service agent in your cluster project:
- In the Manage Access pane, click Add principal. The Grant access pane opens.
In the New principals field, specify the email address of the Compute Engine service agent in your cluster project:
service-CLUSTER_PROJECT_NUMBER@compute-system.iam.gserviceaccount.comReplace
CLUSTER_PROJECT_NUMBERwith the project number of your cluster project.In the Select a role menu, select the Service Account Token Creator role.
Click Save. The Grant access pane closes.
Grant the Service Account User role to the GKE service agent in your cluster project:
- In the Manage Access pane, click Add principal. The Grant access pane opens.
In the New principals field, specify the email address of the GKE service agent in your cluster project:
service-CLUSTER_PROJECT_NUMBER@container-engine-robot.iam.gserviceaccount.comIn the Select a role menu, select the Service Account User role.
Click Save. The Grant access pane closes.
Close the Manage Access pane.
gcloud
Get the project number of your cluster project:
gcloud projects describe CLUSTER_PROJECT_ID \ --format='value(projectNumber)'Replace
CLUSTER_PROJECT_IDwith the project ID of your cluster project.The output is similar to
123456789.Grant the
roles/iam.serviceAccountTokenCreatorrole on the custom service account to the Compute Engine service agent in your cluster project:gcloud iam service-accounts add-iam-policy-binding \ SA_NAME@SERVICE_ACCOUNT_PROJECT_ID.iam.gserviceaccount.com \ --member=service-CLUSTER_PROJECT_NUMBER@compute-system.iam.gserviceaccount.com \ --role=roles/iam.serviceAccountTokenCreatorReplace the following:
SA_NAME: the name of the custom service account.SERVICE_ACCOUNT_PROJECT_ID: the project ID of the project that contains your custom service account.CLUSTER_PROJECT_NUMBER: the project number of your cluster project.
Grant the
roles/iam.serviceAccountUserrole on the custom service account to the GKE service agent in your cluster project:gcloud iam service-accounts add-iam-policy-binding \ SA_NAME@SERVICE_ACCOUNT_PROJECT_ID.iam.gserviceaccount.com \ --member=service-CLUSTER_PROJECT_NUMBER@container-engine-robot.iam.gserviceaccount.com \ --role=roles/iam.serviceAccountUser
Allow image pulls from private repositories
If you have images in private Artifact Registry repositories, you must give your node service account access to those repositories. Even if you use the default Compute Engine service account, you might need to give the service account access to your repository if the repository is in another project.
To pull private images from Artifact Registry, grant the
Artifact Registry Reader role
(roles/artifactregistry.reader) on the repository to your node service account.
Console
In the Google Cloud console, go to the Repositories page.
Select the checkbox for your repository.
Click Show info panel. The repository information pane opens.
In the Permissions tab, click Add principal. The Grant access pane opens.
In the New principals field, specify the email address of your node service account.
Click Select a role to open the role selection dialog.
Select the Artifact Registry Reader role.
Click Save.
gcloud
Grant the roles/artifactregistry.reader role on the repository:
gcloud artifacts repositories add-iam-policy-binding REPOSITORY_NAME \
--member=serviceAccount:SERVICE_ACCOUNT_EMAIL \
--project=REPOSITORY_PROJECT_ID \
--role=roles/artifactregistry.reader
Replace the following:
REPOSITORY_NAME: the name of the Artifact Registry repository.SERVICE_ACCOUNT_EMAIL: the email address of your node service account.REPOSITORY_PROJECT_ID: the project ID of the project that contains your repository.
Config Connector
Note: This step requires Config Connector. Follow the installation instructions to install Config Connector on your cluster.
Save the following manifest as
policy-artifact-registry-reader.yaml:Replace the following:
- SA_NAME: the name of your IAM service account.
- PROJECT_ID: your Google Cloud project ID.
- REPOSITORY_NAME: the name of your Artifact Registry repository.
Grant the Artifact Registry Reader role to the service account:
kubectl apply -f policy-artifact-registry-reader.yaml