From 05fdaaef84a94f4018bae821fffcdd75e633648d Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Wed, 31 Jul 2019 13:51:51 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 56 ++++++++++++-- install/command/command.go | 2 + install/command/containerd.go | 133 +++++++++++++++++++++++++++------- install/command/docker.go | 35 +++++---- 4 files changed, 177 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 591f619..c517234 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ # container-install -### Install +### Install Docker install container from url: location file: ```shell script container-install install - --docker true + --docker T --host 172.16.213.131 --host 172.16.213.132 --user root @@ -18,7 +18,7 @@ install container from url: remote url: ```shell script container-install install - --docker true + --docker T --host 172.16.213.131 --host 172.16.213.132 --user root @@ -28,11 +28,41 @@ install container from url: --lib /var/lib/docker --pkg-url https://download.docker.com/linux/static/stable/x86_64/docker-18.09.4.tgz ``` -### UnInstall + +### Install Containerd +install container from url: + location file: + ```shell script + container-install install + --docker F + --host 172.16.213.131 + --host 172.16.213.132 + --user root + --passwd admin + --registry 127.0.0.1 + --registry 127.0.0.2 + --lib /var/lib/containerd + --pkg-url /root/containerd-1.2.7.tgz +``` + remote url: + ```shell script + container-install install + --docker F + --host 172.16.213.131 + --host 172.16.213.132 + --user root + --passwd admin + --registry 127.0.0.1 + --registry 127.0.0.2 + --lib /var/lib/containerd + --pkg-url https://github.com/containerd/containerd/releases/download/v1.2.7/containerd-1.2.7.linux-amd64.tar.gz +``` + +### UnInstall Docker uninstall container: ```shell script container-install uninstall - --docker true + --docker T --host 172.16.213.131 --host 172.16.213.132 --user root @@ -40,10 +70,22 @@ uninstall container: --docker-lib /var/lib/docker ``` +### UnInstall Containerd +uninstall container: + ```shell script + container-install uninstall + --docker F + --host 172.16.213.131 + --host 172.16.213.132 + --user root + --passwd admin + --docker-lib /var/lib/containerd +``` + ### Print Download Url print download url for docker: ```shell script - container-install print --docker true + container-install print --docker T ``` the docker Newest version is v19.03.0. @@ -82,7 +124,7 @@ https://download.docker.com/linux/static/stable/x86_64/docker-19.03.0.tgz the containerd Newest version is v1.2.7 ex: ```shell script -cuisongliu@cuisongliu-PC:~$ go run main.go print --docker false +cuisongliu@cuisongliu-PC:~$ go run main.go print --docker F https://github.com/containerd/containerd/releases/download/v1.1.0/containerd-1.1.0.linux-amd64.tar.gz https://github.com/containerd/containerd/releases/download/v1.1.1/containerd-1.1.1.linux-amd64.tar.gz https://github.com/containerd/containerd/releases/download/v1.1.2/containerd-1.1.2.linux-amd64.tar.gz diff --git a/install/command/command.go b/install/command/command.go index 8eb785c..5228cd4 100644 --- a/install/command/command.go +++ b/install/command/command.go @@ -17,7 +17,9 @@ type stepInterface interface { Version(host string) Uninstall(host string) Print() + // + lib() string serviceFile() []byte configFile() []byte } diff --git a/install/command/containerd.go b/install/command/containerd.go index d3595d1..c0f8f97 100644 --- a/install/command/containerd.go +++ b/install/command/containerd.go @@ -17,51 +17,59 @@ func NewContainerd() stepInterface { const containerdFileName = "containerd.tgz" -func (d Containerd) SendPackage(host string) { +func (d *Containerd) lib() string { + if Lib == "" { + return "/var/lib/containerd" + } else { + return Lib + } +} +func (d *Containerd) SendPackage(host string) { SendPackage(host, PkgUrl, containerdFileName) } -func (d Containerd) Tar(host string) { +func (d *Containerd) Tar(host string) { cmd := fmt.Sprintf("tar --strip-components=1 -xvzf /root/%s -C /usr/local/bin", containerdFileName) Cmd(host, cmd) } - -func (d Containerd) Config(host string) { - cmd := "mkdir -p " + Lib +func (d *Containerd) Config(host string) { + cmd := "mkdir -p " + d.lib() Cmd(host, cmd) cmd = "mkdir -p /etc/containerd" Cmd(host, cmd) - cmd = "containerd config default > /etc/containerd/config.toml" - //cmd = "echo \"" + string(d.configFile()) + "\" > /etc/docker/daemon.json" + //cmd = "containerd config default > /etc/containerd/config.toml" + cmd = "echo \"" + string(d.configFile()) + "\" > /etc/containerd/config.toml" Cmd(host, cmd) } -func (d Containerd) Enable(host string) { +func (d *Containerd) Enable(host string) { cmd := "echo \"" + string(d.serviceFile()) + "\" > /usr/lib/systemd/system/containerd.service" Cmd(host, cmd) cmd = "systemctl enable containerd.service && systemctl restart containerd.service" Cmd(host, cmd) } -func (d Containerd) Version(host string) { +func (d *Containerd) Version(host string) { cmd := "containerd --version" Cmd(host, cmd) + logger.Warn("pull docker hub command. ex: ctr images pull docker.io/library/alpine:3.8") + logger.Warn("pull http registry command. ex: ctr images pull 10.0.45.222/library/alpine:3.8 --plain-http") } -func (d Containerd) Uninstall(host string) { +func (d *Containerd) Uninstall(host string) { cmd := "systemctl stop containerd.service && systemctl disable containerd.service" Cmd(host, cmd) cmd = "rm -rf /usr/local/bin/ctr && rm -rf /usr/local/bin/containerd* " Cmd(host, cmd) cmd = "rm -rf /var/lib/containerd && rm -rf /etc/containerd/* " Cmd(host, cmd) - if Lib != "" { - cmd = "rm -rf " + Lib + if d.lib() != "" { + cmd = "rm -rf " + d.lib() Cmd(host, cmd) } } -func (d Containerd) serviceFile() []byte { +func (d *Containerd) serviceFile() []byte { var templateText = string(`[Unit] Description=containerd container runtime Documentation=https://containerd.io @@ -83,32 +91,101 @@ WantedBy=multi-user.target `) return []byte(templateText) } -func (d Containerd) configFile() []byte { - var templateText = string(`{ - \"registry-mirrors\": [ - \"http://373a6594.m.daocloud.io\" - ], - {{if len .DOCKER_REGISTRY}} - \"insecure-registries\": - [{{range $i,$v :=.DOCKER_REGISTRY}}{{if eq $i 0}}\"{{$v}}\"{{else}},\"{{$v}}\"{{end}}{{end}}], - {{end}} - \"graph\":\"{{.DOCKER_LIB}}\" -}`) +func (d *Containerd) configFile() []byte { + var templateText = string(`root = \"{{.CONTAINERD_LIB}}\" +state = \"/run/containerd\" +oom_score = 0 + +[grpc] + address = \"/run/containerd/containerd.sock\" + uid = 0 + gid = 0 + max_recv_message_size = 16777216 + max_send_message_size = 16777216 + +[debug] + address = \"\" + uid = 0 + gid = 0 + level = \"\" + +[metrics] + address = \"\" + grpc_histogram = false + +[cgroup] + path = \"\" + +[plugins] + [plugins.cgroups] + no_prometheus = false + [plugins.cri] + stream_server_address = \"127.0.0.1\" + stream_server_port = \"0\" + enable_selinux = false + sandbox_image = \"k8s.gcr.io/pause:3.1\" + stats_collect_period = 10 + systemd_cgroup = false + enable_tls_streaming = false + max_container_log_line_size = 16384 + [plugins.cri.containerd] + snapshotter = \"overlayfs\" + no_pivot = false + [plugins.cri.containerd.default_runtime] + runtime_type = \"io.containerd.runtime.v1.linux\" + runtime_engine = \"\" + runtime_root = \"\" + [plugins.cri.containerd.untrusted_workload_runtime] + runtime_type = \"\" + runtime_engine = \"\" + runtime_root = \"\" + [plugins.cri.cni] + bin_dir = \"/opt/cni/bin\" + conf_dir = \"/etc/cni/net.d\" + conf_template = \"\" + [plugins.cri.registry] + [plugins.cri.registry.mirrors] + [plugins.cri.registry.mirrors.\"docker.io\"] + endpoint = [\"https://registry-1.docker.io\"] + {{range .CONTAINERD_REGISTRY -}}[plugins.cri.registry.mirrors.\"{{.}}\"] + endpoint = [\"{{.}}\"] + {{end -}} + [plugins.cri.x509_key_pair_streaming] + tls_cert_file = \"\" + tls_key_file = \"\" + [plugins.diff-service] + default = [\"walking\"] + [plugins.linux] + shim = \"containerd-shim\" + runtime = \"runc\" + runtime_root = \"\" + no_shim = false + shim_debug = false + [plugins.opt] + path = \"/opt/containerd\" + [plugins.restart] + interval = \"10s\" + [plugins.scheduler] + pause_threshold = 0.02 + deletion_threshold = 0 + mutation_threshold = 100 + schedule_delay = \"0s\" + startup_delay = \"100ms\" +`) tmpl, err := template.New("text").Parse(templateText) if err != nil { logger.Error("template parse failed:", err) panic(1) } var envMap = make(map[string]interface{}) - envMap["DOCKER_REGISTRY"] = RegistryArr - envMap["DOCKER_LIB"] = Lib - envMap["ZERO"] = 0 + envMap["CONTAINERD_REGISTRY"] = RegistryArr + envMap["CONTAINERD_LIB"] = d.lib() var buffer bytes.Buffer _ = tmpl.Execute(&buffer, envMap) return buffer.Bytes() } -func (d Containerd) Print() { +func (d *Containerd) Print() { urlPrefix := "https://github.com/containerd/containerd/releases/download/v%s/containerd-%s.linux-amd64.tar.gz" versions := []string{ "1.1.0", diff --git a/install/command/docker.go b/install/command/docker.go index 45c80ad..bca3dd5 100644 --- a/install/command/docker.go +++ b/install/command/docker.go @@ -17,17 +17,25 @@ func NewDocker() stepInterface { const dockerFileName = "docker.tgz" -func (d Docker) SendPackage(host string) { +func (d *Docker) lib() string { + if Lib == "" { + return "/var/lib/docker" + } else { + return Lib + } +} + +func (d *Docker) SendPackage(host string) { SendPackage(host, PkgUrl, dockerFileName) } -func (d Docker) Tar(host string) { +func (d *Docker) Tar(host string) { cmd := fmt.Sprintf("tar --strip-components=1 -xvzf /root/%s -C /usr/local/bin", dockerFileName) Cmd(host, cmd) } -func (d Docker) Config(host string) { - cmd := "mkdir -p " + Lib +func (d *Docker) Config(host string) { + cmd := "mkdir -p " + d.lib() Cmd(host, cmd) cmd = "mkdir -p /etc/docker" Cmd(host, cmd) @@ -35,32 +43,32 @@ func (d Docker) Config(host string) { Cmd(host, cmd) } -func (d Docker) Enable(host string) { +func (d *Docker) Enable(host string) { cmd := "echo \"" + string(d.serviceFile()) + "\" > /usr/lib/systemd/system/docker.service" Cmd(host, cmd) cmd = "systemctl enable docker.service && systemctl restart docker.service" Cmd(host, cmd) } -func (d Docker) Version(host string) { +func (d *Docker) Version(host string) { cmd := "docker version" Cmd(host, cmd) } -func (d Docker) Uninstall(host string) { +func (d *Docker) Uninstall(host string) { cmd := "systemctl stop docker.service && systemctl disable docker.service" Cmd(host, cmd) cmd = "rm -rf /usr/local/bin/runc && rm -rf /usr/local/bin/ctr && rm -rf /usr/local/bin/containerd* " Cmd(host, cmd) cmd = "rm -rf /usr/local/bin/docker* && rm -rf /var/lib/docker && rm -rf /etc/docker/* " Cmd(host, cmd) - if Lib != "" { - cmd = "rm -rf " + Lib + if d.lib() != "" { + cmd = "rm -rf " + d.lib() Cmd(host, cmd) } } -func (d Docker) serviceFile() []byte { +func (d *Docker) serviceFile() []byte { var templateText = string(`[Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com @@ -82,7 +90,7 @@ WantedBy=multi-user.target `) return []byte(templateText) } -func (d Docker) configFile() []byte { +func (d *Docker) configFile() []byte { var templateText = string(`{ \"registry-mirrors\": [ \"http://373a6594.m.daocloud.io\" @@ -100,14 +108,13 @@ func (d Docker) configFile() []byte { } var envMap = make(map[string]interface{}) envMap["DOCKER_REGISTRY"] = RegistryArr - envMap["DOCKER_LIB"] = Lib - envMap["ZERO"] = 0 + envMap["DOCKER_LIB"] = d.lib() var buffer bytes.Buffer _ = tmpl.Execute(&buffer, envMap) return buffer.Bytes() } -func (d Docker) Print() { +func (d *Docker) Print() { urlPrefix := "https://download.docker.com/linux/static/stable/x86_64/docker-%s.tgz" versions := []string{ "17.03.0-ce",