Mercurial > code > home > repos > infra
annotate kube.py @ 61:b46df76991b6
10.1 cleanups; verbose settings; address updates
author | drewp@bigasterisk.com |
---|---|
date | Sun, 01 May 2022 23:34:01 -0700 |
parents | f39ada0b8827 |
children | 50100cad1f38 |
rev | line source |
---|---|
61
b46df76991b6
10.1 cleanups; verbose settings; address updates
drewp@bigasterisk.com
parents:
58
diff
changeset
|
1 import tempfile |
8 | 2 from pyinfra import host |
3 from pyinfra.facts.files import FindInFile | |
12 | 4 from pyinfra.facts.server import Arch, LinuxDistribution |
5 from pyinfra.operations import files, server, systemd | |
8 | 6 |
7 is_pi = host.get_fact(LinuxDistribution)['name'] in ['Debian', 'Raspbian GNU/Linux'] | |
8 | |
40 | 9 # https://github.com/k3s-io/k3s/releases |
46 | 10 k3s_version = 'v1.23.3+k3s1' |
40 | 11 |
12 # https://github.com/GoogleContainerTools/skaffold/releases | |
56 | 13 skaffold_version = 'v1.37.0' |
40 | 14 |
8 | 15 master_ip = "10.5.0.1" |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
16 server_node = 'bang' |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
17 nodes = ['slash', 'dash', 'frontbed', 'garage'] |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
18 admin_from = ['bang', 'slash', 'dash'] |
8 | 19 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
20 if host.name in [nodes + [server_node]]: |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
21 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
|
22 server.sysctl(key='net.ipv6.conf.all.forwarding', value="1", persist=True) |
8 | 23 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
24 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
|
25 files.download( |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
26 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
|
27 dest='/usr/local/bin/k3s', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
28 user='root', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
29 group='root', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
30 mode='755', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
31 cache_time=43000, |
58
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
32 # force=True, # to get a new version |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
33 ) |
8 | 34 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
35 if is_pi: |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
36 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
|
37 print(repr(old_cmdline)) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
38 if 'cgroup' not in old_cmdline: |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
39 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
|
40 files.line(path='/boot/cmdline.txt', line='.*', replace=cmdline) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
41 # pi needs reboot now |
8 | 42 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
43 server.shell(commands=[ |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
44 'update-alternatives --set iptables /usr/sbin/iptables-nft', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
45 'update-alternatives --set ip6tables /usr/sbin/ip6tables-nft', |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
46 ]) |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
47 # needs reboot if this changed |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
48 |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
49 # 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
|
50 files.directory(path='/etc/rancher/k3s') |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
51 files.template(src='templates/kube/registries.yaml.j2', dest='/etc/rancher/k3s/registries.yaml') |
8 | 52 |
28 | 53 service_name = 'k3s.service' if host.name == 'bang' else 'k3s-node.service' |
54 which_conf = 'config.yaml.j2' if host.name == 'bang' else 'node-config.yaml.j2' | |
55 | |
56 # /var/lib/rancher/k3s/server/node-token is the source of the string in secrets/k3s_token | |
57 token = open('secrets/k3s_token', 'rt').read().strip() | |
8 | 58 files.template( |
28 | 59 src=f'templates/kube/{which_conf}', |
21
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
60 dest='/etc/k3s_config.yaml', |
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
61 master_ip=master_ip, |
28 | 62 token=token, |
63 wg_ip=host.host_data['wireguard_address'], | |
8 | 64 ) |
12 | 65 |
8 | 66 files.template( |
28 | 67 src='templates/kube/k3s.service.j2', |
68 dest=f'/etc/systemd/system/{service_name}', | |
69 role='server' if host.name == 'bang' else 'agent', | |
8 | 70 ) |
28 | 71 systemd.service(service=service_name, daemon_reload=True, enabled=True, restarted=True) |
8 | 72 |
58
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
73 if host.name == 'bang': |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
74 files.put( |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
75 src="templates/kube/coredns.yaml", |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
76 dest="/var/lib/rancher/k3s/server/manifests/coredns.yaml", |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
77 mode="600", |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
78 ) |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
79 # files.put( |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
80 # src="templates/kube/coredns-map.yaml", |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
81 # dest="/var/lib/rancher/k3s/server/manifests/coredns-map.yaml", |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
82 # mode="600", |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
83 # ) |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
84 # tmp = tempfile.NamedTemporaryFile(suffix='.yaml') |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
85 # files.template( |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
86 # src='templates/kube/Corefile.yaml.j2', |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
87 # dest=tmp.name, |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
88 # ) |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
89 # server.shell(commands=[ |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
90 # 'kubectl replace configmap ' |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
91 # # '-n kube-system ' |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
92 # # 'coredns ' |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
93 # f'--filename={tmp.name} ' |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
94 # '-o yaml ' |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
95 # # '--dry-run=client | kubectl apply -', |
f39ada0b8827
push a coredns config with the right forwarding server
drewp@bigasterisk.com
parents:
56
diff
changeset
|
96 # ]) |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
97 # one-time thing at cluster create time? not sure |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
98 # - 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
|
99 # command: >- |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
100 # k3s kubectl config set-cluster default |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
101 # --server=https://{{ master_ip }}:6443 |
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
102 # --kubeconfig ~{{ ansible_user }}/.kube/config |
8 | 103 |
27
7b22ff272001
refactor (may not be a correct commit)
drewp@bigasterisk.com
parents:
21
diff
changeset
|
104 if host.name in admin_from: |
8 | 105 files.link(path='/usr/local/bin/kubectl', target='/usr/local/bin/k3s') |
106 files.directory(path='/home/drewp/.kube', user='drewp', group='drewp') | |
107 files.line(path="/home/drewp/.zshrc", line="KUBECONFIG", replace='export KUBECONFIG=/etc/rancher/k3s/k3s.yaml') | |
108 | |
109 files.chown(target='/etc/rancher/k3s/k3s.yaml', user='root', group='drewp') | |
110 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
|
111 |
29 | 112 # see https://github.com/GoogleContainerTools/skaffold/releases |
40 | 113 files.download(src=f'https://storage.googleapis.com/skaffold/releases/{skaffold_version}/skaffold-linux-amd64', |
17
0c1496e11b8f
get skaffold on hosts that want to do deploys
drewp@bigasterisk.com
parents:
12
diff
changeset
|
114 dest='/usr/local/bin/skaffold', |
0c1496e11b8f
get skaffold on hosts that want to do deploys
drewp@bigasterisk.com
parents:
12
diff
changeset
|
115 user='root', |
0c1496e11b8f
get skaffold on hosts that want to do deploys
drewp@bigasterisk.com
parents:
12
diff
changeset
|
116 group='root', |
21
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
117 mode='755', |
948d9d72267d
k3s update and some config refactoring
drewp@bigasterisk.com
parents:
19
diff
changeset
|
118 cache_time=1000) |