From 34624bc3f2494b566f8985ef87e7ae84c9a5801a Mon Sep 17 00:00:00 2001 From: Philip Haberkern <59010269+thedatabaseme@users.noreply.github.com> Date: Tue, 5 Apr 2022 04:51:21 +0200 Subject: [PATCH] Added Vagrantfile for build environment with Vagrant (#14) * Added .vagrant to ignore * Added Vagrantfile * Added section for Vagrant * made retry_count customizeable (default 20) --- .gitignore | 1 + README.md | 7 +++ roles/k3s/master/tasks/main.yml | 3 +- vagrant/Vagrantfile | 79 +++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 vagrant/Vagrantfile diff --git a/.gitignore b/.gitignore index e69de29..997ca2f 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1 @@ +.vagrant \ No newline at end of file diff --git a/README.md b/README.md index c763371..72a775b 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,13 @@ scp debian@master_ip:~/.kube/config ~/.kube/config See the commands [here](https://docs.technotim.live/posts/k3s-etcd-ansible/#testing-your-cluster). +### Vagrant + +You may want to kickstart your k3s cluster by using Vagrant to quickly build you all needed VMs with one command. +Head to the `vagrant` subfolder and type `vagrant up` to get your environment setup. +After the VMs have got build, deploy k3s using the Ansible playbook `site.yml` by the +`vagrant provision --provision-with ansible` command. + ## Thanks 🤝 This repo is really standing on the shoulders of giants. To all those who have contributed. diff --git a/roles/k3s/master/tasks/main.yml b/roles/k3s/master/tasks/main.yml index 689eb36..51ab7ac 100644 --- a/roles/k3s/master/tasks/main.yml +++ b/roles/k3s/master/tasks/main.yml @@ -1,4 +1,5 @@ --- + - name: Clean previous runs of k3s-init systemd: name: k3s-init @@ -83,7 +84,7 @@ cmd: k3s kubectl get nodes -l "node-role.kubernetes.io/master=true" -o=jsonpath="{.items[*].metadata.name}" register: nodes until: nodes.rc == 0 and (nodes.stdout.split() | length) == (groups['master'] | length) - retries: 20 + retries: "{{ retry_count | default(20) }}" delay: 10 changed_when: false always: diff --git a/vagrant/Vagrantfile b/vagrant/Vagrantfile new file mode 100755 index 0000000..0e9ac61 --- /dev/null +++ b/vagrant/Vagrantfile @@ -0,0 +1,79 @@ +# -*- mode: ruby -*- +# vi: set ft=ruby : + +Vagrant.configure("2") do |config| + # General configuration + config.vm.box = "generic/ubuntu2110" + config.vm.synced_folder ".", "/vagrant", disabled: true + config.ssh.insert_key = false + + config.vm.provider :virtualbox do |v| + v.memory = 4096 + v.cpus = 2 + v.linked_clone = true + end + + # Control Node 1 + config.vm.define "control1" do |control1| + control1.vm.hostname = "control1" + control1.vm.network "private_network", ip: "192.168.30.38" + end + + # Control Node 2 + config.vm.define "control2" do |control2| + control2.vm.hostname = "control2" + control2.vm.network "private_network", ip: "192.168.30.39" + end + + # Control Node 3 + config.vm.define "control3" do |control3| + control3.vm.hostname = "control3" + control3.vm.network "private_network", ip: "192.168.30.40" + end + + # Worker Node 1 + config.vm.define "node1" do |node1| + node1.vm.hostname = "node1" + node1.vm.network "private_network", ip: "192.168.30.41" + end + + # Worker Node 2 + config.vm.define "node2" do |node2| + node2.vm.hostname = "node2" + node2.vm.network "private_network", ip: "192.168.30.42" + end + + config.vm.provision "ansible",type: "ansible", run: "never" do |ansible| + ansible.playbook = "../site.yml" + ansible.limit = "all" + ansible.groups = { + "master" => ["control1", "control2", "control3"], + "node" => ["node1", "node2"], + "k3s_cluster:children" => ["master", "node"], + "k3s_cluster:vars" => {"k3s_version" => "v1.23.4+k3s1", + "ansible_user" => "vagrant", + "systemd_dir" => "/etc/systemd/system", + "flannel_iface" => "eth1", + "apiserver_endpoint" => "192.168.30.222", + "k3s_token" => "supersecret", + "extra_server_args" => "--node-ip={{ ansible_eth1.ipv4.address }} --flannel-iface={{ flannel_iface }} --no-deploy servicelb --no-deploy traefik", + "extra_agent_args" => "--flannel-iface={{ flannel_iface }}", + "kube_vip_tag_version" => "v0.4.2", + "metal_lb_speaker_tag_version" => "v0.12.1", + "metal_lb_controller_tag_version" => "v0.12.1", + "metal_lb_ip_range" => "192.168.30.80-192.168.30.90", + "retry_count" => "30"} + } + ansible.host_vars = { + "control1" => { + "server_init_args" => "--cluster-init --token {{ k3s_token }} {{ extra_server_args | default('') }}" + }, + "control2" => { + "server_init_args" => "--server https://192.168.30.38:6443 --token {{ k3s_token }} {{ extra_server_args | default('') }}" + }, + "control3" => { + "server_init_args" => "--server https://192.168.30.38:6443 --token {{ k3s_token }} {{ extra_server_args | default('') }}" + } + } + end +end \ No newline at end of file