提交 1cac7786 编写于 作者: P Priya Wadhwa

Use preload tarball for VMs

Since preloaded tarballs are container runtime specific, I added a Preload() func
to the cruntime interface. To use  it with VMs, the tarball is first
copied into the VM and then extracted. The leftover tarball is deleted,
and the container runtime (currently only docker is supported) is
restarted.
上级 34adb345
......@@ -308,3 +308,8 @@ func (r *Containerd) ContainerLogCmd(id string, len int, follow bool) string {
func (r *Containerd) SystemLogCmd(len int) string {
return fmt.Sprintf("sudo journalctl -u containerd -n %d", len)
}
// Preload preloads the container runtime with k8s images
func (r *Containerd) Preload(k8sVersion string) error {
return nil
}
......@@ -225,3 +225,8 @@ func (r *CRIO) ContainerLogCmd(id string, len int, follow bool) string {
func (r *CRIO) SystemLogCmd(len int) string {
return fmt.Sprintf("sudo journalctl -u crio -n %d", len)
}
// Preload preloads the container runtime with k8s images
func (r *CRIO) Preload(k8sVersion string) error {
return nil
}
......@@ -23,6 +23,7 @@ import (
"github.com/golang/glog"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/out"
)
......@@ -46,6 +47,8 @@ func (cs ContainerState) String() string {
// CommandRunner is the subset of command.Runner this package consumes
type CommandRunner interface {
RunCmd(cmd *exec.Cmd) (*command.RunResult, error)
// Copy is a convenience method that runs a command to copy a file
Copy(assets.CopyableFile) error
}
// Manager is a common interface for container runtimes
......@@ -94,6 +97,8 @@ type Manager interface {
ContainerLogCmd(string, int, bool) string
// SystemLogCmd returns the command to return the system logs
SystemLogCmd(int) string
// Preload preloads the container runtime with k8s images
Preload(string) error
}
// Config is runtime configuration
......
......@@ -19,11 +19,15 @@ package cruntime
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/golang/glog"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/out"
"k8s.io/minikube/pkg/minikube/preload"
)
// KubernetesContainerPrefix is the prefix of each kubernetes container
......@@ -93,6 +97,15 @@ func (r *Docker) Enable(disOthers bool) error {
return nil
}
// Restart restarts Docker on a host
func (r *Docker) Restart() error {
c := exec.Command("sudo", "systemctl", "restart", "docker")
if _, err := r.Runner.RunCmd(c); err != nil {
return errors.Wrap(err, "restarting docker.")
}
return nil
}
// Disable idempotently disables Docker on a host
func (r *Docker) Disable() error {
c := exec.Command("sudo", "systemctl", "stop", "docker", "docker.socket")
......@@ -252,3 +265,34 @@ func (r *Docker) ContainerLogCmd(id string, len int, follow bool) string {
func (r *Docker) SystemLogCmd(len int) string {
return fmt.Sprintf("sudo journalctl -u docker -n %d", len)
}
// Preload preloads docker with k8s images:
// 1. Copy over the preloaded tarball into the VM
// 2. Extract the preloaded tarball to the correct directory
// 3. Remove the tarball within the VM
func (r *Docker) Preload(k8sVersion string) error {
tarballPath := preload.TarballFilepath(k8sVersion)
dest := "/tmp/preloaded.tar"
// Copy over tarball into host
fa, err := assets.NewFileAsset(tarballPath, filepath.Dir(dest), filepath.Base(dest), "0644")
if err != nil {
return errors.Wrap(err, "getting file asset")
}
t := time.Now()
if err := r.Runner.Copy(fa); err != nil {
return errors.Wrap(err, "copying file")
}
glog.Infof("Took %f seconds to copy over tarball", time.Since(t).Seconds())
// extract the tarball to /var in the VM
if rr, err := r.Runner.RunCmd(exec.Command("sudo", "tar", "-I", "lz4", "-C", "/var", "-xvf", dest)); err != nil {
return errors.Wrapf(err, "extracting tarball: %s", rr.Output())
}
// remove the tarball in the VM
if rr, err := r.Runner.RunCmd(exec.Command("sudo", "rm", dest)); err != nil {
return errors.Wrapf(err, "removing tarball: %s", rr.Output())
}
return r.Restart()
}
......@@ -65,6 +65,9 @@ func configureRuntimes(runner cruntime.CommandRunner, drvName string, k8s config
if driver.BareMetal(drvName) {
disableOthers = false
}
if err := cr.Preload(k8s.KubernetesVersion); err != nil {
glog.Infof("Failed to preload container runtime %s: %v", cr.Name(), err)
}
err = cr.Enable(disableOthers)
if err != nil {
exit.WithError("Failed to enable container runtime", err)
......
......@@ -19,7 +19,6 @@ package node
import (
"os"
"github.com/golang/glog"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
"k8s.io/minikube/pkg/addons"
......@@ -65,12 +64,6 @@ func Start(mc config.ClusterConfig, n config.Node, primary bool, existingAddons
mRunner, preExists, machineAPI, host := startMachine(&mc, &n)
defer machineAPI.Close()
if !driver.IsKIC(driverName) {
if err := preload.CopyIntoVMAndExtract(mc, mRunner); err != nil {
glog.Infof("error extracting preloaded images to VM: %v", err)
}
}
// configure the runtime (docker, containerd, crio)
cr := configureRuntimes(mRunner, driverName, mc.KubernetesConfig)
showVersionInfo(k8sVersion, cr)
......
......@@ -23,10 +23,7 @@ import (
"io/ioutil"
"net/http"
"os"
"os/exec"
"path"
"path/filepath"
"time"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
......@@ -34,9 +31,6 @@ import (
"github.com/golang/glog"
"github.com/hashicorp/go-getter"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/command"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/localpath"
"k8s.io/minikube/pkg/minikube/out"
......@@ -163,40 +157,3 @@ func verifyChecksum(k8sVersion string) error {
}
return nil
}
// CopyIntoVMAndExtract tries to:
// 1. Copy over the preloaded tarball into the VM
// 2. Extract the preloaded tarball to the correct directory
// 3. Remove the tarball within the VM
func CopyIntoVMAndExtract(cc config.ClusterConfig, runner command.Runner) error {
k8sVersion := cc.KubernetesConfig.KubernetesVersion
tarballPath := TarballFilepath(k8sVersion)
dest := "/tmp/preloaded.tar"
// Copy over tarball into host
fa, err := assets.NewFileAsset(tarballPath, filepath.Dir(dest), filepath.Base(dest), "0644")
if err != nil {
return errors.Wrap(err, "getting file asset")
}
t := time.Now()
if err := runner.Copy(fa); err != nil {
return errors.Wrap(err, "copying file")
}
glog.Infof("Took %f seconds to copy over tarball", time.Since(t).Seconds())
// Now, extract the tarball
if rr, err := runner.RunCmd(exec.Command("sudo", "tar", "-I", "lz4", "-C", "/var", "-xvf", dest)); err != nil {
return errors.Wrapf(err, "extracting tarball: %s", rr.Output())
}
// remove the tarball on the host
if rr, err := runner.RunCmd(exec.Command("sudo", "rm", dest)); err != nil {
return errors.Wrapf(err, "removing tarball: %s", rr.Output())
}
// restart the docker daemon
if rr, err := runner.RunCmd(exec.Command("sudo", "systemctl", "restart", "docker")); err != nil {
return errors.Wrapf(err, "removing tarball: %s", rr.Output())
}
return fmt.Errorf("error extracting preloaded tarball")
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册