From fa2acd8e330344d9d17ff778a7a48c1d09ab74be Mon Sep 17 00:00:00 2001
From: cheliequan <liequanche@126.com>
Date: 星期二, 30 五月 2023 15:42:02 +0800
Subject: [PATCH] Merge branch 'master' of http://192.168.5.5:10010/r/aps/aps_deploy
---
src/cluster/cluster.go | 108 ++++--------------------------------------------------
1 files changed, 8 insertions(+), 100 deletions(-)
diff --git a/src/cluster/cluster.go b/src/cluster/cluster.go
index 4245064..de960b7 100644
--- a/src/cluster/cluster.go
+++ b/src/cluster/cluster.go
@@ -1,16 +1,15 @@
package cluster
import (
- "_/E_/git/aps_deploy/src/util"
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
- "os"
"strings"
- "../util"
+ "basic.com/aps/aps_deploy.git/src/util"
+ "basic.com/aps/aps_deploy.git/src/rancher"
)
type Cluster struct {
@@ -156,10 +155,6 @@
SSHPort int `json:"sshPort"`
}
-type RancherConfig struct {
- RancherURL string `json:"rancherURL"`
- BearerToken string `json:"bearerToken"`
-}
type ClusterCreateRequest struct {
Name string `json:"name"`
@@ -200,7 +195,7 @@
// Deploy Kubernetes roles on a node using SSH
func Deployk8sRolesOnNode(nodeIP, sshUsername, sshPassword, remoteSSHCommand string, sshPort int, roles []string) error {
- rancherAgentInstalled, err := isRancherAgentInstalled(nodeIP, sshUsername, sshPassword, sshPort)
+ rancherAgentInstalled, err := IsRancherAgentInstalled(nodeIP, sshUsername, sshPassword, sshPort)
if err == nil {
return nil
}
@@ -220,18 +215,18 @@
}
}
- _, err := sshExec(nodeIP, sshUsername, sshPassword, remoteSSHCommand, sshPort)
+ _, err := util.SSHExec(nodeIP, sshUsername, sshPassword, remoteSSHCommand, sshPort)
if err != nil {
return fmt.Errorf("failed to deploy Kubernetes roles on the remote server: %v", err)
}
- return nil
}
+ return nil
}
func IsRancherInstalled(ip, username, password string, sshPort int) (bool, error) {
// 妫�鏌ancher瀹瑰櫒鏄惁宸茶繍琛�
checkRancherCommand := "sudo docker ps --format '{{.Image}}' | grep -q rancher/rancher:v2.5.17"
- _, err := sshExec(ip, username, password, checkRancherCommand, sshPort)
+ _, err := util.SSHExec(ip, username, password, checkRancherCommand, sshPort)
if err != nil {
// 濡傛灉鎵ц鍛戒护鍑洪敊锛屽垯璇存槑Rancher鏈畨瑁呮垨鍙戠敓鍏朵粬閿欒
return false, fmt.Errorf("failed to check Rancher installation: %v", err)
@@ -243,7 +238,7 @@
func IsRancherAgentInstalled(ip, username, password string, sshPort int) (bool, error) {
// 妫�鏌ancher瀹瑰櫒鏄惁宸茶繍琛�
checkRancherCommand := "sudo docker ps --format '{{.Image}}' | grep -q rancher/rancher-agent:v2.5."
- _, err := sshExec(ip, username, password, checkRancherCommand, sshPort)
+ _, err := util.SSHExec(ip, username, password, checkRancherCommand, sshPort)
if err != nil {
// 濡傛灉鎵ц鍛戒护鍑洪敊锛屽垯璇存槑Rancher鏈畨瑁呮垨鍙戠敓鍏朵粬閿欒
return false, fmt.Errorf("failed to check Rancher installation: %v", err)
@@ -252,7 +247,7 @@
return true, nil
}
-func CreateCluster(rancherConfig RancherConfig, clusterName string) error {
+func CreateCluster(rancherConfig rancher.RancherConfig, clusterName string) error {
requestBody := createClusterData(clusterName)
url := fmt.Sprintf("%s/v3/clusters", rancherConfig.RancherURL)
@@ -285,91 +280,4 @@
fmt.Printf("Cluster created: ID=%s, Name=%s\n", responseBody.ID, responseBody.Name)
return nil
-}
-
-func main() {
- 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 := RancherConfig{
- RancherURL: "https://192.168.20.189:8443",
- BearerToken: "token-t4cdf:h7zhmbvbzdvd9mmjw8zmt8rh4m7rl5gtqpqljlhl9tlr2z26j9lf4l",
- }
-
- // Deploy clusterId
- clusterID, err := GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName)
- if err != nil {
- log.Fatal(err)
- err = CreateCluster(rancherConfig, clusterName)
- if err != nil {
- log.Fatalf("Failed to create cluster: %v", err)
- }
- fmt.Printf("Cluster created: %s\n", clusterName)
- clusterID, err = GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName)
- if err != nil {
- log.Fatal(err)
- }
- }
- fmt.Println(clusterID)
-
- // Deploy nodeCommand
- nodeCommand, err := 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 = Deployk8sRolesOnNode(node.IP, node.SSHUsername, node.SSHPassword, nodeCommand, node.SSHPort, node.Roles)
- if err != nil {
- log.Fatal(err)
- }
- }
- os.Exit(0)
}
--
Gitblit v1.8.0