From d3a63ffbec643f7a2746dfe2db2313492ef163af Mon Sep 17 00:00:00 2001 From: cheliequan <liequanche@126.com> Date: 星期三, 31 五月 2023 09:57:21 +0800 Subject: [PATCH] 更新rancher部署代码 --- src/main/main.go | 88 ++++++++++++----------------- src/cluster/cluster.go | 9 +- src/main/config/config.yaml | 20 ++++++ src/util/util.go | 4 src/rancher/rancher.go | 15 ++++ install/etcd_cleanup.sh | 34 +++++++++++ 6 files changed, 111 insertions(+), 59 deletions(-) diff --git a/install/etcd_cleanup.sh b/install/etcd_cleanup.sh new file mode 100644 index 0000000..177af23 --- /dev/null +++ b/install/etcd_cleanup.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# Backup your data +# Use at your own risk +# Usage ./extended-cleanup-rancher2.sh +# Include clearing all iptables: ./extended-cleanup-rancher2.sh flush +containers=$(docker ps -qa) +[[ ! -z "$containers" ]] && docker rm -f $containers +images=$(docker images -q) +[[ ! -z "$images" ]] && docker rmi -f $images +volumes=$(docker volume ls -q) +[[ ! -z "$volumes" ]] && docker volume rm $volumes +for mount in $(mount | grep '/var/lib/kubelet' | awk '{ print $3 }') /var/lib/kubelet /var/lib/rancher; do umount $mount; done +cleanupdirs="/opt/rancher /etc/ceph /etc/cni /etc/kubernetes /opt/cni /opt/rke /run/secrets/kubernetes.io /run/calico /run/flannel /var/lib/calico /var/lib/etcd /var/lib/cni /var/lib/kubelet /var/lib/rancher/rke/log /var/log/containers /var/log/pods /var/run/calico" +for dir in $cleanupdirs; do + echo "Removing $dir" + rm -rf $dir +done +cleanupinterfaces="flannel.1 cni0 tunl0" +for interface in $cleanupinterfaces; do + echo "Deleting $interface" + ip link delete $interface +done +if [ "$1" = "flush" ]; then + echo "Parameter flush found, flushing all iptables" + iptables -F -t nat + iptables -X -t nat + iptables -F -t mangle + iptables -X -t mangle + iptables -F + iptables -X + service docker restart +else + echo "Parameter flush not found, iptables not cleaned" +fi diff --git a/src/cluster/cluster.go b/src/cluster/cluster.go index de960b7..39e167d 100644 --- a/src/cluster/cluster.go +++ b/src/cluster/cluster.go @@ -111,7 +111,7 @@ req, err := http.NewRequest("GET", url, nil) if err != nil { - return "", fmt.Errorf("failed to create cluster list request: %v", err) + return "", fmt.Errorf("failed to get cluster list request: %v", err) } req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", bearerToken)) @@ -247,16 +247,17 @@ return true, nil } -func CreateCluster(rancherConfig rancher.RancherConfig, clusterName string) error { +func CreateCluster(rancherClusterConfig rancher.RancherClusterConfig, clusterName string) error { requestBody := createClusterData(clusterName) + fmt.Println(rancherClusterConfig.RancherURL) - url := fmt.Sprintf("%s/v3/clusters", rancherConfig.RancherURL) + url := fmt.Sprintf("%s/v3/clusters", rancherClusterConfig.RancherURL) req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody)) if err != nil { return fmt.Errorf("Failed to create HTTP request: %v", err) } - req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rancherConfig.BearerToken)) + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", rancherClusterConfig.BearerToken)) req.Header.Set("Content-Type", "application/json") client := util.CreateHTTPClient() diff --git a/src/main/config/config.yaml b/src/main/config/config.yaml new file mode 100644 index 0000000..2b5f4ed --- /dev/null +++ b/src/main/config/config.yaml @@ -0,0 +1,20 @@ +RancherURL: "https://121.31.232.83:8443" +BearerToken: "token-ftsdj:qd26z229rzxdqz9r4vxzglqks22ww8sl86d5tfgg64krm94swf6c8p" +ClusterName: "kubernetus" +Nodes: + - Roles: ["worker"] + IP: "121.31.232.83" + SSHUsername: "root" + SSHPassword: "Y2Q5YTNlMT" + SSHPort: 22127 + - Roles: ["etcd", "worker"] + IP: "172.20.11.128" + SSHUsername: "root" + SSHPassword: "YmMxNjJjM2" + SSHPort: 22128 + - Roles: ["etcd", "controlplane", "worker"] + IP: "172.20.11.129" + SSHUsername: "root" + SSHPassword: "ZDMyZDY4OW" + SSHPort: 22129 + diff --git a/src/main/main.go b/src/main/main.go index dce6324..db889ec 100644 --- a/src/main/main.go +++ b/src/main/main.go @@ -26,17 +26,8 @@ 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{ +func rancher_install_test(node rancher.Node) { +/* nodes := []Node{ { Roles: []string{"etcd", "controlplane", "worker"}, IP: "192.168.20.189", @@ -59,10 +50,10 @@ 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) + err := rancher.InstallDockerAndRancher(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort) if err != nil { log.Fatalf("Failed to install Rancher: %v", err) } @@ -70,7 +61,7 @@ os.Exit(0) } -func cluster_test(clusterName string,nodes []Node,rancherConfig rancher.RancherConfig) { +func cluster_test(clusterName string,rancherClusterConfig rancher.RancherClusterConfig) { // Create the cluster // Rancher configuration @@ -79,16 +70,19 @@ BearerToken: "token-nnrsc:w68zdt8s47fnpjd5xqdl5hhzpz4j2d56kt5nx49nsswcbpdzc28kh5", }*/ + fmt.Println(rancherClusterConfig) + fmt.Println(rancherClusterConfig.RancherURL) + fmt.Println(rancherClusterConfig.BearerToken) // Deploy clusterId - clusterID, err := cluster.GetClusterID(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterName) + clusterID, err := cluster.GetClusterID(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterName) if err != nil { - log.Fatal(err) - err = cluster.CreateCluster(rancherConfig, clusterName) + fmt.Printf("Cluster not found: %s %v\n", clusterName, err) + err = cluster.CreateCluster(rancherClusterConfig, 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) + clusterID, err = cluster.GetClusterID(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterName) if err != nil { log.Fatal(err) } @@ -96,13 +90,13 @@ fmt.Println(clusterID) // Deploy nodeCommand - nodeCommand, err := cluster.GetNodeCommand(rancherConfig.RancherURL, rancherConfig.BearerToken, clusterID) + nodeCommand, err := cluster.GetNodeCommand(rancherClusterConfig.RancherURL, rancherClusterConfig.BearerToken, clusterID) if err != nil { log.Fatal(err) } fmt.Println(nodeCommand) - for _, node := range nodes { + for _, node := range rancherClusterConfig.Nodes { //Deploy Docker on each node err = util.InstallDocker(node.IP, node.SSHUsername, node.SSHPassword, node.SSHPort) if err != nil { @@ -161,7 +155,24 @@ } if rancherInstallCmd.Parsed() { - rancher_install_test() + // 鍒濆鍖栭厤缃В鏋愬簱 + viper.SetConfigName("config") + viper.SetConfigType("yaml") + viper.AddConfigPath("./config") + + // 璇诲彇閰嶇疆鏂囦欢 + err := viper.ReadInConfig() + if err != nil { + log.Fatalf("Failed to read config file: %v", err) + } + + // 瑙f瀽閰嶇疆鏂囦欢涓殑瀛楁 + var rancherClusterConfig rancher.RancherClusterConfig + err = viper.Unmarshal(&rancherClusterConfig) + if err != nil { + log.Fatalf("Failed to unmarshal config: %v", err) + } + rancher_install_test(rancherClusterConfig.Nodes[0]) os.Exit(1) } @@ -300,41 +311,14 @@ } // 瑙f瀽閰嶇疆鏂囦欢涓殑瀛楁 - var rancherConfig rancher.RancherConfig - err = viper.Unmarshal(&rancherConfig) + var rancherClusterConfig rancher.RancherClusterConfig + err = viper.Unmarshal(&rancherClusterConfig) if err != nil { log.Fatalf("Failed to unmarshal config: %v", err) } clusterName := "kubernetus" - nodes := []Node{ - { - ClusterName: clusterName, - Roles: []string{"worker"}, - IP: "192.168.20.189", - SSHUsername: "basic", - SSHPassword: "123", - SSHPort: 22, - }, - { - ClusterName: clusterName, - Roles: []string{"etcd", "worker"}, - IP: "192.168.20.10", - SSHUsername: "basic", - SSHPassword: "123", - SSHPort: 22, - }, - { - ClusterName: clusterName, - Roles: []string{"etcd", "controlplane","worker"}, - IP: "192.168.20.115", - SSHUsername: "basic", - SSHPassword: "alien123", - SSHPort: 22, - }, - // Add more nodes here if needed - } - - cluster_test(clusterName, nodes, rancherConfig) + fmt.Println(rancherClusterConfig) + cluster_test(clusterName, rancherClusterConfig) } } diff --git a/src/rancher/rancher.go b/src/rancher/rancher.go index 5bba2ba..80b2662 100644 --- a/src/rancher/rancher.go +++ b/src/rancher/rancher.go @@ -6,11 +6,24 @@ "basic.com/aps/aps_deploy.git/src/util" ) -type RancherConfig struct { + +type Node struct { + Roles []string `json:"roles"` + IP string `json:"ip"` + SSHUsername string `json:"sshUsername"` + SSHPassword string `json:"sshPassword"` + SSHPort int `json:"sshPort"` +} + + +type RancherClusterConfig struct { RancherURL string `json:"rancherURL"` BearerToken string `json:"bearerToken"` + ClusterName string `mapstructure:"ClusterName"` + Nodes []Node `mapstructure:"Nodes"` } + func isRancherInstalled(ip, username, password string, sshPort int) (bool, error) { // 妫�鏌ancher瀹瑰櫒鏄惁宸茶繍琛� checkRancherCommand := "sudo docker ps --format '{{.Image}}' | grep -q rancher/rancher:v2.5.17" diff --git a/src/util/util.go b/src/util/util.go index a26d5d8..f396fcd 100644 --- a/src/util/util.go +++ b/src/util/util.go @@ -107,10 +107,10 @@ } // 瀹夎Docker - installCommand := "sudo curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh && sudo systemctl restart docker" + installCommand := "sudo curl -fsSL https://get.docker.com -o get-docker.sh && sudo sh get-docker.sh && sudo systemctl start docker && sudo systemctl enable docker" _, err = SSHExec(nodeIP, sshUsername, sshPassword, installCommand, sshPort) if err != nil { - return fmt.Errorf("failed to install Docker on the remote server: %v", err) + return fmt.Errorf("failed to install Docker on the remote server:%v %v", nodeIP, err) } fmt.Println("Docker has been installed on the remote server.") -- Gitblit v1.8.0