mirror of
https://github.com/techno-tim/k3s-ansible.git
synced 2025-12-26 10:43:04 +01:00
Add Tigera Operator/Calico CNI option
Small tweak to reduce delta from head Set calico option to be disabled by default Add rescue blocks in case updating existing Refactor items and update comments Refactor and consolidate calico.yml into block Refactor to use template for Calico CRs Revert use_calico to false Template blockSize Align default cidr in template with all.yml sample Apply upstream version tags Revert to current ver tags. Upstream's don't work. Update template address detection Add Tigera Operator/Calico CNI option
This commit is contained in:
2
.github/ISSUE_TEMPLATE.md
vendored
2
.github/ISSUE_TEMPLATE.md
vendored
@@ -35,7 +35,7 @@ k3s_version: ""
|
|||||||
ansible_user: NA
|
ansible_user: NA
|
||||||
systemd_dir: ""
|
systemd_dir: ""
|
||||||
|
|
||||||
flannel_iface: ""
|
container_iface: ""
|
||||||
|
|
||||||
apiserver_endpoint: ""
|
apiserver_endpoint: ""
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
---
|
---
|
||||||
k3s_version: v1.25.12+k3s1
|
k3s_version: v1.25.16+k3s4
|
||||||
# this is the user that has ssh access to these machines
|
# this is the user that has ssh access to these machines
|
||||||
ansible_user: ansibleuser
|
ansible_user: ansibleuser
|
||||||
systemd_dir: /etc/systemd/system
|
systemd_dir: /etc/systemd/system
|
||||||
@@ -7,8 +7,14 @@ systemd_dir: /etc/systemd/system
|
|||||||
# Set your timezone
|
# Set your timezone
|
||||||
system_timezone: "Your/Timezone"
|
system_timezone: "Your/Timezone"
|
||||||
|
|
||||||
# interface which will be used for flannel
|
# node interface which will be used for the container network interface (flannel or calico)
|
||||||
flannel_iface: "eth0"
|
container_iface: "eth0"
|
||||||
|
|
||||||
|
# set use_calico to true to use tigera operator/calico instead of the default CNI flannel
|
||||||
|
# install reference: https://docs.tigera.io/calico/latest/getting-started/kubernetes/k3s/multi-node-install#install-calico
|
||||||
|
use_calico: false
|
||||||
|
calico_cidr: "10.52.0.0/16" # pod cidr pool
|
||||||
|
calico_tag: "v3.27.0" # calico version tag
|
||||||
|
|
||||||
# apiserver_endpoint is virtual ip-address which will be configured on each master
|
# apiserver_endpoint is virtual ip-address which will be configured on each master
|
||||||
apiserver_endpoint: "192.168.30.222"
|
apiserver_endpoint: "192.168.30.222"
|
||||||
@@ -20,23 +26,30 @@ k3s_token: "some-SUPER-DEDEUPER-secret-password"
|
|||||||
# The IP on which the node is reachable in the cluster.
|
# The IP on which the node is reachable in the cluster.
|
||||||
# Here, a sensible default is provided, you can still override
|
# Here, a sensible default is provided, you can still override
|
||||||
# it for each of your hosts, though.
|
# it for each of your hosts, though.
|
||||||
k3s_node_ip: '{{ ansible_facts[flannel_iface]["ipv4"]["address"] }}'
|
k3s_node_ip: '{{ ansible_facts[container_iface]["ipv4"]["address"] }}'
|
||||||
|
|
||||||
# Disable the taint manually by setting: k3s_master_taint = false
|
# Disable the taint manually by setting: k3s_master_taint = false
|
||||||
k3s_master_taint: "{{ true if groups['node'] | default([]) | length >= 1 else false }}"
|
k3s_master_taint: "{{ true if groups['node'] | default([]) | length >= 1 else false }}"
|
||||||
|
|
||||||
# these arguments are recommended for servers as well as agents:
|
# these arguments are recommended for servers as well as agents:
|
||||||
extra_args: >-
|
extra_args: >-
|
||||||
--flannel-iface={{ flannel_iface }}
|
{{ '--flannel-iface=' + container_iface if not use_calico else '' }}
|
||||||
--node-ip={{ k3s_node_ip }}
|
--node-ip={{ k3s_node_ip }}
|
||||||
|
|
||||||
# change these to your liking, the only required are: --disable servicelb, --tls-san {{ apiserver_endpoint }}
|
# change these to your liking, the only required are: --disable servicelb, --tls-san {{ apiserver_endpoint }}
|
||||||
|
# the contents of the if block is also required if using calico
|
||||||
extra_server_args: >-
|
extra_server_args: >-
|
||||||
{{ extra_args }}
|
{{ extra_args }}
|
||||||
{{ '--node-taint node-role.kubernetes.io/master=true:NoSchedule' if k3s_master_taint else '' }}
|
{{ '--node-taint node-role.kubernetes.io/master=true:NoSchedule' if k3s_master_taint else '' }}
|
||||||
|
{% if use_calico %}
|
||||||
|
--flannel-backend=none
|
||||||
|
--disable-network-policy
|
||||||
|
--cluster-cidr={{ calico_cidr }}
|
||||||
|
{% endif %}
|
||||||
--tls-san {{ apiserver_endpoint }}
|
--tls-san {{ apiserver_endpoint }}
|
||||||
--disable servicelb
|
--disable servicelb
|
||||||
--disable traefik
|
--disable traefik
|
||||||
|
|
||||||
extra_agent_args: >-
|
extra_agent_args: >-
|
||||||
{{ extra_args }}
|
{{ extra_args }}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
# See:
|
# See:
|
||||||
# https://github.com/flannel-io/flannel/blob/67d603aaf45ef80f5dd39f43714fc5e6f8a637eb/Documentation/troubleshooting.md#Vagrant
|
# https://github.com/flannel-io/flannel/blob/67d603aaf45ef80f5dd39f43714fc5e6f8a637eb/Documentation/troubleshooting.md#Vagrant
|
||||||
flannel_iface: eth1
|
container_iface: eth1
|
||||||
|
|
||||||
# The test VMs might be a bit slow, so we give them more time to join the cluster:
|
# The test VMs might be a bit slow, so we give them more time to join the cluster:
|
||||||
retry_count: 45
|
retry_count: 45
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
# See:
|
# See:
|
||||||
# https://github.com/flannel-io/flannel/blob/67d603aaf45ef80f5dd39f43714fc5e6f8a637eb/Documentation/troubleshooting.md#Vagrant
|
# https://github.com/flannel-io/flannel/blob/67d603aaf45ef80f5dd39f43714fc5e6f8a637eb/Documentation/troubleshooting.md#Vagrant
|
||||||
flannel_iface: eth1
|
container_iface: eth1
|
||||||
|
|
||||||
# In this scenario, we have multiple interfaces that the VIP could be
|
# In this scenario, we have multiple interfaces that the VIP could be
|
||||||
# broadcasted on. Since we have assigned a dedicated private network
|
# broadcasted on. Since we have assigned a dedicated private network
|
||||||
@@ -27,13 +27,13 @@
|
|||||||
- fdad:bad:ba55::1b:0/112
|
- fdad:bad:ba55::1b:0/112
|
||||||
- 192.168.123.80-192.168.123.90
|
- 192.168.123.80-192.168.123.90
|
||||||
|
|
||||||
# k3s_node_ip is by default set to the IPv4 address of flannel_iface.
|
# k3s_node_ip is by default set to the IPv4 address of container_iface.
|
||||||
# We want IPv6 addresses here of course, so we just specify them
|
# We want IPv6 addresses here of course, so we just specify them
|
||||||
# manually below.
|
# manually below.
|
||||||
k3s_node_ip: "{{ node_ipv4 }},{{ node_ipv6 }}"
|
k3s_node_ip: "{{ node_ipv4 }},{{ node_ipv6 }}"
|
||||||
|
|
||||||
- name: Override host variables (2/2)
|
- name: Override host variables (2/2)
|
||||||
# Since "extra_args" depends on "k3s_node_ip" and "flannel_iface" we have
|
# Since "extra_args" depends on "k3s_node_ip" and "container_iface" we have
|
||||||
# to set this AFTER overriding the both of them.
|
# to set this AFTER overriding the both of them.
|
||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
# A few extra server args are necessary:
|
# A few extra server args are necessary:
|
||||||
|
|||||||
@@ -30,7 +30,7 @@
|
|||||||
name: net.ipv6.conf.{{ item }}.accept_dad
|
name: net.ipv6.conf.{{ item }}.accept_dad
|
||||||
value: "0"
|
value: "0"
|
||||||
with_items:
|
with_items:
|
||||||
- "{{ flannel_iface }}"
|
- "{{ container_iface }}"
|
||||||
|
|
||||||
- name: Write IPv4 configuration
|
- name: Write IPv4 configuration
|
||||||
ansible.builtin.template:
|
ansible.builtin.template:
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ network:
|
|||||||
version: 2
|
version: 2
|
||||||
renderer: networkd
|
renderer: networkd
|
||||||
ethernets:
|
ethernets:
|
||||||
{{ flannel_iface }}:
|
{{ container_iface }}:
|
||||||
addresses:
|
addresses:
|
||||||
- {{ node_ipv4 }}/24
|
- {{ node_ipv4 }}/24
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
ansible.builtin.set_fact:
|
ansible.builtin.set_fact:
|
||||||
# See:
|
# See:
|
||||||
# https://github.com/flannel-io/flannel/blob/67d603aaf45ef80f5dd39f43714fc5e6f8a637eb/Documentation/troubleshooting.md#Vagrant
|
# https://github.com/flannel-io/flannel/blob/67d603aaf45ef80f5dd39f43714fc5e6f8a637eb/Documentation/troubleshooting.md#Vagrant
|
||||||
flannel_iface: eth1
|
container_iface: eth1
|
||||||
|
|
||||||
# The test VMs might be a bit slow, so we give them more time to join the cluster:
|
# The test VMs might be a bit slow, so we give them more time to join the cluster:
|
||||||
retry_count: 45
|
retry_count: 45
|
||||||
|
|||||||
97
roles/k3s_server_post/tasks/calico.yml
Normal file
97
roles/k3s_server_post/tasks/calico.yml
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
---
|
||||||
|
- block:
|
||||||
|
- name: Create manifests directory on first master
|
||||||
|
file:
|
||||||
|
path: /tmp/k3s
|
||||||
|
state: directory
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: 0755
|
||||||
|
|
||||||
|
- name: "Download to first master: manifest for Tigera Operator and Calico CRDs"
|
||||||
|
ansible.builtin.get_url:
|
||||||
|
url: "https://raw.githubusercontent.com/projectcalico/calico/{{ calico_tag }}/manifests/tigera-operator.yaml"
|
||||||
|
dest: "/tmp/k3s/tigera-operator.yaml"
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
mode: 0755
|
||||||
|
|
||||||
|
- name: Copy Calico custom resources manifest to first master
|
||||||
|
ansible.builtin.template:
|
||||||
|
src: "calico.crs.j2"
|
||||||
|
dest: /tmp/k3s/custom-resources.yaml
|
||||||
|
|
||||||
|
- name: Deploy or replace Tigera Operator
|
||||||
|
block:
|
||||||
|
- name: Deploy Tigera Operator
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: kubectl create -f /tmp/k3s/tigera-operator.yaml
|
||||||
|
register: create_operator
|
||||||
|
changed_when: "'created' in create_operator.stdout"
|
||||||
|
failed_when: "'Error' in create_operator.stderr and 'already exists' not in create_operator.stderr"
|
||||||
|
rescue:
|
||||||
|
- name: Replace existing Tigera Operator
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: kubectl replace -f /tmp/k3s/tigera-operator.yaml
|
||||||
|
register: replace_operator
|
||||||
|
changed_when: "'replaced' in replace_operator.stdout"
|
||||||
|
failed_when: "'Error' in replace_operator.stderr"
|
||||||
|
|
||||||
|
- name: Wait for Tigera Operator resources
|
||||||
|
command: >-
|
||||||
|
k3s kubectl wait {{ item.type }}/{{ item.name }}
|
||||||
|
--namespace='tigera-operator'
|
||||||
|
--for=condition=Available=True
|
||||||
|
--timeout=7s
|
||||||
|
register: tigera_result
|
||||||
|
changed_when: false
|
||||||
|
until: tigera_result is succeeded
|
||||||
|
retries: 7
|
||||||
|
delay: 7
|
||||||
|
with_items:
|
||||||
|
- { name: tigera-operator, type: deployment }
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.type }}/{{ item.name }}"
|
||||||
|
|
||||||
|
- name: Deploy Calico custom resources
|
||||||
|
block:
|
||||||
|
- name: Deploy custom resources for Calico
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: kubectl create -f /tmp/k3s/custom-resources.yaml
|
||||||
|
register: create_cr
|
||||||
|
changed_when: "'created' in create_cr.stdout"
|
||||||
|
failed_when: "'Error' in create_cr.stderr and 'already exists' not in create_cr.stderr"
|
||||||
|
rescue:
|
||||||
|
- name: Apply new Calico custom resource manifest
|
||||||
|
ansible.builtin.command:
|
||||||
|
cmd: kubectl apply -f /tmp/k3s/custom-resources.yaml
|
||||||
|
register: apply_cr
|
||||||
|
changed_when: "'configured' in apply_cr.stdout or 'created' in apply_cr.stdout"
|
||||||
|
failed_when: "'Error' in apply_cr.stderr"
|
||||||
|
|
||||||
|
- name: Wait for Calico system resources to be available
|
||||||
|
command: >-
|
||||||
|
{% if item.type == 'daemonset' %}
|
||||||
|
k3s kubectl wait pods
|
||||||
|
--namespace='calico-system'
|
||||||
|
--selector={{ item.selector }}
|
||||||
|
--for=condition=Ready
|
||||||
|
{% else %}
|
||||||
|
k3s kubectl wait {{ item.type }}/{{ item.name }}
|
||||||
|
--namespace='calico-system'
|
||||||
|
--for=condition=Available
|
||||||
|
{% endif %}
|
||||||
|
--timeout=7s
|
||||||
|
register: cr_result
|
||||||
|
changed_when: false
|
||||||
|
until: cr_result is succeeded
|
||||||
|
retries: 30
|
||||||
|
delay: 7
|
||||||
|
with_items:
|
||||||
|
- { name: calico-typha, type: deployment }
|
||||||
|
- { name: calico-kube-controllers, type: deployment }
|
||||||
|
- { name: csi-node-driver, type: daemonset, selector: 'k8s-app=csi-node-driver' }
|
||||||
|
- { name: calico-node, type: daemonset, selector: 'k8s-app=calico-node' }
|
||||||
|
loop_control:
|
||||||
|
label: "{{ item.type }}/{{ item.name }}"
|
||||||
|
when: ansible_hostname == hostvars[groups[group_name_master | default('master')][0]]['ansible_hostname']
|
||||||
@@ -1,4 +1,9 @@
|
|||||||
---
|
---
|
||||||
|
- name: Deploy calico
|
||||||
|
include_tasks: calico.yml
|
||||||
|
tags: calico
|
||||||
|
when: use_calico == true
|
||||||
|
|
||||||
- name: Deploy metallb pool
|
- name: Deploy metallb pool
|
||||||
include_tasks: metallb.yml
|
include_tasks: metallb.yml
|
||||||
tags: metallb
|
tags: metallb
|
||||||
|
|||||||
28
roles/k3s_server_post/templates/calico.crs.j2
Normal file
28
roles/k3s_server_post/templates/calico.crs.j2
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# This section includes base Calico installation configuration.
|
||||||
|
# For more information, see: https://docs.tigera.io/calico/latest/reference/installation/api#operator.tigera.io/v1.Installation
|
||||||
|
apiVersion: operator.tigera.io/v1
|
||||||
|
kind: Installation
|
||||||
|
metadata:
|
||||||
|
name: default
|
||||||
|
spec:
|
||||||
|
# Configures Calico networking.
|
||||||
|
calicoNetwork:
|
||||||
|
# Note: The ipPools section cannot be modified post-install.
|
||||||
|
ipPools:
|
||||||
|
- blockSize: {{ calico_blockSize if calico_blockSize is defined else '26' }}
|
||||||
|
cidr: {{ calico_cidr if calico_cidr is defined else '10.52.0.0/16' }}
|
||||||
|
encapsulation: {{ calico_encapsulation if calico_encapsulation is defined else 'VXLANCrossSubnet' }}
|
||||||
|
natOutgoing: {{ calico_natOutgoing if calico_natOutgoing is defined else 'Enabled' }}
|
||||||
|
nodeSelector: {{ calico_nodeSelector if calico_nodeSelector is defined else 'all()' }}
|
||||||
|
nodeAddressAutodetectionV4:
|
||||||
|
interface: {{ container_iface if container_iface is defined else 'eth0' }}
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# This section configures the Calico API server.
|
||||||
|
# For more information, see: https://docs.tigera.io/calico/latest/reference/installation/api#operator.tigera.io/v1.APIServer
|
||||||
|
apiVersion: operator.tigera.io/v1
|
||||||
|
kind: APIServer
|
||||||
|
metadata:
|
||||||
|
name: default
|
||||||
|
spec: {}
|
||||||
Reference in New Issue
Block a user