Skip to content

Commit 0736144

Browse files
Merge pull request #10 from garutilorenzo/bugfix/kubernetes_repo
Repo refactor for new k8s version
2 parents 861dcc0 + 525220e commit 0736144

24 files changed

+271
-202
lines changed

README.md

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77

88
This ansible role will install and configure a high available Kubernetes cluster. This repo automate the installation process of Kubernetes using [kubeadm](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/).
99

10-
This repo is only a example on how to use Ansible automation to install and configure a Kubernetes cluster. For a production environment use [Kubespray](https://kubernetes.io/docs/setup/production-environment/tools/kubespray/)
10+
This repo is only a example on how to use Ansible automation to install and configure a Kubernetes cluster. For a production environment use [Kubespray](https://kubespray.io)
11+
12+
## Supported OS
13+
14+
This role was tested with:
15+
16+
Ubuntu: 22.04, 24.04
17+
18+
Fedora: 40
19+
20+
Tested partially with RHEL 8.
1121

1222
## Requirements
1323

@@ -27,31 +37,35 @@ ansible-galaxy install git+https://github.com/garutilorenzo/ansible-role-linux-k
2737

2838
This role accept this variables:
2939

30-
| Var | Required | Default | Desc |
31-
| ------- | ------- | ----------- | ----------- |
32-
| `kubernetes_subnet` | `yes` | `192.168.25.0/24` | Subnet where Kubernetess will be deployed. If the VM or bare metal server has more than one interface, Ansible will filter the interface used by Kubernetes based on the interface subnet |
33-
| `disable_firewall` | `no` | `no` | If set to yes Ansible will disable the firewall. |
34-
| `kubernetes_version` | `no` | `1.25.0` | Kubernetes version to install |
35-
| `kubernetes_cri` | `no` | `containerd` | Kubernetes [CRI](https://kubernetes.io/docs/concepts/architecture/cri/) to install. |
36-
| `kubernetes_cni` | `no` | `flannel` | Kubernetes [CNI](https://github.com/containernetworking/cni) to install. |
37-
| `kubernetes_dns_domain` | `no` | `cluster.local` | Kubernetes default DNS domain |
38-
| `kubernetes_pod_subnet` | `no` | `10.244.0.0/16` | Kubernetes pod subnet |
39-
| `kubernetes_service_subnet` | `no` | `10.96.0.0/12` | Kubernetes service subnet |
40-
| `kubernetes_api_port` | `no` | `6443` | kubeapi listen port |
41-
| `setup_vip` | `no` | `no` | Setup kubernetes VIP addres using [kube-vip](https://kube-vip.io/) |
42-
| `kubernetes_vip_ip` | `no` | `192.168.25.225` | **Required** if setup_vip is set to *yes*. Vip ip address for the control plane |
43-
| `kubevip_version` | `no` | `v0.4.3` | kube-vip container version |
44-
| `install_longhorn` | `no` | `no` | Install [Longhorn](#longhorn), Cloud native distributed block storage for Kubernetes. |
45-
| `longhorn_version` | `no` | `v1.3.1` | Longhorn release. |
46-
| `install_nginx_ingress` | `no` | `no` | Install [nginx ingress controller](#nginx-ingress-controller) |
47-
| `nginx_ingress_controller_version` | `no` | `controller-v1.3.0` | nginx ingress controller version |
48-
| `nginx_ingress_controller_http_nodeport` | `no` | `30080` | NodePort used by nginx ingress controller for the incoming http traffic |
49-
| `nginx_ingress_controller_https_nodeport` | `no` | `30443` | NodePort used by nginx ingress controller for the incoming https traffic |
50-
| `enable_nginx_ingress_proxy_protocol` | `no` | `no` | Enable nginx ingress controller proxy protocol mode |
51-
| `enable_nginx_real_ip` | `no` | `no` | Enable nginx ingress controller real-ip module |
52-
| `nginx_ingress_real_ip_cidr` | `no` | `0.0.0.0/0` | **Required** if enable_nginx_real_ip is set to *yes* Trusted subnet to use with the real-ip module |
53-
| `nginx_ingress_proxy_body_size` | `no` | `20m` | nginx ingress controller max proxy body size |
54-
| `sans_base` | `no` | `[list of values, see defaults/main.yml]` | list of ip addresses or FQDN uset to sign the kube-api certificate |
40+
| Var | Default | Desc |
41+
| ------- | ----------- | ----------- |
42+
| `disable_firewall` | If set to yes Ansible will disable the firewall. |
43+
| `disable_selinux` | `yes` | If set to yes Ansible will disable Selinux on RedHat based distro. Default `yes` [Ref.](https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/) |
44+
| `kubernetes_version` | `1.31.4` | Kubernetes version to install |
45+
| `kubernetes_image_registry` | `registry.k8s.io` | Default k8s registry |
46+
| `kubernetes_pause_image` | `registry.k8s.io/pause:3.10` | Default `pause` image version (includes registry). Needed to solve [this](https://github.com/kubernetes/kubeadm/issues/3146) issue. |
47+
| `kubernetes_cri` | `containerd` | Kubernetes [CRI](https://kubernetes.io/docs/concepts/architecture/cri/) to install. |
48+
| `kubernetes_cni` | `flannel` | Kubernetes [CNI](https://github.com/containernetworking/cni) to install. |
49+
| `flannel_version` | `v0.26.2` | Required if `kubernetes_cni` is set to `flannel`. Documentation available [here](https://github.com/flannel-io/flannel) |
50+
| `kubernetes_dns_domain` | `cluster.local` | Kubernetes default DNS domain |
51+
| `kubernetes_pod_subnet` | `10.244.0.0/16` | Kubernetes pod subnet |
52+
| `kubernetes_service_subnet` | `10.96.0.0/12` | Kubernetes service subnet |
53+
| `kubernetes_api_port` | `6443` | kubeapi listen port |
54+
| `kubernetes_subnet` | `192.168.25.0/24` | Subnet where Kubernetess will be deployed. If the VM or bare metal server has more than one interface, Ansible will filter the interface used by Kubernetes based on the interface subnet |
55+
| `setup_vip` | Setup kubernetes VIP addres using [kube-vip](https://kube-vip.io/) |
56+
| `kubernetes_vip_ip` | `192.168.25.225` | **Required** if setup_vip is set to *yes*. Vip ip address for the control plane |
57+
| `kubevip_version` | `v0.8.7` | kube-vip container version |
58+
| `install_longhorn` | Install [Longhorn](#longhorn), Cloud native distributed block storage for Kubernetes. |
59+
| `longhorn_version` | `v1.7.2` | Longhorn release. |
60+
| `install_nginx_ingress` | Install [nginx ingress controller](#nginx-ingress-controller) |
61+
| `nginx_ingress_controller_version` | `v1.12.0` | nginx ingress controller version |
62+
| `nginx_ingress_controller_http_nodeport` | `30080` | NodePort used by nginx ingress controller for the incoming http traffic |
63+
| `nginx_ingress_controller_https_nodeport` | `30443` | NodePort used by nginx ingress controller for the incoming https traffic |
64+
| `enable_nginx_ingress_proxy_protocol` | Enable nginx ingress controller proxy protocol mode |
65+
| `enable_nginx_real_ip` | Enable nginx ingress controller real-ip module |
66+
| `nginx_ingress_real_ip_cidr` | `0.0.0.0/0` | **Required** if enable_nginx_real_ip is set to *yes* Trusted subnet to use with the real-ip module |
67+
| `nginx_ingress_proxy_body_size` | `20m` | nginx ingress controller max proxy body size |
68+
| `sans_base` | `[list of values, see defaults/main.yml]` | list of ip addresses or FQDN uset to sign the kube-api certificate |
5569

5670
## Extra Variables
5771

@@ -86,7 +100,10 @@ In the Vagrantfile you can inject your public ssh key directly in the authorized
86100

87101
## Using this role
88102

89-
To use this role you follow the example in the [examples/](examples/) dir. Adjust the hosts.ini file with your hosts and run the playbook:
103+
To use this role you follow the example in the [examples/](examples/) dir.
104+
On the very first run add the extra variable parameter `-e kubernetes_init_host=<HOSTNAME>` to the `ansible-playbook` command where HOSTNAME is the hostname where the k8s cluster will be initialized.
105+
106+
Adjust the example hosts.ini file with your hosts and run the playbook:
90107

91108
```
92109
lorenzo@mint-virtual:~$ ansible-playbook -i hosts-ubuntu.ini site.yml -e kubernetes_init_host=k8s-ubuntu-0

defaults/main.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
---
22

33
disable_firewall: no
4+
disable_selinux: yes # Ref. https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
45

5-
kubernetes_version: 1.27.3
6+
kubernetes_version: 1.31.4
7+
kubernetes_image_registry: registry.k8s.io
8+
kubernetes_pause_image: registry.k8s.io/pause:3.10
69
kubernetes_cri: containerd
710
kubernetes_cni: flannel
811
kubernetes_dns_domain: cluster.local
@@ -15,13 +18,14 @@ kubernetes_subnet: 192.168.25.0/24
1518
setup_vip: no
1619
kubernetes_vip_ip: 192.168.25.225
1720

18-
kubevip_version: v0.6.0
21+
kubevip_version: v0.8.7
22+
flannel_version: v0.26.2
1923

2024
install_longhorn: no
21-
longhorn_version: v1.4.3
25+
longhorn_version: v1.7.2
2226

2327
install_nginx_ingress: no
24-
nginx_ingress_controller_version: controller-v1.8.1
28+
nginx_ingress_controller_version: v1.12.0
2529
nginx_ingress_controller_http_nodeport: 30080
2630
nginx_ingress_controller_https_nodeport: 30443
2731
enable_nginx_ingress_proxy_protocol: no

examples/hosts.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ k8s-ubuntu-2 ansible_host=192.168.25.112
1010
[kubeworker]
1111
k8s-ubuntu-3 ansible_host=192.168.25.113
1212
k8s-ubuntu-4 ansible_host=192.168.25.114
13-
k8s-ubuntu-5 ansible_host=192.168.25.115
13+
k8s-ubuntu-5 ansible_host=192.168.25.115
14+
15+
[kubernetes:vars]
16+
ansible_python_interpreter=/usr/bin/python3

examples/site.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
---
2+
3+
- hosts: kubernetes
4+
become: yes
5+
remote_user: vagrant
6+
tasks:
7+
- ansible.builtin.import_role:
8+
name: ansible-role-linux-kubernetes
9+
tasks_from: set_custom_fact
10+
vars_files:
11+
- vars.yaml
12+
213
- hosts: kubemaster
314
become: yes
415
remote_user: vagrant

examples/vars.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22

33
disable_firewall: yes
4-
kubernetes_subnet: 192.168.25.0/24
54

6-
setup_vip: yes
75
install_nginx_ingress: yes
86
install_longhorn: yes

meta/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ galaxy_info:
88
versions:
99
- jammy
1010
- focal
11+
- noble
1112
- name: EL
1213
versions:
1314
- 8
15+
- name: Fedora
16+
versions:
17+
- 40
1418
galaxy_tags:
1519
- kubernetes
1620
- k8s

tasks/init_cluster.yml

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,10 @@
44
ansible.builtin.shell: kubectl get nodes
55
ignore_errors: true
66
register: cluster_exist
7-
8-
- set_fact:
9-
kubernetes_init: "{% if kubernetes_init_host is defined and kubernetes_init_host == inventory_hostname %}yes{% else %}no{% endif %}"
10-
11-
- set_fact:
12-
kubernetes_ip_address: "{{ item }}"
13-
when: "item | ansible.utils.ipaddr( kubernetes_subnet )"
14-
with_items: "{{ ansible_all_ipv4_addresses | difference([kubernetes_vip_ip]) }}"
15-
16-
- set_fact:
17-
kubernetes_image_repository: "registry.k8s.io"
187

198
- block:
20-
21-
- set_fact:
22-
kubernetes_iface: "{{ hostvars[inventory_hostname]['ansible_' + item ]['device'] }}"
23-
when:
24-
- hostvars[inventory_hostname]['ansible_' + item ].ipv4 is defined
25-
- hostvars[inventory_hostname]['ansible_' + item ]['ipv4']['address'] | ansible.utils.ipaddr( kubernetes_subnet )
26-
with_items: "{{ ansible_interfaces }}"
279

28-
- set_fact:
10+
- ansible.builtin.set_fact:
2911
apiserver_sans: "{{ (sans_base + [kubernetes_vip_ip] ) | unique }}"
3012

3113
- name: render kubeadm-init.yml
@@ -39,7 +21,7 @@
3921
- setup_vip
4022
- inventory_hostname in groups['kubemaster']
4123

42-
- set_fact:
24+
- ansible.builtin.set_fact:
4325
kubeadm_extra_args: "{% if groups['kubemaster'] | length > 1 %}--upload-certs{% else %}{% endif %}"
4426

4527
- name: Init kubernetes cluster

tasks/install_cni.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,22 @@
33
- block:
44
- name: Download kube-flannel.yml
55
ansible.builtin.get_url:
6-
url: https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
6+
url: https://raw.githubusercontent.com/flannel-io/flannel/refs/tags/{{ flannel_version }}/Documentation/kube-flannel.yml
77
dest: /root/kube-flannel.yml
88

99
- name: Set flannel iface
1010
ansible.builtin.lineinfile:
1111
path: /root/kube-flannel.yml
1212
insertafter: '^(\s*).*kube-subnet-mgr(.*)$'
1313
line: ' - --iface={{ kubernetes_iface }}'
14+
15+
- name: Fix cni-plugin path only for Fedora distribution
16+
ansible.builtin.replace:
17+
path: /root/kube-flannel.yml
18+
regexp: '^(\s*)path: \/opt\/cni\/bin(.*)$'
19+
replace: '\1path: /usr/libexec/cni'
20+
when:
21+
- ansible_distribution == 'Fedora'
1422

1523
- name: Install flannel
1624
ansible.builtin.command: kubectl apply -f /root/kube-flannel.yml

tasks/install_cri.yml

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323

2424
- name: Render containerd default config
2525
ansible.builtin.shell: containerd config default > /etc/containerd/config.toml
26-
when:
27-
- containerd_config.found
26+
when: containerd_config.found
2827

2928
- name: enable SystemdCgroup for containerd
3029
ansible.builtin.replace:
@@ -34,6 +33,38 @@
3433
notify:
3534
- reload containerd
3635

36+
- name: Update the [grpc] block in /etc/containerd/config.toml
37+
ansible.builtin.blockinfile:
38+
path: /etc/containerd/config.toml
39+
marker: "{mark}"
40+
block: |2
41+
address = "{{ cri_socket_paths[kubernetes_cri] }}"
42+
gid = 0
43+
max_recv_message_size = 16777216
44+
max_send_message_size = 16777216
45+
tcp_address = ""
46+
tcp_tls_ca = ""
47+
tcp_tls_cert = ""
48+
tcp_tls_key = ""
49+
uid = 0
50+
marker_begin: "[grpc]"
51+
marker_end: "[metrics]"
52+
notify:
53+
- reload containerd
54+
55+
# container runtime is inconsistent with that used by kubeadm
56+
# Ref. https://github.com/kubernetes/kubeadm/issues/3146
57+
- name: Update sandbox_image in /etc/containerd/config.toml
58+
ansible.builtin.lineinfile:
59+
path: /etc/containerd/config.toml
60+
regexp: '^\s*sandbox_image\s*=.*'
61+
line: ' sandbox_image = "{{ kubernetes_pause_image }}"'
62+
63+
- name: render crictl.yaml
64+
ansible.builtin.template:
65+
src: crictl.yaml.j2
66+
dest: /etc/crictl.yaml
67+
3768
- name: start and enable containerd
3869
ansible.builtin.systemd:
3970
name: containerd
@@ -42,49 +73,5 @@
4273

4374
when: kubernetes_cri == 'containerd'
4475

45-
# Fedora hack
46-
- name: Check if /opt/cni/bin directory exist
47-
ansible.builtin.stat:
48-
path: /opt/cni/bin
49-
register: cni_bin_dir
50-
51-
- block:
52-
- name: Install containernetworking-plugins
53-
ansible.builtin.package:
54-
name: containernetworking-plugins
55-
state: present
56-
57-
- name: stop containerd
58-
ansible.builtin.systemd:
59-
name: containerd
60-
state: stopped
61-
62-
- name: Create /opt/cni/ directory
63-
ansible.builtin.file:
64-
name: /opt/cni/
65-
state: directory
66-
67-
- name: Remove /opt/cni/bin directory if exist
68-
ansible.builtin.file:
69-
name: /opt/cni/bin
70-
state: removed
71-
72-
- name: Create symlink from /usr/libexec/cni to /opt/cni/bin
73-
ansible.builtin.file:
74-
src: /usr/libexec/cni
75-
dest: /opt/cni/bin
76-
state: link
77-
78-
- name: start containerd
79-
ansible.builtin.systemd:
80-
name: containerd
81-
state: started
82-
when:
83-
- ansible_distribution == 'Fedora'
84-
- kubernetes_cri == 'containerd'
85-
- cni_bin_dir.stat.islnk is defined
86-
- not cni_bin_dir.stat.islnk
87-
# END Fedora hack
88-
8976
- name: Force all notified handlers to run now
9077
meta: flush_handlers

tasks/install_nginx_ingress.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
- block:
1111
- name: Install ingress-nginx
12-
ansible.builtin.command: kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/{{ nginx_ingress_controller_version }}/deploy/static/provider/baremetal/deploy.yaml
12+
ansible.builtin.command: kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-{{ nginx_ingress_controller_version }}/deploy/static/provider/baremetal/deploy.yaml
1313
delegate_to: "{{ groups['kubemaster'][0] }}"
1414
run_once: true
1515

0 commit comments

Comments
 (0)