Compare commits

...

6 commits

Author SHA1 Message Date
b5bbc4e7a8 merge fixes 2023-04-19 14:14:40 -05:00
05ab05a84a improve timing/error handling in k8s bootstrap 2023-04-19 13:39:27 -05:00
101ad08bd3 formatting fixes 2023-04-19 13:38:32 -05:00
6cd9786752 formatting fixes 2023-04-19 13:37:41 -05:00
3a13a5b3fa blank out example key 2023-04-19 13:36:58 -05:00
0b13e195dc improve handling of SSH keys 2023-04-19 13:36:29 -05:00
9 changed files with 101 additions and 97 deletions

View file

@ -178,7 +178,6 @@ autoinstall:
hostname: ${ vm_guest_os_hostname } hostname: ${ vm_guest_os_hostname }
users: users:
- name: ${ build_username } - name: ${ build_username }
passwd: "${ build_password }"
groups: [adm, cdrom, dip, plugdev, lxd, sudo] groups: [adm, cdrom, dip, plugdev, lxd, sudo]
lock-passwd: false lock-passwd: false
sudo: ALL=(ALL) NOPASSWD:ALL sudo: ALL=(ALL) NOPASSWD:ALL

View file

@ -0,0 +1,3 @@
-----BEGIN OPENSSH PRIVATE KEY-----
-----END OPENSSH PRIVATE KEY-----

View file

@ -77,12 +77,11 @@ communicator_port = 22
communicator_timeout = "20m" communicator_timeout = "20m"
common_ip_wait_timeout = "20m" common_ip_wait_timeout = "20m"
common_shutdown_timeout = "15m" common_shutdown_timeout = "15m"
vm_shutdown_command = "sudo /usr/sbin/shutdown -P now"
build_remove_keys = true build_remove_keys = true
build_username = "admin" build_username = "admin"
build_password = "VMware1!" build_password = "VMware1!"
ssh_keys = [ ssh_keys = [
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOpLvpxilPjpCahAQxs4RQgv+Lb5xObULXtwEoimEBpA builder" "ssh-ed25519 blahblahblah builder"
] ]
// Provisioner Settings // Provisioner Settings

View file

