Skip to content

Instantly share code, notes, and snippets.

@vfarcic
Last active August 2, 2022 17:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save vfarcic/d8113b6f149583e1cf1614d76f2a4182 to your computer and use it in GitHub Desktop.
Save vfarcic/d8113b6f149583e1cf1614d76f2a4182 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