|
| 1 | +--- |
| 2 | +title: Install K3S and tools |
| 3 | +layout: home |
| 4 | +nav_order: 2 |
| 5 | +grand_parent: Ansible |
| 6 | +parent: Ansible Playbooks |
| 7 | +permalink: docs/ansible/playbooks/install-k3s-and-tools |
| 8 | +last_modified_date: 2024-04-21 |
| 9 | +--- |
| 10 | + |
| 11 | +# Ansible Playbook: Install Kubernetes Locally |
| 12 | + |
| 13 | +This Ansible playbook automates the installation of Kubernetes locally using k3s, as well as additional tools such as k9s and Helm. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +- Ansible installed on the control machine |
| 18 | +- `sudo` privileges on the target machine |
| 19 | +- Internet access on the target machine |
| 20 | + |
| 21 | +## How to Use |
| 22 | + |
| 23 | +1. Clone this repository: |
| 24 | + |
| 25 | +```bash |
| 26 | +git clone https://github.com/user-cube/ansible-playbooks.git |
| 27 | +``` |
| 28 | + |
| 29 | +2. Navigate to the playbook directory: |
| 30 | + |
| 31 | +```bash |
| 32 | +cd ubuntu/k3s/ |
| 33 | +``` |
| 34 | + |
| 35 | +3. Edit the `hosts` file to specify the target host where you want to install Kubernetes locally. |
| 36 | + |
| 37 | +4. Run the playbook: |
| 38 | + |
| 39 | +```bash |
| 40 | +ansible-playbook k3s.yml |
| 41 | +``` |
| 42 | + |
| 43 | +5. Once the playbook execution is complete, you should have Kubernetes installed locally, along with k9s and Helm. |
| 44 | + |
| 45 | +## Customization |
| 46 | + |
| 47 | +- Modify the playbook variables as needed to customize the installation process. |
| 48 | +- Adjust the playbook tasks to suit your environment and requirements. |
| 49 | + |
| 50 | +## Notes |
| 51 | + |
| 52 | +- This playbook assumes you are running it on a Linux-based system. |
| 53 | +- Ensure you have sufficient permissions and network access for the installation process to complete successfully. |
| 54 | +- Review the playbook tasks and verify they meet your security and operational requirements. |
| 55 | + |
| 56 | +## Code |
| 57 | + |
| 58 | +You can find this code on [ansible-playbooks](https://github.com/user-cube/ansible-playbooks) repo. |
| 59 | + |
| 60 | +```yaml |
| 61 | +--- |
| 62 | +- name: Install Kubernetes Locally |
| 63 | + hosts: localhost |
| 64 | + become: yes |
| 65 | + become_method: sudo |
| 66 | + |
| 67 | + tasks: |
| 68 | + |
| 69 | + # Install k3s |
| 70 | + - name: Download k3s installation script |
| 71 | + get_url: |
| 72 | + url: "https://get.k3s.io" |
| 73 | + dest: "/tmp/install_k3s.sh" |
| 74 | + |
| 75 | + - name: Execute k3s installation script |
| 76 | + shell: "/bin/bash /tmp/install_k3s.sh" |
| 77 | + args: |
| 78 | + creates: /etc/rancher/k3s/k3s.yaml |
| 79 | + register: k3s_install_output |
| 80 | + environment: |
| 81 | + K3S_KUBECONFIG_MODE: "644" |
| 82 | + INSTALL_K3S_EXEC: "--disable=traefik" |
| 83 | + |
| 84 | + - debug: |
| 85 | + msg: "{{ k3s_install_output.stdout_lines }}" |
| 86 | + |
| 87 | + - name: Get user's home directory |
| 88 | + set_fact: |
| 89 | + user_home: "{% raw %} {{ lookup('env', 'HOME') }}{% endraw %}" |
| 90 | + |
| 91 | + - name: Create .kube directory |
| 92 | + file: |
| 93 | + path: "{{ user_home }}/.kube" |
| 94 | + state: directory |
| 95 | + owner: "{% raw %} {{ lookup('env', 'USER') }}{% endraw %}" |
| 96 | + become: no |
| 97 | + |
| 98 | + - name: Append export command to shell config |
| 99 | + ansible.builtin.lineinfile: |
| 100 | + path: "{{ user_home }}/.{{ shell_config }}" |
| 101 | + line: "export KUBECONFIG=~/.kube/config" |
| 102 | + insertafter: EOF |
| 103 | + become: no |
| 104 | + vars: |
| 105 | + shell_config: "{% raw %}{{ 'bashrc' if ansible_env.SHELL | regex_search('bash') else 'zshrc' }}{% endraw %}" |
| 106 | + |
| 107 | + # Copy k3s configuration to user's home directory |
| 108 | + - name: Copy k3s configuration to user's home directory |
| 109 | + copy: |
| 110 | + src: "/etc/rancher/k3s/k3s.yaml" |
| 111 | + dest: "{{ user_home }}/.kube/config" |
| 112 | + remote_src: yes |
| 113 | + owner: "{% raw %}{{ lookup('env', 'USER') }}{% endraw %}" |
| 114 | + mode: '0600' |
| 115 | + |
| 116 | + # Setup alias for kubectl |
| 117 | + - name: Append export command to shell config |
| 118 | + ansible.builtin.lineinfile: |
| 119 | + path: "{{ user_home }}/.{{ shell_config }}" |
| 120 | + line: "{{ item }}" |
| 121 | + insertafter: EOF |
| 122 | + become: no |
| 123 | + loop: |
| 124 | + - "alias k=kubectl" |
| 125 | + - "setNS() { kubectl config set-context --current --namespace=\"$@\" ; }" |
| 126 | + vars: |
| 127 | + shell_config: "{% raw %}{{ 'bashrc' if ansible_env.SHELL | regex_search('bash') else 'zshrc' }}{% endraw %}" |
| 128 | + |
| 129 | + # Install K9S |
| 130 | + - name: Install required packages |
| 131 | + apt: |
| 132 | + name: "{{ item }}" |
| 133 | + state: present |
| 134 | + loop: |
| 135 | + - wget |
| 136 | + - unzip |
| 137 | + tags: |
| 138 | + - k9s |
| 139 | + |
| 140 | + - name: Find latest k9s release version |
| 141 | + uri: |
| 142 | + url: "https://api.github.com/repos/derailed/k9s/releases/latest" |
| 143 | + return_content: yes |
| 144 | + register: latest_release |
| 145 | + tags: |
| 146 | + - k9s |
| 147 | + |
| 148 | + - name: Extract latest k9s release version |
| 149 | + set_fact: |
| 150 | + k9s_version: "{{ latest_release.json.tag_name }}" |
| 151 | + tags: |
| 152 | + - k9s |
| 153 | + |
| 154 | + - name: Download k9s binary |
| 155 | + get_url: |
| 156 | + url: "https://github.com/derailed/k9s/releases/download/{{ k9s_version }}/k9s_Linux_amd64.tar.gz" |
| 157 | + dest: "/tmp/k9s.tar.gz" |
| 158 | + mode: '0644' |
| 159 | + tags: |
| 160 | + - k9s |
| 161 | + |
| 162 | + - name: Extract k9s binary |
| 163 | + unarchive: |
| 164 | + src: "/tmp/k9s.tar.gz" |
| 165 | + dest: "/usr/local/bin" |
| 166 | + remote_src: yes |
| 167 | + tags: |
| 168 | + - k9s |
| 169 | + |
| 170 | + - name: Make k9s executable |
| 171 | + file: |
| 172 | + path: "/usr/local/bin/k9s" |
| 173 | + mode: '0755' |
| 174 | + tags: |
| 175 | + - k9s |
| 176 | + |
| 177 | + # Install helm |
| 178 | + - name: Install required dependencies |
| 179 | + package: |
| 180 | + name: "{{ item }}" |
| 181 | + state: present |
| 182 | + loop: |
| 183 | + - curl |
| 184 | + - tar |
| 185 | + |
| 186 | + - name: Fetch latest Helm version |
| 187 | + uri: |
| 188 | + url: https://api.github.com/repos/helm/helm/releases/latest |
| 189 | + return_content: yes |
| 190 | + register: helm_latest_release |
| 191 | + |
| 192 | + - set_fact: |
| 193 | + helm_version: "{% raw %}{{ helm_latest_release.json.tag_name | regex_replace('^v', '') }}{% endraw %}" |
| 194 | + |
| 195 | + - name: Download Helm binary |
| 196 | + get_url: |
| 197 | + url: "https://get.helm.sh/helm-v{{ helm_version }}-linux-amd64.tar.gz" |
| 198 | + dest: "/tmp/helm-v{{ helm_version }}-linux-amd64.tar.gz" |
| 199 | + |
| 200 | + - name: Extract Helm binary |
| 201 | + ansible.builtin.unarchive: |
| 202 | + src: "/tmp/helm-v{{ helm_version }}-linux-amd64.tar.gz" |
| 203 | + dest: /usr/local/bin |
| 204 | + remote_src: yes |
| 205 | + creates: "/usr/local/bin/linux-amd64/helm" |
| 206 | + |
| 207 | + - name: Ensure Helm binary is executable |
| 208 | + file: |
| 209 | + path: /usr/local/bin/helm |
| 210 | + state: link |
| 211 | + src: /usr/local/bin/linux-amd64/helm |
| 212 | + mode: u+x |
| 213 | + |
| 214 | + - name: Verify Helm installation |
| 215 | + command: helm version |
| 216 | + register: helm_version_output |
| 217 | + |
| 218 | + - debug: |
| 219 | + var: helm_version_output.stdout_lines |
| 220 | +``` |
0 commit comments