In the previous blog, I explained how to create and manage Kubernetes with cluster API locally with the help of docker infrastructure.
In this blog, I will explain how to create and manage the k8s with Cluster API in the google cloud.
Note – Throughout the blog, I will use Kubernetes version 1.22.9 and it is recommended to use the version of our OS image created by the image builder. You can check from kubernetes.json and use that.
Step 1 –
- Create the kind cluster –
kind create cluster --image kindest/node:v1.22.9 --wait 5m
Step 2 –
Follow image builder for GCP steps and build an image.
Step 3 –
- Export the following env variables – (reference)
export GCP_PROJECT_ID=<YOUR PROJECT ID>
export GOOGLE_APPLICATION_CREDENTIALS=<PATH TO GCP CREDENTIALS>
export GCP_B64ENCODED_CREDENTIALS=$( cat /path/to/gcp-credentials.json | base64 | tr -d '\n' )
export CLUSTER_TOPOLOGY=true
export GCP_REGION="us-east4"
export GCP_PROJECT="<YOU GCP PROJECT NAME>"
export KUBERNETES_VERSION=1.22.9
export IMAGE_ID=projects/$GCP_PROJECT/global/images/<IMAGE ID>
export GCP_CONTROL_PLANE_MACHINE_TYPE=n1-standard-2
export GCP_NODE_MACHINE_TYPE=n1-standard-2
export GCP_NETWORK_NAME=default
export CLUSTER_NAME=test
Step 4 –
setup the network in this example we are using the default network so we will create some router/nats for our workload cluster to have internet access.
gcloud compute routers create "${CLUSTER_NAME}-myrouter" --project="${GCP_PROJECT}" --region="${GCP_REGION}" --network="default"
gcloud compute routers nats create "${CLUSTER_NAME}-mynat" --project="${GCP_PROJECT}" --router-region="${GCP_REGION}" --router="${CLUSTER_NAME}-myrouter" --nat-all-subnet-ip-ranges --auto-allocate-nat-external-ips
Step 5 –
- Initialize the infrastructure
clusterctl init --infrastructure gcp
- Generate the workload cluster config and apply it
clusterctl generate cluster $CLUSTER_NAME --kubernetes-version v1.22.9 > workload-test.yaml
kubectl apply -f workload-test.yaml
- View the cluster and its resources
$ clusterctl describe cluster $CLUSTER_NAME
NAME READY SEVERITY REASON SINCE MESSAGE
/test False Info WaitingForKubeadmInit 5s
├─ClusterInfrastructure - GCPCluster/test
└─ControlPlane - KubeadmControlPlane/test-control-plane False Info WaitingForKubeadmInit 5s
└─Machine/test-control-plane-x57zs True 31s
└─MachineInfrastructure - GCPMachine/test-control-plane-7xzw2
- Check the status of the control plane
$ kubectl get kubeadmcontrolplane
NAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSION
test-control-plane test 1 1 1 2m9s v1.22.9
Note – The controller plane won’t be ready until the next step when I install the CNI (Container Network Interface).
Step 6 –
- Get the kubeconfig for the workload cluster
$ clusterctl get kubeconfig $CLUSTER_NAME > workload-test.kubeconfig
- Apply the cni
kubectl --kubeconfig=./workload-test.kubeconfig \
apply -f https://docs.projectcalico.org/v3.20/manifests/calico.yaml
- Wait a bit and you should see this when getting the kubeadmcontrolplane
$ kubectl get kubeadmcontrolplane
NAME CLUSTER INITIALIZED API SERVER AVAILABLE REPLICAS READY UPDATED UNAVAILABLE AGE VERSION
test-control-plane test true true 1 1 1 0 6m33s v1.22.9
$ kubectl get nodes --kubeconfig=./workload-test.kubeconfig
NAME STATUS ROLES AGE VERSION
test-control-plane-7xzw2 Ready control-plane,master 62s v1.22.9
Step 7 –
- Edit the
MachineDeployment
in theworkload-test.yaml
it has 0 replicas add the replicas you want to have your nodes, in thiscase,
we used 2. Apply theworkload-test.yaml
$ kubectl apply -f workload-test.yaml
- After a few minutes, you should see something like this –
$ clusterctl describe cluster $CLUSTER_NAME
NAME READY SEVERITY REASON SINCE MESSAGE
/test True 15m
├─ClusterInfrastructure - GCPCluster/test
├─ControlPlane - KubeadmControlPlane/test-control-plane True 15m
│ └─Machine/test-control-plane-x57zs True 19m
│ └─MachineInfrastructure - GCPMachine/test-control-plane-7xzw2
└─Workers
└─MachineDeployment/test-md-0 True 10m
└─2 Machines... True 13m See test-md-0-68bd55744b-qpk67, test-md-0-68bd55744b-tsgf6
$ kubectl get nodes --kubeconfig=./workload-test.kubeconfig
NAME STATUS ROLES AGE VERSION
test-control-plane-7xzw2 Ready control-plane,master 21m v1.22.9
test-md-0-b7766 Ready <none> 17m v1.22.9
test-md-0-wsgpj Ready <none> 17m v1.22.9
Yaaa! Now we have a Kubernetes cluster in the GCP with 1 control pannel with 2 worker nodes.
Step 8 –
Delete what you have created –
$ kubectl delete cluster $CLUSTER_NAME
$ gcloud compute routers nats delete "${CLUSTER_NAME}-mynat" --project="${GCP_PROJECT}" \
--router-region="${GCP_REGION}" --router="${CLUSTER_NAME}-myrouter"
$ gcloud compute routers delete "${CLUSTER_NAME}-myrouter" --project="${GCP_PROJECT}" \
--region="${GCP_REGION}"
$ kind delete cluster