forked from loft-sh/vcluster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add high availability support for k3s with external database (loft-sh…
…#795) * feat(k3s): add HA support with external database Adjusted the k3s Helm chart so support the HA setup of k3s with an external database. * docs: add description for k3s in HA mode * fix(k3s): create service for STS only when HA mode is disabled * feat(k3s): always use policy/v1 for PodDisruptionBudget * feat(k3s): allow configuration of PodDisruptionBudget Adjusted the Helm template to allow the conditional creation of the PodDisruptionBudget as well as to provide user-defined values. * refactor(k3s): move changes for HA support into existing template In order to prevent the duplication of content, all necessary changes to add HA support was moved into the existing template `statefulset.yaml`. All k3s-specific named templates were added to a dedicated file to keep `_helpers.tpl` consistent across all charts. * feat(k3s): automatically generate server token Added the necessary changes to automatically generate the secret containing the k3s server token - in case no value is supplied. * refactor(k3s): generate k3s tokens with Helm hook Instead of using the `lookup` function, which is known to cause problems with the Helm CLI, ArgoCD, ..., to generate the k3s server token, it is now generated by a Helm pre-install hook. * refactor(k3s): only create secret in pre-install hook * docs: fix link to external datastore page
- Loading branch information
Showing
10 changed files
with
241 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{{/* vim: set filetype=mustache: */}} | ||
{{/* | ||
Returns the desired workload kind (StatefulSet / Deployment) for k3s | ||
*/}} | ||
{{- define "vcluster.k3s.workloadKind" -}} | ||
{{- ternary "Deployment" "StatefulSet" (.Values.enableHA) -}} | ||
{{- end -}} | ||
|
||
{{/* | ||
Returns the name of the secret containing the k3s tokens. | ||
*/}} | ||
{{- define "vcluster.k3s.tokenSecretName" -}} | ||
{{- with .Values.serverToken.secretKeyRef.name -}} | ||
{{- . -}} | ||
{{- else -}} | ||
{{- printf "%s-tokens" .Release.Name -}} | ||
{{- end -}} | ||
{{- end -}} | ||
|
||
{{/* | ||
Returns the secret key name containing the k3s server token. | ||
*/}} | ||
{{- define "vcluster.k3s.serverTokenKey" -}} | ||
{{- with .Values.serverToken.secretKeyRef.key -}} | ||
{{- . -}} | ||
{{- else -}} | ||
{{- "server-token" -}} | ||
{{- end -}} | ||
{{- end -}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{{- if and (.Values.enableHA) (.Values.podDisruptionBudget.enabled) (gt (int .Values.replicas) 1) -}} | ||
apiVersion: policy/v1 | ||
kind: PodDisruptionBudget | ||
metadata: | ||
name: {{ .Release.Name }} | ||
namespace: {{ .Release.Namespace }} | ||
labels: | ||
app: vcluster | ||
chart: "{{ .Chart.Name }}-{{ .Chart.Version }}" | ||
release: "{{ .Release.Name }}" | ||
heritage: "{{ .Release.Service }}" | ||
{{- if .Values.globalAnnotations }} | ||
annotations: | ||
{{ toYaml .Values.globalAnnotations | indent 4 }} | ||
{{- end }} | ||
spec: | ||
{{- with .Values.podDisruptionBudget.minAvailable }} | ||
minAvailable: {{ . }} | ||
{{- end }} | ||
{{- with .Values.podDisruptionBudget.maxUnavailable }} | ||
maxUnavailable: {{ . }} | ||
{{- end }} | ||
selector: | ||
matchLabels: | ||
app: vcluster | ||
release: {{ .Release.Name }} | ||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{{- if (and (.Values.enableHA) (not .Values.serverToken.secretKeyRef)) }} | ||
apiVersion: v1 | ||
kind: Secret | ||
metadata: | ||
name: {{ include "vcluster.k3s.tokenSecretName" . | quote }} | ||
namespace: {{ .Release.Namespace }} | ||
annotations: | ||
helm.sh/hook: pre-install | ||
helm.sh/hook-weight: "3" | ||
# helm.sh/hook-delete-policy: before-hook-creation # Default value | ||
helm.sh/resource-policy: keep | ||
type: Opaque | ||
data: | ||
{{- if .Values.serverToken.value }} | ||
server-token: {{ .Values.serverToken.value | b64enc | quote }} | ||
{{- else }} | ||
server-token: {{ (randAlphaNum 32) | b64enc | quote }} | ||
{{- end }} | ||
{{- end }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
### Enabling High Availability | ||
|
||
In order to run vcluster with k3s as Kubernetes distribution in high availability mode, the following steps are required: | ||
|
||
* create and use an [external datastore](../operator/external-datastore.mdx) (as opposed to the embedded SQLite datastore used in single-server setups) | ||
* run two or more k3s pods that will serve the Kubernetes API and run other control plane services | ||
|
||
First create a `values.yaml` in the following form and make sure to change the connection string in `K3S_DATASTORE_ENDPOINT`: | ||
|
||
``` | ||
# Enable HA mode | ||
enableHA: true | ||
# Scale up k3s replicas | ||
replicas: 2 | ||
# Set external datastore endpoint | ||
vcluster: | ||
env: | ||
- name: K3S_DATASTORE_ENDPOINT | ||
value: mysql://username:password@tcp(hostname:3306)/database-name | ||
# Disable persistent storage as all data (including bootstrap data) is stored in external datastore | ||
storage: | ||
persistence: false | ||
# Scale up CoreDNS replicas | ||
coredns: | ||
replicas: 2 | ||
``` | ||
|
||
Then create the vcluster with the following command: | ||
|
||
``` | ||
vcluster create ... --connect=false -f values.yaml | ||
``` | ||
|
||
Check that vcluster including the control plane is running correctly: | ||
|
||
``` | ||
kubectl get pods -n vcluster | ||
NAME READY STATUS RESTARTS AGE | ||
coredns-66ffcc6b58-bhk4s-x-kube-system-x-vcluster 1/1 Running 0 21s | ||
coredns-66ffcc6b58-n7npd-x-kube-system-x-vcluster 1/1 Running 0 21s | ||
vcluster-54fb5dd76-92szq 2/2 Running 0 3m1s | ||
vcluster-54fb5dd76-ntbrh 2/2 Running 0 3m1s | ||
``` | ||
|
||
Now connect to the vcluster: | ||
|
||
``` | ||
vcluster connect vcluster -n vcluster | ||
# Then execute in a new terminal | ||
export KUBECONFIG=kubeconfig.yaml | ||
kubectl get ns | ||
... | ||
``` | ||
|
||
|
||
Check the [GitHub repository](https://github.com/loft-sh/vcluster/tree/main/charts/k3s) for all available chart options. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters