Skip to content

Commit 9b7879d

Browse files
committed
K8s setup with Vagrant and Ansible.
1 parent 35db9d6 commit 9b7879d

File tree

5 files changed

+298
-1
lines changed

5 files changed

+298
-1
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
/.idea/
22
/**/*.class
3-
/**/target/*
3+
/**/target/*
4+
/**/*.iml
5+
/kubernetes-basics/kubernetes-setup/.vagrant
6+
/kubernetes-basics/kubernetes-setup/kubeconf
7+
/**/join-command
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Kubernetes Setup
2+
3+
4+
#### Prerequisites
5+
- Install link:../ansible-basics/README.asciidoc[Ansible].
6+
- Install VirtualBox and Vagrant.
7+
- Install https://github.com/ahmetb/kubectx[kubectx] to help with switching context.
8+
- Install https://github.com/derailed/k9s[k9s], UI to your cluster.
9+
10+
11+
#### Prepare K8s cluster with Vagrant and Ansible
12+
Inspired by https://kubernetes.io/blog/2019/03/15/kubernetes-setup-using-ansible-and-vagrant/
13+
14+
15+
#### Configure kubectl in your machine
16+
```
17+
# Extract kubecofig from master node
18+
vagrant ssh k8s-master -c "sudo cat /etc/kubernetes/admin.conf" > kubeconf
19+
20+
# Add kubeconf path to $PATH
21+
# Note: KUBECONFIG only exposed to current terminal, to make it permanent add it to ~/.bashrc or /~.zshrc.
22+
export KUBECONFIG=kubeconf:$HOME/.kube/config
23+
```
24+
25+
26+
#### Verify
27+
```
28+
$ kubectl get nodes
29+
30+
NAME STATUS ROLES AGE VERSION
31+
k8s-master Ready master 60m v1.18.2
32+
node-1 Ready <none> 57m v1.18.2
33+
node-2 Ready <none> 54m v1.18.2
34+
```
35+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
IMAGE_NAME = "bento/ubuntu-16.04"
2+
N = 2
3+
4+
Vagrant.configure("2") do |config|
5+
config.ssh.insert_key = false
6+
7+
config.vm.provider "virtualbox" do |v|
8+
v.memory = 1024
9+
v.cpus = 2
10+
end
11+
12+
config.vm.define "k8s-master" do |master|
13+
master.vm.box = IMAGE_NAME
14+
master.vm.network "private_network", ip: "192.168.50.10"
15+
master.vm.hostname = "k8s-master"
16+
master.vm.provision "ansible" do |ansible|
17+
ansible.playbook = "master-playbook.yml"
18+
ansible.extra_vars = {
19+
node_ip: "192.168.50.10",
20+
}
21+
end
22+
end
23+
24+
(1..N).each do |i|
25+
config.vm.define "node-#{i}" do |node|
26+
node.vm.box = IMAGE_NAME
27+
node.vm.network "private_network", ip: "192.168.50.#{i + 10}"
28+
node.vm.hostname = "node-#{i}"
29+
node.vm.provision "ansible" do |ansible|
30+
ansible.playbook = "node-playbook.yml"
31+
ansible.extra_vars = {
32+
node_ip: "192.168.50.#{i + 10}",
33+
}
34+
end
35+
end
36+
end
37+
38+
end
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
- hosts: all
3+
become: true
4+
tasks:
5+
- name: Install packages that allow apt to be used over HTTPS
6+
apt:
7+
name: "{{ packages }}"
8+
state: present
9+
update_cache: yes
10+
vars:
11+
packages:
12+
- apt-transport-https
13+
- ca-certificates
14+
- curl
15+
- gnupg-agent
16+
- software-properties-common
17+
18+
- name: Add an apt signing key for Docker
19+
apt_key:
20+
url: https://download.docker.com/linux/ubuntu/gpg
21+
state: present
22+
23+
- name: Add apt repository for stable version
24+
apt_repository:
25+
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
26+
state: present
27+
28+
- name: Install docker and its dependecies
29+
apt:
30+
name: "{{ packages }}"
31+
state: present
32+
update_cache: yes
33+
vars:
34+
packages:
35+
- docker-ce
36+
- docker-ce-cli
37+
- containerd.io
38+
notify:
39+
- docker status
40+
41+
- name: Add vagrant user to docker group
42+
user:
43+
name: vagrant
44+
group: docker
45+
46+
- name: Remove swapfile from /etc/fstab
47+
mount:
48+
name: "{{ item }}"
49+
fstype: swap
50+
state: absent
51+
with_items:
52+
- swap
53+
- none
54+
55+
- name: Disable swap
56+
command: swapoff -a
57+
when: ansible_swaptotal_mb > 0
58+
59+
- name: Add an apt signing key for Kubernetes
60+
apt_key:
61+
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
62+
state: present
63+
64+
- name: Adding apt repository for Kubernetes
65+
apt_repository:
66+
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
67+
state: present
68+
filename: kubernetes.list
69+
70+
- name: Install Kubernetes binaries
71+
apt:
72+
name: "{{ packages }}"
73+
state: present
74+
update_cache: yes
75+
vars:
76+
packages:
77+
- kubelet
78+
- kubeadm
79+
- kubectl
80+
81+
- name: Configure node ip
82+
lineinfile:
83+
path: /etc/default/kubelet
84+
line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
85+
create: yes
86+
87+
- name: Restart kubelet
88+
service:
89+
name: kubelet
90+
daemon_reload: yes
91+
state: restarted
92+
93+
94+
- name: Initialize the Kubernetes cluster using kubeadm
95+
command: kubeadm init --apiserver-advertise-address=192.168.50.10 --apiserver-cert-extra-sans=192.168.50.10 --node-name k8s-master --pod-network-cidr=192.168.0.0/16
96+
97+
- name: Setup kubeconfig for vagrant user
98+
command: "{{ item }}"
99+
with_items:
100+
- mkdir -p /home/vagrant/.kube
101+
- cp -i /etc/kubernetes/admin.conf /home/vagrant/.kube/config
102+
- chown vagrant:vagrant /home/vagrant/.kube/config
103+
104+
- name: Install calico pod network
105+
become: false
106+
command: kubectl create -f https://docs.projectcalico.org/manifests/calico.yaml
107+
108+
- name: Generate join command
109+
command: kubeadm token create --print-join-command
110+
register: join_command
111+
112+
- name: Copy join command to local file
113+
become: false
114+
local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"
115+
116+
117+
handlers:
118+
- name: docker status
119+
service: name=docker state=started
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
- hosts: all
3+
become: true
4+
tasks:
5+
- name: Install packages that allow apt to be used over HTTPS
6+
apt:
7+
name: "{{ packages }}"
8+
state: present
9+
update_cache: yes
10+
vars:
11+
packages:
12+
- apt-transport-https
13+
- ca-certificates
14+
- curl
15+
- gnupg-agent
16+
- software-properties-common
17+
18+
- name: Add an apt signing key for Docker
19+
apt_key:
20+
url: https://download.docker.com/linux/ubuntu/gpg
21+
state: present
22+
23+
- name: Add apt repository for stable version
24+
apt_repository:
25+
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
26+
state: present
27+
28+
- name: Install docker and its dependecies
29+
apt:
30+
name: "{{ packages }}"
31+
state: present
32+
update_cache: yes
33+
vars:
34+
packages:
35+
- docker-ce
36+
- docker-ce-cli
37+
- containerd.io
38+
notify:
39+
- docker status
40+
41+
- name: Add vagrant user to docker group
42+
user:
43+
name: vagrant
44+
group: docker
45+
46+
- name: Remove swapfile from /etc/fstab
47+
mount:
48+
name: "{{ item }}"
49+
fstype: swap
50+
state: absent
51+
with_items:
52+
- swap
53+
- none
54+
55+
- name: Disable swap
56+
command: swapoff -a
57+
when: ansible_swaptotal_mb > 0
58+
59+
- name: Add an apt signing key for Kubernetes
60+
apt_key:
61+
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
62+
state: present
63+
64+
- name: Adding apt repository for Kubernetes
65+
apt_repository:
66+
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
67+
state: present
68+
filename: kubernetes.list
69+
70+
- name: Install Kubernetes binaries
71+
apt:
72+
name: "{{ packages }}"
73+
state: present
74+
update_cache: yes
75+
vars:
76+
packages:
77+
- kubelet
78+
- kubeadm
79+
- kubectl
80+
81+
- name: Configure node ip
82+
lineinfile:
83+
path: /etc/default/kubelet
84+
line: KUBELET_EXTRA_ARGS=--node-ip={{ node_ip }}
85+
create: yes
86+
87+
- name: Restart kubelet
88+
service:
89+
name: kubelet
90+
daemon_reload: yes
91+
state: restarted
92+
93+
- name: Copy the join command to server location
94+
copy: src=join-command dest=/tmp/join-command.sh mode=0777
95+
96+
- name: Join the node to cluster
97+
command: sh /tmp/join-command.sh
98+
99+
handlers:
100+
- name: docker status
101+
service: name=docker state=started

0 commit comments

Comments
 (0)