Mercurial > code > home > repos > infra
annotate kube.py @ 29:911da5e10834
bump k3s and skaffold
author | drewp@bigasterisk.com |
---|---|
date | Sat, 15 Jan 2022 14:45:01 -0800 |
parents | d844cdf86164 |
children | 193cd3b051c6 |
rev | line source |
---|---|
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 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux'] | |
7 | |
29 | 8 k3s_version = 'v1.23.1+k3s1' |
8 | 9 master_ip = "10.5.0.1" |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
10 server_node = 'bang' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
11 nodes = ['slash', 'dash', 'frontbed', 'garage'] |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
12 admin_from = ['bang', 'slash', 'dash'] |
8 | 13 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
14 if host.name in [nodes + [server_node]]: |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
15 server.sysctl(key='net.ipv4.ip_forward', value="1", persist=True) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
16 server.sysctl(key='net.ipv6.conf.all.forwarding', value="1", persist=True) |
8 | 17 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
18 tail = 'k3s' if host.get_fact(Arch) == 'x86_64' else 'k3s-armhf' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
19 files.download( |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
20 src=f'https://github.com/rancher/k3s/releases/download/{k3s_version}/{tail}', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
21 dest='/usr/local/bin/k3s', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
22 user='root', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
23 group='root', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
24 mode='755', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
25 cache_time=43000, |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
26 # force=True, # to get a new version |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
27 ) |
8 | 28 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
29 if is_pi: |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
30 old_cmdline = host.get_fact(FindInFile, path='/boot/cmdline.txt', pattern=r'.*')[0] |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
31 print(repr(old_cmdline)) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
32 if 'cgroup' not in old_cmdline: |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
33 cmdline = old_cmdline + ' cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
34 files.line(path='/boot/cmdline.txt', line='.*', replace=cmdline) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
35 # pi needs reboot now |
8 | 36 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
37 server.shell(commands=[ |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
38 'update-alternatives --set iptables /usr/sbin/iptables-nft', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
39 'update-alternatives --set ip6tables /usr/sbin/ip6tables-nft', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
40 ]) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
41 # needs reboot if this changed |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
42 |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
43 # See https://github.com/rancher/k3s/issues/1802 and https://rancher.com/docs/k3s/latest/en/installation/private-registry/ |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
44 files.directory(path='/etc/rancher/k3s') |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
45 files.template(src='templates/kube/registries.yaml.j2', dest='/etc/rancher/k3s/registries.yaml') |
8 | 46 |
28 | 47 service_name = 'k3s.service' if host.name == 'bang' else 'k3s-node.service' |
48 which_conf = 'config.yaml.j2' if host.name == 'bang' else 'node-config.yaml.j2' | |
49 | |
50 # /var/lib/rancher/k3s/server/node-token is the source of the string in secrets/k3s_token | |
51 token = open('secrets/k3s_token', 'rt').read().strip() | |
8 | 52 files.template( |
28 | 53 src=f'templates/kube/{which_conf}', |
21
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
54 dest='/etc/k3s_config.yaml', |
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
55 master_ip=master_ip, |
28 | 56 token=token, |
57 wg_ip=host.host_data['wireguard_address'], | |
8 | 58 ) |
12 | 59 |
8 | 60 files.template( |
28 | 61 src='templates/kube/k3s.service.j2', |
62 dest=f'/etc/systemd/system/{service_name}', | |
63 role='server' if host.name == 'bang' else 'agent', | |
8 | 64 ) |
28 | 65 systemd.service(service=service_name, daemon_reload=True, enabled=True, restarted=True) |
8 | 66 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
67 # if bang: |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
68 # files.template( |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
69 # src='templates/kube/Corefile.j2', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
70 # dest='/etc/k3s_coredns_config', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
71 # ) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
72 # server.shell(commands=[ |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
73 # 'kubectl replace configmap ' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
74 # '-n kube-system ' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
75 # 'coredns ' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
76 # '--from-file=Corefile=/etc/k3s_coredns_config ' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
77 # '-o yaml ' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
78 # '--dry-run=client | kubectl apply -', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
79 # ]) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
80 # one-time thing at cluster create time? not sure |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
81 # - name: Replace https://localhost:6443 by https://master-ip:6443 |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
82 # command: >- |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
83 # k3s kubectl config set-cluster default |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
84 # --server=https://{{ master_ip }}:6443 |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
85 # --kubeconfig ~{{ ansible_user }}/.kube/config |
8 | 86 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
87 if host.name in admin_from: |
8 | 88 files.link(path='/usr/local/bin/kubectl', target='/usr/local/bin/k3s') |
89 files.directory(path='/home/drewp/.kube', user='drewp', group='drewp') | |
90 files.line(path="/home/drewp/.zshrc", line="KUBECONFIG", replace='export KUBECONFIG=/etc/rancher/k3s/k3s.yaml') | |
91 | |
92 files.chown(target='/etc/rancher/k3s/k3s.yaml', user='root', group='drewp') | |
93 files.chmod(target='/etc/rancher/k3s/k3s.yaml', mode='640') | |
17
0c1496e11b8f
get skaffold on hosts that want to do deploys
drewp@bigasterisk.com
parents:
12
diff
changeset
|
94 |
29 | 95 # see https://github.com/GoogleContainerTools/skaffold/releases |
96 files.download(src='https://storage.googleapis.com/skaffold/releases/v1.35.1/skaffold-linux-amd64', | |
17
0c1496e11b8f
get skaffold on hosts that want to do deploys
drewp@bigasterisk.com
parents:
12
diff
changeset
|
97 dest='/usr/local/bin/skaffold', |
0c1496e11b8f
get skaffold on hosts that want to do deploys
drewp@bigasterisk.com
parents:
12
diff
changeset
|
98 user='root', |
0c1496e11b8f
get skaffold on hosts that want to do deploys
drewp@bigasterisk.com
parents:
12
diff
changeset
|
99 group='root', |
21
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
100 mode='755', |
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
101 cache_time=1000) |