Mercurial > code > home > repos > infra
annotate multikube.py @ 202:a5399e8b47b6
upgrade some ubuntu
author | drewp@bigasterisk.com |
---|---|
date | Fri, 30 Jun 2023 22:36:53 -0700 |
parents | 375515ab60ba |
children | 3af02e24eaf9 |
rev | line source |
---|---|
111 | 1 # leave kube.py running single-host and try again |
2 import os | |
3 | |
4 from pyinfra import host | |
5 from pyinfra.facts.files import FindInFile | |
6 from pyinfra.facts.server import Arch, LinuxDistribution | |
7 from pyinfra.operations import files, server, systemd | |
8 | |
9 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux'] | |
10 | |
112 | 11 from multikube_config import server_node, server_ip, nodes, admin_from, k3s_version, skaffold_version |
111 | 12 |
136
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
13 def install_k3s(): |
111 | 14 tail = 'k3s' if host.get_fact(Arch) == 'x86_64' else 'k3s-armhf' |
15 files.download( | |
16 src=f'https://github.com/rancher/k3s/releases/download/{k3s_version}/{tail}', | |
17 dest='/usr/local/bin/k3s', | |
18 user='root', | |
19 group='root', | |
20 mode='755', | |
21 cache_time=43000, | |
22 #force=True, # to get a new version | |
23 ) | |
24 | |
136
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
25 def install_k3sup(): |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
26 files.download( |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
27 src='https://github.com/alexellis/k3sup/releases/download/0.12.0/k3sup', |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
28 dest='/usr/local/bin/k3sup', |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
29 mode='755' |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
30 ) |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
31 # then do like: |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
32 # root@slash:/home/drewp# ./k3sup install --ip 10.2.0.84 --k3s-extra-args '--no-deploy traefik' --ssh-key /root/.ssh/id_ecdsa |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
33 # root@slash:/home/drewp# ./k3sup join --ip 10.2.0.23 --server-ip 10.2.0.84 --ssh-key /root/.ssh/id_ecdsa |
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
34 # but it doesn't seem to make networking go any better |
111 | 35 |
36 def install_skaffold(): | |
37 files.download(src=f'https://storage.googleapis.com/skaffold/releases/{skaffold_version}/skaffold-linux-amd64', | |
38 dest='/usr/local/bin/skaffold', | |
39 user='root', | |
40 group='root', | |
41 mode='755', | |
42 cache_time=1000) | |
43 # one time; writes to $HOME | |
44 #skaffold config set --global insecure-registries bang5:5000 | |
45 | |
46 | |
47 def pi_cgroup_setup(): | |
48 old_cmdline = host.get_fact(FindInFile, path='/boot/cmdline.txt', pattern=r'.*')[0] | |
49 if 'cgroup' not in old_cmdline: | |
50 cmdline = old_cmdline + ' cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory' | |
51 files.line(path='/boot/cmdline.txt', line='.*', replace=cmdline) | |
52 # pi needs reboot now | |
53 | |
54 | |
55 def host_prep(): | |
56 server.sysctl(key='net.ipv4.ip_forward', value="1", persist=True) | |
57 server.sysctl(key='net.ipv6.conf.all.forwarding', value="1", persist=True) | |
58 server.sysctl(key='net.ipv6.conf.all.disable_ipv6' , value='1',persist=True) | |
59 server.sysctl(key='fs.inotify.max_user_instances', value='8192', persist=True) | |
60 server.sysctl(key='fs.inotify.max_user_watches', value='524288', persist=True) | |
61 | |
62 # https://sysctl-explorer.net/net/ipv4/rp_filter/ | |
63 #none, strict, loose = 0, 1, 2 | |
64 #server.sysctl(key='net.ipv4.conf.default.rp_filter', value=loose, persist=True) | |
65 | |
66 if is_pi: | |
67 pi_cgroup_setup() | |
68 | |
69 def service_name(): | |
70 return 'k3s.service' if host.name == server_node else 'k3s-node.service' | |
71 | |
72 def config_and_run_service(): | |
73 role = 'server' if host.name == server_node else 'agent' | |
74 which_conf = 'config-server.yaml.j2' if host.name == server_node else 'config-agent.yaml.j2' | |
75 | |
76 if host.name == server_node: | |
77 token = "unused" | |
78 else: | |
79 token = open('/tmp/k3s-token', 'rt').read().strip() | |
80 files.template( | |
81 src=f'templates/kube/{which_conf}', | |
82 dest='/etc/k3s_config.yaml', | |
112 | 83 server_ip=server_ip, |
111 | 84 token=token, |
85 wg_ip=host.host_data['mk_addr'],#wireguard_address'], | |
86 ) | |
87 | |
88 files.template( | |
89 src='templates/kube/k3s.service.j2', | |
90 dest=f'/etc/systemd/system/{service_name()}', | |
91 role=role, | |
92 ) | |
93 systemd.service(service=service_name(), daemon_reload=True, enabled=True, restarted=True) | |
94 | |
95 if host.name == server_node: | |
96 files.get(src='/var/lib/rancher/k3s/server/node-token', dest='/tmp/k3s-token') | |
97 files.get(src='/etc/rancher/k3s/k3s.yaml', dest='/tmp/k3s-yaml') | |
98 | |
112 | 99 |
111 | 100 if host.name in nodes + [server_node]: |
101 host_prep() | |
102 files.directory(path='/etc/rancher/k3s') | |
136
375515ab60ba
multikube experiment junk. do not run
drewp@bigasterisk.com
parents:
113
diff
changeset
|
103 install_k3s() |
111 | 104 config_and_run_service() |
105 | |
106 # docs: https://rancher.com/docs/k3s/latest/en/installation/private-registry/ | |
107 # user confusions: https://github.com/rancher/k3s/issues/1802 | |
108 files.template(src='templates/kube/registries.yaml.j2', dest='/etc/rancher/k3s/registries.yaml') | |
109 # for the possible registries update: | |
110 systemd.service(service=service_name(), daemon_reload=True, enabled=True, restarted=True) | |
111 | |
112 if host.name in admin_from: | |
113 files.directory(path='/etc/rancher/k3s') | |
114 install_skaffold() | |
115 files.link(path='/usr/local/bin/kubectl', target='/usr/local/bin/k3s') | |
116 files.directory(path='/home/drewp/.kube', user='drewp', group='drewp') | |
117 # .zshrc has: export KUBECONFIG=/etc/rancher/k3s/k3s.yaml | |
118 | |
119 if host.name != server_node: | |
120 files.put(src='/tmp/k3s-yaml', dest='/etc/rancher/k3s/k3s.yaml') | |
121 | |
122 files.file(path='/etc/rancher/k3s/k3s.yaml', user='root', group='drewp', mode='640') | |
112 | 123 server.shell(f"kubectl config set-cluster default --server=https://{server_ip}:6443 --kubeconfig=/etc/rancher/k3s/k3s.yaml") |