Quick 'n dirty kubernetes state backup script, designed to be ran as kubernetes Job. Think of it like RANCID for kubernetes.
Props to @gianrubio for coming up with the idea.
Setup
Use the deployment example (ssh or AWS CodeCommit authentication) and deploy a kubernetes CronJob primitive in your kubernetes (1.5 and up) cluster ensuring backups of kubernetes resource definitions to your private git repo.
Define the following environment parameters:
GIT_REPO - GIT repo url. Required
GIT_PREFIX_PATH - Path to the subdirectory in your repository. Default: .
NAMESPACES - List of namespaces to export. Default: all
GLOBALRESOURCES - List of global resource types to export. Default: namespace
RESOURCETYPES - List of resource types to export. Default: ingress deployment configmap svc rc ds networkpolicy statefulset storageclass cronjob. Notice that Secret objects are intentionally not exported by default (see git-crypt section for details).
GIT_USERNAME - Display name of git user. Default: kube-backup
GIT_BRANCH - Use a specific git branch . Default: master
GITCRYPT_ENABLE - Use git-crypt for data encryption. See git-crypt section for details. Default: false
GITCRYPT_PRIVATE_KEY - Path to private gpg key for git-crypt. See git-crypt section for details. Default: /secrets/gpg-private.key
GITCRYPT_SYMMETRIC_KEY - Path to shared symmetric key for git-crypt. See git-crypt section. Default: /secrets/symmetric.key
Choose one of two authentication mechanisms:
When using AWS CodeCommit and policy-based access from AWS, modify your cluster configuration to provide GitPull and GitPush access for that CodeCommit repo to your cluster. If using kops, the configuration will look something like this:
NOTE: If id_rsa isn't found in your ssh directory, the backup script will assume you're using AWS CodeCommit.
Optional:
Modify the snapshot frequency in spec.schedule using the cron format.
Modify the number of successful and failed finished jobs to retain in spec.successfulJobsHistoryLimit and spec.failedJobsHistoryLimit.
If using RBAC (1.6+), use the ClusterRole and ClusterRoleBindings in rbac.yaml.
git-crypt
For security reasons Secret objects are not exported by default. However there is a possibility to store them safely using the git-crypt project.
Prerequisites
Your repository has to be already initialized with git-crypt. Minimal configuration is listed below. For details and full information see using git-crypt.
cd repo
git-crypt init
cat <<EOF > .gitattributes
*.secret.yaml filter=git-crypt diff=git-crypt
.gitattributes !filter !diff
EOF
git-crypt add-gpg-user <USER_ID>
git add -A
git commit -a -m "initialize git-crypt"
Optional:
You may choose any subdirectory for storing .gitattributes file (useful when using GIT_PREFIX_PATH).
You may encrypt additional files other than secret.yaml. Add additional lines before the .gitattribute filter. You may also use wildcard * to encrypt all files within the directory.
Enable git-crypt
To enable encryption feature:
Set pod environment variable GITCRYPT_ENABLE to true
(Optional): $GITCRYPT_PRIVATE_KEY and $GITCRYPT_SYMMETRIC_KEY variables are the combination of path where Secret volume is mounted and the name of item key from that object. If you change any value of them from the above example you may need to set this variables accordingly.
Result
All configured resources will be exported into a directory tree structure in YAML format following a $namespace/$name.$type.yaml file structure.
请发表评论