Skip to content

Instantly share code, notes, and snippets.

@rmetzler
Forked from vfarcic/135-cluster-api.sh
Created March 30, 2022 20:38
Show Gist options
  • Save rmetzler/0d44ae054e1cdf3343deb696a33bad19 to your computer and use it in GitHub Desktop.
Save rmetzler/0d44ae054e1cdf3343deb696a33bad19 to your computer and use it in GitHub Desktop.
# Source: https://gist.github.com/d8113b6f149583e1cf1614d76f2a4182
#####################################################################
# How To Create, Provision, And Operate Kubernetes With Cluster API #
# https://youtu.be/8yUDUhZ6ako #
#####################################################################
# Referenced videos:
# - Should We Replace Docker Desktop With Rancher Desktop?: https://youtu.be/bYVfCp9dRTE
#########
# Setup #
#########
# Create a Kubernetes cluster with Ingress
# This demo was tested on Rancher Desktop, but it should work with any Kubernetes cluster
# Please watch https://youtu.be/bYVfCp9dRTE if you are not familiar with Rancher Desktop
# Install the CLI from https://cluster-api.sigs.k8s.io/user/quick-start.html#install-clusterctl
################
# DigitalOcean #
################
export DIGITALOCEAN_ACCESS_TOKEN=[...]
export DO_B64ENCODED_CREDENTIALS="$(\
echo -n "$DIGITALOCEAN_ACCESS_TOKEN" \
| base64 \
| tr -d '\n')"
export DO_REGION=nyc1
# Replace `[...]` with the SSH key fingerprint (e.g., `18:9c:3d:5c:9d:b8:0e:6f:df:58:f2:20:dd:2d:5d:54`)
export DO_SSH_KEY_FINGERPRINT=[...]
export DO_CONTROL_PLANE_MACHINE_TYPE=s-2vcpu-2gb
export DO_NODE_MACHINE_TYPE=s-2vcpu-2gb
#################################
# Create Virtual Machine Images #
#################################
git clone https://github.com/kubernetes-sigs/image-builder
cd image-builder/images/capi
cat Makefile
make build-do-ubuntu-2004
# The command might fail asking you to install additional tools (e.g., Ansible)
doctl compute image list
export DO_CONTROL_PLANE_MACHINE_IMAGE=[...]
export DO_NODE_MACHINE_IMAGE=[...]
cd ../../../
###################################
# Setup Cluster API And Providers #
###################################
clusterctl init \
--infrastructure digitalocean
###############################
# Create A Kubernetes Cluster #
###############################
clusterctl generate cluster devops-toolkit \
--infrastructure digitalocean \
--target-namespace infra \
--kubernetes-version v1.20.10 \
--control-plane-machine-count 3 \
--worker-machine-count 3 \
| tee cluster.yaml
kubectl create namespace infra
kubectl apply --filename cluster.yaml
kubectl --namespace infra \
get clusters
kubectl --namespace infra \
get machinedeployments
kubectl --namespace infra \
get kubeadmcontrolplane
kubectl --namespace infra \
describe kubeadmcontrolplane \
devops-toolkit-control-plane
clusterctl --namespace infra \
get kubeconfig devops-toolkit \
| tee kubeconfig.yaml
kubectl --kubeconfig kubeconfig.yaml \
apply \
--filename https://docs.projectcalico.org/v3.20/manifests/calico.yaml
kubectl --kubeconfig kubeconfig.yaml \
get nodes
#################################################
# Upgrade A Kubernetes Clsuter With Cluster API #
#################################################
kubectl --namespace infra \
get kubeadmcontrolplane
cd image-builder/images/capi
# Replace `1.20.10` with `1.21.6` in `packer/config/kubernetes.json`
# Replace `1.20` with `1.21` in `packer/config/kubernetes.json`
make build-do-ubuntu-2004
doctl compute image list
# Copy the ID of the newly created image
cd ../../../
# Open `cluster.yaml`
# Copy `DOMachineTemplate` resource of the control plane
# Change the `image` of new `MachineTemplate` resources
# Save the changes
kubectl apply --filename cluster.yaml
# Open `cluster.yaml`
# Change the `name` of the `MachineTemplate` reference of the control plane
# Replace `v1.20.10` with `v1.21.6`
# Save the changes
kubectl apply --filename cluster.yaml
# Sometimes it works, sometimes it doesn't.
# Do the same for worker nodes
###########
# Destroy #
###########
kubectl delete --filename cluster.yaml
kubectl --namespace infra \
get clusters
# Wait until the cluster is deleted
# Destroy or reset the management cluster
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment