From 74ae1a42a6e68e5ef2070568ef35437877428f28 Mon Sep 17 00:00:00 2001 From: Muhammad Yuga Date: Thu, 31 Dec 2020 22:13:56 +0700 Subject: [PATCH] Add cluster setup with Vagrant --- hands-on/vagrant/.gitignore | 1 + hands-on/vagrant/README.md | 12 +++ hands-on/vagrant/Vagrantfile | 37 +++++++++ hands-on/vagrant/ansible.cfg | 4 + hands-on/vagrant/hosts | 2 + hands-on/vagrant/join | 1 + hands-on/vagrant/main.yml | 13 +++ .../roles/general/tasks/01-install.yml | 83 +++++++++++++++++++ .../vagrant/roles/general/tasks/02-config.yml | 31 +++++++ .../roles/general/tasks/03-service.yml | 14 ++++ hands-on/vagrant/roles/general/tasks/main.yml | 4 + .../roles/general/templates/daemon.json.j2 | 5 ++ .../general/templates/kubernetes.conf.j2 | 3 + hands-on/vagrant/roles/master/tasks/main.yml | 21 +++++ hands-on/vagrant/roles/worker/tasks/main.yml | 8 ++ hands-on/vagrant/vars/main.yml | 3 + 16 files changed, 242 insertions(+) create mode 100644 hands-on/vagrant/.gitignore create mode 100644 hands-on/vagrant/README.md create mode 100644 hands-on/vagrant/Vagrantfile create mode 100644 hands-on/vagrant/ansible.cfg create mode 100644 hands-on/vagrant/hosts create mode 100644 hands-on/vagrant/join create mode 100644 hands-on/vagrant/main.yml create mode 100644 hands-on/vagrant/roles/general/tasks/01-install.yml create mode 100644 hands-on/vagrant/roles/general/tasks/02-config.yml create mode 100644 hands-on/vagrant/roles/general/tasks/03-service.yml create mode 100644 hands-on/vagrant/roles/general/tasks/main.yml create mode 100644 hands-on/vagrant/roles/general/templates/daemon.json.j2 create mode 100644 hands-on/vagrant/roles/general/templates/kubernetes.conf.j2 create mode 100644 hands-on/vagrant/roles/master/tasks/main.yml create mode 100644 hands-on/vagrant/roles/worker/tasks/main.yml create mode 100644 hands-on/vagrant/vars/main.yml diff --git a/hands-on/vagrant/.gitignore b/hands-on/vagrant/.gitignore new file mode 100644 index 0000000..8000dd9 --- /dev/null +++ b/hands-on/vagrant/.gitignore @@ -0,0 +1 @@ +.vagrant diff --git a/hands-on/vagrant/README.md b/hands-on/vagrant/README.md new file mode 100644 index 0000000..ff88ab2 --- /dev/null +++ b/hands-on/vagrant/README.md @@ -0,0 +1,12 @@ +# Vagrant + +We've created the automation to initialize cluster in your local machine using Vagrant, Ansible and kubeadm. You just need execute the following command and make sure Vagrant, Ansible are installed on your system. + +``` +$ vagrant up +# Once cluster done, you can ssh into each node +$ vagrant ssh master # or worker1 or worker2 + +# List all pods in the cluster +vagrant@master:~$ kubectl get po -A +``` \ No newline at end of file diff --git a/hands-on/vagrant/Vagrantfile b/hands-on/vagrant/Vagrantfile new file mode 100644 index 0000000..dcb4288 --- /dev/null +++ b/hands-on/vagrant/Vagrantfile @@ -0,0 +1,37 @@ +base = "ubuntu/bionic64" + +Vagrant.configure("2") do |config| + config.ssh.insert_key = false + config.vm.provider "virtualbox" do |v| + v.memory = 2048 + v.cpus = 2 + end + + config.vm.define "master" do |master| + master.vm.box = base + master.vm.network "private_network", ip: "10.18.0.11" + master.vm.hostname = "cluster-master" + master.vm.provision "ansible" do |ansible| + ansible.playbook = "main.yml" + ansible.extra_vars = { + node: "master", + node_ip: "10.18.0.11", + } + end + end + + (1..2).each do |i| + config.vm.define "worker#{i}" do |worker| + worker.vm.box = base + worker.vm.network "private_network", ip: "10.18.0.#{i + 11}" + worker.vm.hostname = "cluster-worker#{i}" + worker.vm.provision "ansible" do |ansible| + ansible.playbook = "main.yml" + ansible.extra_vars = { + node: "worker", + node_ip: "10.18.0.#{i + 11}", + } + end + end + end +end diff --git a/hands-on/vagrant/ansible.cfg b/hands-on/vagrant/ansible.cfg new file mode 100644 index 0000000..2959b1f --- /dev/null +++ b/hands-on/vagrant/ansible.cfg @@ -0,0 +1,4 @@ +[defaults] +inventory = hosts +host_key_checking = False +callback_whitelist = timer, profile_tasks \ No newline at end of file diff --git a/hands-on/vagrant/hosts b/hands-on/vagrant/hosts new file mode 100644 index 0000000..d4afd3d --- /dev/null +++ b/hands-on/vagrant/hosts @@ -0,0 +1,2 @@ +[local] +localhost ansible_connection=local \ No newline at end of file diff --git a/hands-on/vagrant/join b/hands-on/vagrant/join new file mode 100644 index 0000000..f702fd3 --- /dev/null +++ b/hands-on/vagrant/join @@ -0,0 +1 @@ +kubeadm join cluster-master:6443 --token wxnidi.3sheyw4kjtrw2pbi --discovery-token-ca-cert-hash sha256:c80e54d13e8f1b1f4a277a22df60cb3693f362b3ab9649df6699751decd80072 \ No newline at end of file diff --git a/hands-on/vagrant/main.yml b/hands-on/vagrant/main.yml new file mode 100644 index 0000000..a7378bc --- /dev/null +++ b/hands-on/vagrant/main.yml @@ -0,0 +1,13 @@ +--- +- name: Installation and Configuration cluster + hosts: all + become: yes + gather_facts: True + + vars_files: + - vars/main.yml + + roles: + - general + - { role: master, when: node == "master"} + - { role: worker, when: node == "worker"} \ No newline at end of file diff --git a/hands-on/vagrant/roles/general/tasks/01-install.yml b/hands-on/vagrant/roles/general/tasks/01-install.yml new file mode 100644 index 0000000..5ac9fb1 --- /dev/null +++ b/hands-on/vagrant/roles/general/tasks/01-install.yml @@ -0,0 +1,83 @@ +- name: Update and upgrade apt packages + apt: + upgrade: dist + update_cache: yes + +- name: Install required package + apt: + name: + - apt-transport-https + - ca-certificates + - curl + - gnupg-agent + - software-properties-common + state: present + update_cache: yes + +- name: Copy bridge configuration + template: + src: templates/kubernetes.conf.j2 + dest: "/etc/sysctl.d/kubernetes.conf" + +- name: Update sysctl configuration + shell: sysctl --system + +- name: Add an apt signing key for Docker + apt_key: + url: https://download.docker.com/linux/ubuntu/gpg + state: present + +- name: Add apt repository for stable version + apt_repository: + repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable + state: present + +- name: Install docker and its dependecies + apt: + name: docker-ce + state: present + update_cache: yes + +- name: Copy docker configuration + template: + src: templates/daemon.json.j2 + dest: "/etc/docker/ddaemon.json" + +- name: Add vagrant users to docker group + user: + name: vagrant + groups: docker + append: yes + +- name: Remove swapfile + mount: + name: "{{ item }}" + fstype: swap + state: absent + with_items: + - swap + - none + +- name: Disable swap + shell: swapoff -a + +- name: Add an apt signing key for Kubernetes + apt_key: + url: https://packages.cloud.google.com/apt/doc/apt-key.gpg + state: present + +- name: Adding apt repository for Kubernetes + apt_repository: + repo: deb https://apt.kubernetes.io/ kubernetes-xenial main + state: present + filename: kubernetes.list + +- name: Install Kubernetes binaries + apt: + name: "{{ item }}={{ k8s_version }}-00" + state: present + update_cache: yes + with_items: + - kubelet + - kubeadm + - kubectl \ No newline at end of file diff --git a/hands-on/vagrant/roles/general/tasks/02-config.yml b/hands-on/vagrant/roles/general/tasks/02-config.yml new file mode 100644 index 0000000..e5b7f39 --- /dev/null +++ b/hands-on/vagrant/roles/general/tasks/02-config.yml @@ -0,0 +1,31 @@ +- name: Load overlay and br_netfilter modules + shell: | + modprobe overlay + modprobe br_netfilter + +- name: Copy bridge configuration + template: + src: templates/kubernetes.conf.j2 + dest: "/etc/sysctl.d/kubernetes.conf" + +- name: Update sysctl configuration + shell: sysctl --system + +- name: Copy docker configuration + template: + src: templates/daemon.json.j2 + dest: "/etc/docker/ddaemon.json" + +- name: Add DNS alias to each node + shell: | + cat >> /etc/hosts < /proc/sys/vm/drop_caches \ No newline at end of file diff --git a/hands-on/vagrant/roles/general/tasks/03-service.yml b/hands-on/vagrant/roles/general/tasks/03-service.yml new file mode 100644 index 0000000..83712dc --- /dev/null +++ b/hands-on/vagrant/roles/general/tasks/03-service.yml @@ -0,0 +1,14 @@ +--- +- name: Restart docker service + service: + name: docker + state: restarted + daemon_reload: yes + enabled: yes + +- name: Restart kubelet service + service: + name: kubelet + state: restarted + daemon_reload: yes + enabled: yes \ No newline at end of file diff --git a/hands-on/vagrant/roles/general/tasks/main.yml b/hands-on/vagrant/roles/general/tasks/main.yml new file mode 100644 index 0000000..d146ea0 --- /dev/null +++ b/hands-on/vagrant/roles/general/tasks/main.yml @@ -0,0 +1,4 @@ +--- +- include_tasks: 01-install.yml +- include_tasks: 02-config.yml +- include_tasks: 03-service.yml \ No newline at end of file diff --git a/hands-on/vagrant/roles/general/templates/daemon.json.j2 b/hands-on/vagrant/roles/general/templates/daemon.json.j2 new file mode 100644 index 0000000..5c8b70c --- /dev/null +++ b/hands-on/vagrant/roles/general/templates/daemon.json.j2 @@ -0,0 +1,5 @@ +{ + "exec-opts": ["native.cgroupdriver=systemd"], + "log-driver": "json-file", + "storage-driver": "overlay2" +} \ No newline at end of file diff --git a/hands-on/vagrant/roles/general/templates/kubernetes.conf.j2 b/hands-on/vagrant/roles/general/templates/kubernetes.conf.j2 new file mode 100644 index 0000000..703438d --- /dev/null +++ b/hands-on/vagrant/roles/general/templates/kubernetes.conf.j2 @@ -0,0 +1,3 @@ +net.ipv4.ip_forward = 1 +net.bridge.bridge-nf-call-ip6tables = 1 +net.bridge.bridge-nf-call-iptables = 1 \ No newline at end of file diff --git a/hands-on/vagrant/roles/master/tasks/main.yml b/hands-on/vagrant/roles/master/tasks/main.yml new file mode 100644 index 0000000..bea7827 --- /dev/null +++ b/hands-on/vagrant/roles/master/tasks/main.yml @@ -0,0 +1,21 @@ +--- +- name: Initialize the cluster using kubeadm + shell: kubeadm init --control-plane-endpoint="cluster-master:6443" --node-name {{ ansible_hostname }} --pod-network-cidr=192.168.0.0/16 + +- name: Setup cluster config on master node + shell: | + mkdir -p /home/vagrant/.kube + cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config + chown vagrant:vagrant /home/vagrant/.kube/config + +- name: Generate join command + command: kubeadm token create --print-join-command + register: join_command + +- name: Copy join command + local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join" + become: false + +- name: Deploy flannel to cluster + shell: kubectl --kubeconfig /home/vagrant/.kube/config create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml + when: network == "flannel" \ No newline at end of file diff --git a/hands-on/vagrant/roles/worker/tasks/main.yml b/hands-on/vagrant/roles/worker/tasks/main.yml new file mode 100644 index 0000000..9164cf8 --- /dev/null +++ b/hands-on/vagrant/roles/worker/tasks/main.yml @@ -0,0 +1,8 @@ +- name: Copy join command to node + copy: + src: join + dest: /tmp/join + mode: 0777 + +- name: Join the node to cluster + shell: sh /tmp/join \ No newline at end of file diff --git a/hands-on/vagrant/vars/main.yml b/hands-on/vagrant/vars/main.yml new file mode 100644 index 0000000..3f26474 --- /dev/null +++ b/hands-on/vagrant/vars/main.yml @@ -0,0 +1,3 @@ +--- +k8s_version: "1.19.1" +network: "flannel" \ No newline at end of file