Last active
March 16, 2020 08:53
-
-
Save rizkidoank/14e2be9a08785aec2fad4daf79c3bdda to your computer and use it in GitHub Desktop.
Packer QEMU/KVM to build Kubernetes image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"builders":[ | |
{ | |
"type": "qemu", | |
"iso_url": "{{ user `image_url` }}", | |
"iso_checksum": "{{ user `image_checksum` }}", | |
"iso_checksum_type": "{{ user `image_checksum_type` }}", | |
"format": "qcow2", | |
"disk_image": true, | |
"disk_size": "{{ user `disk_size` }}", | |
"output_directory": "build", | |
"disk_compression": true, | |
"headless": true, | |
"boot_command": [ | |
"<enter>" | |
], | |
"accelerator": "kvm", | |
"ssh_username": "{{ user `ssh_username` }}", | |
"ssh_password": "{{ user `ssh_password` }}", | |
"ssh_port": 22, | |
"ssh_wait_timeout": "300s", | |
"vm_name": "{{ user `vm_name` }}", | |
"use_default_display": false, | |
"qemuargs": [ | |
["-m", "{{ user `memory` }}"], | |
["-smp", "cpus={{ user `cpus` }}"], | |
["-cdrom", "{{ user `cloud_init_image` }}"], | |
["-serial", "mon:stdio"] | |
] | |
} | |
], | |
"provisioners": [ | |
{ | |
"execute_command": "echo '{{ user `ssh_password` }}' | {{.Vars}} sudo -E -S bash -x '{{.Path}}'", | |
"scripts": [ | |
"setup.sh" | |
], | |
"type": "shell" | |
}, | |
{ | |
"type": "shell", | |
"inline": ["sudo sync"] | |
} | |
], | |
"variables": { | |
"cpus": "2", | |
"image_checksum": "2ca8b726be5bbd929fb5675372819f1471fc831d05b6b31444e10adc3a06a8d5", | |
"image_checksum_type": "sha256", | |
"image_url" : "https://cloud-images.ubuntu.com/minimal/releases/bionic/release/ubuntu-18.04-minimal-cloudimg-amd64.img", | |
"cloud_init_image": "cloud-init.img", | |
"disk_size": "8192", | |
"memory": "2048M", | |
"ssh_username": "ubuntu", | |
"ssh_password": "P4s5W0rD", | |
"vm_name": "kubernetes-{{timestamp}}.qcow2" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -x | |
apt-get update && apt-get install -y \ | |
apt-transport-https ca-certificates curl software-properties-common | |
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add - | |
add-apt-repository \ | |
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \ | |
$(lsb_release -cs) \ | |
stable" | |
apt-get update && apt-get install -y \ | |
containerd.io=1.2.10-3 \ | |
docker-ce=5:19.03.4~3-0~ubuntu-$(lsb_release -cs) \ | |
docker-ce-cli=5:19.03.4~3-0~ubuntu-$(lsb_release -cs) | |
cat > /etc/docker/daemon.json <<EOF | |
{ | |
"exec-opts": ["native.cgroupdriver=systemd"], | |
"log-driver": "json-file", | |
"log-opts": { | |
"max-size": "100m" | |
}, | |
"storage-driver": "overlay2" | |
} | |
EOF | |
mkdir -p /etc/systemd/system/docker.service.d | |
systemctl daemon-reload | |
systemctl restart docker | |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add - | |
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list | |
deb https://apt.kubernetes.io/ kubernetes-xenial main | |
EOF | |
sudo apt-get update | |
sudo apt-get install -y kubelet kubeadm kubectl | |
sudo apt-mark hold kubelet kubeadm kubectl | |
systemctl daemon-reload | |
systemctl restart kubelet | |
kubeadm config images pull | |
swapoff -a | |
apt autoclean | |
rm -Rf /var/cache/apt/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cloud-config | |
password: P4s5W0rD | |
ssh_pwauth: true | |
chpasswd: | |
expire: false | |
locale: en_US.UTF-8 | |
locale_configfile: /etc/default/locale |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment