本文主要是介绍kubeedge keadm init命令逻辑详解,keadm-init-v1.10.0,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
puml格式,装plant uml查看
@startuml keadm-init-v1.10.0
' Author: xiaoyang.chen
' mail: xiaoyang.chen@???.comstart
: keadm init args (基于v1.10.0);
note left
args:
kubeedge-version, kubeedge的版本
kube-config, kubeedge的配置文件路径, 默认"/root/.kube/config"
master, k8s Master节点地址
advertise-address, cloud-core证书下的扩展项SubAltNames的ip信息, 即kubeedge云端节点ip地址
domainname, cloud-core证书下的扩展项SubAltNames的域名信息, 即kubeedge云端节点域名
tarballpath, kubeedge二进制安装包tar包的路径,tar包的目录层次参照github的kubeedge仓库, 默认KubeEdgePath = "/etc/kubeedge/"example:
keadm init
--advertise-address="101.33.227.236"
--kubeedge-version 1.10.0
end note: 解析keadm init args的args, AddToolsVals;
partition "添加部署工具信息\n (func Add2ToolsList(\n toolList map[string]types.ToolsInstaller, \n flagData map[string]types.FlagData, \n initOptions *types.InitOptions \n) error)" {: var kubeVer string;if (参数"kubeedge-version"存在) then (yes): kubeVar = val of args "kubeedge-version";endifif (kubeVer == "") then (yes): var latestVersion string;while (i := 0; i < util.RetryTimes(默认 5); i++): 通过 https://kubeedge.io/latestversion 获取最新的版本信息;if (获取最新版本的err, err != nil) then (yes): 打印错误信息;else (no)if (获取到的版本号字符串长度大于0) then (yes): 版本号剪除前缀"v", v1.10.0=>1.10.0 并赋值给 kubeVar;: latestVersion = 版本号(未剪除"v");breakendifendifendwhileif (len(latestVersion) == 0) then (yes): kubeVer = types.DefaultKubeEdgeVersion (1.10.0);: 打印错误信息;endifendif: common := util.Common{...} \n (\n 携带kubeedge版本号(解析 kubeVar), \n kubeedge配置文件路径(args kube-config), \n k8s Master节点地址(args master) \n);: toolList["Cloud"] = &util.KubeCloudInstTool{common, ...} \n (\n 携带cloud-core证书下的扩展项SubAltNames的ip信息(args advertise-address), \n cloud-core证书下的扩展项SubAltNames的域名信息(args domainname), \n kubeedge二进制安装包tar包的路径(args tarballpath) \n);: toolList["Kubernetes"] = &util.K8SInstTool{common};: return nil;
}
if (err from Add2ToolsList, err != nil) then (yes): return err;end
endifpartition "根据部署工具信息安装\n (func Execute(toolList map[string]types.ToolsInstaller) error)" {while (name, tool := range toolList)if (name != "Cloud") then (yes): 安装对应的模块(err := tool.InstallTools());if (err != nil) then (yes): return err;endendifendifendwhile: return toolList["Cloud"].InstallTools();
}
end: toolList["Kubernetes"].InstallTools();
partition "func (ks *K8SInstTool) InstallTools() error" {: 根据包管理工具类型设置k8s部署工具的系统接口参数 ks.SetOSInterface(GetOSInterface());switch (包管理工具类型)case ("apt"): 设置为DebOS;case ("yum") : 设置为RpmOS;case ("pacman"): 设置为PacmanOS;case (default): 打印错误信息;: panic("...package manager...");endendswitch: 根据二进制文件名称"cloudcore"判断cloudCore是否在运行, (isRunning, err);if (err != nil) then (yes): return err;endendifif (isRunning) then (yes): return fmt.Errorf(\n "CloudCore is already running on this node, \n please run reset to clean up first" \n);endendif: 判断k8s组建是否已安装, err = ks.IsK8SComponentInstalled(ks.KubeConfig, ks.Master, ), kubeConfig配置文件路径 (args kube-config), k8s Master节点地址(args master);: fmt.Println("Kubernetes version verification passed, KubeEdge installation will start...");: 安装CRDs( err = installCRDs(ks) );if (err != nil) then (yes): return err;endendifpartition "创建kubeedge的命名空间 \n err = createKubeEdgeNs(\n ks.KubeConfig, ks.Master \n) func createKubeEdgeNs(kubeConfig, master string) error" {:构建配置, 同"安装CRDs"中的构建配置;:根据配置生成k8s客户端;:默认命名空间 const constants.SystemNamespace = "kubeedge";:用k8s客户端创建默认命名空间;}if (err != nil) then (yes): return err;endendif: return nil;
}
end: 安装CRDs( err = installCRDs(ks) );
partition "func installCRDs(ks *K8SInstTool) error" {partition "构建配置 \n config, err := BuildConfig(ks.KubeConfig, ks.Master) \n func BuildConfig(kubeConfig, master string) (conf *rest.Config, err error)" {partition "config, err := clientcmd.BuildConfigFromFlags(master, kubeConfig) \n func BuildConfigFromFlags(masterUrl, kubeconfigPath string) (*restclient.Config, error)" {if (kubeconfigPath == "" && masterUrl == "") then (yes): klog.Warning("Neither --kubeconfig nor--master was specified. Using the inClusterConfig. This might not work.");: 集群默认配置, tokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token" rootCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt" host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT");: 应用集群默认配置, kubeconfig, err := restclient.InClusterConfig(); if (err == nil) then (yes): return kubeconfig, nil;else (no): klog.Warning("error creating inClusterConfig,falling back to default config: ", err);: 使用args(kube-config, master)构建配置 return NewNonInteractiveDeferredLoadingClientConfig(&ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},&ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterUrl}}).ClientConfig();endifelse (no): 使用args(kube-config, master)构建配置 return NewNonInteractiveDeferredLoadingClientConfig(&ClientConfigLoadingRules{ExplicitPath: kubeconfigPath},&ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: masterUrl}}).ClientConfig();endif} if (err != nil) then (yes): return nil, err;else (no): return config, nil; endif }if (err != nil) then (yes): return fmt.Errorf(""failed to build config, err: %v"", err);endendif: 生成动态客户端 dynamicClient, err := dynamic.NewForConfig(config);if (err != nil) then (yes): return fmt.Errorf(""failed to create a new dynamic client: %v"", err);endendif: crds := map[string][]string{"devices": {"devices_v1alpha2_device.yaml","devices_v1alpha2_devicemodel.yaml",},"reliablesyncs": {"cluster_objectsync_v1alpha1.yaml","objectsync_v1alpha1.yaml",},"router": {"router_v1_rule.yaml","router_v1_ruleEndpoint.yaml",},};: 遍历crds这个map, 判断crd文件(文件路径为"/etc/kubeedge/crds"+"crds->key"+"/"+"crds->value")是否已存在, 不存在则从github仓库下载, KubeEdgeCRDDownloadURL ="https://raw.githubusercontent.com/kubeedge/kubeedge/release-%s/build/crds", %s为版本号, 根据这个crd文件, 使用动态客户端向k8s请求创建kubeedge的crd;
}
end:toolList["Cloud"].InstallTools();
partition "func (cu *KubeCloudInstTool) InstallTools() error" {:根据包管理工具类型设置部署工具的系统接口参数 cu.SetOSInterface(GetOSInterface());switch (包管理工具类型)case ("apt"):设置为DebOS;case ("yum") :设置为RpmOS;case ("pacman"):设置为PacmanOS;case (default):打印错误信息;:panic("...package manager...");endendswitch:设置kubeCloud安装工具的kubeedge版本;:设置安装工具的安装选项TarballPath(args tarballpath)ComponentType(types.CloudCore, 默认"cloudcore");partition "根据安装选项使用kubeCloud安装工具安装cloudcore, \n func installKubeEdge(options types.InstallOptions, arch string, version semver.Version) error" {:二进制安装包tar.gz的文件名为fmt.Sprintf("kubeedge-v%s-linux-%s.tar.gz", version, arch), 具体见kubeedge仓库的tar.gz文件的命名;:二进制安装包tar.gz的文件的CheckSum文件名为fmt.Sprintf("checksum_kubeedge-v%s-linux-%s.tar.gz.txt", version, arch);:判断二进制安装包tar.gz是否存在,不存在则从github kubeedge仓库的release tag中下载对应的tar.gz包和对应的checksum文件用于SHA512验证,默认的下载尝试次数为3;partition 下载service文件 {if (HasSystemd()) then (yes):var ServiceFileName string;switch (安装的组件类型)case ("cloudcore"):ServiceFileName="cloudcore.service";case ("edgecore"):ServiceFileName="edgecore.service";case (default):return fmt.Errorf("component type %s not support", componentType);endendswitch:service文件存放路径为"/etc/kubeedge/"+"/"+ServiceFileName;:如果没有找到service文件, 则从github kubeedge仓库的release tag中下载对应的service文件,默认的下载尝试次数为5;endif}if (options.ComponentType == types.CloudCore) then (yes):解压tar.gz并将解压后放在cloud/cloudcore下的"cloudcore"文件复制到/usr/local/bin;elseif (options.ComponentType == types.EdgeCore) then (yes):解压tar.gz并将解压后放在edge下的"edgecore"文件复制到/usr/local/bin;endif}:检查配置文件目录"/etc/kubeedge/config/"是否存在, 不存在则创建并设置511权限;#Red:生成cloudcore的默认配置,**cloudCoreConfig := v1alpha1.NewDefaultCloudCoreConfig()**;:根据"keadm init args"的args修改cloudCoreConfig(设置kube配置文件路径 args kube-config, k8s-master节点地址 args master, cloudhub的ip地址 args advertise-address, cloudhub的dns域名 args domainname);:将cloudCoreConfig的内容写入"/etc/kubeedge/config/cloudcore.yaml";partition 启动cloudcore进程 {:检查日志目录"/var/log/kubeedge/"是否存在, 不存在则创建并设置511权限;:检查二进制文件存放目录"/usr/local/bin"是否存在, 不存在则创建并设置511权限;:对二进制文件存放目录的cloudcore设置可执行权限;:执行命令"/usr/local/bin/cloudcore > /var/log/kubeedge/cloudcore.log 2>&1 &";:fmt.Println("KubeEdge cloudcore is running, For logs visit: ", KubeEdgeLogPath+KubeCloudBinaryName+".log");}:fmt.Println("CloudCore started");:return nil;
}
end@enduml
这篇关于kubeedge keadm init命令逻辑详解,keadm-init-v1.10.0的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!