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