Skip to content

Commit

Permalink
Add ansible configuration for installing kubernetes on master and wor…
Browse files Browse the repository at this point in the history
…ker VMS
  • Loading branch information
matevskial committed Mar 21, 2021
0 parents commit d277cac
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ansible/tmp
6 changes: 6 additions & 0 deletions ansible/ansible.cfg
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
73 changes: 73 additions & 0 deletions ansible/install_kubernetes.yaml
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

5 changes: 5 additions & 0 deletions ansible/inventory/servers
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
75 changes: 75 additions & 0 deletions ansible/setup_kubernetes_cluster.yaml
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
23 changes: 23 additions & 0 deletions ansible/setup_kubernetes_workers.yaml
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 }}"

0 comments on commit d277cac

Please sign in to comment.