Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
hornjo committed Nov 16, 2024
1 parent 12380ff commit 3f9fbe4
Show file tree
Hide file tree
Showing 18 changed files with 674 additions and 1 deletion.
14 changes: 14 additions & 0 deletions .github/workflows/ansible-galaxy-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: anible-galaxy-publish
on:
push:
branches: ["main"]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
- name: Ansible role Publish
uses: 0x022b/galaxy-role-import-action@1.0.0
with:
galaxy_api_key: ${{ secrets.galaxy_api_key }}
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,40 @@
# rke2-installation
# rke2-installation

Role Name
=========

This Role installes a HA RKE2 Cluster with the Kube-VIP for HA and the Loadbalancer to expose. As this is onpremise, the kube-vip-cloud-controler is used to expose the loadbalancer IPs.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
roles:
- { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).
11 changes: 11 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
# RKE installation details
rke2_version: ""
arch: "" # amd64 or arm64
rke2_install_dir: "/usr/local/bin"
rke2_binary_url: "https://github.com/rancher/rke2/releases/download/{{ rke2_version }}/rke2.linux-{{ arch }}"

# Kube-VIP - for HA and Loadbalancer
kube_vip_version: v0.8.6
vip_interface: eth0
vip: ""
1 change: 1 addition & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
34 changes: 34 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)

min_ansible_version: 2.1

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
126 changes: 126 additions & 0 deletions tasks/bootstrap-rke.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
- name: Create directory for RKE2 config
ansible.builtin.file:
path: "/etc/rancher/rke2"
state: directory
mode: '0644'
become: true

- name: Create directory for RKE2 token
ansible.builtin.file:
path: "/var/lib/rancher/rke2/server"
state: directory
mode: '0644'
become: true

- name: Deploy RKE2 server Configuration
ansible.builtin.template:
src: templates/rke2-server-config.j2
dest: /etc/rancher/rke2/config.yaml
owner: root
group: root
mode: '0644'
when: inventory_hostname == groups["server"][0]
become: true

- name: Create systemd service file for RKE2 server
ansible.builtin.template:
src: templates/rke2-server.service.j2
dest: /etc/systemd/system/rke2-server.service
owner: root
group: root
mode: '0644'
when: inventory_hostname == groups["server"][0]
become: true

- name: Reload RKE-Server service
ansible.builtin.systemd:
name: rke2-server
enabled: true
state: restarted
daemon_reload: true
when: inventory_hostname == groups["server"][0]
become: true


- name: Wait for node-token
ansible.builtin.wait_for:
path: /var/lib/rancher/rke2/server/node-token
when: inventory_hostname == groups["server"][0]
become: true

- name: Wait for kubectl
ansible.builtin.wait_for:
path: /var/lib/rancher/rke2/bin/kubectl
when: inventory_hostname == groups["server"][0]
become: true

- name: Copy kubectl to user bin
ansible.builtin.copy:
src: /var/lib/rancher/rke2/bin/kubectl
dest: /usr/local/bin/kubectl
mode: '0755'
remote_src: true
become: true
when: inventory_hostname == groups["server"][0]

- name: Wait for kubectl
ansible.builtin.wait_for:
path: /usr/local/bin/kubectl
when: inventory_hostname == groups["server"][0]

- name: Check if the token exists and get details
ansible.builtin.stat:
path: /var/lib/rancher/rke2/server
register: token_stats
become: true

- name: Change file access for node-token
ansible.builtin.file:
path: /var/lib/rancher/rke2/server
mode: "g+rx,o+rx"
when: inventory_hostname == groups["server"][0]
become: true

- name: Fetch the token from the first server node and make var accessible for all
ansible.builtin.slurp:
src: /var/lib/rancher/rke2/server/token
register: rke2_token
when: inventory_hostname == groups["server"][0]
run_once: true
become: true

- name: Save Master node-token for later
ansible.builtin.set_fact:
token: "{{ rke2_token.content | b64decode | regex_replace('\n', '') }}"

- name: Restore node-token file access
ansible.builtin.file:
path: /var/lib/rancher/rke2/server
mode: "{{ token_stats.stat.mode }}"
when: inventory_hostname == groups["server"][0]
become: true

- name: Ensure .kube directory exists in user's home
ansible.builtin.file:
path: "/home/{{ ansible_user }}/.kube"
state: directory
mode: '0755'
become: true

- name: Copy config file to user home directory
ansible.builtin.copy:
src: /etc/rancher/rke2/rke2.yaml
dest: "/home/{{ ansible_user }}/.kube/config"
remote_src: true
owner: "{{ ansible_user }}"
mode: "u=rw,g=,o="
when: inventory_hostname == groups["server"][0]
become: true

- name: Replace IP address with VIP
ansible.builtin.replace:
path: /home/{{ ansible_user }}/.kube/config
regexp: '127.0.0.1'
replace: "{{ vip }}"
when: inventory_hostname == groups["server"][0]
become: true
61 changes: 61 additions & 0 deletions tasks/join-servers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
- name: Create systemd service file for RKE2 server
ansible.builtin.template:
src: templates/rke2-server.service.j2
dest: /etc/systemd/system/rke2-server.service
owner: root
group: root
mode: '0644'
when: inventory_hostname in groups['server'] and inventory_hostname != groups['server'][0]
become: true

