Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 532d373

Browse files
committed
etcd: add self hosted etcd mode
- add a feature flag that will enable self hosted etcd - on self hosted etcd mode, create a etcd static pod have bootkube wait until it's up
1 parent 3326051 commit 532d373

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

cmd/bootkube/start.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/kubernetes-incubator/bootkube/pkg/bootkube"
1212
"github.com/kubernetes-incubator/bootkube/pkg/util"
13+
"github.com/kubernetes-incubator/bootkube/pkg/util/etcdutil"
1314
)
1415

1516
var (
@@ -23,15 +24,17 @@ var (
2324
}
2425

2526
startOpts struct {
26-
assetDir string
27-
etcdServer string
27+
assetDir string
28+
etcdServer string
29+
selfHostedEtcd bool
2830
}
2931
)
3032

3133
func init() {
3234
cmdRoot.AddCommand(cmdStart)
3335
cmdStart.Flags().StringVar(&startOpts.etcdServer, "etcd-server", "http://127.0.0.1:2379", "Single etcd node to use during bootkube bootstrap process.")
3436
cmdStart.Flags().StringVar(&startOpts.assetDir, "asset-dir", "", "Path to the cluster asset directory. Expected layout genereted by the `bootkube render` command.")
37+
cmdStart.Flags().BoolVar(&startOpts.selfHostedEtcd, "experimental-self-hosted-etcd", false, "Self hosted etcd mode. Includes starting the initial etcd member by bootkube.")
3538
}
3639

3740
func runCmdStart(cmd *cobra.Command, args []string) error {
@@ -40,6 +43,13 @@ func runCmdStart(cmd *cobra.Command, args []string) error {
4043
return fmt.Errorf("Invalid etcd etcdServer %q: %v", startOpts.etcdServer, err)
4144
}
4245

46+
// TODO: this should likely move into bootkube.Run() eventually.
47+
if startOpts.selfHostedEtcd {
48+
if err := etcdutil.StartEtcd(); err != nil {
49+
return fmt.Errorf("fail to start etcd: %v", err)
50+
}
51+
}
52+
4353
bk, err := bootkube.NewBootkube(bootkube.Config{
4454
AssetDir: startOpts.assetDir,
4555
EtcdServer: etcdServer,

pkg/util/etcdutil/start_etcd.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package etcdutil
2+
3+
import (
4+
"fmt"
5+
"io/ioutil"
6+
"net/http"
7+
"time"
8+
9+
"github.com/golang/glog"
10+
)
11+
12+
func StartEtcd() error {
13+
if err := ioutil.WriteFile("/etc/kubernetes/manifests/boot-etcd.yaml", []byte(etcdPodYaml), 0600); err != nil {
14+
return fmt.Errorf("fail to write file '/etc/kubernetes/manifests/boot-etcd.yaml': %v", err)
15+
}
16+
glog.Info("etcd server has been defined to run by kubelet. Please wait...")
17+
return waitEtcdUp()
18+
}
19+
20+
func waitEtcdUp() error {
21+
httpcli := &http.Client{
22+
Timeout: 10 * time.Second,
23+
}
24+
for {
25+
_, err := httpcli.Get("http://127.0.0.1:2379/version")
26+
if err != nil {
27+
glog.Infof("couldn't talk to etcd server (retrying 10s later): %v\n", err)
28+
time.Sleep(10 * time.Second)
29+
continue
30+
}
31+
break
32+
}
33+
return nil
34+
}
35+
36+
var etcdPodYaml = `apiVersion: v1
37+
kind: Pod
38+
metadata:
39+
name: boot-etcd
40+
namespace: kube-system
41+
spec:
42+
containers:
43+
- command:
44+
- /bin/sh
45+
- -c
46+
- /usr/local/bin/etcd
47+
--name boot-etcd
48+
--listen-client-urls=http://0.0.0.0:2379
49+
--listen-peer-urls=http://0.0.0.0:2380
50+
--advertise-client-urls=http://$(MY_POD_IP):2379
51+
--initial-advertise-peer-urls http://$(MY_POD_IP):2380
52+
--initial-cluster boot-etcd=http://$(MY_POD_IP):2380
53+
--initial-cluster-token bootkube
54+
--initial-cluster-state new
55+
--data-dir=/var/etcd/data
56+
env:
57+
- name: MY_POD_IP
58+
valueFrom:
59+
fieldRef:
60+
fieldPath: status.podIP
61+
image: quay.io/coreos/etcd:v3.1.0-alpha.1
62+
name: etcd
63+
hostNetwork: true
64+
`

0 commit comments

Comments
 (0)