Skip to content

Commit

Permalink
Add cluster setup with Vagrant
Browse files Browse the repository at this point in the history
  • Loading branch information
Muhammad Yuga committed Dec 31, 2020
1 parent b3c8430 commit 74ae1a4
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions hands-on/vagrant/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vagrant
12 changes: 12 additions & 0 deletions hands-on/vagrant/README.md
Original file line number Diff line number Diff line change
@@ -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
```
37 changes: 37 additions & 0 deletions hands-on/vagrant/Vagrantfile
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions hands-on/vagrant/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[defaults]
inventory = hosts
host_key_checking = False
callback_whitelist = timer, profile_tasks
2 changes: 2 additions & 0 deletions hands-on/vagrant/hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[local]
localhost ansible_connection=local
1 change: 1 addition & 0 deletions hands-on/vagrant/join
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
kubeadm join cluster-master:6443 --token wxnidi.3sheyw4kjtrw2pbi --discovery-token-ca-cert-hash sha256:c80e54d13e8f1b1f4a277a22df60cb3693f362b3ab9649df6699751decd80072
13 changes: 13 additions & 0 deletions hands-on/vagrant/main.yml
Original file line number Diff line number Diff line change
@@ -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"}
83 changes: 83 additions & 0 deletions hands-on/vagrant/roles/general/tasks/01-install.yml
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions hands-on/vagrant/roles/general/tasks/02-config.yml
Original file line number Diff line number Diff line change
@@ -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 <<EOF
10.18.0.11 cluster-master
10.18.0.12 cluster-worker1
10.18.0.13 cluster-worker2
EOF
- name: Fix kubelet IP
shell: echo 'Environment="KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}"' | tee -a /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

- name: Clear memory cache
shell: sync; echo 3 > /proc/sys/vm/drop_caches
14 changes: 14 additions & 0 deletions hands-on/vagrant/roles/general/tasks/03-service.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions hands-on/vagrant/roles/general/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
- include_tasks: 01-install.yml
- include_tasks: 02-config.yml
- include_tasks: 03-service.yml
5 changes: 5 additions & 0 deletions hands-on/vagrant/roles/general/templates/daemon.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exec-opts": ["native.cgroupdriver=systemd"],
"log-driver": "json-file",
"storage-driver": "overlay2"
}
3 changes: 3 additions & 0 deletions hands-on/vagrant/roles/general/templates/kubernetes.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
21 changes: 21 additions & 0 deletions hands-on/vagrant/roles/master/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 8 additions & 0 deletions hands-on/vagrant/roles/worker/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions hands-on/vagrant/vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
k8s_version: "1.19.1"
network: "flannel"

0 comments on commit 74ae1a4

Please sign in to comment.