-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ansible configuration for installing kubernetes on master and wor…
…ker VMS
- Loading branch information
0 parents
commit d277cac
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ansible/tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[defaults] | ||
# disable host_key_checking | ||
# https://docs.ansible.com/ansible/latest/user_guide/connection_details.html#host-key-checking | ||
host_key_checking = False | ||
|
||
inventory=inventory |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
- name: Install kubernetes on master and workers | ||
hosts: [masters, workers] | ||
remote_user: kubenode | ||
become: yes | ||
become_method: sudo | ||
become_user: root | ||
gather_facts: yes | ||
connection: ssh | ||
|
||
tasks: | ||
|
||
- name: Disable swap | ||
command: swapoff -a | ||
|
||
- name: Remove swap entry in fstab | ||
lineinfile: | ||
dest: /etc/fstab | ||
regexp: swap | ||
state: absent | ||
|
||
- name: Install prerequisites | ||
apt: | ||
name: | ||
- apt-transport-https | ||
- ca-certificates | ||
- curl | ||
- software-properties-common | ||
state: present | ||
|
||
- name: Install docker | ||
apt: | ||
name: | ||
- docker.io | ||
state: present | ||
|
||
- name: Enable Docker service | ||
service: | ||
name: docker | ||
enabled: yes | ||
|
||
- name: Add Google official GPG key | ||
apt_key: | ||
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg | ||
state: present | ||
|
||
- name: Add kubernetes repository | ||
apt_repository: | ||
repo: deb https://apt.kubernetes.io/ kubernetes-xenial main | ||
state: present | ||
filename: kubernetes | ||
mode: 0600 | ||
|
||
- name: Install Kubernetes | ||
apt: | ||
name: | ||
- kubelet | ||
- kubeadm | ||
- kubectl | ||
state: present | ||
|
||
- name: Enable Kubernetes service | ||
service: | ||
name: kubelet | ||
enabled: yes | ||
|
||
- name: Reboot | ||
reboot: | ||
post_reboot_delay: 10 | ||
reboot_timeout: 60 | ||
connect_timeout: 60 | ||
test_command: uptime | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[masters] | ||
master ansible_host=192.168.0.13 ansible_user=kubenode ansible_ssh_pass=12345678 ansible_port=22 | ||
|
||
[workers] | ||
worker ansible_host=192.168.0.14 ansible_user=kubenode ansible_ssh_pass=12345678 ansible_port=22 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
--- | ||
- name: Initialize and setup Kubernetes cluster | ||
hosts: masters | ||
remote_user: kubenode | ||
become: yes | ||
become_method: sudo | ||
become_user: root | ||
gather_facts: yes | ||
connection: ssh | ||
|
||
tasks: | ||
- name: Initialize Kubernetes cluster | ||
command: kubeadm init --pod-network-cidr=192.168.1.0/24 --apiserver-advertise-address=192.168.0.13 | ||
run_once: true | ||
|
||
- pause: seconds=30 | ||
|
||
- name: Create directory for kube config | ||
become_user: kubenode | ||
become_method: sudo | ||
become: yes | ||
file: | ||
path: /home/kubenode/.kube | ||
state: directory | ||
owner: "kubenode" | ||
group: "kubenode" | ||
mode: 0755 | ||
|
||
- name: Copy admin.conf to .kube/config | ||
become_user: root | ||
become_method: sudo | ||
become: yes | ||
copy: | ||
src: /etc/kubernetes/admin.conf | ||
dest: /home/kubenode/.kube/config | ||
remote_src: yes | ||
owner: "kubenode" | ||
group: "kubenode" | ||
mode: 0644 | ||
|
||
- name: Remove the cache directory | ||
become_user: kubenode | ||
become_method: sudo | ||
become: yes | ||
file: | ||
path: /home/kubenode/.kube/cache | ||
state: absent | ||
|
||
- name: Create Pod Network | ||
become_user: kubenode | ||
become_method: sudo | ||
become: yes | ||
command: "{{ item }}" | ||
with_items: | ||
- kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml | ||
|
||
- name: Get the token for joining nodes to the cluster | ||
shell: kubeadm token create --print-join-command | ||
register: kubernetes_join_command | ||
|
||
- debug: | ||
msg: "{{ kubernetes_join_command.stdout }}" | ||
|
||
- name: Create tmp folder to store temporary files | ||
delegate_to: localhost | ||
file: | ||
path: tmp | ||
state: directory | ||
|
||
- name: Copy join command to local file. | ||
delegate_to: localhost | ||
copy: | ||
content: "{{ kubernetes_join_command.stdout_lines[0] }}" | ||
dest: tmp/kubernetes_join_command | ||
mode: 0777 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
--- | ||
- name: Join workers to Kubernetes cluster | ||
hosts: workers | ||
remote_user: kubenode | ||
become: yes | ||
become_method: sudo | ||
become_user: root | ||
gather_facts: yes | ||
connection: ssh | ||
|
||
tasks: | ||
- name: Copy join command to worker nodes | ||
copy: | ||
src: tmp/kubernetes_join_command | ||
dest: /tmp/kubernetes_join_command | ||
mode: 0777 | ||
|
||
- name: Join the worker nodes to the cluster | ||
command: sh /tmp/kubernetes_join_command | ||
register: joined_or_not | ||
|
||
- debug: | ||
msg: "{{ joined_or_not.stdout }}" |