forked from testdrivenio/kubernetes-fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
214 lines (175 loc) · 5.62 KB
/
fabfile.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
import re
import sys
import time
from contextlib import contextmanager
from digitalocean import Droplet, Manager
from fabric import Connection, task
DIGITAL_OCEAN_ACCESS_TOKEN = os.getenv("DIGITAL_OCEAN_ACCESS_TOKEN")
user = "root"
hosts = []
# tasks
@task
def ping(ctx, output):
"""Sanity check"""
print("pong!")
print(f"hello {output}!")
@task
def create_droplets(ctx):
"""
Create three new DigitalOcean droplets -
node-1, node-2, node-3
"""
manager = Manager(token=DIGITAL_OCEAN_ACCESS_TOKEN)
keys = manager.get_all_sshkeys()
for num in range(3):
node = f"node-{num + 1}"
droplet = Droplet(
token=DIGITAL_OCEAN_ACCESS_TOKEN,
name=node,
region="nyc3",
image="ubuntu-20-04-x64",
size="s-2vcpu-4gb",
tags=[node],
ssh_keys=keys,
)
droplet.create()
print(f"{node} has been created.")
@task
def wait_for_droplets(ctx):
"""Wait for each droplet to be ready and active"""
for num in range(3):
node = f"node-{num + 1}"
while True:
status = get_droplet_status(node)
if status == "active":
print(f"{node} is ready.")
break
else:
print(f"{node} is not ready.")
time.sleep(1)
def get_droplet_status(node):
"""Given a droplet's tag name, return the status of the droplet"""
manager = Manager(token=DIGITAL_OCEAN_ACCESS_TOKEN)
droplet = manager.get_all_droplets(tag_name=node)
return droplet[0].status
@task
def destroy_droplets(ctx):
"""Destroy the droplets - node-1, node-2, node-3"""
manager = Manager(token=DIGITAL_OCEAN_ACCESS_TOKEN)
for num in range(3):
node = f"node-{num + 1}"
droplets = manager.get_all_droplets(tag_name=node)
for droplet in droplets:
droplet.destroy()
print(f"{node} has been destroyed.")
@task
def get_addresses(ctx, type):
"""Get IP address"""
manager = Manager(token=DIGITAL_OCEAN_ACCESS_TOKEN)
if type == "master":
droplet = manager.get_all_droplets(tag_name="node-1")
print(droplet[0].ip_address)
hosts.append(droplet[0].ip_address)
elif type == "workers":
for num in range(2, 4):
node = f"node-{num}"
droplet = manager.get_all_droplets(tag_name=node)
print(droplet[0].ip_address)
hosts.append(droplet[0].ip_address)
elif type == "all":
for num in range(3):
node = f"node-{num + 1}"
droplet = manager.get_all_droplets(tag_name=node)
print(droplet[0].ip_address)
hosts.append(droplet[0].ip_address)
else:
print('The "type" should be either "master", "workers", or "all".')
print(f"Host addresses - {hosts}")
@task
def install_docker(ctx):
"""Install Docker"""
print(f"Installing Docker on {ctx.host}")
ctx.sudo("apt-get update && apt-get install -qy docker.io")
ctx.run("docker --version")
ctx.sudo("systemctl enable docker.service")
@task
def disable_selinux_swap(ctx):
"""
Disable SELinux so kubernetes can communicate with other hosts
Disable Swap https://github.com/kubernetes/kubernetes/issues/53533
"""
ctx.sudo('sed -i "/ swap / s/^/#/" /etc/fstab')
ctx.sudo("swapoff -a")
@task
def install_kubernetes(ctx):
"""Install Kubernetes"""
print(f"Installing Kubernetes on {ctx.host}")
ctx.sudo("apt-get update && apt-get install -y apt-transport-https")
ctx.sudo(
"curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -"
)
ctx.sudo(
'echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | \
tee -a /etc/apt/sources.list.d/kubernetes.list && apt-get update'
)
ctx.sudo(
"apt-get update && apt-get install -y kubelet=1.21.1-00 kubeadm=1.21.1-00 kubectl=1.21.1-00"
)
ctx.sudo("apt-mark hold kubelet kubeadm kubectl")
@task
def provision_machines(ctx):
for conn in get_connections(hosts):
install_docker(conn)
disable_selinux_swap(conn)
install_kubernetes(conn)
def get_connections(hosts):
for host in hosts:
yield Connection(
f"{user}@{host}",
)
@task
def configure_master(ctx):
"""
Init Kubernetes
Set up the Kubernetes Config
Deploy flannel network to the cluster
"""
ctx.sudo("kubeadm init")
ctx.sudo("mkdir -p $HOME/.kube")
ctx.sudo("cp -i /etc/kubernetes/admin.conf $HOME/.kube/config")
ctx.sudo("chown $(id -u):$(id -g) $HOME/.kube/config")
ctx.sudo(
"kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml"
)
@task
def get_join_key(ctx):
sudo_command_res = ctx.sudo("kubeadm token create --print-join-command")
token = re.findall("^kubeadm.*$", str(sudo_command_res), re.MULTILINE)[0]
with open("join.txt", "w") as f:
with stdout_redirected(f):
print(token)
@contextmanager
def stdout_redirected(new_stdout):
save_stdout = sys.stdout
sys.stdout = new_stdout
try:
yield None
finally:
sys.stdout = save_stdout
@task
def create_cluster(ctx):
for conn in get_connections(hosts):
configure_master(conn)
get_join_key(conn)
@task
def configure_worker_node(ctx):
"""Join a worker to the cluster"""
with open("join.txt") as f:
join_command = f.readline()
for conn in get_connections(hosts):
conn.sudo(f"{join_command}")
@task
def get_nodes(ctx):
for conn in get_connections(hosts):
conn.sudo("kubectl get nodes")