mirror of
https://github.com/rancher/rke.git
synced 2025-08-12 12:13:25 +00:00
Start containers if stopped and use net=host for etcd
This commit is contained in:
parent
3bfa9bd49b
commit
d7ffdc04b5
@ -22,11 +22,26 @@ var K8sDockerVersions = map[string][]string{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName string, hostname string, plane string) error {
|
func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *container.Config, hostCfg *container.HostConfig, containerName string, hostname string, plane string) error {
|
||||||
isRunning, err := IsContainerRunning(ctx, dClient, hostname, containerName, false)
|
container, err := dClient.ContainerInspect(ctx, containerName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if !client.IsErrNotFound(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if isRunning {
|
if err := UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
resp, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||||
|
}
|
||||||
|
if err := dClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
||||||
|
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||||
|
}
|
||||||
|
log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// Check for upgrades
|
||||||
|
if container.State.Running {
|
||||||
log.Infof(ctx, "[%s] Container [%s] is already running on host [%s]", plane, containerName, hostname)
|
log.Infof(ctx, "[%s] Container [%s] is already running on host [%s]", plane, containerName, hostname)
|
||||||
isUpgradable, err := IsContainerUpgradable(ctx, dClient, imageCfg, containerName, hostname, plane)
|
isUpgradable, err := IsContainerUpgradable(ctx, dClient, imageCfg, containerName, hostname, plane)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -38,18 +53,11 @@ func DoRunContainer(ctx context.Context, dClient *client.Client, imageCfg *conta
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
err = UseLocalOrPull(ctx, dClient, hostname, imageCfg.Image, plane)
|
// start if not running
|
||||||
if err != nil {
|
log.Infof(ctx, "[%s] Starting stopped container [%s] on host [%s]", plane, containerName, hostname)
|
||||||
return err
|
if err := dClient.ContainerStart(ctx, container.ID, types.ContainerStartOptions{}); err != nil {
|
||||||
}
|
|
||||||
resp, err := dClient.ContainerCreate(ctx, imageCfg, hostCfg, nil, containerName)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to create [%s] container on host [%s]: %v", containerName, hostname, err)
|
|
||||||
}
|
|
||||||
if err := dClient.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
|
|
||||||
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
return fmt.Errorf("Failed to start [%s] container on host [%s]: %v", containerName, hostname, err)
|
||||||
}
|
}
|
||||||
logrus.Debugf("[%s] Successfully started [%s] container: [%s]", plane, containerName, resp.ID)
|
|
||||||
log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname)
|
log.Infof(ctx, "[%s] Successfully started [%s] container on host [%s]", plane, containerName, hostname)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
etcdclient "github.com/coreos/etcd/client"
|
etcdclient "github.com/coreos/etcd/client"
|
||||||
"github.com/docker/docker/api/types/container"
|
"github.com/docker/docker/api/types/container"
|
||||||
"github.com/docker/go-connections/nat"
|
|
||||||
"github.com/rancher/rke/docker"
|
"github.com/rancher/rke/docker"
|
||||||
"github.com/rancher/rke/hosts"
|
"github.com/rancher/rke/hosts"
|
||||||
"github.com/rancher/rke/log"
|
"github.com/rancher/rke/log"
|
||||||
@ -64,20 +63,7 @@ func buildEtcdConfig(host *hosts.Host, etcdService v3.ETCDService, initCluster s
|
|||||||
RestartPolicy: container.RestartPolicy{Name: "always"},
|
RestartPolicy: container.RestartPolicy{Name: "always"},
|
||||||
Binds: []string{
|
Binds: []string{
|
||||||
"/var/lib/etcd:/etcd-data"},
|
"/var/lib/etcd:/etcd-data"},
|
||||||
PortBindings: nat.PortMap{
|
NetworkMode: "host",
|
||||||
"2379/tcp": []nat.PortBinding{
|
|
||||||
{
|
|
||||||
HostIP: "0.0.0.0",
|
|
||||||
HostPort: "2379",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"2380/tcp": []nat.PortBinding{
|
|
||||||
{
|
|
||||||
HostIP: "0.0.0.0",
|
|
||||||
HostPort: "2380",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
for arg, value := range etcdService.ExtraArgs {
|
for arg, value := range etcdService.ExtraArgs {
|
||||||
cmd := fmt.Sprintf("--%s=%s", arg, value)
|
cmd := fmt.Sprintf("--%s=%s", arg, value)
|
||||||
|
Loading…
Reference in New Issue
Block a user