DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Mastering Daily Kubernetes Operations: A Guide To Useful kubectl Commands for Software Engineers
  • Main Features and Benefits of Google Kubernetes Engine
  • Kubernetes — Replication, and Self-Healing
  • Explaining Simple WSO2 Identity Server Kubernetes Deployment

Trending

  • How to Perform Custom Error Handling With ANTLR
  • How Can Developers Drive Innovation by Combining IoT and AI?
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • The Role of AI in Identity and Access Management for Organizations
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Enabling GKE Workload Identity

Enabling GKE Workload Identity

This in-depth tutorial demonstrates how to enable the GKE Workload Identity feature on the Google Cloud Platform to achieve better security.

By 
Pradeep Bhadani user avatar
Pradeep Bhadani
·
Updated Aug. 18, 22 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
10.5K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, I will talk about the GKE Workload Identity feature and why to use this feature.

What’s the Problem?

An application running on GKE must authenticate to use Google Services such as Google Cloud Storage (GCS), Cloud SQL, BigQuery, etc. Authentication can be done by providing a service account key JSON file to an application using Kubernetes Secret space or a different method such as Vault. 

However, in these approaches, the service account key JSON (which has a lifetime of 10 years) must be stored in plain text within the pod or Base64 encoded in Kubernetes secret space. Also, the key rotation process must be in a place (that is not a fun process).

We can avoid using the service account key by attaching a service account to a Kubernetes node, but then all the pods running on the node gets the same permission, which is not an ideal thing to do.

Goal

We want to assign a service account to a pod so we can isolate permissions for different pods.

Hurray: we have the Workload Identity feature available in beta, which solves this problem on GKE.

What Is Workload Identity?

As per the Google documentation:

“Workload Identity is the recommended way to access Google Cloud services from within GKE due to its improved security properties and manageability.“

GKE Workload identity allows us to attach the service account to the Kubernetes pod and remove the hassle to manage the service account credentials JSON file within the pod or cluster.

Workload Identity in a GKE Cluster

Prerequisites

  1. If you have not setup gcloud on your workstation, then refer to my previous blog to get it up and running quickly. Alternatively, you can use Google Cloud Shell to run the commands.

  2. Make sure you are a Project Editor or Project Owner or have enough permissions to run the below commands.

Setup a GKE Cluster

Follow the below step to create a new GKE Cluster and enable Workload Identity.

1.  Enable the Cloud IAM API.

2.  Install and configure gke-gcloud-auth-plugin. gke-gcloud-auth-plugin is the new Kubectl authentication plugin for GKE. Please read the documentation for more details.

  • Install plugin:
 
gcloud components install gke-gcloud-auth-plugin


Note: If gcloud CLI component manager is disabled, use the yum or apt package to install this plugin. For Debian: 

 
sudo apt-get install google-cloud-sdk-gke-gcloud-auth-plugin


  • Configure plugin:
 
echo "export USE_GKE_GCLOUD_AUTH_PLUGIN=True" >> ~/.bashrc
source ~/.bashrc


3.  Set GCP defaults.

  • Set GCP Project:
 
export GCP_PROJECT_ID=<YOUR_GCP_PROJECT_ID>


 
gcloud config set project $GCP_PROJECT_ID


  • Set default region and zone:
 
gcloud config set compute/region europe-west1


 
gcloud config set compute/zone europe-west1-b


4.  Make sure you have kubectl command installed.

 
sudo apt-get install kubectl


Run the following to verify:

 
kubectl help


5.  Create a new Google Service Account (GSA).

 
gcloud iam service-accounts create workload-identity-test


Notes: You can use the existing service account.

Permission Required: iam.serviceAccounts.create on the GCP Project.

6.  Add permissions to the Google Service Account required by an application: for example, roles/storage.objectViewer.

 
gcloud projects add-iam-policy-binding $GCP_PROJECT_ID \
  --member serviceAccount:workload-identity-test@${GCP_PROJECT_ID}.iam.gserviceaccount.com \
  --role roles/storage.objectViewer


7.  Setup a GKE cluster with Workload Identity enabled.

export GKE_CLUSTER_NAME=gke-wi
 
gcloud container clusters create $GKE_CLUSTER_NAME \
--cluster-version=1.24 \
--workload-pool=$GCP_PROJECT_ID.svc.id.goog


8.  Configure kubectl command on your terminal.

 
gcloud container clusters get-credentials $GKE_CLUSTER_NAME


Notes: This will populate ~/.kube/config file. 

Permission Required: container.clusters.get on the GCP Project.

9.  (Optional) Create a Kubernetes namespace if you dont want to use the default namespace.

 
kubectl create namespace newspace


10. Create Kubernetes Service Account (KSA).

 
kubectl create serviceaccount \
 --namespace newspace \
 workload-identity-test-ksa


11. Bind the Google Service Account (GSA) and Kubernetes Service Account (KSA), so that KSA can use the permissions granted to GSA.

 
gcloud iam service-accounts add-iam-policy-binding \
--role roles/iam.workloadIdentityUser \
--member "serviceAccount:${GCP_PROJECT_ID}.svc.id.goog[newspace/workload-identity-test-ksa]" \
workload-identity-test@${GCP_PROJECT_ID}.iam.gserviceaccount.com


12. Add annotation:

 
kubectl annotate serviceaccount \
--namespace newspace \
workload-identity-test-ksa \
iam.gke.io/gcp-service-account=workload-identity-test@${GCP_PROJECT_ID}.iam.gserviceaccount.com


13. Create a Pod with the KSA created to verify.

 
kubectl run --rm -it test-pod \
--image google/cloud-sdk:slim \
--namespace newspace \
--overrides='{ "spec": { "serviceAccount": "workload-identity-test-ksa" }  }' sh


Running the above command will log in to Pod and provides its bash shell. Now run below command to see which service account this pod is configured with.

 
gcloud auth list


 This should print the GSA name. 

 
 Credentialed Accounts
ACTIVE  ACCOUNT
*       workload-identity-test@workshop-demo-namwcb.iam.gserviceaccount.com


Cleanup

Don’t forget to clean up the resources, once you no longer need them. 

Run the following commands: 

1.  Delete the GKE cluster.

 
gcloud container clusters delete $GKE_CLUSTER_NAME


2.  Delete the Google Service Account (GSA). 

 
gcloud iam service-accounts delete workload-identity-test@${GCP_PROJECT_ID}.iam.gserviceaccount.com


Below is the terminal recording:


I hope this blog helps you get familiar with Workload Identity and securely deploy apps on GKE.

If you have feedback or questions, please reach out to me on LinkedIn or Twitter.

Kubernetes shell cluster Google (verb) pods Command (computing)

Published at DZone with permission of Pradeep Bhadani. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Daily Kubernetes Operations: A Guide To Useful kubectl Commands for Software Engineers
  • Main Features and Benefits of Google Kubernetes Engine
  • Kubernetes — Replication, and Self-Healing
  • Explaining Simple WSO2 Identity Server Kubernetes Deployment

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!