Vault

Vault Kubernetes Auth Guide: Service Account Integration and Sidecar Patterns

2026-04-19
NicheeLab Editorial Team

The standard way to securely authenticate Kubernetes Pods to Vault is the Kubernetes Auth Method backed by Service Account tokens. This article walks through RBAC and role design, plus the implementation details of secret distribution via the Vault Agent Injector sidecar, from both exam and operational perspectives.

On the Associate exam, expect questions on the components of auth/kubernetes, how Service Accounts map to Vault roles, Agent Injector annotations and behavior, and the concepts of token renewal and TTL. The configuration examples, comparison tables, and diagrams below should help cement the picture.

Big Picture: Kubernetes Auth + Service Account + Sidecar Flow

The Kubernetes Auth Method uses the Service Account (SA) JWT that is auto-mounted into a Pod. Vault validates it through the Kubernetes API and issues a Vault token in return. In the sidecar pattern, the Vault Agent handles auto-authentication, secret fetching, and template rendering inside the Pod, and the application reads secrets from files.

In practice, distributing secrets via files through a sidecar (or Init container) is usually easier to operate than having the application talk to Vault directly, and it makes rotation and reload control simpler. The diagram below shows the flow from authentication to secret injection.

  • Input: the Pod's Service Account JWT (short-lived Projected Tokens recommended)
  • Verification: Vault queries the Kubernetes API to validate the token
  • Output: a Vault token (with policy-scoped permissions) and secrets that the sidecar fetches and refreshes
PatternProsConsiderations
App logs in to Vault directly (SDK / HTTP)Fewer dependencies; offers flexible control.App must implement auth, renewal, and retries — and the implementation varies per language.
Sidecar (Vault Agent Injector)Easy to onboard via Pod annotations; auto-auth, templating, and renewal are bundled together.Extra container consumes resources; you need to understand the annotations and permission model.
Init container for one-shot injectionCompletes at startup; the app only needs to read a file.Weak at post-startup rotation; updates require a Pod restart.
CSI driver (Secrets Store CSI + Vault provider)Accessed via a standard K8s volume; files can be projected without going through a K8s Secret.Adds cluster-side operational overhead; you need to understand feature gaps and sync behavior.

Typical Kubernetes Auth + sidecar flow

TokenReviewloginfetchrenderreloadKubernetes APITokenReviewVault Serverauth/kubernetesPolicies/Secretskv, db, transit...App Containerreads files from volumeSidecar: Vault Agentauto-auth / templatePod (Namespace: app)shared volume (tmpfs)*.env, *.crt, config.json1)SA JWT→2)login→3)token→4)fetch→5)render

Minimal configuration in concept (excerpt)

# Pod内のService Accountトークンを使い、VaultのKubernetes Authでlogin。
# SidecarのVault Agentがテンプレートでシークレットをファイル出力し、
# アプリはread-onlyでマウントされたボリュームから読み取る。

Step by Step: Configuring Vault's Kubernetes Auth

On the Vault server, enable the Kubernetes Auth Method, prepare a Service Account that can call TokenReview against the Kubernetes API, and register the required information into auth/kubernetes/config. This lets Vault validate Pod JWTs as legitimate Kubernetes-issued tokens.

Least-privilege RBAC is sufficient — grant only the permissions needed for TokenReview and validating Service Account tokens in the relevant namespaces.

  • Prerequisite: Vault can reach the Kubernetes API (network and certificates)
  • Recommended: Projected Service Account Tokens (short-lived, audience-scoped)
  • Security: be explicit about how kubernetes_ca_cert is handled and how TLS verification is performed
ParameterPurposeConfiguration Tip
kubernetes_hostThe Kubernetes API endpoint Vault will queryFrom inside the cluster, use something like https://$KUBERNETES_PORT_443_TCP_ADDR:443
token_reviewer_jwtJWT used to call TokenReview against the Kubernetes APIUse a dedicated Service Account's token; Projected Tokens are recommended.
kubernetes_ca_certThe CA certificate for the API serverMount the cluster CA from a ConfigMap and pass it in; the system trust store may also work (environment dependent).

Relationship between the configuration components

uses JWT (TokenReview RBAC)TokenReview / valid・invalidvault write auth/kubernetes/configSA for ReviewerK8s API ServerVault auth/kubernetesstores: kubernetes_host, token_reviewer_jwt, CA cert

Enabling Kubernetes Auth and configuring RBAC (example)

# 1) Vault側: Kubernetes Authを有効化
vault auth enable kubernetes

# 2) Reviewer用のService Account/RBAC(kube側)
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: vault-token-reviewer
  namespace: vault
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: system:token-reviewer
rules:
- apiGroups: ["authentication.k8s.io"]
  resources: ["tokenreviews"]
  verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: vault-token-reviewer-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:token-reviewer
subjects:
- kind: ServiceAccount
  name: vault-token-reviewer
  namespace: vault

