├── .github └── workflows │ └── lint.yml ├── .gitignore ├── .yamllint ├── LICENSE ├── README.md ├── ansible.cfg ├── collections └── requirements.yml ├── inventory ├── .gitignore └── sample │ ├── group_vars │ └── all.yml │ └── hosts.ini ├── reset.yml ├── roles ├── download │ └── tasks │ │ └── main.yml ├── k3s │ ├── master │ │ ├── defaults │ │ │ └── main.yml │ │ ├── tasks │ │ │ └── main.yml │ │ └── templates │ │ │ └── k3s.service.j2 │ └── node │ │ ├── tasks │ │ └── main.yml │ │ └── templates │ │ └── k3s.service.j2 ├── prereq │ └── tasks │ │ └── main.yml ├── raspberrypi │ ├── handlers │ │ └── main.yml │ └── tasks │ │ ├── main.yml │ │ └── prereq │ │ ├── CentOS.yml │ │ ├── Raspbian.yml │ │ ├── Ubuntu.yml │ │ └── default.yml └── reset │ └── tasks │ ├── main.yml │ └── umount_with_children.yml └── site.yml /.github/workflows/lint.yml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Lint 3 | 'on': 4 | pull_request: 5 | push: 6 | branches: 7 | - master 8 | 9 | jobs: 10 | 11 | test: 12 | name: Lint 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - name: Check out the codebase. 17 | uses: actions/checkout@v2 18 | 19 | - name: Set up Python 3.7. 20 | uses: actions/setup-python@v2 21 | with: 22 | python-version: '3.x' 23 | 24 | - name: Install test dependencies. 25 | run: pip3 install yamllint ansible-lint ansible 26 | 27 | - name: Run yamllint. 28 | run: yamllint . 29 | 30 | - name: Run ansible-lint. 31 | run: ansible-lint 32 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/212850a/k3s-ansible/5c9a7949a5821d6b6fc3cf760e69d94e82c99527/.gitignore -------------------------------------------------------------------------------- /.yamllint: -------------------------------------------------------------------------------- 1 | --- 2 | extends: default 3 | 4 | rules: 5 | line-length: 6 | max: 120 7 | level: warning 8 | truthy: 9 | allowed-values: ['true', 'false', 'yes', 'no'] 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Build a Kubernetes cluster using k3s via Ansible 2 | 3 | Author: 4 | 5 | ## K3s Ansible Playbook 6 | 7 | Build a Kubernetes cluster using Ansible with k3s. The goal is easily install a Kubernetes cluster on machines running: 8 | 9 | - [X] Debian 10 | - [X] Ubuntu 11 | - [X] CentOS 12 | 13 | on processor architecture: 14 | 15 | - [X] x64 16 | - [X] arm64 17 | - [X] armhf 18 | 19 | ## System requirements 20 | 21 | Deployment environment must have Ansible 2.4.0+ 22 | Master and nodes must have passwordless SSH access 23 | 24 | ## Usage 25 | 26 | First create a new directory based on the `sample` directory within the `inventory` directory: 27 | 28 | ```bash 29 | cp -R inventory/sample inventory/my-cluster 30 | ``` 31 | 32 | Second, edit `inventory/my-cluster/hosts.ini` to match the system information gathered above. For example: 33 | 34 | ```bash 35 | [master] 36 | 192.16.35.12 37 | 38 | [node] 39 | 192.16.35.[10:11] 40 | 41 | [k3s_cluster:children] 42 | master 43 | node 44 | ``` 45 | 46 | If needed, you can also edit `inventory/my-cluster/group_vars/all.yml` to match your environment. 47 | 48 | Start provisioning of the cluster using the following command: 49 | 50 | ```bash 51 | ansible-playbook site.yml -i inventory/my-cluster/hosts.ini 52 | ``` 53 | 54 | ## Kubeconfig 55 | 56 | To get access to your **Kubernetes** cluster just 57 | 58 | ```bash 59 | scp debian@master_ip:~/.kube/config ~/.kube/config 60 | ``` 61 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | nocows = True 3 | roles_path = ./roles 4 | inventory = ./hosts.ini 5 | 6 | remote_tmp = $HOME/.ansible/tmp 7 | local_tmp = $HOME/.ansible/tmp 8 | pipelining = True 9 | become = True 10 | host_key_checking = False 11 | deprecation_warnings = False 12 | callback_whitelist = profile_tasks 13 | -------------------------------------------------------------------------------- /collections/requirements.yml: -------------------------------------------------------------------------------- 1 | --- 2 | collections: 3 | - name: community.general 4 | -------------------------------------------------------------------------------- /inventory/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | !sample/ -------------------------------------------------------------------------------- /inventory/sample/group_vars/all.yml: -------------------------------------------------------------------------------- 1 | --- 2 | k3s_version: v1.22.3+k3s1 3 | ansible_user: debian 4 | systemd_dir: /etc/systemd/system 5 | master_ip: "{{ hostvars[groups['master'][0]]['ansible_host'] | default(groups['master'][0]) }}" 6 | extra_server_args: "" 7 | extra_agent_args: "" 8 | -------------------------------------------------------------------------------- /inventory/sample/hosts.ini: -------------------------------------------------------------------------------- 1 | [master] 2 | 192.168.1.26 3 | 4 | [node] 5 | 192.168.1.34 6 | 192.168.1.39 7 | 192.168.1.16 8 | 192.168.1.32 9 | 10 | [k3s_cluster:children] 11 | master 12 | node 13 | -------------------------------------------------------------------------------- /reset.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - hosts: k3s_cluster 4 | gather_facts: yes 5 | become: yes 6 | roles: 7 | - role: reset 8 | -------------------------------------------------------------------------------- /roles/download/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Download k3s binary x64 4 | get_url: 5 | url: https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/k3s 6 | checksum: sha256:https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/sha256sum-amd64.txt 7 | dest: /usr/local/bin/k3s 8 | owner: root 9 | group: root 10 | mode: 0755 11 | when: ansible_facts.architecture == "x86_64" 12 | 13 | - name: Download k3s binary arm64 14 | get_url: 15 | url: https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/k3s-arm64 16 | checksum: sha256:https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/sha256sum-arm64.txt 17 | dest: /usr/local/bin/k3s 18 | owner: root 19 | group: root 20 | mode: 0755 21 | when: 22 | - ( ansible_facts.architecture is search("arm") and 23 | ansible_facts.userspace_bits == "64" ) or 24 | ansible_facts.architecture is search("aarch64") 25 | 26 | - name: Download k3s binary armhf 27 | get_url: 28 | url: https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/k3s-armhf 29 | checksum: sha256:https://github.com/k3s-io/k3s/releases/download/{{ k3s_version }}/sha256sum-arm.txt 30 | dest: /usr/local/bin/k3s 31 | owner: root 32 | group: root 33 | mode: 0755 34 | when: 35 | - ansible_facts.architecture is search("arm") 36 | - ansible_facts.userspace_bits == "32" 37 | -------------------------------------------------------------------------------- /roles/k3s/master/defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | k3s_server_location: /var/lib/rancher/k3s 3 | -------------------------------------------------------------------------------- /roles/k3s/master/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Copy K3s service file 4 | register: k3s_service 5 | template: 6 | src: "k3s.service.j2" 7 | dest: "{{ systemd_dir }}/k3s.service" 8 | owner: root 9 | group: root 10 | mode: 0644 11 | 12 | - name: Enable and check K3s service 13 | systemd: 14 | name: k3s 15 | daemon_reload: yes 16 | state: restarted 17 | enabled: yes 18 | 19 | - name: Wait for node-token 20 | wait_for: 21 | path: "{{ k3s_server_location }}/server/node-token" 22 | 23 | - name: Register node-token file access mode 24 | stat: 25 | path: "{{ k3s_server_location }}/server/node-token" 26 | register: p 27 | 28 | - name: Change file access node-token 29 | file: 30 | path: "{{ k3s_server_location }}/server/node-token" 31 | mode: "g+rx,o+rx" 32 | 33 | - name: Read node-token from master 34 | slurp: 35 | path: "{{ k3s_server_location }}/server/node-token" 36 | register: node_token 37 | 38 | - name: Store Master node-token 39 | set_fact: 40 | token: "{{ node_token.content | b64decode | regex_replace('\n', '') }}" 41 | 42 | - name: Restore node-token file access 43 | file: 44 | path: "{{ k3s_server_location }}/server/node-token" 45 | mode: "{{ p.stat.mode }}" 46 | 47 | - name: Create directory .kube 48 | file: 49 | path: ~{{ ansible_user }}/.kube 50 | state: directory 51 | owner: "{{ ansible_user }}" 52 | mode: "u=rwx,g=rx,o=" 53 | 54 | - name: Copy config file to user home directory 55 | copy: 56 | src: /etc/rancher/k3s/k3s.yaml 57 | dest: ~{{ ansible_user }}/.kube/config 58 | remote_src: yes 59 | owner: "{{ ansible_user }}" 60 | mode: "u=rw,g=,o=" 61 | 62 | - name: Replace https://localhost:6443 by https://master-ip:6443 63 | command: >- 64 | k3s kubectl config set-cluster default 65 | --server=https://{{ master_ip }}:6443 66 | --kubeconfig ~{{ ansible_user }}/.kube/config 67 | changed_when: true 68 | 69 | - name: Create kubectl symlink 70 | file: 71 | src: /usr/local/bin/k3s 72 | dest: /usr/local/bin/kubectl 73 | state: link 74 | 75 | - name: Create crictl symlink 76 | file: 77 | src: /usr/local/bin/k3s 78 | dest: /usr/local/bin/crictl 79 | state: link 80 | -------------------------------------------------------------------------------- /roles/k3s/master/templates/k3s.service.j2: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Lightweight Kubernetes 3 | Documentation=https://k3s.io 4 | After=network-online.target 5 | 6 | [Service] 7 | Type=notify 8 | ExecStartPre=-/sbin/modprobe br_netfilter 9 | ExecStartPre=-/sbin/modprobe overlay 10 | ExecStart=/usr/local/bin/k3s server --data-dir {{ k3s_server_location }} {{ extra_server_args | default("") }} 11 | KillMode=process 12 | Delegate=yes 13 | # Having non-zero Limit*s causes performance problems due to accounting overhead 14 | # in the kernel. We recommend using cgroups to do container-local accounting. 15 | LimitNOFILE=1048576 16 | LimitNPROC=infinity 17 | LimitCORE=infinity 18 | TasksMax=infinity 19 | TimeoutStartSec=0 20 | Restart=always 21 | RestartSec=5s 22 | 23 | [Install] 24 | WantedBy=multi-user.target 25 | -------------------------------------------------------------------------------- /roles/k3s/node/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - name: Copy K3s service file 4 | template: 5 | src: "k3s.service.j2" 6 | dest: "{{ systemd_dir }}/k3s-node.service" 7 | owner: root 8 | group: root 9 | mode: 0755 10 | 11 | - name: Enable and check K3s service 12 | systemd: 13 | name: k3s-node 14 | daemon_reload: yes 15 | state: restarted 16 | enabled: yes 17 | -------------------------------------------------------------------------------- /roles/k3s/node/templates/k3s.service.j2: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Lightweight Kubernetes 3 | Documentation=https://k3s.io 4 | After=network-online.target 5 | 6 | [Service] 7 | Type=notify 8 | ExecStartPre=-/sbin/modprobe br_netfilter 9 | ExecStartPre=-/sbin/modprobe overlay 10 | ExecStart=/usr/local/bin/k3s agent --server https://{{ master_ip }}:6443 --token {{ hostvars[groups['master'][0]]['token'] }} {{ extra_agent_args | default("") }} 11 | KillMode=process 12 | Delegate=yes 13 | # Having non-zero Limit*s causes performance problems due to accounting overhead 14 | # in the kernel. We recommend using cgroups to do container-local accounting. 15 | LimitNOFILE=1048576 16 | LimitNPROC=infinity 17 | LimitCORE=infinity 18 | TasksMax=infinity 19 | TimeoutStartSec=0 20 | Restart=always 21 | RestartSec=5s 22 | 23 | [Install] 24 | WantedBy=multi-user.target 25 | -------------------------------------------------------------------------------- /roles/prereq/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Set SELinux to disabled state 3 | selinux: 4 | state: disabled 5 | when: ansible_distribution in ['CentOS', 'Red Hat Enterprise Linux'] 6 | 7 | - name: Enable IPv4 forwarding 8 | sysctl: 9 | name: net.ipv4.ip_forward 10 | value: "1" 11 | state: present 12 | reload: yes 13 | 14 | - name: Enable IPv6 forwarding 15 | sysctl: 16 | name: net.ipv6.conf.all.forwarding 17 | value: "1" 18 | state: present 19 | reload: yes 20 | when: ansible_all_ipv6_addresses 21 | 22 | - name: Add br_netfilter to /etc/modules-load.d/ 23 | copy: 24 | content: "br_netfilter" 25 | dest: /etc/modules-load.d/br_netfilter.conf 26 | mode: "u=rw,g=,o=" 27 | when: ansible_distribution in ['CentOS', 'Red Hat Enterprise Linux'] 28 | 29 | - name: Load br_netfilter 30 | modprobe: 31 | name: br_netfilter 32 | state: present 33 | when: ansible_distribution in ['CentOS', 'Red Hat Enterprise Linux'] 34 | 35 | - name: Set bridge-nf-call-iptables (just to be sure) 36 | sysctl: 37 | name: "{{ item }}" 38 | value: "1" 39 | state: present 40 | reload: yes 41 | when: ansible_distribution in ['CentOS', 'Red Hat Enterprise Linux'] 42 | loop: 43 | - net.bridge.bridge-nf-call-iptables 44 | - net.bridge.bridge-nf-call-ip6tables 45 | 46 | - name: Add /usr/local/bin to sudo secure_path 47 | lineinfile: 48 | line: 'Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin' 49 | regexp: "Defaults(\\s)*secure_path(\\s)*=" 50 | state: present 51 | insertafter: EOF 52 | path: /etc/sudoers 53 | validate: 'visudo -cf %s' 54 | when: ansible_distribution in ['CentOS', 'Red Hat Enterprise Linux'] 55 | -------------------------------------------------------------------------------- /roles/raspberrypi/handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: reboot 3 | reboot: 4 | -------------------------------------------------------------------------------- /roles/raspberrypi/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Test for raspberry pi /proc/cpuinfo 3 | command: grep -E "Raspberry Pi|BCM2708|BCM2709|BCM2835|BCM2836" /proc/cpuinfo 4 | register: grep_cpuinfo_raspberrypi 5 | failed_when: false 6 | changed_when: false 7 | 8 | - name: Test for raspberry pi /proc/device-tree/model 9 | command: grep -E "Raspberry Pi" /proc/device-tree/model 10 | register: grep_device_tree_model_raspberrypi 11 | failed_when: false 12 | changed_when: false 13 | 14 | - name: Set raspberry_pi fact to true 15 | set_fact: 16 | raspberry_pi: true 17 | when: 18 | grep_cpuinfo_raspberrypi.rc == 0 or grep_device_tree_model_raspberrypi.rc == 0 19 | 20 | - name: Set detected_distribution to Raspbian 21 | set_fact: 22 | detected_distribution: Raspbian 23 | when: > 24 | raspberry_pi|default(false) and 25 | ( ansible_facts.lsb.id|default("") == "Raspbian" or 26 | ansible_facts.lsb.description|default("") is match("[Rr]aspbian.*") ) 27 | 28 | - name: Set detected_distribution to Raspbian (ARM64 on Debian Buster) 29 | set_fact: 30 | detected_distribution: Raspbian 31 | when: 32 | - ansible_facts.architecture is search("aarch64") 33 | - raspberry_pi|default(false) 34 | - ansible_facts.lsb.description|default("") is match("Debian.*buster") 35 | 36 | - name: Set detected_distribution_major_version 37 | set_fact: 38 | detected_distribution_major_version: "{{ ansible_facts.lsb.major_release }}" 39 | when: 40 | - detected_distribution | default("") == "Raspbian" 41 | 42 | - name: execute OS related tasks on the Raspberry Pi 43 | include_tasks: "{{ item }}" 44 | with_first_found: 45 | - "prereq/{{ detected_distribution }}-{{ detected_distribution_major_version }}.yml" 46 | - "prereq/{{ detected_distribution }}.yml" 47 | - "prereq/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml" 48 | - "prereq/{{ ansible_distribution }}.yml" 49 | - "prereq/default.yml" 50 | when: 51 | - raspberry_pi|default(false) 52 | -------------------------------------------------------------------------------- /roles/raspberrypi/tasks/prereq/CentOS.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Enable cgroup via boot commandline if not already enabled for Centos 3 | lineinfile: 4 | path: /boot/cmdline.txt 5 | backrefs: yes 6 | regexp: '^((?!.*\bcgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory\b).*)$' 7 | line: '\1 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory' 8 | notify: reboot 9 | -------------------------------------------------------------------------------- /roles/raspberrypi/tasks/prereq/Raspbian.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Activating cgroup support 3 | lineinfile: 4 | path: /boot/cmdline.txt 5 | regexp: '^((?!.*\bcgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory\b).*)$' 6 | line: '\1 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory' 7 | backrefs: true 8 | notify: reboot 9 | 10 | - name: Flush iptables before changing to iptables-legacy 11 | iptables: 12 | flush: true 13 | changed_when: false # iptables flush always returns changed 14 | 15 | - name: Changing to iptables-legacy 16 | alternatives: 17 | path: /usr/sbin/iptables-legacy 18 | name: iptables 19 | register: ip4_legacy 20 | 21 | - name: Changing to ip6tables-legacy 22 | alternatives: 23 | path: /usr/sbin/ip6tables-legacy 24 | name: ip6tables 25 | register: ip6_legacy 26 | -------------------------------------------------------------------------------- /roles/raspberrypi/tasks/prereq/Ubuntu.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Enable cgroup via boot commandline if not already enabled for Ubuntu on a Raspberry Pi 3 | lineinfile: 4 | path: /boot/firmware/cmdline.txt 5 | backrefs: yes 6 | regexp: '^((?!.*\bcgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory\b).*)$' 7 | line: '\1 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory' 8 | notify: reboot 9 | -------------------------------------------------------------------------------- /roles/raspberrypi/tasks/prereq/default.yml: -------------------------------------------------------------------------------- 1 | --- 2 | -------------------------------------------------------------------------------- /roles/reset/tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Disable services 3 | systemd: 4 | name: "{{ item }}" 5 | state: stopped 6 | enabled: no 7 | failed_when: false 8 | with_items: 9 | - k3s 10 | - k3s-node 11 | 12 | - name: pkill -9 -f "k3s/data/[^/]+/bin/containerd-shim-runc" 13 | register: pkill_containerd_shim_runc 14 | command: pkill -9 -f "k3s/data/[^/]+/bin/containerd-shim-runc" 15 | changed_when: "pkill_containerd_shim_runc.rc == 0" 16 | failed_when: false 17 | 18 | - name: Umount k3s filesystems 19 | include_tasks: umount_with_children.yml 20 | with_items: 21 | - /run/k3s 22 | - /var/lib/kubelet 23 | - /run/netns 24 | - /var/lib/rancher/k3s 25 | loop_control: 26 | loop_var: mounted_fs 27 | 28 | - name: Remove service files, binaries and data 29 | file: 30 | name: "{{ item }}" 31 | state: absent 32 | with_items: 33 | - /usr/local/bin/k3s 34 | - "{{ systemd_dir }}/k3s.service" 35 | - "{{ systemd_dir }}/k3s-node.service" 36 | - /etc/rancher/k3s 37 | - /var/lib/kubelet 38 | - /var/lib/rancher/k3s 39 | 40 | - name: daemon_reload 41 | systemd: 42 | daemon_reload: yes 43 | -------------------------------------------------------------------------------- /roles/reset/tasks/umount_with_children.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: Get the list of mounted filesystems 3 | shell: set -o pipefail && cat /proc/mounts | awk '{ print $2}' | grep -E "^{{ mounted_fs }}" 4 | register: get_mounted_filesystems 5 | args: 6 | executable: /bin/bash 7 | failed_when: false 8 | changed_when: get_mounted_filesystems.stdout | length > 0 9 | check_mode: false 10 | 11 | - name: Umount filesystem 12 | mount: 13 | path: "{{ item }}" 14 | state: unmounted 15 | with_items: 16 | "{{ get_mounted_filesystems.stdout_lines | reverse | list }}" 17 | -------------------------------------------------------------------------------- /site.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | - hosts: k3s_cluster 4 | gather_facts: yes 5 | become: yes 6 | roles: 7 | - role: prereq 8 | - role: download 9 | - role: raspberrypi 10 | 11 | - hosts: master 12 | become: yes 13 | roles: 14 | - role: k3s/master 15 | 16 | - hosts: node 17 | become: yes 18 | roles: 19 | - role: k3s/node 20 | --------------------------------------------------------------------------------