2_prepare.yml
09-29-2019
Ansible playbooks for building Kubernetes cluster.
Make sure Docker is install and configured.
Add your Master and Worker nodes to the hosts file:
###################################
[master]
master ansible_host=10.1.140.11
[workers]
worker1 ansible_host=10.1.140.12
worker2 ansible_host=10.1.140.13
###################################
Run each playbook in order:
1_depend.yml
2_prepare.yml
3_join.yml
Specify the username for your servers.
Example command:
sudo ansible-playbook /etc/ansible/kube_cluster/1_depend.yml --user MyUser --ask-pass --ask-become-pass
Language or Platform: Python
Code:
# Prepare Kubernetes
- hosts: master
vars:
# use home_usr to specify your ssh username for the master server
home_usr: MYUSERNAME
become: yes
tasks:
- name: Disable Swap for Kubernetes | Also make sure you have 2 or more CPU
become: true
shell: |
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
args:
executable: /bin/bash
- name: Enable Docker service
become: true
shell: systemctl enable docker.service
args:
executable: /bin/bash
- name: initialize the cluster
become: true
shell: kubeadm init --pod-network-cidr=192.168.0.0/16 >> cluster_initialized.txt
args:
chdir: /home/{{ home_usr }}
creates: cluster_initialized.txt
- name: create .kube directory
file:
path: /home/{{ home_usr }}/.kube
state: directory
mode: 0755
- name: copy admin.conf to user's kube config
become: true
copy:
src: /etc/kubernetes/admin.conf
dest: /home/{{ home_usr }}/.kube/config
remote_src: yes
owner: "{{ home_usr }}"
- name: Set ownership recursively
become: true
file:
path: /home/{{ home_usr }}/.kube
state: directory
recurse: yes
owner: "{{ home_usr }}"
group: "{{ home_usr }}"
- name: install Pod network (Calico)
become_user: "{{ home_usr }}"
shell: |
kubectl apply -f https://docs.projectcalico.org/v3.9/manifests/calico.yaml
kubectl taint nodes --all node-role.kubernetes.io/master-
args:
chdir: /home/{{ home_usr }}
executable: /bin/bash
- hosts: workers
become: yes
tasks:
- name: Disable Swap for Kubernetes | Also make sure you have 2 or more CPU
become: true
shell: |
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
args:
executable: /bin/bash
- name: Enable Docker service
become: true
shell: systemctl enable docker.service
args:
executable: /bin/bash
Back