-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk8scluster.yaml
220 lines (186 loc) · 7.23 KB
/
k8scluster.yaml
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
- hosts: localhost
gather_facts: yes
tasks:
- name: replace line
lineinfile:
dest: /etc/ansible/ansible.cfg
line: 'command_warnings = False'
- name: Add an Kubernetes apt signing key for Ubuntu
when: "ansible_distribution == 'Ubuntu'"
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: Adding Kubernetes apt repository for Ubuntu
when: "ansible_distribution == 'Ubuntu'"
apt_repository:
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
state: present
filename: kubernetes
- name: install kubernetes components for Ubuntu
when: "ansible_distribution == 'Ubuntu'"
apt:
name: ['apt-transport-https', 'curl', 'ca-certificates', 'gnupg-agent' ,'software-properties-common', 'kubelet=1.15.3-00', 'kubeadm=1.15.3-00', 'kubectl=1.15.3-00', 'git']
state: present
- name: Creating a Kubernetes repository file for RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS']"
file:
path: /etc/yum.repos.d/kubernetes.repo
state: touch
- name: Adding repository details in Kubernetes repo file for RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS']"
blockinfile:
path: /etc/yum.repos.d/kubernetes.repo
block: |
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
- name: Installing required packages for RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS']"
yum:
name: ['bind-utils', 'yum-utils', 'device-mapper-persistent-data', 'lvm2', 'telnet', 'kubelet-1.15.5', 'kubeadm-1.15.5', 'kubectl-1.15.5', 'firewalld', 'curl']
state: present
- name: Validate whether Kubernetes cluster installed
shell: kubectl cluster-info
register: k8sup
ignore_errors: yes
- name: Add Docker GPG key for Ubuntu
when: "ansible_distribution == 'Ubuntu' and 'running' not in k8sup.stdout"
apt_key: url=https://download.docker.com/linux/ubuntu/gpg
- name: Add Docker APT repository for Ubuntu
when: "ansible_distribution == 'Ubuntu' and 'running' not in k8sup.stdout"
apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/{{ansible_distribution|lower}} {{ansible_distribution_release}} stable
- name: Install Docker-CE Engine on Ubuntu
when: " ansible_distribution == 'Ubuntu' and 'running' not in k8sup.stdout"
become: root
apt:
name: [ 'docker-ce=5:19.03.1~3-0~ubuntu-bionic' ]
update_cache: yes
- name: "Configuring Docker-CE repo for RHEL/CentOS"
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
get_url:
url: https://download.docker.com/linux/centos/docker-ce.repo
dest: /etc/yum.repos.d/docker-ce.repo
mode: 0644
- name: Install Docker-CE Engine on RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
args:
warn: false
shell: yum install docker -y
- name: SetEnforce for RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
ignore_errors: yes
command: "setenforce 0"
- name: SELinux for RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
args:
warn: false
command: sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
- name: Enable Firewall Service for RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
service:
name: firewalld
state: started
enabled: yes
ignore_errors: yes
- name: Allow Network Ports in Firewalld for RHEL/CentOS
when: "ansible_distribution in ['RedHat', 'CentOS'] and 'running' not in k8sup.stdout"
firewalld:
port: "{{ item }}"
state: enabled
permanent: yes
immediate: yes
with_items:
- "6443/tcp"
- "10250/tcp"
- name: Remove swapfile from /etc/fstab
when: "'running' not in k8sup.stdout"
mount:
name: "{{ item }}"
fstype: swap
state: absent
with_items:
- swap
- none
- name: Disable swap
when: "'running' not in k8sup.stdout"
command: swapoff -a
- name: Starting and enabling the required services
when: "'running' not in k8sup.stdout"
service:
name: "{{ item }}"
state: started
enabled: yes
ignore_errors: yes
with_items:
- docker
- kubelet
- name: Reset Kubernetes component
when: "'running' not in k8sup.stdout"
shell: "kubeadm reset --force"
- name: Remove etcd directory
when: "'running' not in k8sup.stdout"
file:
path: "/var/lib/etcd"
state: absent
- name: Initialize the Kubernetes cluster using kubeadm
when: "'running' not in k8sup.stdout"
command: kubeadm init --pod-network-cidr=192.168.0.0/16
- name: Create kube directory
when: "'running' not in k8sup.stdout"
file:
path: $HOME/.kube
state: directory
- name: Copy kubeconfig to home
when: "'running' not in k8sup.stdout"
copy:
src: /etc/kubernetes/admin.conf
dest: $HOME/.kube/config
mode: '0644'
- name: Install networking plugin to kubernetes cluster
when: "'running' not in k8sup.stdout"
command: kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml
- name: Taint the Kubernetes Control Plane node
when: "'running' not in k8sup.stdout"
command: kubectl taint nodes --all node-role.kubernetes.io/master-
- name: "Check if Helm is installed"
shell: command -v helm >/dev/null 2>&1
register: helm_exists
ignore_errors: yes
- name: "Install Helm"
command: "{{ item }}"
args:
warn: false
with_items:
- curl -O https://get.helm.sh/helm-v2.14.3-linux-amd64.tar.gz
- tar -xvzf helm-v2.14.3-linux-amd64.tar.gz
- cp linux-amd64/helm /usr/local/bin/
- cp linux-amd64/helm /usr/bin/
- rm -rf helm-v2.14.3-linux-amd64.tar.gz linux-amd64
when: helm_exists.rc > 0
- name: "Tiller RBAC configuration"
when: "'running' not in k8sup.stdout"
command: "{{ item }}"
args:
warn: false
with_items:
- kubectl create serviceaccount -n kube-system tiller
- kubectl create clusterrolebinding tiller-cluster-admin --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
- name: "Initialize Helm"
when: "'running' not in k8sup.stdout"
shell: "{{ item }}"
with_items:
- helm init --service-account tiller
- sleep 5
- kubectl --namespace kube-system patch deploy tiller-deploy -p '{"spec":{"template":{"spec":{"serviceaccount":"tiller"}}}}'
- name: "Install Kubernetes Dashboard"
when: "'running' not in k8sup.stdout"
shell: "{{ item }}"
with_items:
- git clone https://github.com/angudadevops/k8s_addons.git
- cd k8s_addons/dashboard && bash dashboard.sh