|
| 1 | +--- |
| 2 | +- name: Set up Kubernetes |
| 3 | + hosts: all |
| 4 | + become: true |
| 5 | + tasks: |
| 6 | + |
| 7 | +# Disable swap on all the Nodes |
| 8 | + |
| 9 | + - name: 1. Disable Swap for kubeadm to work properly |
| 10 | + ansible.builtin.shell: |
| 11 | + cmd: swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab |
| 12 | + |
| 13 | +#Enable IpTables Bridge traffic on all the Nodes |
| 14 | + |
| 15 | + - name: 2. Added overlay & br_netfilter to /etc/modules-load.d/k8s.conf # this file ensures these modules are loaded automatically every time the system boots |
| 16 | + ansible.builtin.copy: |
| 17 | + dest: /etc/modules-load.d/k8s.conf |
| 18 | + content: | |
| 19 | + overlay |
| 20 | + br_netfilter |
| 21 | + owner: root |
| 22 | + group: root |
| 23 | + mode: '0644' |
| 24 | + |
| 25 | + - name: 3. Used modprobe for immediantly loading overlay and br_netfilter in running linux kernel # loading kernel modules |
| 26 | + ansible.builtin.shell: |
| 27 | + cmd: sudo modprobe overlay && sudo modprobe br_netfilter |
| 28 | + |
| 29 | + - name: 4. Set iptables setting |
| 30 | + ansible.builtin.shell: |
| 31 | + cmd: cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf |
| 32 | + net.bridge.bridge-nf-call-iptables = 1 |
| 33 | + net.bridge.bridge-nf-call-ip6tables = 1 |
| 34 | + net.ipv4.ip_forward = 1 |
| 35 | + EOF |
| 36 | + # net.bridge.bridge-nf-call-iptables = ensures that traffic passing through a linux bridge is processed by iptables rules. This is essantial for kubernetes policies |
| 37 | + # net.bridge.bridge-nf-call-ip6tables = Similar to the previoes setting but for ip6 traffic |
| 38 | + # net.ipv4.ip_forward = allows the system to forward ipv4 packages between network interfaces. |
| 39 | + |
| 40 | + - name: 5. Apply sysctl params without reboot |
| 41 | + ansible.builtin.shell: |
| 42 | + cmd: sysctl --system |
| 43 | + |
| 44 | +# Install CRI-O Runtime On All The Nodes |
| 45 | + |
| 46 | + |
| 47 | + - name: 8.2 Download software-properties-common, gpg, curl, apt-transport-https, ca-certificates |
| 48 | + ansible.builtin.shell: |
| 49 | + cmd: sudo apt-get update -y && sudo apt-get install -y software-properties-common gpg curl apt-transport-https ca-certificates |
| 50 | + ignore_errors: true |
| 51 | + |
| 52 | + - name: 8.3.1 Check if cri-o-apt-keyring.gpg exists |
| 53 | + ansible.builtin.stat: |
| 54 | + path: /etc/apt/keyrings/cri-o-apt-keyring.gpg |
| 55 | + register: cri_o_apt_keyring |
| 56 | + |
| 57 | + - name: 8.3 Download the CRI-O GPG key and save it to a keyring |
| 58 | + ansible.builtin.shell: |
| 59 | + cmd: curl -fsSL https://pkgs.k8s.io/addons:/cri-o:/prerelease:/main/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg |
| 60 | + when: not cri_o_apt_keyring.stat.exists |
| 61 | + |
| 62 | + - name: 8.4 Add the CRI-O repository to APT sources list |
| 63 | + ansible.builtin.shell: |
| 64 | + cmd: echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://pkgs.k8s.io/addons:/cri-o:/prerelease:/main/deb/ /" | tee /etc/apt/sources.list.d/cri-o.list |
| 65 | + |
| 66 | + |
| 67 | + # GPG (GNU Privacy Guard), are cryptographic keys used to sign packages. By adding the GPG key for the repository, you ensure that your system can trust the packages coming from that repository. |
| 68 | + |
| 69 | + - name: 9. Download cri-o |
| 70 | + ansible.builtin.shell: |
| 71 | + cmd: sudo apt-get update -y && sudo apt-get install -y cri-o |
| 72 | + ignore_errors: true |
| 73 | + |
| 74 | + |
| 75 | + - name: 10. Reload the systemd configurations and enable cri-o. |
| 76 | + ansible.builtin.shell: |
| 77 | + cmd: sudo systemctl daemon-reload && sudo systemctl enable crio --now && sudo systemctl start crio.service |
| 78 | + |
| 79 | +# Install Kubeadm & Kubelet & Kubectl |
| 80 | + |
| 81 | + - name: 11. Establish /etc/apt/keyrings and set Kubernetes_version |
| 82 | + ansible.builtin.shell: |
| 83 | + cmd: KUBERNETES_VERSION=1.30 && sudo mkdir -p /etc/apt/keyrings |
| 84 | + |
| 85 | + - name: 11. Download the GPG key for Kubernetes componants download |
| 86 | + ansible.builtin.shell: |
| 87 | + cmd: curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg |
| 88 | + |
| 89 | + - name: 11.x |
| 90 | + ansible.builtin.shell: |
| 91 | + cmd: echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.30/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list |
| 92 | + |
| 93 | + - name: 11.2 update |
| 94 | + ansible.builtin.shell: |
| 95 | + cmd: sudo apt-get update -y |
| 96 | + ignore_errors: true |
| 97 | + |
| 98 | + # /usr/share/keyrings/kubernetes-archive-keyring.gpg = It's a system-wide location typically used for storing trusted keys for package management. |
| 99 | + # https://dl.k8s.io/apt/doc/apt-key.gpg = The Kubernetes APT repository is a software repository where Kubernetes packages (such as kubelet, kubeadm and kubectl) are stored for installation on systems using the APT package management system (such as Debian and Ubuntu) |
| 100 | + |
| 101 | + - name: 12.x |
| 102 | + ansible.builtin.shell: |
| 103 | + cmd: apt-cache madison kubeadm | tac |
| 104 | + |
| 105 | + - name: 13. Download kubelet kubeadm kubectl |
| 106 | + ansible.builtin.shell: |
| 107 | + cmd: sudo apt-get install -y kubelet kubeadm kubectl |
| 108 | + |
| 109 | + - name: 14. apt-mark hold prevents updates kubelet, kubectl and kubeadm # if we didn't prevent it, so it can be dangerous for stability |
| 110 | + ansible.builtin.shell: |
| 111 | + cmd: sudo apt-mark hold kubelet kubeadm kubectl |
| 112 | + |
| 113 | + - name: 15. Downlaod jq for local_ip |
| 114 | + ansible.builtin.shell: |
| 115 | + sudo apt-get install -y jq |
| 116 | + |
| 117 | + - name: 16. Take the local IP address of the 'eth1' network interface |
| 118 | + ansible.builtin.shell: |
| 119 | + cmd: | |
| 120 | + local_ip="$(ip --json a s | jq -r '.[] | if .ifname == "eth1" then .addr_info[] | if .family == "inet" then .local else empty end else empty end')" |
| 121 | + cat > /etc/default/kubelet << EOF |
| 122 | + KUBELET_EXTRA_ARGS=--node-ip=$local_ip |
| 123 | + EOF |
| 124 | +
|
| 125 | +
|
0 commit comments