-
Notifications
You must be signed in to change notification settings - Fork 0
/
playbook.yml
130 lines (100 loc) · 4.12 KB
/
playbook.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
- name: Basic Setup
hosts: all
gather_facts: false
become: true
tasks:
- name: Check connectivity
ansible.builtin.ping:
- name: Set a hostname
ansible.builtin.hostname:
name: "{{ node_hostname }}"
- name: Copy /etc/hosts template
ansible.builtin.copy:
backup: true
src: ./files/hosts
dest: /tmp/hosts
- name: Insert/Update configuration using a local file and validate it
ansible.builtin.blockinfile:
block: "{{ lookup('file', './files/hosts') }}"
path: /etc/hosts
backup: yes
- name: Disable swap on all the nodes
ansible.builtin.shell: swapoff -a
- name: add br_netfilter module
community.general.modprobe:
name: br_netfilter
state: present
- name: sysctl params required by setup
ansible.builtin.shell: |
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
- name: apply sysctl params without reboot
ansible.builtin.shell: sysctl --system
- name: Setting up the repository
ansible.builtin.shell: |
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
sudo yum install -y containerd.io
- name: Install containerd package
ansible.builtin.shell: sudo yum install -y containerd.io
- name: create containerd config file
ansible.builtin.shell: |
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml
- name: Update containerd configuration to use SystemdCgroup
ansible.builtin.shell: |
sed -i -e 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml
sed -i -e 's|sandbox_image = "registry.k8s.io/pause:3.8"|sandbox_image = "registry.k8s.io/pause:3.9"|g' /etc/containerd/config.toml
- name: Enable containerd, and issue "systemctl daemon-reload" to pick up on configuration changes
ansible.builtin.systemd:
name: containerd
daemon_reload: yes
state: started
enabled: yes
# Kubeadm installation and setup
- name: Add kubernetes repo
ansible.builtin.shell: |
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/v1.31/rpm/repodata/repomd.xml.key
EOF
- name: Install kubelet, kubeadm and kubectl
ansible.builtin.shell: yum install -y kubelet kubeadm kubectl yum-plugin-versionlock
- name: hold kubelet, kubeadm and kubectl
ansible.builtin.shell: yum versionlock add kubelet kubeadm kubectl
- name: Setting up master node (control-plane)
hosts: master
become: true
tasks:
- name: init kubeadm
ansible.builtin.shell: kubeadm init --pod-network-cidr=10.0.0.0/16 --control-plane-endpoint "{{ansible_host}}:6443"
- name: create ~/.kube directory
ansible.builtin.file:
path: ~/.kube
state: directory
mode: "0755"
- name: copy kubeconfig file
ansible.builtin.shell: sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
- name: set the correct permission on kubeconfig file
ansible.builtin.shell: sudo chown $(id -u):$(id -g) $HOME/.kube/config
- name: install flannel
ansible.builtin.shell: kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
- name: Generate join token for the worker node
ansible.builtin.shell: sudo kubeadm token create --print-join-command
register: join_node_token_command
- name: Save join command as variable
ansible.builtin.set_fact:
join_node: "{{ join_node_token_command.stdout_lines[0] }}"
- name: Setting up Worker node (data-plane)
hosts: workers
become: true
tasks:
- name: add worker nodes to cluster
shell: "sudo {{ hostvars['control-plane'].join_node }}"