- name: Create systemd service file for RKE2 agent
ansible.builtin.template:
src: templates/rke2-agent.service.j2
dest: /etc/systemd/system/rke2-agent.service
owner: root
group: root
mode: '0644'
when: inventory_hostname in groups['server'] and inventory_hostname != groups['server'][0]
become: true

- name: Wait for cluster API to be ready (can take 5-10 mins depending on internet/hardware)
ansible.builtin.command:
cmd: "kubectl get nodes"
register: kubectl_output
until: "'connection refused' not in kubectl_output.stderr"
retries: 120
delay: 10
changed_when: true
when: inventory_hostname == groups['server'][0]

# Use kubectl to deploy yaml. Perhaps this can be added to the manifest folder initially
- name: Apply kube vip configuration file
ansible.builtin.command:
cmd: kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml apply -f https://kube-vip.io/manifests/rbac.yaml
changed_when: true
when: inventory_hostname == groups['servers'][0]

# Apply the kube-vip configration. Perhaps this can be added to the manifest folder initially
- name: Apply kube vip configuration file
ansible.builtin.command:
cmd: kubectl --kubeconfig /etc/rancher/rke2/rke2.yaml apply -f https://raw.githubusercontent.com/kube-vip/kube-vip-cloud-provider/main/manifest/kube-vip-cloud-controller.yaml
changed_when: true
when: inventory_hostname == groups['servers'][0]

# Check that additional servers are restarted
- name: Ensure additional RKE2 servers are enabled and running
ansible.builtin.systemd:
name: rke2-server
enabled: true
state: restarted
daemon_reload: true
when: inventory_hostname != groups['servers'][0]

# enable additional servers
- name: Ensure RKE2 server is enabled and running
ansible.builtin.systemd:
name: rke2-server
enabled: true
state: restarted
daemon_reload: true
when: inventory_hostname != groups['servers'][0]
27 changes: 27 additions & 0 deletions tasks/kube-vip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
- name: Create directory for Kube VIP Manifest
ansible.builtin.file:
path: "/var/lib/rancher/rke2/server/manifests"
state: directory
mode: "0755"
when: inventory_hostname in groups['server']
become: true

- name: Deploy Kube VIP Configuration
ansible.builtin.template:
src: templates/kube-vip.yaml.j2
dest: /var/lib/rancher/rke2/server/manifests/kube-vip.yaml
owner: root
group: root
mode: "0644"
when: inventory_hostname == groups['server'][0]
become: true

- name: Deploy Kube VIP Configuration
ansible.builtin.template:
src: templates/kube-vip-controller.yaml.j2
dest: /var/lib/rancher/rke2/server/manifests/kube-vip.yaml
owner: root
group: root
mode: "0644"
when: inventory_hostname == groups['server'][0]
become: true
33 changes: 33 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
- name: Preparation of the nodes
ansible.builtin.include_tasks: prepare-nodes.yml
tags: prepare-nodes

- name: Creation of the kube-vip manifest for HA and prepare for loadbalancing
ansible.builtin.include_tasks: kube-vip.yml
tags: kube-vip

- name: Bootstrap the RKE cluster on the first node
ansible.builtin.include_tasks: bootstrap-rke.yml
tags: bootstrap-rke

# # Adds additional servers using the token from the previous task
# - name: Add additional RKE2 Servers
# hosts: servers
# gather_facts: true
# roles:
# - add-server

# # Adds agents to the cluster
# - name: Add additional RKE2 Agents
# hosts: agents
# gather_facts: true
# roles:
# - add-agent

# # Finish kube-vip, add metallb
# - name: Apply manifests after cluster is created
# hosts: servers
# gather_facts: true
# roles:
# - apply-manifests
36 changes: 36 additions & 0 deletions tasks/prepare-nodes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
- name: Enable IPv4 forwarding
ansible.posix.sysctl:
name: net.ipv4.ip_forward
value: "1"
state: present
reload: true
become: true

- name: Enable IPv6 forwarding
ansible.posix.sysctl:
name: net.ipv6.conf.all.forwarding
value: "1"
state: present
reload: true
become: true

- name: Create directory for RKE2 binary
ansible.builtin.file:
path: "{{ rke2_install_dir }}"
state: directory
mode: '0755'
become: true

- name: Download RKE2 binary
ansible.builtin.get_url:
url: "{{ rke2_binary_url }}"
dest: "{{ rke2_install_dir }}/rke2"
mode: '0755'
become: true

- name: Set executable permissions on the RKE2 binary
ansible.builtin.file:
path: "{{ rke2_install_dir }}/rke2"
mode: '0755'
state: file
become: true
Loading

0 comments on commit 3f9fbe4

Please sign in to comment.