1
0
mirror of https://github.com/rancher/rke.git synced 2025-07-23 11:32:12 +00:00
rke/services/etcd.go

74 lines
2.0 KiB
Go
Raw Normal View History

2017-10-29 09:45:21 +00:00
package services
import (
"github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types/container"
"github.com/docker/go-connections/nat"
"github.com/rancher/rke/docker"
2017-10-29 09:45:21 +00:00
"github.com/rancher/rke/hosts"
)
type Etcd struct {
Version string `yaml:"version"`
Image string `yaml:"image"`
}
func RunEtcdPlane(etcdHosts []hosts.Host, etcdService Etcd) error {
logrus.Infof("[%s] Building up Etcd Plane..", ETCDRole)
2017-10-29 09:45:21 +00:00
for _, host := range etcdHosts {
imageCfg, hostCfg := buildEtcdConfig(host, etcdService)
err := docker.DoRunContainer(imageCfg, hostCfg, EtcdContainerName, &host, ETCDRole)
2017-10-29 09:45:21 +00:00
if err != nil {
return err
}
}
logrus.Infof("[%s] Successfully started Etcd Plane..", ETCDRole)
2017-10-29 09:45:21 +00:00
return nil
}
func buildEtcdConfig(host hosts.Host, etcdService Etcd) (*container.Config, *container.HostConfig) {
2017-10-29 09:45:21 +00:00
imageCfg := &container.Config{
Image: etcdService.Image + ":" + etcdService.Version,
Cmd: []string{"/usr/local/bin/etcd",
"--name=etcd-" + host.Hostname,
"--data-dir=/etcd-data",
"--advertise-client-urls=http://" + host.ControlPlaneIP + ":2379,http://" + host.ControlPlaneIP + ":4001",
2017-10-29 09:45:21 +00:00
"--listen-client-urls=http://0.0.0.0:2379",
"--initial-advertise-peer-urls=http://" + host.ControlPlaneIP + ":2380",
2017-10-29 09:45:21 +00:00
"--listen-peer-urls=http://0.0.0.0:2380",
"--initial-cluster-token=etcd-cluster-1",
"--initial-cluster=etcd-" + host.Hostname + "=http://" + host.ControlPlaneIP + ":2380"},
2017-10-29 09:45:21 +00:00
}
hostCfg := &container.HostConfig{
RestartPolicy: container.RestartPolicy{Name: "always"},
Binds: []string{
"/var/lib/etcd:/etcd-data"},
PortBindings: nat.PortMap{
"2379/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "2379",
},
},
"2380/tcp": []nat.PortBinding{
{
HostIP: "0.0.0.0",
HostPort: "2380",
},
},
},
}
return imageCfg, hostCfg
2017-10-29 09:45:21 +00:00
}
func getEtcdConnString(hosts []hosts.Host) string {
connString := ""
for i, host := range hosts {
connString += "http://" + host.ControlPlaneIP + ":2379"
2017-10-29 09:45:21 +00:00
if i < (len(hosts) - 1) {
connString += ","
}
}
return connString
}