From ab70148fc0213a4523d976099a18e438cb2ed067 Mon Sep 17 00:00:00 2001 From: cheliequan <liequanche@126.com> Date: 星期三, 24 五月 2023 19:55:49 +0800 Subject: [PATCH] 更新代码 --- src/main/main.go | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 156 insertions(+), 17 deletions(-) diff --git a/src/main/main.go b/src/main/main.go index c788412..a6f27a7 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -8,11 +8,13 @@ "path/filepath" "time" + "basic.com/aps/aps_deploy.git/src/cluster" + "basic.com/aps/aps_deploy.git/src/rancher" + "basic.com/aps/aps_deploy.git/src/util" "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" - "basic.com/aps/aps_deploy.git/src/create" - "basic.com/aps/aps_deploy.git/src/delete" + "basic.com/aps/aps_deploy.git/src/k8s" ) var ( @@ -22,10 +24,143 @@ namespaces = []string{"guangsheng", "geruimi", "tongsheng"} ) +type Node struct { + ClusterName string `json:"clusterName"` + Roles []string `json:"roles"` + IP string `json:"ip"` + SSHUsername string `json:"sshUsername"` + SSHPassword string `json:"sshPassword"` + SSHPort int `json:"sshPort"` +} + +func rancher_install_test() { + nodes := []Node{ + { + Roles: []string{"etcd", "controlplane", "worker"}, + IP: "192.168.20.189", + SSHUsername: "basic", + SSHPassword: "123", + SSHPort: 22, + }, + { + Roles: []string{"worker"}, + IP: "192.168.20.10", + SSHUsername: "basic", + SSHPassword: "123", + SSHPort: 22, + }, + { + Roles: []string{"worker"}, + IP: "192.168.20.115", + SSHUsername: "basic", + SSHPassword: "alien123", + SSHPort: 22, + }, + // Add more nodes here if needed + } + + //install rancher on master node + err := rancher.InstallDockerAndRancher(nodes[0].IP, nodes[0].SSHUsername, nodes[0].SSHPassword, nodes[0].SSHPort) + if err != nil { + log.Fatalf("Failed to install Rancher: %v", err) + } + + os.Exit(0) +} + +func cluster_test() { + clusterName := "kubernetus" + nodes := []Node{ + { + ClusterName: clusterName, + Roles: []string{"etcd", "controlplane", "worker"}, + IP: "192.168.20.189", + SSHUsername: "basic", + SSHPassword: "123", + SSHPort: 22, + }, + { + ClusterName: clusterName, + Roles: []string{"worker"}, + IP: "192.168.20.10", + SSHUsername: "basic", + SSHPassword: "123", + SSHPort: 22, + }, + { + ClusterName: clusterName, + Roles: []string{"worker"}, + IP: "192.168.20.115", + SSHUsername: "basic", + SSHPassword: "alien123", + SSHPort: 22, + }, + // Add more nodes here if needed + } + + // Create the cluster + // Rancher configuration + /*rancherConfig := RancherConfig{ + RancherURL: "https://192.168.20.119:8443", + BearerToken: "token-nnrsc:w68zdt8s47fnpjd5xqdl5hhzpz4j2d56kt5nx49nsswcbpdzc28kh5", + }*/ + + rancherConfig := rancher.RancherConfig{ + RancherURL: "https://192.168.20.189:8443", + BearerToken: "token-t4cdf:h7zhmbvbzdvd9mmjw8zmt8rh4m7rl5gtqpqljlhl9tlr2z26j9lf4l", + } + + // Deploy clusterId + clusterID, err := cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName) + if err != nil { + log.Fatal(err) + err = cluster.CreateCluster(rancherConfig, clusterName) + if err != nil { + log.Fatalf("Failed to create cluster: %v", err) + } + fmt.Printf("Cluster created: %s\n", clusterName) + clusterID, err = cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName) + if err != nil { + log.Fatal(err) + } + } + fmt.Println(clusterID) + + // Deploy nodeCommand + nodeCommand, err := cluster.GetNodeCommand(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterID) + if err != nil { + log.Fatal(err) + } + fmt.Println(nodeCommand) + + for _, node := range nodes { + //Deploy Docker on each node + err = util.InstallDocker(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort) + if err != nil { + log.Fatal(err) + } + + // Deploy Kubectl on each node + err = util.InstallKubectl(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort) + if err != nil { + log.Fatal(err) + } + + // Deploy Kubernetes roles on each node + err = cluster.Deployk8sRolesOnNode(node.IP, node.SSHUsername, node.SSHPassword, nodeCommand, node.SSHPort, node.Roles) + if err != nil { + log.Fatal(err) + } + } + os.Exit(0) +} + func main() { createCmd := flag.NewFlagSet("create", flag.ExitOnError) deleteCmd := flag.NewFlagSet("delete", flag.ExitOnError) testCmd := flag.NewFlagSet("test", flag.ExitOnError) + clusterCmd := flag.NewFlagSet("cluster", flag.ExitOnError) + rancherInstallCmd := flag.NewFlagSet("rancher_install", flag.ExitOnError) createNamespace := createCmd.String("ns", "", "Namespace name") createDeployment := createCmd.String("deployment", "", "Deployment name") @@ -36,7 +171,7 @@ deleteService := deleteCmd.String("service", "", "Service name") if len(os.Args) < 2 { - fmt.Println("create/delete/test command is required") + fmt.Println("create/delete/test/cluster/rancher_install command is required") os.Exit(1) } @@ -47,13 +182,17 @@ deleteCmd.Parse(os.Args[2:]) case "test": testCmd.Parse(os.Args[2:]) + case "cluster": + clusterCmd.Parse(os.Args[2:]) + case "rancher_install": + rancherInstallCmd.Parse(os.Args[2:]) default: flag.PrintDefaults() os.Exit(1) } // 閰嶇疆 Kubernetes 闆嗙兢鐨� kubeconfig 璺緞 - kubeconfig := flag.String("kubeconfig", filepath.Join(homeDir(), ".kube", "config"), "kubeconfig file") + kubeconfig := flag.String("kubeconfig", filepath.Join(util.HomeDir(), ".kube", "config"), "kubeconfig file") flag.Parse() // 鍒涘缓 Kubernetes 瀹㈡埛绔� @@ -75,12 +214,12 @@ os.Exit(1) } - err := create.CreateDeploymentAndService(clientset, *createNamespace, *createDeployment, *createService) + err := k8s.CreateDeploymentAndService(clientset, *createNamespace, *createDeployment, *createService) if err != nil { panic(err) } - nodeport, err := create.GetServiceNodePort(clientset, *createNamespace, *createService) + nodeport, err := k8s.GetServiceNodePort(clientset, *createNamespace, *createService) if err != nil { panic(err) } @@ -96,7 +235,7 @@ os.Exit(1) } - err := delete.DeleteResources(clientset, *deleteNamespace, *deleteDeployment, *deleteService) + err := k8s.DeleteResources(clientset, *deleteNamespace, *deleteDeployment, *deleteService) if err != nil { panic(err) } @@ -113,7 +252,7 @@ for _, ns := range namespaces { deploymentName := ns serviceName := ns - err := create.CreateDeploymentAndService(clientset, ns, deploymentName, serviceName) + err := k8s.CreateDeploymentAndService(clientset, ns, deploymentName, serviceName) if err != nil { log.Printf("\033[97;41mFailed to create resources in namespace %s: %v\033[0m\n", ns, err) } else { @@ -135,7 +274,7 @@ // 鍒犻櫎澶氫釜 Namespace 涓嬬殑鐩稿悓鍚嶇О鐨� Deployment 鍜� Service for _, ns := range namespaces { - err = delete.DeleteResources(clientset, ns, ns, ns) + err = k8s.DeleteResources(clientset, ns, ns, ns) if err != nil { panic(err) } @@ -144,12 +283,12 @@ fmt.Println("Resources deleted.") } -} -// homeDir 鑾峰彇褰撳墠鐢ㄦ埛鐨勫鐩綍璺緞 -func homeDir() string { - if h := os.Getenv("HOME"); h != "" { - return h - } - return os.Getenv("USERPROFILE") // Windows 鐜涓嬭幏鍙栫敤鎴风洰褰� -} + if clusterCmd.Parsed() { + cluster_test() + } + if rancherInstallCmd.Parsed() { + rancher_install_test() + } + +} -- Gitblit v1.8.0