-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaybook.yml
159 lines (148 loc) · 5.01 KB
/
playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Configure an EdgeOS router connected on one side to
# the internet and on the other to a private network.
# all Kubernetes targets should be in the network.
# - hosts: routers
# gather_facts: false
# roles:
# - edgeos
# Gather MAC addresses of targets collected by inventory
# in order to configure PXE booting of targets.
- hosts: all
gather_facts: no
tasks:
# used to configure PXE booting
- name: Fetch the MAC without a requirement on Python
raw: ifconfig | grep -A2 192.168.2 | tail -n1 | awk '{ print $2 }'
changed_when: false
ignore_errors: yes
register: mac
- name: Save MAC and IP address in variable
ignore_errors: yes
set_fact:
mac: "{{ mac.stdout | trim }}"
ip_address: "192.168.2.{{ groups['container_linux'].index(inventory_hostname) | int + 101 }}"
# Set up a PXE server
# so that by restarting computers in the network,
# we can reinstall operating systems.
#
# Set up an NFS server
# so that persistent volumes remain between
# resets of the nodes in the cluster
#
# Set up Postgresql DB
# so that the kubernetes state can
# remain between resets of nodes in the cluster
- hosts: localhost
become: yes
vars:
postgresql_hba_entries:
- type: host
database: all
user: all
address: '192.168.2.0/24'
auth_method: md5
postgresql_global_config_options:
- option: listen_addresses
value: '0.0.0.0'
postgresql_service_state: started
postgresql_service_enabled: true
postgresql_databases:
- name: k3s_db
postgresql_users:
- name: k3s
password: "{{ lookup('password', '~/.kube/k3s_postgresql_password length=64 chars=digits,ascii_letters') }}"
nfs_exports:
- "/data/nfs/read-only *(ro,sync,no_root_squash)"
- "/data/nfs/read-only/movies *(ro,sync,no_root_squash)"
- "/data/nfs/read-only/music *(ro,sync,no_root_squash)"
- "/data/nfs/read-write *(rw,sync,no_root_squash)"
roles:
- geerlingguy.nfs
# - geerlingguy.postgresql
- pxe-server
# Targets should have their NIC first in their
# boot order.
- name: Reset nodes
hosts: container_linux
gather_facts: no
roles:
- container-linux-reboot
- container-linux-setup-for-k3s
# Deploy the first master
- name: k3s server
hosts: container_linux[0]
gather_facts: no
tasks:
- name: Deploy first master
docker_container:
name: k3s-server
env:
K3S_DATASTORE_ENDPOINT: "postgres://k3s:{{ lookup('password', '~/.kube/k3s_postgresql_password length=64 chars=digits,ascii_letters') }}@192.168.2.100:5432/k3s_db"
K3S_TOKEN: "{{ lookup('password', '~/.kube/k3s_token length=64 chars=digits,ascii_letters') }}"
mounts: "{{ mounts }}"
devices: "{{ devices }}"
privileged: yes
pull: no
image: k3s
network_mode: host
command: "server --with-node-id {{ inventory_hostname }}"
# restart_policy: always
- name: Get the kubeconfig
command: |
docker exec -it k3s-server cat /etc/rancher/k3s/k3s.yaml
retries: 15
delay: 2
changed_when: no
register: kubeconfig
until: kubeconfig.rc == 0
- name: Write the kubeconfig to the localhost
become: yes
copy:
content: "{{ kubeconfig.stdout }}"
dest: /tmp/kubeconfig
delegate_to: localhost
- name: Overwrite the IP address in the Kubeconfig file
become: yes
lineinfile:
dest: /tmp/kubeconfig
regexp: '^\s*server.*'
line: " server: https://{{ hostvars[groups['container_linux'][0]]['ansible_host'] }}:6443"
state: present
delegate_to: localhost
- name: k3s masters
hosts: container_linux[1:3]
gather_facts: no
tasks:
- name: Join remaining masters
docker_container:
name: k3s-server
env:
K3S_DATASTORE_ENDPOINT: "postgres://k3s:{{ lookup('password', '~/.kube/k3s_postgresql_password length=64 chars=digits,ascii_letters') }}@192.168.2.100:5432/k3s_db"
K3S_TOKEN: "{{ lookup('password', '~/.kube/k3s_token length=64 chars=digits,ascii_letters') }}"
K3S_URL: "https://{{ hostvars[groups['container_linux'][0]]['ansible_host'] }}:6443"
mounts: "{{ mounts }}"
devices: "{{ devices }}"
privileged: yes
image: k3s
pull: no
network_mode: host
command: "server --with-node-id {{ inventory_hostname }}"
# restart_policy: always
- name: k3s agents
hosts: container_linux[3:]
gather_facts: no
tasks:
- name: Join k3s agents
docker_container:
name: k3s-server
env:
K3S_TOKEN: "{{ lookup('password', '~/.kube/k3s_token length=64 chars=digits,ascii_letters') }}"
K3S_URL: "https://{{ hostvars[groups['container_linux'][0]]['ansible_host'] }}:6443"
mounts: "{{ mounts }}"
devices: "{{ devices }}"
privileged: yes
image: k3s
pull: no
network_mode: host
command: "agent --with-node-id {{ inventory_hostname }}"
# restart_policy: always