Skip to content

Commit

Permalink
Merge pull request #13 from adfinis/feat/add/k8s-cronjob
Browse files Browse the repository at this point in the history
feat(kubernetes): example cronjob & container image creation
  • Loading branch information
eyenx authored Jun 2, 2023
2 parents ed95552 + dcd2116 commit 1cf2a9c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
12 changes: 12 additions & 0 deletions kubernetes/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM alpine

ARG VAULT_VERSION=1.13.2

COPY entrypoint.sh /

RUN wget https://releases.hashicorp.com/vault/${VAULT_VERSION}/vault_${VAULT_VERSION}_linux_amd64.zip && \
unzip vault_${VAULT_VERSION}_linux_amd64.zip && \
mv vault /usr/local/bin && rm vault*zip && \
apk add s3cmd && chmod +x entrypoint.sh

CMD ["/vault-snapshot.sh"]
19 changes: 19 additions & 0 deletions kubernetes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Cronjob for snapshotting Vault running on Kubernetes

This assumes the Kubernetes authentication backend is configured in Vault.

The script being executed in this cronjob, is authenticating with Vault using the Kubernetes authentication backend, via its serviceaccount JWT.

The role and policy being used must be created before hand and must be used by the cronjob.

After the snapshot is created in a temporary directory, `s3cmd` is used to sync it to a s3 endpoint.

## Configuration over environment variables

* `VAULT_ADDR` - Vault address to access
* `VAULT_ROLE` - Vault role to use to create the snapshot
* `S3_URI` - S3 URI to use to upload (s3://xxx)
* `S3_BUCKET` - S3 bucket to point to
* `S3_HOST` - S3 endpoint
* `AWS_ACCESS_KEY_ID` - Access key to use to access S3
* `AWS_SECRET_ACCESS_KEY` - Secret access key to use to access S3
55 changes: 55 additions & 0 deletions kubernetes/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
labels:
app.kubernetes.io/name: vault-snapshot
app.kubernetes.io/version: v0.1.0
name: vault-snapshot
spec:
jobTemplate:
schedule: 0 4 * * *
metadata:
labels:
app.kubernetes.io/name: vault-snapshot
app.kubernetes.io/version: v0.1.0
spec:
template:
metadata:
labels:
app.kubernetes.io/name: vault-snapshot
app.kubernetes.io/version: v0.1.0
spec:
automountServiceAccountToken: true
serviceAccountName: vault-raft-snapshot
containers:
- name: vault-snapshot
env:
- name: S3_HOST
value: s3.example.com
- name: S3_BUCKET
value: bucketname
- name: S3_URI
value: s3://bucketname
- name: VAULT_ROLE
value: vault-snapshot
- name: VAULT_ADDR
value: https://vault.example.com
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
key: aws_secret_access_key
name: vault-snapshot-credentials
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
key: aws_access_key_id
name: vault-snapshot-credentials
image: ghcr.io/adfinis/vault-snapshot:0.1.-0
volumeMounts:
- name: snapshot-dir
mountPath: /vault-snaphots
imagePullPolicy: IfNotPresent
volumes:
- name: snapshot-dir
emptyDir: {}
5 changes: 5 additions & 0 deletions kubernetes/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: vault-raft-snapshot
15 changes: 15 additions & 0 deletions kubernetes/vault-snapshot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env sh

# authenticate using kubernetes auth
export JWT=$(cat /var/run/secrets/kubernetes/io/serviceaccount/token)
export VAULT_TOKEN=$(vault write -field=token auth/kubernetes/login role=$VAULT_ROLE jwt=$JWT)

# use the leader node as VAULT_ADDR
export VAULT_ADDR=$(vault status -format=yaml | egrep -o '^leader_addr.*' | awk '{print $2}')

# create snapshot

vault operator raft snapshot save /vault-snapshots/vault_$(date +%F-%H%M).snapshot

# upload to s3
s3cmd put /vault-snapshots/* $S3_URI --host=$S3_HOST --host-bucket=$S3_BUCKET

0 comments on commit 1cf2a9c

Please sign in to comment.