cheliequan
2023-05-31 d3a63ffbec643f7a2746dfe2db2313492ef163af
更新rancher部署代码
2个文件已添加
4个文件已修改
170 ■■■■■ 已修改文件
install/etcd_cleanup.sh 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/cluster/cluster.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/config/config.yaml 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/main.go 88 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/rancher/rancher.go 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/util/util.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
install/etcd_cleanup.sh
New file
@@ -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
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()
src/main/config/config.yaml
New file
@@ -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
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)
        }
        // 解析配置文件中的字段
        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 @@
        }
        // 解析配置文件中的字段
        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)
    }
}
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) {
    // 检查Rancher容器是否已运行
    checkRancherCommand := "sudo docker ps --format '{{.Image}}' | grep -q rancher/rancher:v2.5.17"
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.")