THIS PROJECT IS STILL WORK IN PROGRESS
The configuration and automatation of a k3s cluster.
Flux is used to automatically provision the manifests.
MacOS: brew install fluxcd/tap/flux
- Specify cluster name:
export CLUSTER=
- Setup ansible vault and store the vault password in
$CLUSTER-ansible.key
- Create inventory:
cp inventory-sample.yaml inventory-$CLUSTER.yaml
- Edit your inventory (Generate vault entries using
pbpaste | ansible-vault encrypt_string --vault-password-file $CLUSTER-ansible.key --name k3sToken
.--name
has to be the name of the encrypted key.) - Copy cluster manifests:
cp -r clusters/sample clusters/$CLUSTER
- Edit
clusters/$CLUSTER/infrastructure.yaml
manifest - Copy infrastructure:
cp -r infrastructure/sample infrastructure/$CLUSTER
- Generate files:
ansible-playbook -i inventory-$CLUSTER.yaml tools/generate_files.yml --extra-vars=cluster_name=$CLUSTER
- Setup SOPS
- Create secrets:
./generate-secrets.sh
- Store known hosts:
ansible-playbook -i inventory-$CLUSTER.yaml tools/store_known_hosts.yml
- Install k3s (wait until it hangs at 'Enable and check K3s service'):
ansible-playbook -i inventory-$CLUSTER.yaml main.yml --extra-vars=cluster_name=$CLUSTER --vault-password-file $CLUSTER-ansible.key
- Get access to the cluster:
- New terminal and
export CLUSTER=
again. - Get kubeconfig:
ansible-playbook -i inventory-$CLUSTER.yaml tools/get_kubeconfig.yml --extra-vars=cluster_name=$CLUSTER
- Tunnel api server:
ssh -L 6443:10.1.0.1:6443 IP_OF_A_MASTER
- New terminal and
export CLUSTER=
again. - Replace IP at server in
kubeconfig-$CLUSTER.yaml
with127.0.0.1
. - Use config:
KUBECONFIG=kubeconfig-$CLUSTER.yaml
- New terminal and
- Setup Flux:
- Make sure GITHUB_TOKEN is set. Run
export GITHUB_TOKEN=$(pbpaste)
with a personal access token (everything in repo is enabled) in your clipboard. - Setup flux:
flux bootstrap github --owner=fischerscode --repository=my-k3s --path=clusters/$CLUSTER --branch master --personal
- Make sure GITHUB_TOKEN is set. Run
- Playbook should finish now. If not check
flux get all
. - Replace IP at server in
kubeconfig-$CLUSTER.yaml
withkubernetes_api_public_address
.
- Add cluster to
.github/workflows/update-flux.yaml
- get grafana admin password:
kubectl get secret -n monitoring grafana-cred --template={{.data.ADMIN_PASSWORD}} | base64 -d | pbcopy
brew install gnupg sops
- Generate a GPG/OpenPGP key with no passphrase (%no-protection):
export KEY_NAME="$CLUSTER.my-k3s.fischerscode.com" export KEY_COMMENT="flux secrets" gpg --batch --full-generate-key <<EOF %no-protection Key-Type: 1 Key-Length: 4096 Subkey-Type: 1 Subkey-Length: 4096 Expire-Date: 0 Name-Comment: ${KEY_COMMENT} Name-Real: ${KEY_NAME} EOF
gpg --list-keys "${KEY_NAME}"
- Store the fingerprint:
export KEY_FP=
- Backup private key:
- To file:
gpg --export-secret-keys --armor ${KEY_NAME} > $CLUSTER.key
(Ansible will search for this key and apply it as a secret if present.) - To clipboard:
gpg --export-secret-keys --armor ${KEY_NAME} | pbcopy
- To file:
- Add public key to git:
gpg --export --armor ${KEY_NAME} > ./clusters/$CLUSTER/.sops.pub.asc
-
cat <<EOF >> .sops.yaml - path_regex: /$CLUSTER\/.*\.yaml$ encrypted_regex: ^(data|stringData)$ pgp: ${KEY_FP} - path_regex: /$CLUSTER\/.*\.encrypted$ pgp: ${KEY_FP} EOF
- Remove private key:
gpg --delete-secret-keys ${KEY_NAME}
- Import secret key:
gpg --import $CLUSTER.key