# 3) VaultにK8sクラスタ情報を登録
# 環境に合わせて$K8S_HOST, $REVIEWER_JWT, $CA_CERTを用意
vault write auth/kubernetes/config \
  kubernetes_host="$K8S_HOST" \
  token_reviewer_jwt="$REVIEWER_JWT" \
  kubernetes_ca_cert=@/var/run/secrets/kubernetes.io/serviceaccount/ca.crt

Designing the Service Account to Vault Role Mapping

A Vault Kubernetes role binds Vault policies to a specific Service Account name + namespace combination and issues a token to logins from authorized Pods. Use bound_service_account_names, bound_service_account_namespaces, and optionally bound_audiences to constrain the JWT audience.

TTL and token type (service, batch) are also controlled by the role. Choose a TTL that is not too short and decide whether to allow renewal, based on your app's refresh cadence and the cost of restarts.

  • Bind both SA and namespace to prevent misapplication
  • Scope the audience to Vault to reduce replay risk
  • Keep policies minimal — be especially careful with where list permissions are granted
Role Design PatternWhen to UseRisks / Considerations
Per-SA (fine-grained)Grant least privilege per microserviceRole count grows; consistent naming and management become important
Per-namespace (medium-grained)Share the same permissions across multiple Pods in one namespaceRisk of over-permissioning; requires solid namespace boundary discipline
Bundle multiple SAs into one roleManage a group of services with similar requirements togetherPoor fit when requirements diverge; future split costs go up

How roles and policies are wired together

mapsmapsSA: web (ns: app)role: app-webbound SA/ns, policies=[kv-app]SA: batch (ns: app)role: app-batchpolicies=[kv-job]

Role creation and policy example

# ポリシー: kv v2の特定パスのみ許可
tee kv-app.hcl <<'EOF'
path "kv/data/app/*" {
  capabilities = ["read"]
}
EOF
vault policy write kv-app kv-app.hcl

# ロール: app Namespaceのweb SAにバインド
aud="vault"
vault write auth/kubernetes/role/app-web \
  bound_service_account_names=web \
  bound_service_account_namespaces=app \
  bound_audiences="$aud" \
  policies=kv-app \
  ttl=1h \
  token_type=default

Distributing via Sidecar: Implementing the Vault Agent Injector

The Vault Agent Injector automatically injects a sidecar and shared volume into a Pod just by adding annotations. It authenticates via Kubernetes Auth, evaluates templates, and writes files. The app simply reads them as environment-variable files or config files.

For certificates and short-lived tokens that need rotation, combining the Agent's auto-renewal with SIGHUP-based reloads typically yields a stable setup.

  • Add annotations to the Pod template in your Deployment or StatefulSet
  • Specify the Vault role name with vault.hashicorp.com/role
  • Templates can produce JSON, YAML, ENV, or any other format you need
Annotation KeyExample ValueEffect
vault.hashicorp.com/agent-inject"true"Injects the sidecar and a shared volume
vault.hashicorp.com/role"app-web"The Vault role used by Kubernetes Auth
vault.hashicorp.com/agent-inject-secret-config"kv/data/app/config"The secret path that serves as the data source for the template
vault.hashicorp.com/agent-inject-template-config"{{ with secret \\"kv/data/app/config\\" }}{{ .Data.data | toJSON }}{{ end }}"Template for the file content (Go template syntax)

In-Pod layout produced by the Injector

shared emptyDir volume (/vault/)Appreads /vault/Vault Agent (sc)auto-auth, renderContainer layout inside the Pod

Minimal Deployment annotation example (generates config.json from kv v2)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
  namespace: app
spec:
  replicas: 1
  selector:
    matchLabels: { app: web }
  template:
    metadata:
      labels: { app: web }
      annotations:
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/role: "app-web"
        # 出力ファイル名は注釈の末尾と一致
        vault.hashicorp.com/agent-inject-secret-config: "kv/data/app/config"
        vault.hashicorp.com/agent-inject-template-config: |
          {{- with secret "kv/data/app/config" -}}{{ .Data.data | toJSON }}{{- end -}}
    spec:
      serviceAccountName: web
      containers:
      - name: app
        image: ghcr.io/example/web:1.0
        volumeMounts:
        - name: vault-secrets
          mountPath: /vault/secrets
          readOnly: true
      volumes:
      - name: vault-secrets
        emptyDir:
          medium: Memory

Security and Operations: Token Renewal, TTL, and Failure Behavior

Vault tokens issued via Kubernetes Auth are automatically renewable within the role's ttl and max_ttl. The sidecar Agent renews before expiry and retries on failure. If renewal keeps failing, template re-evaluation stops, and the app continues to reference the last file the Agent produced.

For stable operation, combine monitoring (remaining token TTL, template update timestamps), a Pod restart strategy, and SIGHUP-based config reloads. This limits the blast radius if an auth break ever occurs.

  • Keep Projected SA Token lifetimes short (minutes to tens of minutes)
  • Balance the Vault token TTL against your app's reload characteristics
  • Plan around retries and backoff for transient network or API outages
