1. Why Authentication Inside a Pod?
Before we dive in, let’s quickly review why you’d even bother doing authentication inside a pod instead of just at the API gateway.
- π Zero Trust Security (Wikipedia) - Every service must authenticate every request, even if itβs internal.
- π Identity Propagation - Services need identity-aware access control, like OAuth tokens or mTLS.
- πͺ Fine-grained access control - You want different pods to have different access levels, instead of just relying on network policies.
Now, let’s implement it!
2. Using OAuth2 Proxy Inside a Pod
One of the easiest ways to add authentication inside a pod is by using OAuth2 Proxy (GitHub).
What is OAuth2 Proxy?
- It acts as a reverse proxy in front of your app.
- It requires users to authenticate via an OAuth provider (e.g., Google, GitHub, Okta).
- It passes authenticated requests to your backend.
Step 1: Deploy OAuth2 Proxy
We’ll assume you’re using Google OAuth. First, create an OAuth client in Google:
- Go to Google Cloud Console.
- Create OAuth 2.0 credentials.
- Set the redirect URI to
https://your-app/oauth2/callback
.
Now, deploy OAuth2 Proxy inside a pod.
oauth2-proxy Deployment (Kubernetes)
|
|
3. Using JWT-Based Authentication
If you need stateless authentication, JWTs (JSON Web Tokens) (Wikipedia) are the way to go.
Step 1: Generate JWTs with Keycloak
Keycloak (Official Site) is a great open-source identity provider. You can deploy it in Kubernetes:
|
|
4. Enforcing Authentication with Istio
If youβre using Istio (Wikipedia), you can enforce JWT authentication at the proxy level.
Step 1: Create a JWT Policy in Istio
|
|
Now, any request without a valid JWT will be blocked! π«
5. Using Cloud IAM for Workload Identity
Instead of hardcoding API keys, you should use cloud IAM roles.
Example: If you’re running in GCP, you can enable Workload Identity so that pods automatically get IAM permissions.
Step 1: Enable Workload Identity on a GKE Cluster
|
|
Step 2: Link a Service Account to a Pod
|
|
This lets your pod access GCP services securely without hardcoded credentials! π
Key Takeaways
β
OAuth2 Proxy helps enforce SSO authentication.
β
JWT authentication enables stateless security.
β
Istio enforces zero-trust security with JWT policies.
β
Cloud IAM prevents hardcoded credentials.