Build Kubernetes control plane image with Packer

Steps to prepare single control plane image is quite simple:

  • Prepare Docker and Kubernetes packages and settings
  • Execute kubeadm bootstrap script when EC2 start up first time

One unanswered question is: How to add additional control plane nodes and worker nodes which required tokens and certificates to be preset when joining the cluster?

Packer builders

Builders section define base image, EC2 instate type and other settings to build an image.

"source_ami_filter": {
    "filters": {
        "virtualization-type": "hvm",
        "name": "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*",
        "root-device-type": "ebs"
    },
    "owners": ["099720109477"],
    "most_recent": true
},
"instance_type": "t2.medium",

Packer provisioners

Prepare base image with Docker and Kubernetes

I have used install-kubeadm.sh script to setup Docker and Kubernetes packages and settings.

{
    "type": "shell",
    "script": "../kubeadm/scripts/install-kubeadm.sh",
    "execute_command": "sudo sh -c '{{ .Vars }} {{ .Path }}'"
}

Create kubelet bootstrap script

Bootstrap Kubernetes kubelet process when instance created from AMI. Upload kubelet-init.sh in /var/lib/cloud/scripts/per-instance/ which will be executed first time when server is creted from AMI.

{
    "type": "file",
    "source": "kubelet-init.sh",
    "destination": "/tmp/"
},
{
    "type": "shell",
    "inline": 
    [
    "sudo cp /tmp/kubelet-init.sh /var/lib/cloud/scripts/per-instance/",
    "sudo chmod +x /var/lib/cloud/scripts/per-instance/kubelet-init.sh"
    ]
}

References