Skip to content

Commit

Permalink
fix: install a separate containerd service for k3s
Browse files Browse the repository at this point in the history
  • Loading branch information
aby913 committed Aug 13, 2024
1 parent e92a057 commit 2b1a56e
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 59 deletions.
11 changes: 4 additions & 7 deletions cmd/ctl/options/cli_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package options
import "github.com/spf13/cobra"

type CliKubeInitializeOptions struct {
KubeType string
RegistryMirrors string
MiniKube bool
MiniKubeProfile string
K3sContainerRuntimeEndpoint string
KubeType string
RegistryMirrors string
MiniKube bool
MiniKubeProfile string
}

func NewCliKubeInitializeOptions() *CliKubeInitializeOptions {
Expand All @@ -19,8 +18,6 @@ func (o *CliKubeInitializeOptions) AddFlags(cmd *cobra.Command) {
cmd.Flags().BoolVar(&o.MiniKube, "minikube", false, "Set minikube flag")
cmd.Flags().StringVar(&o.MiniKubeProfile, "profile", "", "Set minikube profile name")
cmd.Flags().StringVarP(&o.RegistryMirrors, "registry-mirrors", "", "", "Docker Container registry mirrors, multiple mirrors are separated by commas")
cmd.Flags().StringVar(&o.K3sContainerRuntimeEndpoint, "k3s-container-runtime-endpoint", "", "Disable k3s embedded containerd and use alternative CRI implementation, e.g., unix:///run/containerd/containerd.sock")

}

type CliTerminusUninstallOptions struct {
Expand Down
5 changes: 2 additions & 3 deletions cmd/ctl/os/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,15 @@ func NewCmdInitializeOs() *cobra.Command {
os.Exit(1)
}

logger.Infof("options: version: %s, kube: %s, minikube: %v, minikubeprofile: %s, registry: %s, k3sctr: %s",
logger.Infof("options: version: %s, kube: %s, minikube: %v, minikubeprofile: %s, registry: %s",
version.VERSION, o.InitializeOptions.KubeType, o.InitializeOptions.MiniKube,
o.InitializeOptions.MiniKubeProfile, o.InitializeOptions.RegistryMirrors, o.InitializeOptions.K3sContainerRuntimeEndpoint)
o.InitializeOptions.MiniKubeProfile, o.InitializeOptions.RegistryMirrors)

if err := pipelines.CliInitializeTerminusPipeline(
o.InitializeOptions.KubeType,
o.InitializeOptions.MiniKube,
o.InitializeOptions.MiniKubeProfile,
o.InitializeOptions.RegistryMirrors,
o.InitializeOptions.K3sContainerRuntimeEndpoint,
); err != nil {
logger.Errorf("initialize kube error: %v", err)
os.Exit(1)
Expand Down
8 changes: 4 additions & 4 deletions pkg/binaries/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func K3sFilesDownloadHTTP(kubeConf *common.KubeConf, path, version, arch string,
helm := files.NewKubeBinary("helm", arch, kubekeyapiv1alpha2.DefaultHelmVersion, path)
k3s := files.NewKubeBinary("k3s", arch, version, path)

// crictl := files.NewKubeBinary("crictl", arch, kubekeyapiv1alpha2.DefaultCrictlVersion, path)
// containerd := files.NewKubeBinary("containerd", arch, kubekeyapiv1alpha2.DefaultContainerdVersion, path)
// runc := files.NewKubeBinary("runc", arch, kubekeyapiv1alpha2.DefaultRuncVersion, path)
crictl := files.NewKubeBinary("crictl", arch, kubekeyapiv1alpha2.DefaultCrictlVersion, path)
containerd := files.NewKubeBinary("containerd", arch, kubekeyapiv1alpha2.DefaultContainerdVersion, path)
runc := files.NewKubeBinary("runc", arch, kubekeyapiv1alpha2.DefaultRuncVersion, path)

binaries := []*files.KubeBinary{k3s, helm, kubecni, etcd} // crictl, containerd, runc
binaries := []*files.KubeBinary{k3s, helm, kubecni, etcd, crictl, containerd, runc}
binariesMap := make(map[string]*files.KubeBinary)
for _, binary := range binaries {
if err := binary.CreateBaseDir(); err != nil {
Expand Down
2 changes: 0 additions & 2 deletions pkg/common/kube_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ type Argument struct {
Minikube bool
MinikubeProfile string
DeleteCache bool

K3sContainerRuntimeEndpoint string
}

type AwsHost struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/images/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type LoadImages struct {
}

func (t *LoadImages) Execute(runtime connector.Runtime) (reserr error) {

var minikubepath = getMinikubePath(t.PipelineCache)
var minikubeprofile = t.KubeConf.Arg.MinikubeProfile
var containerManager = t.KubeConf.Cluster.Kubernetes.ContainerManager
Expand Down
1 change: 1 addition & 0 deletions pkg/images/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (p *PreloadImagesModule) Init() {
&MasterPullImages{Not: true},
&plugins.IsCloudInstance{Not: true},
&CheckImageManifest{},
&ContainerdInstalled{},
},
Action: new(LoadImages),
Parallel: false,
Expand Down
21 changes: 21 additions & 0 deletions pkg/images/prepares.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
package images

import (
"fmt"
"strings"

"bytetrade.io/web3os/installer/pkg/common"
"bytetrade.io/web3os/installer/pkg/core/connector"
)
Expand All @@ -33,3 +36,21 @@ func (n *MasterPullImages) PreCheck(runtime connector.Runtime) (bool, error) {
}
return n.Not, nil
}

type ContainerdInstalled struct {
common.KubePrepare
}

func (c *ContainerdInstalled) PreCheck(runtime connector.Runtime) (bool, error) {
output, err := runtime.GetRunner().SudoCmd(
"if [ -z $(which containerd) ] || [ ! -e /run/containerd/containerd.sock ]; "+
"then echo 'not exist'; "+
"fi", false, false)
if err != nil {
return false, err
}
if strings.Contains(output, "not exist") {
return false, fmt.Errorf("containerd service not installed")
}
return true, nil
}
21 changes: 5 additions & 16 deletions pkg/k3s/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,14 @@ func (s *SyncKubeBinary) Execute(runtime connector.Runtime) error {
}
binariesMap := binariesMapObj.(map[string]*files.KubeBinary)

if err := SyncKubeBinaries(runtime, binariesMap, s.KubeConf.Arg.K3sContainerRuntimeEndpoint); err != nil {
if err := SyncKubeBinaries(runtime, binariesMap); err != nil {
return err
}
return nil
}

// SyncKubeBinaries is used to sync kubernetes' binaries to each node.
func SyncKubeBinaries(runtime connector.Runtime, binariesMap map[string]*files.KubeBinary, k3sContainerRuntimeEndpoint string) error {
// exist := checkContainerExists(runtime)

func SyncKubeBinaries(runtime connector.Runtime, binariesMap map[string]*files.KubeBinary) error {
if err := utils.ResetTmpDir(runtime); err != nil {
return err
}
Expand Down Expand Up @@ -145,11 +143,7 @@ func SyncKubeBinaries(runtime connector.Runtime, binariesMap map[string]*files.K
}
}

var binaries = []string{"kubectl"}
if strings.EqualFold(k3sContainerRuntimeEndpoint, "") {
binaries = append(binaries, "crictl", "ctr")
}

binaries := []string{"kubectl"}
var createLinkCMDs []string
for _, binary := range binaries {
createLinkCMDs = append(createLinkCMDs, fmt.Sprintf("ln -snf /usr/local/bin/k3s /usr/local/bin/%s", binary))
Expand Down Expand Up @@ -229,13 +223,8 @@ func (g *GenerateK3sService) Execute(runtime connector.Runtime) error {
"SchedulerArgs": kubeSchedulerArgs,
"KubeletArgs": kubeletArgs,
"KubeProxyArgs": kubeProxyArgs,
"Container": "",
"CgroupDriver": "",
}

if !strings.EqualFold(g.KubeConf.Arg.K3sContainerRuntimeEndpoint, "") {
data["Container"] = "--container-runtime-endpoint=unix:///run/containerd/containerd.sock"
data["CgroupDriver"] = "--kubelet-arg=cgroup-driver=systemd"
"Container": "--container-runtime-endpoint=unix:///run/containerd/containerd.sock",
"CgroupDriver": "--kubelet-arg=cgroup-driver=systemd",
}

templateAction := action.Template{
Expand Down
3 changes: 2 additions & 1 deletion pkg/phase/cluster/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func NewK3sCreateClusterPhase(runtime *common.KubeRuntime) []module.Module {
&binaries.K3sNodeBinariesModule{},
&os.ConfigureOSModule{},
&k3s.StatusModule{},
&k3s.InstallContainerModule{},
&images.PreloadImagesModule{Skip: runtime.Arg.SkipPullImages},
&etcd.PreCheckModule{Skip: runtime.Cluster.Etcd.Type != kubekeyapiv1alpha2.KubeKey},
&etcd.CertsModule{},
&etcd.InstallETCDBinaryModule{Skip: runtime.Cluster.Etcd.Type != kubekeyapiv1alpha2.KubeKey},
Expand All @@ -75,7 +77,6 @@ func NewK3sCreateClusterPhase(runtime *common.KubeRuntime) []module.Module {
&k3s.InitClusterModule{},
&k3s.StatusModule{},
&k3s.JoinNodesModule{},
&images.PreloadImagesModule{Skip: runtime.Arg.SkipPullImages},
&images.CopyImagesToRegistryModule{Skip: skipPushImages},
&loadbalancer.K3sHaproxyModule{Skip: !runtime.Cluster.ControlPlaneEndpoint.IsInternalLBEnabled()},
&network.DeployNetworkPluginModule{},
Expand Down
37 changes: 11 additions & 26 deletions pkg/pipelines/initialize_terminus.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"bytetrade.io/web3os/installer/pkg/phase/cluster"
)

func CliInitializeTerminusPipeline(kubeType string, minikube bool, minikubeProfileName, registryMirrors, k3sContainerRuntimeEndpoint string) error {
func CliInitializeTerminusPipeline(kubeType string, minikube bool, minikubeProfileName, registryMirrors string) error {
if err := checkMacOSParams(minikube, minikubeProfileName); err != nil {
return err
}
Expand All @@ -29,17 +29,16 @@ func CliInitializeTerminusPipeline(kubeType string, minikube bool, minikubeProfi
}

arg := common.Argument{
KsEnable: true,
KsVersion: common.DefaultKubeSphereVersion,
InstallPackages: false,
SKipPushImages: false,
ContainerManager: common.Containerd,
IsCloudInstance: strings.EqualFold(os.Getenv("TERMINUS_IS_CLOUD_VERSION"), common.TRUE),
Minikube: minikube,
MinikubeProfile: minikubeProfileName,
KubernetesVersion: ksVersion,
RegistryMirrors: registryMirrors,
K3sContainerRuntimeEndpoint: formatK3sContainerRuntimeEndpoint(kubeType, k3sContainerRuntimeEndpoint),
KsEnable: true,
KsVersion: common.DefaultKubeSphereVersion,
InstallPackages: false,
SKipPushImages: false,
ContainerManager: common.Containerd,
IsCloudInstance: strings.EqualFold(os.Getenv("TERMINUS_IS_CLOUD_VERSION"), common.TRUE),
Minikube: minikube,
MinikubeProfile: minikubeProfileName,
KubernetesVersion: ksVersion,
RegistryMirrors: registryMirrors,
}

runtime, err := common.NewKubeRuntime(common.AllInOne, arg)
Expand Down Expand Up @@ -77,20 +76,6 @@ func CliInitializeTerminusPipeline(kubeType string, minikube bool, minikubeProfi
return nil
}

func formatK3sContainerRuntimeEndpoint(kubeType, k3sContainerRuntimeEndpoint string) string {
if kubeType != common.K3s {
return ""
}
endpointEnv := os.Getenv("K3S_CONTAINER_RUNTIME_ENDPOINT")
if !strings.EqualFold(endpointEnv, "") {
return endpointEnv
}
if !strings.EqualFold(k3sContainerRuntimeEndpoint, "") {
return k3sContainerRuntimeEndpoint
}
return ""
}

func checkMacOSParams(minikube bool, minikubeProfileName string) error {
if constants.OsPlatform == common.Darwin && !minikube {
return fmt.Errorf("MacOS startup parameter error, need to specify parameters --minikube --profile PROFILE_NAME")
Expand Down

0 comments on commit 2b1a56e

Please sign in to comment.