-
Notifications
You must be signed in to change notification settings - Fork 20
/
kube_create.go
555 lines (457 loc) · 14.2 KB
/
kube_create.go
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package cmd
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"strings"
"text/template"
"time"
"github.com/blang/semver"
"github.com/cenkalti/backoff"
"github.com/exoscale/egoscale"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh"
)
const (
// kubeDockerVersion is the version installed on Ubuntu Xenial
kubeDockerVersion = "18.06"
// kubeCalicoVersion is the version of Calico installed
kubeCalicoVersion = "3.4"
// kubeDefaultTemplate is the template to install Kubernetes on.
//
// Using xenial means cfssl must be installed by other means
kubeDefaultTemplate = defaultTemplate
)
// kubeCreateDebug represents a debug mode flag
var kubeCreateDebug bool
// kubeBootstrapStep represents a k8s instance bootstrap step
type kubeBootstrapStep struct {
name string
command string
}
type kubeCluster struct {
Name string
KubernetesVersion string
DockerVersion string
CalicoVersion string
Address string
}
// kubeBootstrapSteps represents a k8s instance bootstrap steps
var kubeBootstrapSteps = []kubeBootstrapStep{
{
name: "Instance system upgrade",
command: `\
set -xe
sudo -E DEBIAN_FRONTEND=noninteractive apt-get update
sudo -E DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
sudo -E DEBIAN_FRONTEND=noninteractive apt-get install -y \
apt-transport-https \
ca-certificates \
curl \
golang-cfssl \
software-properties-common
nohup sh -c 'sleep 5s ; sudo reboot' &
exit`,
},
{
name: "Docker Engine installation",
command: `\
set -xe
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo -E DEBIAN_FRONTEND=noninteractive apt-get update
PKG_VERSION=$(apt-cache madison docker-ce | awk '$3 ~ /{{ .DockerVersion }}/ { print $3 }' | sort -t : -k 2 -n | tail -n 1)
if [[ -z "${PKG_VERSION}" ]]; then
echo "error: unable to find docker-ce package for version {{ .DockerVersion }}" >&2
exit 1
fi
sudo -E DEBIAN_FRONTEND=noninteractive apt-get install -y docker-ce=${PKG_VERSION}
sudo apt-mark hold docker-ce
cat <<EOF > csr.json
{
"hosts": ["{{ .Address }}"],
"key": {"algo": "rsa", "size": 2048},
"names": [{"C": "CH", "L": "Lausanne", "O": "Exoscale", "OU": "exokube", "ST": ""}]
}
EOF
cfssl genkey -initca csr.json | cfssljson -bare ca
cfssl gencert \
-ca ca.pem \
-ca-key ca-key.pem \
-hostname {{ .Address }} csr.json | cfssljson -bare
cat <<EOF | sudo tee /etc/docker/daemon.json
{
"hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2376"],
"tlsverify": true,
"tlscacert": "/etc/docker/ca.pem",
"tlscert": "/etc/docker/cert.pem",
"tlskey": "/etc/docker/key.pem",
"exec-opts": ["native.cgroupdriver=systemd"],
"storage-driver": "overlay2",
"log-driver": "json-file",
"log-opts": {
"max-size": "100m"
}
}
EOF
sudo mv ca.pem /etc/docker/ca.pem
sudo mv cert.pem /etc/docker/cert.pem
sudo mv cert-key.pem /etc/docker/key.pem
rm -f *.{csr,json,pem}
sudo mkdir /etc/systemd/system/docker.service.d/
cat <<EOF | sudo tee /etc/systemd/system/docker.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd
EOF
sudo systemctl daemon-reload \
&& sudo systemctl restart docker`,
},
{
name: "Kubernetes cluster node installation", command: `\
set -xe
curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
sudo -E DEBIAN_FRONTEND=noninteractive apt-get update
PKG_VERSION=$(apt-cache madison kubelet | awk '$3 ~ /{{ .KubernetesVersion }}-/ { print $3 }' | sort -t "-" -k 2 -n | tail -n 1)
if [[ -z "${PKG_VERSION}" ]]; then
echo "error: unable to find kubelet package for version {{ .KubernetesVersion }}" >&2
exit 1
fi
sudo -E DEBIAN_FRONTEND=noninteractive apt-get install -y kubelet=${PKG_VERSION} \
kubeadm=${PKG_VERSION} \
kubectl=${PKG_VERSION}
sudo apt-mark hold kubelet kubeadm kubectl`,
}, {
name: "Kubernetes cluster node initialization",
command: `\
set -xe
sudo kubeadm init \
--pod-network-cidr=192.168.0.0/16 \
--kubernetes-version "{{ .KubernetesVersion }}"
sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf taint nodes --all node-role.kubernetes.io/master-
sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf apply \
-f https://docs.projectcalico.org/v{{ .CalicoVersion }}/getting-started/kubernetes/installation/hosted/etcd.yaml
sudo kubectl --kubeconfig=/etc/kubernetes/admin.conf apply \
-f https://docs.projectcalico.org/v{{ .CalicoVersion }}/getting-started/kubernetes/installation/hosted/calico.yaml`,
},
}
// kubeCreateCmd represents the create command
var kubeCreateCmd = &cobra.Command{
Use: "create <cluster name>",
Short: "Create and configure a standalone Kubernetes cluster",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
clusterName := args[0]
vm, err := getVirtualMachineByNameOrID(clusterName)
if err != nil {
// If the cluster instance doesn't exist (expected case) we receive a well deserved
// "431 not found" error, but check if we got any other error just in case...
if csError, ok := err.(*egoscale.ErrorResponse); ok && csError.ErrorCode != egoscale.ParamError {
return err
}
}
if vm != nil {
return fmt.Errorf("cluster instance %q already exists", clusterName)
}
kubeCreateDebug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
requestedKubernetesVersion, err := cmd.Flags().GetString("version")
if err != nil {
return err
}
kubernetesVersion, err := fetchKubernetesVersion(requestedKubernetesVersion)
if err != nil {
return err
}
sizeOpt, err := cmd.Flags().GetString("size")
if err != nil {
return err
}
size, err := getServiceOfferingByName(sizeOpt)
if err != nil {
return err
}
zone, err := getZoneIDByName(gCurrentAccount.DefaultZone)
if err != nil {
return err
}
sg, err := createExokubeSecurityGroup()
if err != nil {
return err
}
template, err := getTemplateByName(zone, kubeDefaultTemplate)
if err != nil {
return err
}
userName, ok := template.Details["username"]
if !ok {
return fmt.Errorf("cannot fetch username from template %q", defaultTemplate)
}
r, errs := createVM([]egoscale.DeployVirtualMachine{{
Name: clusterName,
ZoneID: zone,
ServiceOfferingID: size.ID,
TemplateID: template.ID,
RootDiskSize: 10,
SecurityGroupIDs: []egoscale.UUID{*sg.ID},
}})
if len(errs) > 0 {
return errs[0]
}
vm = &r[0]
if _, err := cs.Request(egoscale.CreateTags{
ResourceType: vm.ResourceType(),
ResourceIDs: []egoscale.UUID{*vm.ID},
Tags: []egoscale.ResourceTag{
{Key: "managedby", Value: "exokube"},
{Key: kubeTagName, Value: kubernetesVersion},
},
}); err != nil {
return fmt.Errorf("unable to tag cluster instance: %s", err)
}
if err := saveKubeData(clusterName, "instance", []byte(vm.ID.String())); err != nil {
return fmt.Errorf("unable to write Kubernetes configuration file: %s", err)
}
fmt.Println("Bootstrapping Kubernetes cluster (can take up to several minutes):")
sshClient, err := newSSHClient(
vm.IP().String(),
userName,
getKeyPairPath(vm.ID.String()),
)
if err != nil {
return fmt.Errorf("unable to initialize SSH client: %s", err)
}
if err := bootstrapExokubeCluster(sshClient, kubeCluster{
Name: clusterName,
KubernetesVersion: kubernetesVersion,
CalicoVersion: kubeCalicoVersion,
DockerVersion: kubeDockerVersion,
Address: vm.IP().String(),
}, kubeCreateDebug); err != nil {
return fmt.Errorf("cluster bootstrap failed: %s", err)
}
fmt.Printf(`
Your Kubernetes cluster is ready. What do now?
1. Install the "kubectl" command, if you don't have it already:
https://kubernetes.io/docs/tasks/tools/install-kubectl/
2. Execute the following command:
eval $(exo lab kube env "%s")
You might want to persist this change by adding it to your shell startup
configuration (e.g. ~/.bashrc, ~/.zshrc).
3. Check that your cluster is reachable:
kubectl cluster-info
4. When you're done with your cluster, you can either:
* stop it using the "exo lab kube stop" command
* restart it later using the "exo lab kube start" command
* delete it permanently using the "exo lab kube delete" command
`,
clusterName)
return nil
},
}
// createExokubeSecurityGroup creates the firewall security group to put kube VM instances into, or returns it if it
// already exists.
func createExokubeSecurityGroup() (*egoscale.SecurityGroup, error) {
sg, err := getSecurityGroupByNameOrID(kubeSecurityGroup)
if err == nil {
return sg, err
}
r, ok := err.(*egoscale.ErrorResponse)
if !ok || r.ErrorCode != egoscale.ParamError {
return nil, err
}
resp, err := cs.RequestWithContext(gContext, &egoscale.CreateSecurityGroup{
Name: kubeSecurityGroup,
Description: "Created by exo CLI",
})
if err != nil {
return nil, err
}
sg = resp.(*egoscale.SecurityGroup)
cidrList := []egoscale.CIDR{
*egoscale.MustParseCIDR("0.0.0.0/0"),
*egoscale.MustParseCIDR("::/0"),
}
sgRules := []egoscale.AuthorizeSecurityGroupIngress{{
Description: "SSH",
Protocol: "TCP",
StartPort: 22,
EndPort: 22,
}, {
Description: "Docker API",
Protocol: "TCP",
StartPort: 2376,
EndPort: 2376,
}, {
Description: "Kubernetes API",
Protocol: "TCP",
StartPort: 6443,
EndPort: 6443,
}}
for _, rule := range sgRules {
rule.SecurityGroupID = sg.ID
rule.CIDRList = cidrList
if _, err = cs.RequestWithContext(gContext, rule); err != nil {
return nil, err
}
}
return sg, nil
}
func bootstrapExokubeCluster(sshClient *sshClient, cluster kubeCluster, debug bool) error {
for _, step := range kubeBootstrapSteps {
var (
stdout, stderr io.Writer
cmd bytes.Buffer
errBuf bytes.Buffer
)
stderr = &errBuf
if debug {
stdout = os.Stderr
stderr = os.Stderr
}
err := template.Must(template.New("command").Parse(step.command)).Execute(&cmd, cluster)
if err != nil {
return fmt.Errorf("template error: %s", err)
}
fmt.Printf("%s... ", step.name)
if err := sshClient.runCommand(cmd.String(), stdout, stderr); err != nil {
fmt.Printf("\n%s: failed\n", step.name)
if errBuf.Len() > 0 {
fmt.Println(errBuf.String())
}
return err
}
fmt.Printf("success!\n")
}
for _, file := range []string{"ca.pem", "cert.pem", "key.pem"} {
err := sshClient.scp("/etc/docker/"+file, path.Join(getKubeconfigPath(cluster.Name), "docker", file))
if err != nil {
return fmt.Errorf("unable to retrieve Docker host file %q: %s", file, err)
}
}
err := sshClient.scp("/etc/kubernetes/admin.conf", path.Join(getKubeconfigPath(cluster.Name), "kubeconfig"))
if err != nil {
return fmt.Errorf("unable to retrieve Kubernetes cluster configuration: %s", err)
}
return nil
}
type sshClient struct {
host string
hostKey ssh.Signer
user string
c *ssh.Client
}
func newSSHClient(host, hostUser, keyFile string) (*sshClient, error) {
var c = sshClient{
host: host + ":22",
user: hostUser,
}
key, err := ioutil.ReadFile(keyFile)
if err != nil {
return nil, fmt.Errorf("unable to read cluster instance SSH private key: %s", err)
}
if c.hostKey, err = ssh.ParsePrivateKey(key); err != nil {
return nil, fmt.Errorf("unable to parse cluster instance SSH private key: %s", err)
}
return &c, nil
}
func (c *sshClient) runCommand(cmd string, stdout, stderr io.Writer) error {
var err error
retryOp := func() error {
if c.c, err = ssh.Dial("tcp", c.host, &ssh.ClientConfig{
User: c.user,
Auth: []ssh.AuthMethod{ssh.PublicKeys(c.hostKey)},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}); err != nil {
return fmt.Errorf("unable to connect to cluster instance: %s", err)
}
sshSession, err := c.c.NewSession()
if err != nil {
return fmt.Errorf("unable to create SSH session: %s", err)
}
defer sshSession.Close()
sshSession.Stdout = stdout
sshSession.Stderr = stderr
if err := sshSession.Run(cmd); err != nil {
return err
}
return nil
}
if err = backoff.RetryNotify(
retryOp,
backoff.WithMaxRetries(backoff.NewConstantBackOff(10*time.Second), 6),
func(_ error, d time.Duration) {
if kubeCreateDebug {
fmt.Printf("! Cluster instance not ready yet, retrying in %s...\n", d)
}
}); err != nil {
return err
}
return nil
}
func (c *sshClient) scp(src, dst string) error {
var buf bytes.Buffer
if err := c.runCommand(fmt.Sprintf("sudo cat %s", src), &buf, nil); err != nil {
return err
}
if _, err := os.Stat(path.Dir(dst)); os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(dst), os.ModePerm); err != nil {
return fmt.Errorf("unable to create directory %q: %s", path.Dir(dst), err)
}
}
return ioutil.WriteFile(dst, buf.Bytes(), 0600)
}
// fetchKubernetesVersion fetches the latest stable version from the official
// release source, or if a specific version is requested checks if it actually
// exists.
//
// https://godoc.org/github.com/kubernetes/kubernetes/cmd/kubeadm/app/util#KubernetesReleaseVersion
func fetchKubernetesVersion(requested string) (string, error) {
if requested != "" {
r, err := http.Head(fmt.Sprintf("https://dl.k8s.io/release/v%s/bin/linux/amd64/kubectl", requested))
if err != nil {
return "", err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return "", fmt.Errorf("unable to find Kubernetes release for version %q", requested)
}
return requested, nil
}
r, err := http.Get("https://dl.k8s.io/release/stable.txt")
if err != nil {
return "", err
}
defer r.Body.Close()
if r.StatusCode != http.StatusOK {
return "", fmt.Errorf("unable to find Kubernetes release for stable version")
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return "", err
}
v := strings.TrimSuffix(strings.TrimPrefix(string(b), "v"), "\n")
_, err = semver.Parse(v)
return v, err
}
func init() {
kubeCreateCmd.PersistentFlags().BoolVarP(&kubeCreateDebug, "debug", "d", false, "debug mode on")
kubeCreateCmd.Flags().StringP("size", "s", "medium", "<name | id> "+
"(micro|tiny|small|medium|large|extra-large|huge|mega|titan|jumbo)")
kubeCreateCmd.Flags().StringP("version", "v", "", "install a specific Kubernetes version")
kubeCmd.AddCommand(kubeCreateCmd)
}