@ -34,13 +34,13 @@ locals {
build_tool = "HashiCorp Packer ${packer.version}" build_tool = "HashiCorp Packer ${packer.version}"
build_date = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp()) build_date = formatdate("YYYY-MM-DD hh:mm ZZZ", timestamp())
build_description = "Kubernetes Ubuntu 20.04 Node template\nBuild date: ${local.build_date}\nBuild tool: ${local.build_tool}" build_description = "Kubernetes Ubuntu 20.04 Node template\nBuild date: ${local.build_date}\nBuild tool: ${local.build_tool}"
shutdown_command = "sudo /usr/sbin/shutdown -P now"
iso_paths = ["[${var.common_iso_datastore}] ${var.iso_path}/${var.iso_file}"] iso_paths = ["[${var.common_iso_datastore}] ${var.iso_path}/${var.iso_file}"]
iso_checksum = "${var.iso_checksum_type}:${var.iso_checksum_value}" iso_checksum = "${var.iso_checksum_type}:${var.iso_checksum_value}"
data_source_content = { data_source_content = {
"/meta-data" = file("data/meta-data") "/meta-data" = file("data/meta-data")
"/user-data" = templatefile("data/user-data.pkrtpl.hcl", { "/user-data" = templatefile("data/user-data.pkrtpl.hcl", {
build_username = var.build_username build_username = var.build_username
build_password = bcrypt(var.build_password)
ssh_keys = concat([local.ssh_public_key], var.ssh_keys) ssh_keys = concat([local.ssh_public_key], var.ssh_keys)
vm_guest_os_language = var.vm_guest_os_language vm_guest_os_language = var.vm_guest_os_language
vm_guest_os_keyboard = var.vm_guest_os_keyboard vm_guest_os_keyboard = var.vm_guest_os_keyboard

View file

@ -15,7 +15,6 @@ variable "vsphere_endpoint" {
variable "vsphere_username" { variable "vsphere_username" {
type = string type = string
description = "The username to login to the vCenter Server instance. ('packer')" description = "The username to login to the vCenter Server instance. ('packer')"
sensitive = true
} }
variable "vsphere_password" { variable "vsphere_password" {

View file

@ -52,7 +52,7 @@ EOF
sudo chown "$(id -u):$(id -g)" "${HOME}"/.kube/config sudo chown "$(id -u):$(id -g)" "${HOME}"/.kube/config
echo ">> Applying Calico networking..." echo ">> Applying Calico networking..."
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/master/manifests/calico.yaml
echo ">> Creating discovery config..." echo ">> Creating discovery config..."
kubectl -n kube-public get configmap cluster-info -o jsonpath='{.data.kubeconfig}' > discovery.yaml kubectl -n kube-public get configmap cluster-info -o jsonpath='{.data.kubeconfig}' > discovery.yaml
@ -63,7 +63,7 @@ EOF
fi fi
fi fi
echo ">> Waiting up to 10 minutes for all control-plane nodes to be Ready..." echo ">> Waiting up to 10 minutes for all control-plane nodes to be Ready..."
python3 -m http.server 2>/dev/null & python3 -m http.server &>/dev/null &
PROC_ID=$! PROC_ID=$!
attempts_max=60 attempts_max=60
attempt=0 attempt=0
@ -382,20 +382,25 @@ else
sleep 10 sleep 10
done done
echo ">> Continuing after $((attempt*10)) seconds." echo ">> Continuing after $((attempt*10)) seconds."
echo ">> Joining cluster..." echo ">> Retrieving cluster discovery config..."
attempts_max=6 attempts_max=6
attempt=0 attempt=0
until [ -f /etc/kubernetes/discovery.yaml ]; do until [ -f ~/discovery.yaml ] || [ ${attempt} -eq ${attempts_max} ]; do
wget "http://${K8S_CONTROLPLANE_VIP}:8000/discovery.yaml" 2>/dev/null wget "http://${K8S_CONTROLPLANE_VIP}:8000/discovery.yaml"
if ! sudo install -m 600 discovery.yaml /etc/kubernetes/discovery.yaml 2>/dev/null; then sleep 2
if [ ${attempt} -eq ${attempts_max} ]; then if ! [ -f ~/discovery.yaml ]; then
echo ">> [ERROR] Timeout waiting for discovery.yaml! <<" echo ">> Unable to retrieve config..."
exit 1
fi
attempt=$((attempt+1)) attempt=$((attempt+1))
sleep 10 sleep 8
fi fi
done done
if ! [ -f ~/discovery.yaml ]; then
echo ">> Timeout reached while retrieving config!"
echo "Exiting."
exit 1
fi
sudo install -o root -g root -m 600 discovery.yaml /etc/kubernetes/discovery.yaml
echo ">> Successfully discovered cluster!"
cat << EOF > kubeadmjoin.yaml cat << EOF > kubeadmjoin.yaml
apiVersion: kubeadm.k8s.io/v1beta3 apiVersion: kubeadm.k8s.io/v1beta3
caCertPath: /etc/kubernetes/pki/ca.crt caCertPath: /etc/kubernetes/pki/ca.crt
@ -411,17 +416,8 @@ nodeRegistration:
controlPlane: controlPlane:
certificateKey: ${KUBEADM_CERTKEY} certificateKey: ${KUBEADM_CERTKEY}
EOF EOF
if sudo kubeadm join "${K8S_CONTROLPLANE_VIP}:6443" --config kubeadmjoin.yaml; then echo ">> Joining cluster..."
if [ ! -f /etc/kubernetes/manifests/kube-vip.yaml ]; then if sudo kubeadm join "${K8S_CONTROLPLANE_VIP}":6443 --config kubeadmjoin.yaml; then
echo ">> Configuring kube-vip..."
sudo ctr image pull ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}"
sudo ctr run --rm --net-host ghcr.io/kube-vip/kube-vip:"${KUBEVIP_VER}" vip /kube-vip manifest pod \
--interface ens192 \
--vip "${K8S_CONTROLPLANE_VIP}" \
--controlplane \
--arp \
--leaderElection | sudo tee /etc/kubernetes/manifests/kube-vip.yaml
fi
echo ">> Node ${HOSTNAME} successfully initialized!" echo ">> Node ${HOSTNAME} successfully initialized!"
touch .k8s-node-success touch .k8s-node-success
mkdir -p "${HOME}"/.kube mkdir -p "${HOME}"/.kube

View file

@ -17,7 +17,7 @@ echo ">> Continuing after $((attempt*10)) seconds."
echo ">> Waiting up to 10 minutes for all control-plane nodes..." echo ">> Waiting up to 10 minutes for all control-plane nodes..."
attempts_max=60 attempts_max=60
attempt=0 attempt=0
until wget "http://${K8S_CONTROLPLANE_VIP}:8000/.k8s-controlplane-success" 2>/dev/null; do until curl --fail "http://${K8S_CONTROLPLANE_VIP}:8000/.k8s-controlplane-success" 2>/dev/null; do
if [ ${attempt} -eq ${attempts_max} ]; then if [ ${attempt} -eq ${attempts_max} ]; then
echo ">> [ERROR] Timeout waiting for control-plane nodes! <<" echo ">> [ERROR] Timeout waiting for control-plane nodes! <<"
exit 1 exit 1
@ -26,18 +26,26 @@ until wget "http://${K8S_CONTROLPLANE_VIP}:8000/.k8s-controlplane-success" 2>/de
sleep 10 sleep 10
done done
echo ">> Continuing after $((attempt*10)) seconds." echo ">> Continuing after $((attempt*10)) seconds."
echo ">> Joining cluster..." echo ">> Retrieving cluster discovery config..."
attempts_max=6 attempts_max=6
attempt=0 attempt=0
until [ -f /etc/kubernetes/discovery.yaml ]; do until [ -f ~/discovery.yaml ] || [ ${attempt} -eq ${attempts_max} ]; do
wget "http://${K8S_CONTROLPLANE_VIP}:8000/discovery.yaml" 2>/dev/null wget "http://${K8S_CONTROLPLANE_VIP}:8000/discovery.yaml"
sudo install -m 600 discovery.yaml /etc/kubernetes/discovery.yaml 2>/dev/null sleep 2
if [ ! -f /etc/kubernetes/discovery.yaml ]; then if ! [ -f ~/discovery.yaml ]; then
echo ">> Unable to retrieve config..."
attempt=$((attempt+1)) attempt=$((attempt+1))
sleep 10 sleep 8
fi fi
done done
if ! [ -f ~/discovery.yaml ]; then
echo ">> Timeout reached while retrieving config!"
echo "Exiting."
exit 1
fi
sudo install -o root -g root -m 600 discovery.yaml /etc/kubernetes/discovery.yaml
echo ">> Successfully discovered cluster!"
echo ">> Discovered cluster!"
cat << EOF > kubeadmjoin.yaml cat << EOF > kubeadmjoin.yaml
apiVersion: kubeadm.k8s.io/v1beta3 apiVersion: kubeadm.k8s.io/v1beta3
caCertPath: /etc/kubernetes/pki/ca.crt caCertPath: /etc/kubernetes/pki/ca.crt