-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreate-cluster.sh
More file actions
executable file
·190 lines (162 loc) · 4.68 KB
/
create-cluster.sh
File metadata and controls
executable file
·190 lines (162 loc) · 4.68 KB
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
#!/bin/bash
# Source configuration file
[[ -f "cluster.conf" ]] && source "cluster.conf"
# Configuration
MASTER_COUNT=1
WORKER_COUNT=${WORKER_COUNT:-2}
MASTER_IP_NW=${MASTER_IP_NW:-"192.168.1.0/24"}
MASTER_IP=${MASTER_IP:-"192.168.1.10"}
WORKER_IP=${WORKER_IP:-"192.168.1.#{i + 10}"}
NFS_IP=${NFS_IP:-"192.168.1.8"}
create_ansible_inventory_file() {
# Create hosts file
echo "[masters]" > hosts
echo "master" >> hosts
echo "[workers]" >> hosts
for i in `seq 1 $WORKER_COUNT`
do
echo "worker$i" >> hosts
done
echo "[nfs_servers]" >> hosts
echo "nfs" >> hosts
}
create_vagrantfile() {
cat > Vagrantfile <<EOF
BOX="ubuntu/xenial64"
MASTER_COUNT=$MASTER_COUNT
WORKER_COUNT=$WORKER_COUNT
MASTER_IP="$MASTER_IP"
NFS_IP="$NFS_IP"
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.provider "virtualbox" do |v|
v.memory = 2048
v.cpus = 2
end
config.vm.define "master" do |subconfig|
subconfig.vm.box = BOX
subconfig.vm.hostname = "master"
subconfig.vm.network :public_network, ip: "$MASTER_IP", bridge: "$BRIDGE_IF"
end
# default router
config.vm.provision "shell",
run: "always",
inline: "route add default gw $BRIDGE_IF"
# default router ipv6
config.vm.provision "shell",
run: "always",
inline: "route -A inet6 add default gw fc00::1 eth1"
# delete default gw added by vagrant on 10.0.2.2
config.vm.provision "shell",
run: "always",
inline: "route del default gw 10.0.2.2"
config.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y python
SHELL
(1..WORKER_COUNT).each do |i|
config.vm.define "worker#{i}" do |subconfig|
subconfig.vm.box = BOX
subconfig.vm.hostname = "worker#{i}"
subconfig.vm.network :public_network, ip: "$WORKER_IP", bridge: "$BRIDGE_IF"
subconfig.vm.provision "shell", inline: <<-SHELL
apt-get update
apt-get install -y python
SHELL
end
end
config.vm.define "nfs" do |subconfig|
subconfig.vm.box = BOX
subconfig.vm.hostname = "nfs"
subconfig.vm.network :public_network, ip: "$NFS_IP", bridge: "$BRIDGE_IF"
subconfig.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", 4096]
vb.customize ["modifyvm", :id, "--cpus", 2]
end
end
end
EOF
}
create_nfs_client_provisioner_deploy() {
cat > deploy-nfs-cp.yml <<EOF
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-client-provisioner
spec:
replicas: 1
selector:
matchLabels:
app: nfs-client-provisioner
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: quay.io/external_storage/nfs-client-provisioner:latest
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: mynfs
- name: NFS_SERVER
value: $NFS_IP
- name: NFS_PATH
value: /nfs
volumes:
- name: nfs-client-root
nfs:
server: $NFS_IP
path: /nfs
EOF
}
create_vms() {
# Create virtual machines
vagrant up
vagrant ssh-config > ssh_config
}
configure_master() {
# Run playbooks to setup cluster
ansible-playbook kmaster.yml --extra-vars "master_ip_nw=$MASTER_IP_NW"
# Copy cluster config to local
ansible --verbose masters -m fetch -b -a "src=/etc/kubernetes/admin.conf dest=./ flat=true"
}
configure_network() {
# Create overlay network
kubectl apply -f kube-flannel.yml
}
configure_workers() {
# Run worker and NFS playbooks
ansible-playbook kworker.yml
# Join all workers to cluster
JOIN_CMD=$(ansible masters -m shell -b -a "kubeadm token create --print-join-command 2>/dev/null"|awk '{sub(/.*>>/, "");print}')
ansible workers -m shell -b -a "$JOIN_CMD"
}
configure_nfs() {
ansible-playbook nfs.yml
# Setup kubectl configuration
export KUBECONFIG=admin.conf
# Configure NFS Client provisioner
kubectl apply -f rbac-nfs-cp.yml
kubectl apply -f sc-nfs-cp.yml
kubectl apply -f deploy-nfs-cp.yml
}
create_ansible_inventory_file
create_vagrantfile
create_nfs_client_provisioner_deploy
create_vms
configure_master
configure_network
configure_workers
configure_nfs
exit 0