Skip to content

Instantly share code, notes, and snippets.

@gmaliar
Created March 16, 2018 11:54
Show Gist options
  • Save gmaliar/e226efec052b3b8fdf0744fbd0cebf68 to your computer and use it in GitHub Desktop.
Save gmaliar/e226efec052b3b8fdf0744fbd0cebf68 to your computer and use it in GitHub Desktop.
Auto-renewing secrets using Valut and Kubernetes | app-deployment.yml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: vault-dynamic-secrets-rails
labels:
app: vault-dynamic-secrets-rails
spec:
replicas: 3
template:
metadata:
labels:
app: vault-dynamic-secrets-rails
spec:
serviceAccountName: postgres-vault
initContainers:
- name: vault-init
image: everpeace/curl-jq
command:
- "sh"
- "-c"
- >
KUBE_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token);
curl --request POST --data '{"jwt": "'"$KUBE_TOKEN"'", "role": "postgres"}' http://errant-mandrill-vault:8200/v1/auth/kubernetes/login | jq -j '.auth.client_token' > /etc/vault/token;
X_VAULT_TOKEN=$(cat /etc/vault/token);
curl --header "X-Vault-Token: $X_VAULT_TOKEN" http://errant-mandrill-vault:8200/v1/database/creds/postgres-role > /etc/app/creds.json;
volumeMounts:
- name: app-creds
mountPath: /etc/app
- name: vault-token
mountPath: /etc/vault
containers:
- name: rails
image: gmaliar/vault-dynamic-secrets-rails:0.0.1
imagePullPolicy: Always
ports:
- containerPort: 3000
resources:
limits:
memory: "50Mi"
cpu: "100m"
volumeMounts:
- name: app-creds
mountPath: /etc/app
- name: vault-manager
image: everpeace/curl-jq
command:
- "sh"
- "-c"
- >
X_VAULT_TOKEN=$(cat /etc/vault/token);
VAULT_LEASE_ID=$(cat /etc/app/creds.json | jq -j '.lease_id');
while true; do
curl --request PUT --header "X-Vault-Token: $X_VAULT_TOKEN" --data '{"lease_id": "'"$VAULT_LEASE_ID"'", "increment": 3600}' http://errant-mandrill-vault:8200/v1/sys/leases/renew;
sleep 3600;
done
lifecycle:
preStop:
exec:
command:
- "sh"
- "-c"
- >
X_VAULT_TOKEN=$(cat /etc/vault/token);
VAULT_LEASE_ID=$(cat /etc/app/creds.json | jq -j '.lease_id');
curl --request PUT --header "X-Vault-Token: $X_VAULT_TOKEN" --data '{"lease_id": "'"$VAULT_LEASE_ID"'"}' http://errant-mandrill-vault:8200/v1/sys/leases/revoke;
volumeMounts:
- name: app-creds
mountPath: /etc/app
- name: vault-token
mountPath: /etc/vault
volumes:
- name: app-creds
emptyDir: {}
- name: vault-token
emptyDir: {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment