| | |
| | | usedNodePorts = make(map[int32]bool) |
| | | ) |
| | | |
| | | type Config struct { |
| | | Client *kubernetes.Clientset // Kubernetes 客户端 |
| | | Image string // 镜像名称 |
| | | DBHost string // 数据库地址 |
| | | HttpPort int32 // HTTP 端口 |
| | | RpcPort int32 // RPC 端口 |
| | | NameSpace string // Namespace |
| | | DeploymentName string // Deployment 名称 |
| | | ServiceName string // Service 名称 |
| | | } |
| | | |
| | | func create_test() { |
| | | // 配置 Kubernetes 集群的 kubeconfig 路径 |
| | | kubeconfig := flag.String("kubeconfig", filepath.Join(util.HomeDir(), ".kube", "config"), "kubeconfig file") |
| | |
| | | |
| | | // 创建多个 Namespace 下的相同名称的 Deployment 和 Service |
| | | for _, ns := range namespaces { |
| | | err = CreateDeploymentAndService(clientset, ns, ns, ns) |
| | | err = CreateDeploymentAndService(Config{Client: clientset, NameSpace: ns, DeploymentName: ns, ServiceName: ns}) |
| | | if err != nil { |
| | | panic(err) |
| | | } |
| | |
| | | return nil |
| | | } |
| | | |
| | | func CreateDeploymentAndService(clientset *kubernetes.Clientset, namespace, deploymentName, serviceName string) error { |
| | | fmt.Println("\033[1;37;40mCreating resources in Namespace:", namespace, "\033[0m") |
| | | func CreateDeploymentAndService(config Config) error { |
| | | fmt.Println("\033[1;37;40mCreating resources in Namespace:", config.NameSpace, "\033[0m") |
| | | |
| | | // 检测并删除已存在的 Deployment 和 Service |
| | | err := CheckAndDeleteResources(clientset, namespace, deploymentName, serviceName) |
| | | err := CheckAndDeleteResources(config.Client, config.NameSpace, config.DeploymentName, config.ServiceName) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | // 创建 Namespace |
| | | err = createNamespace(clientset, namespace) |
| | | err = createNamespace(config.Client, config.NameSpace) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | nodePort1, nodePort2, err := getTwoNodePort(clientset) |
| | | config.HttpPort, config.RpcPort, err = getTwoNodePort(config.Client) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | port1 := fmt.Sprint(nodePort1) |
| | | port2 := fmt.Sprint(nodePort2) |
| | | // 创建 Deployment |
| | | err = createDeployment(clientset, namespace, deploymentName, port1, port2) |
| | | err = createDeployment(config) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | log.Printf("Waiting for Deployment %s to be ready...\n", deploymentName) |
| | | log.Printf("Waiting for Deployment %s to be ready...\n", config.DeploymentName) |
| | | // 创建 Service |
| | | err = createService(clientset, namespace, serviceName, nodePort1, nodePort2) |
| | | err = createService(config) |
| | | if err != nil { |
| | | return err |
| | | } |
| | |
| | | return nil |
| | | } |
| | | |
| | | func createDeployment(clientset *kubernetes.Clientset, namespace, deploymentName, port1, port2 string) error { |
| | | fmt.Println("\033[1;37;40mCreating Deployment:", deploymentName, "\033[0m") |
| | | func createDeployment(config Config) error { |
| | | fmt.Println("\033[1;37;40mCreating Deployment:", config.DeploymentName, "\033[0m") |
| | | |
| | | deployment := &appsv1.Deployment{ |
| | | ObjectMeta: metav1.ObjectMeta{ |
| | | Name: deploymentName, |
| | | Name: config.DeploymentName, |
| | | }, |
| | | Spec: appsv1.DeploymentSpec{ |
| | | Replicas: &replicas, |
| | | Selector: &metav1.LabelSelector{ |
| | | MatchLabels: map[string]string{ |
| | | "cid": namespace, |
| | | "cid": config.NameSpace, |
| | | }, |
| | | }, |
| | | Template: apiv1.PodTemplateSpec{ |
| | | ObjectMeta: metav1.ObjectMeta{ |
| | | Labels: map[string]string{ |
| | | "cid": namespace, |
| | | "cid": config.NameSpace, |
| | | }, |
| | | }, |
| | | Spec: apiv1.PodSpec{ |
| | |
| | | WhenUnsatisfiable: apiv1.DoNotSchedule, |
| | | LabelSelector: &metav1.LabelSelector{ |
| | | MatchLabels: map[string]string{ |
| | | "cid": namespace, |
| | | "cid": config.NameSpace, |
| | | }, |
| | | }, |
| | | }, |
| | | }, |
| | | Containers: []apiv1.Container{ |
| | | { |
| | | Name: namespace, |
| | | Image: "harbor.smartai.com/apsserver/apsserver:v0.2", |
| | | Name: config.NameSpace, |
| | | Image: config.Image, |
| | | Env: []apiv1.EnvVar{ |
| | | { |
| | | Name: "GRPC_PORT", |
| | | Value: port1, |
| | | Value: fmt.Sprint(config.RpcPort), |
| | | }, |
| | | // todo 从配置文件中读取 |
| | | { |
| | | Name: "DB_HOST", |
| | | Value: "172.20.11.128", |
| | |
| | | }, |
| | | { |
| | | Name: "DB_NAME", |
| | | Value: namespace, |
| | | Value: config.NameSpace, |
| | | }, |
| | | { |
| | | Name: "DB_PORT", |
| | |
| | | }, |
| | | { |
| | | Name: "DB_USER", |
| | | Value: namespace, |
| | | Value: config.NameSpace, |
| | | }, |
| | | { |
| | | Name: "DB_PASSWD", |
| | | Value: namespace + "@Basic2023", |
| | | Value: config.NameSpace + "@Basic2023", |
| | | }, |
| | | }, |
| | | ImagePullPolicy: apiv1.PullAlways, // 设置镜像拉取策略为 Always |
| | |
| | | }, |
| | | } |
| | | |
| | | _, err := clientset.AppsV1().Deployments(namespace).Create(context.TODO(), deployment, metav1.CreateOptions{}) |
| | | _, err := config.Client.AppsV1().Deployments(config.DeploymentName).Create(context.TODO(), deployment, metav1.CreateOptions{}) |
| | | if err != nil { |
| | | if !errors.IsAlreadyExists(err) { |
| | | return fmt.Errorf("failed to create Deployment: %v", err) |
| | | } |
| | | fmt.Printf("Deployment %s already exists in Namespace %s\n", deploymentName, namespace) |
| | | fmt.Printf("Deployment %s already exists in Namespace %s\n", config.DeploymentName, config.NameSpace) |
| | | } else { |
| | | fmt.Printf("Deployment %s created in Namespace %s\n", deploymentName, namespace) |
| | | fmt.Printf("Deployment %s created in Namespace %s\n", config.DeploymentName, config.NameSpace) |
| | | } |
| | | |
| | | return nil |
| | | } |
| | | |
| | | // createService 创建指定的 Service |
| | | func createService(clientset *kubernetes.Clientset, namespace, serviceName string, port1, port2 int32) error { |
| | | fmt.Println("\033[1;37;40mCreating Service:", serviceName, "\033[0m") |
| | | func createService(config Config) error { |
| | | fmt.Println("\033[1;37;40mCreating Service:", config.ServiceName, "\033[0m") |
| | | |
| | | service := &apiv1.Service{ |
| | | ObjectMeta: metav1.ObjectMeta{ |
| | | Name: serviceName, |
| | | Name: config.ServiceName, |
| | | }, |
| | | Spec: apiv1.ServiceSpec{ |
| | | Selector: map[string]string{ |
| | | "cid": namespace, |
| | | "cid": config.NameSpace, |
| | | }, |
| | | Type: apiv1.ServiceTypeNodePort, |
| | | Ports: []apiv1.ServicePort{ |
| | |
| | | Protocol: apiv1.ProtocolTCP, |
| | | Port: port, // 集群内部访问端口 |
| | | TargetPort: intstr.FromInt(int(port)), // 容器对外端口 |
| | | NodePort: port1, // 外部访问端口 |
| | | NodePort: config.HttpPort, // 外部访问端口 |
| | | }, |
| | | { |
| | | Name: "tcp", |
| | | Protocol: apiv1.ProtocolTCP, |
| | | Port: rpcPort, |
| | | TargetPort: intstr.FromInt(int(rpcPort)), |
| | | NodePort: port2, |
| | | NodePort: config.RpcPort, |
| | | }, |
| | | }, |
| | | SessionAffinity: apiv1.ServiceAffinityClientIP, |
| | | }, |
| | | } |
| | | |
| | | _, err := clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{}) |
| | | _, err := config.Client.CoreV1().Services(config.ServiceName).Create(context.TODO(), service, metav1.CreateOptions{}) |
| | | |
| | | if err != nil { |
| | | if !errors.IsAlreadyExists(err) { |
| | | return fmt.Errorf("failed to create Service: %v", err) |
| | | } |
| | | log.Printf("Service %s already exists in Namespace %s\n", serviceName, namespace) |
| | | log.Printf("Service %s already exists in Namespace %s\n", config.ServiceName, config.NameSpace) |
| | | } else { |
| | | log.Printf("Service %s created in Namespace %s\n", serviceName, namespace) |
| | | log.Printf("Service %s created in Namespace %s\n", config.ServiceName, config.NameSpace) |
| | | } |
| | | |
| | | return nil |