Failure ScenarioTypical SymptomsMitigation
Kubernetes API unreachableAuth failures, TokenReview errors, continuous retriesCheck network and firewall, refresh the API endpoint and CA, add redundant paths
SA token audience mismatchLogin fails with an Invalid audience errorMake bound_audiences match the aud in the Pod's Projected Token
Policy misconfigurationPermission denied during template evaluationRe-check read/list on the required paths and watch out for the /data vs. /metadata split in kv v2

Simplified renewal loop

SA JWTloginVault token(ttl)fetchrenderrenew loop / on error: backoff → retry

Template example (environment-variable file) and reload

# テンプレート例: .env 形式
{{- with secret "kv/data/app/db" -}}
DB_USER={{ .Data.data.username }}
DB_PASS={{ .Data.data.password }}
{{- end -}}

# アプリ側: ファイル変更検知でSIGHUP
# inotifywait等で /vault/secrets/.env の更新を監視し、プロセスにSIGHUP送出

Exam Prep and Pitfalls: Key Points Checklist

The Associate exam tests terminology and flow. Make sure you firmly grasp the role of the reviewer token, the role binding conditions, what each Injector annotation does, and kv v2 path conventions.

Common pitfalls include login failures from audience or namespace/SA name mismatches, and confusing /data with /metadata in kv v2 paths.

  • The reviewer SA's token is for Vault to call TokenReview against the K8s API — it is not for the application
  • On auth/kubernetes/role, bound_service_account_names and namespaces are evaluated with AND
  • In kv v2, reads go to secret/data/... and metadata is at secret/metadata/...
TopicKey PointCommon Mistake
Meaning of TokenReviewVault verifies the legitimacy of a K8s-issued JWTAccidentally handing the reviewer token to the app
Role conditionsSpecify both SA name and namespaceOver-permissioning via excessive namespace wildcards
Injector annotationsThe role / secret / template trioGetting the path hierarchy wrong in kv v2

Mini glossary diagram

TokenReviewprovides JWTbindsmapsReviewer SA (for Vault)K8s APIPod SA (for the app)Vault auth/kubernetesVault RoleSA+NamespacePolicies

Manual login check via the CLI (for troubleshooting)

# Pod内での手動テスト例(デバッグ用途)
SA_JWT=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
vault write auth/kubernetes/login role=app-web jwt="$SA_JWT"
# auth成功後に VAULT_TOKEN を使って読み取り
echo $VAULT_TOKEN | vault kv get -format=json kv/app/config

Check Your Understanding

Associate

問題 1

You are connecting a Kubernetes app to Vault using the Vault Agent Injector sidecar pattern. From a security and operations perspective, which design is correct?

  1. Hand the reviewer Service Account's token to the sidecar and use it to log in to auth/kubernetes
  2. Log in to auth/kubernetes with the Pod's Service Account JWT and bind the Vault role to that SA name and namespace
  3. Specify policies directly via Injector annotations and leave the role empty
  4. Reference kv v2 secrets via a plain path such as secret/app/config

正解: B

The reviewer token is used by Vault itself to call TokenReview against the Kubernetes API — it is not for the app or the sidecar. The sidecar logs in to auth/kubernetes with the Pod's SA JWT, and the Vault role is wired up via bound_service_account_names and bound_service_account_namespaces. kv v2 paths must include /data/...

Frequently Asked Questions

Is a dedicated reviewer Service Account always required?

Yes. Vault validates Pod JWTs through the Kubernetes API's TokenReview, so a least-privilege reviewer SA (with matching RBAC) is required. Its token is registered as token_reviewer_jwt in auth/kubernetes/config.

Should I use the Injector sidecar or an Init container?

If you need post-startup rotation or automatic renewal, use the Injector (sidecar). For static settings that only need to be fetched at startup and can tolerate a Pod restart on update, an Init container is also a valid choice.

How do I tell kv v1 and v2 apart, and what should I watch out for?

You can tell from the mount options. kv v2 supports versioning and splits API paths into /data/ and /metadata/. When using Injector annotations or the CLI, remember to include /data in kv v2 paths (e.g. secret/data/...).

Check what you learned with practice questions

Practice with certification-focused question sets

無料で問題を解いてみる
Author

NicheeLab Editorial Team

NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.


Related articles
Vault

Vault Core Concepts: Sealed/Unsealed, Auth, Secrets (2026)

Vault fundamentals — sealed/unsealed state, auth methods, se...

Vault

Vault Operations Professional (VOP-003): Complete Guide (2026)

Pass the Vault Operations Professional exam — enterprise pat...

Vault

Vault Path-Based Routing: API URL Structure (2026)

How Vault's path-based routing works — mount points, sub-pat...

Vault

Vault Tokens: Auth Token Mechanics (2026)

Token fundamentals — service vs. batch tokens, accessor, ren...

Vault

Vault Token Types: Service, Batch, Periodic (2026)

Service vs. batch tokens compared — performance, ACL behavio...

Browse all Vault articles (101)
© 2026 NicheeLab All rights reserved.