The following are different configuration examples to support a variety of
deployment models.
A common mistake is to set the annotation on the Deployment or other resource.
Ensure that the injector annotations are specified on the pod specification when
using higher level constructs such as deployments, jobs or statefulsets.
the Kubernetes API can connect to the Vault Agent injector service on port 443, and
the injector can connect to the Kubernetes API,
Vault can connect to the Kubernetes API,
Pods in the Kubernetes cluster can connect to Vault.
Note: The Kubernetes API typically runs on the master nodes, and the Vault Agent injector
on a worker node in a Kubernetes cluster.
On Kubernetes clusters that have aggregator routing enabled (ex. GKE private
clusters),
the Kubernetes API will connect directly to the injector service endpoint,
which is on port 8080.
To patch existing pods, a Kubernetes patch can be applied to add the required annotations
to pods. When applying a patch, the pods will be rescheduled.
The annotations for configuring Vault Agent injection must be on the pod
specification. Since higher level resources such as Deployments wrap pod
specification templates, Vault Agent Injector can be used with all of these
higher level constructs, too.
An example Deployment below shows how to enable Vault Agent injection:
The following example creates a deployment that mounts a Kubernetes ConfigMap
containing Vault Agent configuration files. For a complete list of the Vault
Agent configuration settings, see the Agent documentation.
The following example demonstrates how templates can be used to create environment
variables. A template should be created that exports a Vault secret as an environment
variable and the application container should source those files during startup.
---apiVersion: apps/v1
kind: Deployment
metadata:name: web-deployment
labels:app: web
spec:replicas:1selector:matchLabels:app: web
template:metadata:labels:app: web
annotations:vault.hashicorp.com/agent-inject:'true'vault.hashicorp.com/role:'web'vault.hashicorp.com/agent-inject-secret-config:'secret/data/web'# Environment variable export templatevault.hashicorp.com/agent-inject-template-config:| {{ with secret "secret/data/web" -}} export api_key="{{ .Data.data.payments_api_key }}" {{- end }}spec:serviceAccountName: web
containers:-name: web
image: alpine:latest
args:['sh','-c','source /vault/secrets/config && <entrypoint script>']ports:-containerPort:9090
The following example demonstrates how the AppRole authentication method can be used by
Vault Agent for retrieving secrets. A Kubernetes secret containing the AppRole secret ID
and role ID should be created first.
---apiVersion: apps/v1
kind: Deployment
metadata:name: web-deployment
labels:app: web
spec:replicas:1selector:matchLabels:app: web
template:metadata:labels:app: web
annotations:vault.hashicorp.com/agent-inject:'true'vault.hashicorp.com/agent-extra-secret:'approle-example'vault.hashicorp.com/auth-type:'approle'vault.hashicorp.com/auth-path:'auth/approle'vault.hashicorp.com/auth-config-role-id-file-path:'/vault/custom/role-id'vault.hashicorp.com/auth-config-secret-id-file-path:'/vault/custom/secret-id'vault.hashicorp.com/agent-inject-secret-db-creds:'database/creds/db-app'vault.hashicorp.com/agent-inject-template-db-creds:| {{- with secret "database/creds/db-app" -}} postgres://{{ .Data.username }}:{{ .Data.password }}@postgres.postgres.svc:5432/wizard?sslmode=disable {{- end }}vault.hashicorp.com/role:'my-role'vault.hashicorp.com/tls-secret:'vault-tls'vault.hashicorp.com/ca-cert:'/vault/tls/ca.crt'spec:serviceAccountName: web
containers:-name: web
image: alpine:latest
args:['sh','-c','source /vault/secrets/config && <entrypoint script>']ports:-containerPort:9090