-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathkube.yml
executable file
·180 lines (154 loc) · 4.46 KB
/
kube.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
---
- hosts: all
become: true
vars:
node_port: '192.168.56.80'
user: 'vagrant'
tasks:
- name: Add IP address of all hosts to all hosts
lineinfile:
dest: /etc/hosts
regexp: '.*{{ item }}$'
line: "{{ hostvars[item].ansible_host }} {{item}}"
state: present
when: hostvars[item].ansible_host is defined
with_items: "{{ groups.all }}"
- name: Install packages that allow apt to be used over HTTPS
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
- 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 xenial stable
state: present
- name: Install docker and its dependecies
apt:
name: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- docker-ce
- docker-ce-cli
- containerd.io
notify:
- docker status
- name: "Add {{ user }} user to docker group"
user:
name: "{{ user }}"
group: docker
- name: Remove swapfile from /etc/fstab
mount:
name: "{{ item }}"
fstype: swap
state: absent
with_items:
- swap
- none
- name: Disable swap
command: swapoff -a
when: ansible_swaptotal_mb > 0
- 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: "{{ packages }}"
state: present
update_cache: yes
vars:
packages:
- kubelet
- kubeadm
- kubectl
- name: Configure node ip
copy:
dest: /etc/default/kubelet
content: "KUBELET_EXTRA_ARGS=--node-ip={{ node_port }}"
- name: Restart kubelet
service:
name: kubelet
daemon_reload: yes
state: restarted
- name: Initialize the Kubernetes cluster using kubeadm
command: 'kubeadm init --apiserver-advertise-address="{{ node_port }}" --pod-network-cidr=10.244.0.0/16 --ignore-preflight-errors=all'
when: master
- name: sleeping for 2 mins
wait_for: timeout=120
when: master
- name: "Setup kubeconfig for {{ user }} user"
command: "{{ item }}"
with_items:
- "mkdir -p /home/{{ user }}/.kube"
- "cp -i /etc/kubernetes/admin.conf /home/{{ user }}/.kube/config"
- "chown {{ user }}:{{ user }} /home/{{ user }}/.kube/config"
when: master
- name: Install flannel networking
become: false
command: kubectl create -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
when: master
- name: sleep for 2 Mins for flannel networking
wait_for: timeout=120
when: master
- name: copy the yml for Dashboard
copy:
src: "files/{{ item }}"
dest: "/home/{{ user }}/{{ item }}"
with_items:
- rb.yml
- recommended.yaml
when: master
- name: Deploy the Dashbord
become: false
shell: "kubectl apply -f /home/{{ user }}/recommended.yaml"
when: master
- name: Creat service account and rbac
become: false
shell: "kubectl apply -f /home/{{ user }}/rb.yml"
when: master
- name: Generate join command
command: kubeadm token create --print-join-command
register: join_command
when: master
- name: Copy join command to local file
copy:
content: "{{ join_command.stdout_lines[0] }}"
dest: "/home/{{ user }}/join-command.sh"
when: master
- name: Fetch the join script to ansible master
run_once: yes
fetch:
src: "/home/{{ user }}/join-command.sh"
dest: /tmp/join-command.sh
flat: yes
when: master
- name: Copy the file from master to worker
copy:
src: /tmp/join-command.sh
dest: "/home/{{ user }}/join-command.sh"
mode: "0755"
when: master == "false"
- name: Join the worker to master node
shell: "sh /home/{{ user }}/join-command.sh"
when: master == "false"
handlers:
- name: docker status
service: name=docker state=started