k8s version 1.19.11
[infra/stack/kubernetes.git] / scenarios / k8-calico-dualstack.yaml
1 ---
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2021 The Nordix Foundation. All rights reserved.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19
20 # ============README_START========================================================
21 # This scenario configures a Kubernetes cluster with the calico network plugin
22 # and configured with IPv6 addresses on the 'public' network. Currently, only
23 # the ubuntu1804 base OS is supported, using the bifrost provisioner.
24 #
25 # WARNING: DUAL-STACK IS AN ALPHA FEATURE AS OF 1.16. HERE BE DRAGONS.
26 #
27 # To run this scenario, the following is required:
28 #   - Kubernetes version >= 1.16. [1]
29 #   - Usage of bifrost provisioner
30 #   - Base OS == ubuntu1804
31 #   - A compatible version of Kubespray [2].
32 #   - Additional Ansible variables `libvirt_ipv6_networks`, `node_ipv6_addresses`
33 #     specified (for example, by the environment variable ENGINE_ANSIBLE_PARAMS).
34 #     Default values are provided below.
35 #
36 # The following tasks are performed prior to running the Kubespray installer:
37 #   - libvirt networks specified in `libvirt_ipv6_networks` are re-created with
38 #     the addition of an IPv6 stanza.
39 #   - The hosts specified in `node_ipv6_addresses` have the provided address
40 #     configured by dropping an additional file `$INTERFACE-ipv6.cfg` into
41 #     `/etc/network/interfaces.d/`. An additional reboot of the instances is
42 #     carried out to ensure the interfaces are configured properly.
43 #   - The parameter `enable_dual_stack_networks: true` is set in the Kubespray
44 #     configuration file on-disk.
45 #   - The Kubelet configuration file `/etc/kubernetes/kubelet.env` is modified to
46 #     append the corresponding IPv6 address of the node's interface to the node-ip
47 #     parameter. This ensures that nodes are aware of both their IPv4 and IPv6
48 #     interfaces.
49 #   - The feature gate is added: `IPv6DualStack=true`
50 #
51 # NOTES:
52 # [1] Assigning dual-stack addresses to kubelet via --node-ip=$IPV4,$IPV6
53 #     requires v1.20, see: https://github.com/kubernetes/kubernetes/issues/95239)
54 # [2] Kubespray supports IPv4/IPv6 DualStack as of commit c2c97c3.
55 # ============README_END==========================================================
56
57 # NOTE (cian): Scenario-specific preinstall tasks
58 - hosts: localhost
59   connection: local
60   gather_facts: false
61   become: false
62   tags:
63     - preinstall
64
65   # Below vars are only for documentation purposes and can be overridden as required.
66   vars:
67     libvirt_ipv6_networks:
68       - name: public
69         address: "2001:db8:0:2::/64"
70     node_ipv6_addresses:
71       master0:
72         interface: ens4
73         address: "2001:db8:0:2::3/64"
74       node0:
75         interface: ens4
76         address: "2001:db8:0:2::4/64"
77
78   tasks:
79     - name: ensure kubernetes version is at least 1.16
80       fail:
81         msg: "Dual-Stack operation requires a Kubernetes version of at least 1.16"
82       when: kubernetes_version is version('v1.16', operator='<', strict=False)
83
84     - name: Set network plugin to Calico
85       lineinfile:
86         path: "{{ engine_cache }}/repos/kubespray/inventory/engine/group_vars/k8s_cluster/k8s-cluster.yml"
87         regexp: "^kube_network_plugin:.*"
88         line: "kube_network_plugin: calico"
89
90     # NOTE (cian): this should really be in the installer configure-network role
91     - name: add IPv6 configuration stanzas to /etc/network/interfaces.d/
92       become: true
93       when: provisioner_type == "bifrost"
94       copy:
95         owner: root
96         group: root
97         mode: 0644
98         content: |
99           iface {{ item.value.interface }} inet6 static
100           address {{ item.value.address | ipaddr("address") }}
101           netmask {{ item.value.address | ipaddr("prefix") }}
102           gateway {{ item.value.address | ipaddr(1) | ipaddr("address") }}
103         dest: "/etc/network/interfaces.d/{{ item.value.interface }}-ipv6.cfg"
104       delegate_to: "{{ item.key }}"
105       with_items: "{{ node_ipv6_addresses | dict2items }}"
106
107     # NOTE (cian): this should really be in the provisioner create-libvirt-networks role
108     - name: add IPv6 to existing libvirt networks
109       when: provisioner_type == "bifrost"
110       become: true
111       block:
112         - name: dump libvirt networks needing IPv6
113           become: true
114           virt_net:
115             command: get_xml
116             name: "{{ item.name }}"
117           with_items: "{{ libvirt_ipv6_networks }}"
118           register: libvirt_net_xml
119
120         - name: dump libvirt networks needing IPv6 to engine cache dir
121           copy:
122             mode: 0644
123             content: "{{ item.get_xml }}"
124             dest: "{{ engine_cache }}/config/libvirt_net_{{ item.item.name }}.xml"
125           with_items: "{{ libvirt_net_xml.results }}"
126           register: libvirt_net_dumpxml
127
128         - name: add IPv6 stanza to libvirt networks requiring IPv6
129           blockinfile:
130             path: "{{ engine_cache }}/config/libvirt_net_{{ item.name }}.xml"
131             marker: "<!-- {mark} ANSIBLE MANAGED BLOCK -->"
132             insertbefore: "</network>"
133             block: |
134               <ip family='ipv6' address='{{ item.address | ipaddr(1) | ipaddr("address") }}' prefix='{{ item.address | ipaddr("prefix") }}'>
135                 <dhcp>
136                   <range start='{{ item.address | ipaddr(2) | ipaddr("address") }}' end='{{ item.address | ipaddr(255) | ipaddr("address") }}'/><!-- ' -->
137                 </dhcp>
138               </ip>
139           loop: "{{ libvirt_ipv6_networks | list }}"
140           register: libvirt_nets_blockinfile
141
142         - name: redefine libvirt networks
143           become: true
144           when: libvirt_nets_blockinfile is changed
145           block:
146             - name: stop libvirt VMs
147               virt:
148                 command: shutdown
149                 name: "{{ item }}"
150               loop: "{{ engine.installers.kubespray.hostnames | dict2items | map(attribute='value') | list }}"
151
152             - name: destroy affected libvirt networks
153               virt_net:
154                 command: destroy
155                 name: "{{ item.name }}"
156               with_items: "{{ libvirt_net_dumpxml.results | json_query('[].{name: item.item.name}') | list }}"
157
158             - name: undefine affected libvirt networks
159               virt_net:
160                 command: undefine
161                 name: "{{ item.name }}"
162               with_items: "{{ libvirt_net_dumpxml.results | json_query('[].{name: item.item.name}') | list }}"
163
164             - name: redefine affected libvirt networks
165               virt_net:
166                 command: define
167                 name: "{{ item.name }}"
168                 xml: "{{ lookup('file', item.path) }}"
169               with_items: "{{ libvirt_net_dumpxml.results | json_query('[].{ name: item.item.name, path: dest }') | list }}"
170
171             - name: start affected libvirt networks
172               virt_net:
173                 command: start
174                 name: "{{ item.name }}"
175               with_items: "{{ libvirt_net_dumpxml.results | json_query('[].{name: item.item.name}') | list }}"
176
177             - name: start libvirt VMs
178               virt:
179                 command: start
180                 name: "{{ item }}"
181               loop: "{{ engine.installers.kubespray.hostnames | dict2items | map(attribute='value') | list }}"
182
183     # NOTE (cian): this will not modify the file if regexp does not match because backrefs=true
184     - name: Enable IPv6DualStack feature gate
185       when: provisioner_type == "bifrost"
186       block:
187         - name: Append IPv6DualStack feature gate if feature gates are already set
188           lineinfile:
189             path: "{{ engine_cache }}/config/kubespray-vars.yml"
190             regexp: "^kube_feature_gates:\\s*\\[(.+)\\]\\s*$"
191             line: "kube_feature_gates: [\"IPv6DualStack=true\",\\1]"
192             backrefs: true
193
194         - name: Set IPv6DualStack feature gate if no feature gates are already set
195           lineinfile:
196             path: "{{ engine_cache }}/config/kubespray-vars.yml"
197             regexp: "^kube_feature_gates:\\s*\\[\\]\\s*$"
198             line: "kube_feature_gates: [\"IPv6DualStack=true\"]"
199
200         - name: enable dual-stack networks
201           lineinfile:
202             path: "{{ engine_cache }}/config/kubespray-vars.yml"
203             regexp: "^enable_dual_stack_networks:.*"
204             line: "enable_dual_stack_networks: true"
205
206 - hosts: k8s-nodes
207   tags:
208     - preinstall
209   tasks:
210     - name: wait for libvirt VMs to come back up
211       wait_for_connection:
212         connect_timeout: 10
213         delay: 30
214         timeout: 300
215       register: result
216       until: result is succeeded
217       retries: 3
218       when: provisioner_type == "bifrost"
219
220 # NOTE (cian): scenario-specific postinstall tasks
221 - hosts: k8s-cluster
222   tags:
223     - postinstall
224   tasks:
225     - name: set node-ip with both IPv4 and IPv6 addresses
226       block:
227         - name: Determine existing IPv4 address for kubelet
228           shell: |
229             set -o pipefail && \
230             egrep -o -- 'KUBELET_ADDRESS="--node-ip=[0-9\.]+' /etc/kubernetes/kubelet.env \
231               | awk -F '=' '{print $3}'
232           args:
233             executable: "/bin/bash"
234           register: kubelet_ipv4_address
235           changed_when: true
236
237         - name: Determine corresponding interface for IPv4 address {{ kubelet_ipv4_address.stdout }}
238           shell: |
239             set -o pipefail && \
240             ip -4 -o a \
241               | cut -d ' ' -f 2,7 \
242               | cut -d '/' -f 1 \
243               | grep {{ kubelet_ipv4_address.stdout }} \
244               | cut -d ' ' -f 1
245           args:
246             executable: "/bin/bash"
247           register: kubelet_ipv4_interface
248           changed_when: true
249
250           # NOTE (cian): this will default to the first IPv6 address assigned to the given interface
251           # excluding link-local addresses. If there are multiple IPv6 addresses assigned to one interface,
252           # this will probably not do what we want.
253         - name: Determine first IPv6 address assigned to interface {{ kubelet_ipv4_interface.stdout }}
254           shell: |
255             set -o pipefail && \
256             ip -6 -o a \
257               | cut -d ' ' -f 2,7 \
258               | cut -d '/' -f 1 \
259               | grep {{ kubelet_ipv4_interface.stdout }} \
260               | grep -v "fe80::" \
261               | cut -d ' ' -f 2 \
262               | head -n 1
263           args:
264             executable: "/bin/bash"
265           register: kubelet_ipv6_address
266           changed_when: true
267
268         # NOTE (cian): Dual-Stack NodeIP handling was added in v1.20.0
269         - name: Add comma-separated dualstack NodeIP parameter to Kubelet
270           become: true
271           lineinfile:
272             path: "/etc/kubernetes/kubelet.env"
273             regexp: "^KUBELET_ADDRESS=\"--node-ip=[0-9\\.]+\"\\s*$"
274             line: "KUBELET_ADDRESS=\"--node-ip={{ kubelet_ipv4_address.stdout }},{{ kubelet_ipv6_address.stdout }}\""
275           register: etc_kubernetes_kubelet_env
276
277         - name: Restart kubelet
278           become: true
279           service: name=kubelet state=restarted
280       when:
281         - provisioner_type == "bifrost"
282         - kubernetes_version is version('v1.20', operator='>=', strict=False)
283
284 - name: Execute common postinstall tasks
285   import_playbook: "../playbooks/postinstall.yaml"
286   tags: postinstall
287
288 # vim: set ts=2 sw=2 expandtab: