8
|
1 from pyinfra import host
|
|
2 from pyinfra.facts.files import FindInFile
|
12
|
3 from pyinfra.facts.server import Arch, LinuxDistribution
|
|
4 from pyinfra.operations import files, server, systemd
|
8
|
5
|
|
6 bang_is_old = True # remove after upgrade
|
|
7 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux']
|
|
8 is_wifi_pi = host.name in ['frontdoor', 'living']
|
|
9
|
|
10 k3s_version = 'v1.21.2+k3s1'
|
|
11 master_ip = "10.5.0.1"
|
|
12
|
|
13 server.sysctl(key='net.ipv4.ip_forward', value="1", persist=True)
|
|
14 server.sysctl(key='net.ipv6.conf.all.forwarding', value="1", persist=True)
|
|
15
|
12
|
16 tail = 'k3s' if host.get_fact(Arch) == 'x86_64' else 'k3s-armhf'
|
|
17 files.download(src=f'https://github.com/rancher/k3s/releases/download/{k3s_version}/{tail}',
|
|
18 dest='/usr/local/bin/k3s',
|
|
19 user='root',
|
|
20 group='root',
|
|
21 mode='755')
|
8
|
22
|
|
23 if is_pi:
|
|
24 old_cmdline = host.get_fact(FindInFile, path='/boot/cmdline.txt', pattern=r'.*')[0]
|
|
25 print(repr(old_cmdline))
|
|
26 if 'cgroup' not in old_cmdline:
|
|
27 cmdline = old_cmdline + ' cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory'
|
|
28 files.line(path='/boot/cmdline.txt', line='.*', replace=cmdline)
|
|
29 # pi needs reboot now
|
|
30
|
|
31 server.shell(commands=[
|
|
32 'update-alternatives --set iptables /usr/sbin/iptables-legacy',
|
|
33 'update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy',
|
|
34 ])
|
|
35 # needs reboot if this changed
|
|
36
|
|
37 # See https://github.com/rancher/k3s/issues/1802 and https://rancher.com/docs/k3s/latest/en/installation/private-registry/
|
|
38 files.directory(path='/etc/rancher/k3s')
|
12
|
39 files.template(src='templates/kube/registries.yaml.j2', dest='/etc/rancher/k3s/registries.yaml')
|
8
|
40
|
12
|
41 if host.name == 'bang': # master
|
8
|
42 files.template(
|
12
|
43 src='templates/kube/k3s-server.service.j2',
|
8
|
44 dest='/etc/systemd/system/k3s.service',
|
|
45 master_ip=master_ip,
|
|
46 )
|
|
47 systemd.service(service='k3s.service', daemon_reload=True, enabled=True, restarted=True)
|
|
48
|
|
49 # one-time thing at cluster create time? not sure
|
|
50 # - name: Replace https://localhost:6443 by https://master-ip:6443
|
|
51 # command: >-
|
|
52 # k3s kubectl config set-cluster default
|
|
53 # --server=https://{{ master_ip }}:6443
|
|
54 # --kubeconfig ~{{ ansible_user }}/.kube/config
|
|
55
|
|
56 if host.name in ['slash', 'dash', 'frontbed', 'garage']: # nodes
|
12
|
57 # /var/lib/rancher/k3s/server/node-token is the source of the string in secrets/k3s_token
|
|
58 token = open('secrets/k3s_token', 'rt').read().strip()
|
|
59
|
8
|
60 files.template(
|
12
|
61 src='templates/kube/k3s-node.service.j2',
|
8
|
62 dest='/etc/systemd/system/k3s-node.service',
|
|
63 master_ip=master_ip,
|
|
64 token=token,
|
|
65 )
|
|
66
|
|
67 systemd.service(service='k3s-node.service', daemon_reload=True, enabled=True, restarted=True)
|
|
68
|
|
69 if host.name in ['bang', 'slash', 'dash']: # hosts to admin from
|
|
70 files.link(path='/usr/local/bin/kubectl', target='/usr/local/bin/k3s')
|
|
71 files.directory(path='/home/drewp/.kube', user='drewp', group='drewp')
|
|
72 files.line(path="/home/drewp/.zshrc", line="KUBECONFIG", replace='export KUBECONFIG=/etc/rancher/k3s/k3s.yaml')
|
|
73
|
|
74 files.chown(target='/etc/rancher/k3s/k3s.yaml', user='root', group='drewp')
|
|
75 files.chmod(target='/etc/rancher/k3s/k3s.yaml', mode='640')
|