From b22c9755d3afe380804a4a90977452547c893ebe Mon Sep 17 00:00:00 2001 From: Angelos Kolaitis Date: Wed, 30 Nov 2022 19:48:31 +0200 Subject: [PATCH] Add GCP cluster template --- README.md | 31 +++++++++ templates/cluster-template-gcp.rc | 15 ++++ templates/cluster-template-gcp.yaml | 103 ++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 templates/cluster-template-gcp.rc create mode 100644 templates/cluster-template-gcp.yaml diff --git a/README.md b/README.md index 950ae94..6ced325 100644 --- a/README.md +++ b/README.md @@ -225,6 +225,37 @@ microk8s kubectl apply -f cluster-azure.yaml > **Note**: Make sure you have the secret to include the password of the Service Principal identity. This secret will be referenced by the AzureClusterIdentity used by the AzureCluster. +#### GCP + +> *NOTE*: Ensure that you have properly deployed the GCP infrastructure provider prior to executing the commands below. See [Initialization for common providers](https://cluster-api.sigs.k8s.io/user/quick-start.html#initialization-for-common-providers) + +Prior to generate a cluster template, you need to create a VM image for use in the cluster. The MicroK8s provider works with any stock Ubuntu image. Use the Ubuntu 22.04 LTS image with: + +```bash +gcloud compute images create ubuntu-2204 --source-image-project ubuntu-os-cloud --source-image-family ubuntu-2204-lts +``` + +Make note of the name of the image `ubuntu-2204`, which we then feed into the cluster template. + +Generate a cluster template with: + +```bash +# review list of variables needed for the cluster template +clusterctl generate cluster microk8s-gcp --from ./templates/cluster-template-gcp.yaml --list-variables + +# set environment variables (edit the file as needed before sourcing it) +source ./templates/cluster-template-gcp.rc + +# generate the cluster +clusterctl generate cluster microk8s-gcp --from ./templates/cluster-template-gcp.yaml > cluster-gcp.yaml +``` + +Then, deploy the cluster with: + +```bash +microk8s kubectl apply -f cluster-gcp.yaml +``` + ## Development The two MicroK8s CAPI providers, the bootstrap and control plane, serve distinct purposes: diff --git a/templates/cluster-template-gcp.rc b/templates/cluster-template-gcp.rc new file mode 100644 index 0000000..af882d6 --- /dev/null +++ b/templates/cluster-template-gcp.rc @@ -0,0 +1,15 @@ +# Kubernetes cluster configuration +export KUBERNETES_VERSION=1.25.0 +export CONTROL_PLANE_MACHINE_COUNT=1 +export WORKER_MACHINE_COUNT=1 + +# GCP configuration +export GCP_REGION="europe-west" +export GCP_NETWORK_NAME="default" +export GCP_PROJECT="my-gcp-project-name" + +# GCP machine configuration +export GCP_PUBLIC_IP=true # set to false if you have configured a cloud NAT +export GCP_CONTROL_PLANE_MACHINE_TYPE=n1-standard-2 +export GCP_NODE_MACHINE_TYPE=n1-standard-2 +export IMAGE_ID=projects/$GCP_PROJECT/global/images/ubuntu-2204 diff --git a/templates/cluster-template-gcp.yaml b/templates/cluster-template-gcp.yaml new file mode 100644 index 0000000..d8dc212 --- /dev/null +++ b/templates/cluster-template-gcp.yaml @@ -0,0 +1,103 @@ +# Based on https://github.com/kubernetes-sigs/cluster-api-provider-aws/releases/download/v1.5.0/cluster-template.yaml +--- +apiVersion: cluster.x-k8s.io/v1beta1 +kind: Cluster +metadata: + name: ${CLUSTER_NAME} +spec: + clusterNetwork: + apiServerPort: 6443 + infrastructureRef: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: GCPCluster + name: ${CLUSTER_NAME} + controlPlaneRef: + kind: MicroK8sControlPlane + apiVersion: controlplane.cluster.x-k8s.io/v1beta1 + name: ${CLUSTER_NAME}-control-plane +--- +apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 +kind: GCPCluster +metadata: + name: ${CLUSTER_NAME} +spec: + project: "${GCP_PROJECT}" + region: "${GCP_REGION}" + network: + name: "${GCP_NETWORK_NAME}" +--- +apiVersion: controlplane.cluster.x-k8s.io/v1beta1 +kind: MicroK8sControlPlane +metadata: + name: "${CLUSTER_NAME}-control-plane" +spec: + controlPlaneConfig: + initConfiguration: + joinTokenTTLInSecs: 900000 + IPinIP: true + addons: + - dns + - ingress + clusterConfiguration: + portCompatibilityRemap: true + machineTemplate: + infrastructureTemplate: + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: GCPMachineTemplate + name: "${CLUSTER_NAME}-control-plane" + replicas: ${CONTROL_PLANE_MACHINE_COUNT:=1} + version: "v${KUBERNETES_VERSION}" +--- +apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 +kind: GCPMachineTemplate +metadata: + name: ${CLUSTER_NAME}-control-plane +spec: + template: + spec: + instanceType: "${GCP_CONTROL_PLANE_MACHINE_TYPE}" + image: "${IMAGE_ID}" + publicIP: ${GCP_PUBLIC_IP} +--- +apiVersion: cluster.x-k8s.io/v1beta1 +kind: MachineDeployment +metadata: + name: "${CLUSTER_NAME}-md-0" +spec: + clusterName: "${CLUSTER_NAME}" + replicas: ${WORKER_MACHINE_COUNT:=1} + selector: + matchLabels: + template: + spec: + clusterName: "${CLUSTER_NAME}" + version: "${KUBERNETES_VERSION}" + bootstrap: + configRef: + name: "${CLUSTER_NAME}-md-0" + apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 + kind: MicroK8sConfigTemplate + infrastructureRef: + name: "${CLUSTER_NAME}-md-0" + apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 + kind: GCPMachineTemplate +--- +apiVersion: infrastructure.cluster.x-k8s.io/v1beta1 +kind: GCPMachineTemplate +metadata: + name: ${CLUSTER_NAME}-md-0 +spec: + template: + spec: + instanceType: "${GCP_NODE_MACHINE_TYPE}" + image: "${IMAGE_ID}" +--- +apiVersion: bootstrap.cluster.x-k8s.io/v1beta1 +kind: MicroK8sConfigTemplate +metadata: + name: "${CLUSTER_NAME}-md-0" +spec: + template: + spec: + clusterConfiguration: + portCompatibilityRemap: true