| | |
| | | "log" |
| | | "math/rand" |
| | | "path/filepath" |
| | | "strconv" |
| | | |
| | | "basic.com/aps/aps_deploy.git/src/util" |
| | | appsv1 "k8s.io/api/apps/v1" |
| | |
| | | var ( |
| | | replicas int32 = 3 |
| | | port int32 = 9081 |
| | | rpcPort int32 = 9091 |
| | | namespaces = []string{"guangsheng", "geruimi", "tongsheng"} |
| | | usedNodePorts = make(map[int32]bool) |
| | | ) |
| | | |
| | | func main() { |
| | | func create_test() { |
| | | // 配置 Kubernetes 集群的 kubeconfig 路径 |
| | | kubeconfig := flag.String("kubeconfig", filepath.Join(util.HomeDir(), ".kube", "config"), "kubeconfig file") |
| | | flag.Parse() |
| | |
| | | return err |
| | | } |
| | | |
| | | // 创建 Deployment |
| | | err = createDeployment(clientset, namespace, deploymentName) |
| | | nodePort1, nodePort2, err := getTwoNodePort(clientset) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | port1 := fmt.Sprint(nodePort1) |
| | | port2 := fmt.Sprint(nodePort2) |
| | | // 创建 Deployment |
| | | err = createDeployment(clientset, namespace, deploymentName, port1, port2) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | log.Printf("Waiting for Deployment %s to be ready...\n", deploymentName) |
| | | // 创建 Service |
| | | err = createService(clientset, namespace, serviceName) |
| | | err = createService(clientset, namespace, serviceName, nodePort1, nodePort2) |
| | | if err != nil { |
| | | return err |
| | | } |
| | |
| | | return nil |
| | | } |
| | | |
| | | func createDeployment(clientset *kubernetes.Clientset, namespace, deploymentName string) error { |
| | | func createDeployment(clientset *kubernetes.Clientset, namespace, deploymentName, port1, port2 string) error { |
| | | fmt.Println("\033[1;37;40mCreating Deployment:", deploymentName, "\033[0m") |
| | | |
| | | deployment := &appsv1.Deployment{ |
| | |
| | | Containers: []apiv1.Container{ |
| | | { |
| | | Name: namespace, |
| | | Image: "192.168.20.119/apsserver/apsserver:v0.2", |
| | | Image: "harbor.smartai.com/apsserver/apsserver:v0.1", |
| | | Env: []apiv1.EnvVar{ |
| | | { |
| | | Name: "NODE_ID", |
| | | Name: "GRPC_PORT", |
| | | Value: port1, |
| | | }, |
| | | // todo 从配置文件中读取 |
| | | { |
| | | Name: "DB_HOST", |
| | | Value: "172.20.11.128", |
| | | }, |
| | | { |
| | | Name: "GRPC_HOST", |
| | | Value: "0.0.0.0", |
| | | }, |
| | | { |
| | | Name: "DB_NAME", |
| | | Value: namespace, |
| | | }, |
| | | { |
| | | Name: "DB_PORT", |
| | | Value: strconv.Itoa(3306), |
| | | }, |
| | | { |
| | | Name: "DB_USER", |
| | | Value: namespace, |
| | | }, |
| | | { |
| | | Name: "DB_PASSWD", |
| | | Value: namespace + "@Basic2023", |
| | | }, |
| | | }, |
| | | ImagePullPolicy: apiv1.PullAlways, // 设置镜像拉取策略为 Always |
| | | }, |
| | | }, |
| | | }, |
| | |
| | | } |
| | | |
| | | // createService 创建指定的 Service |
| | | func createService(clientset *kubernetes.Clientset, namespace, serviceName string) error { |
| | | func createService(clientset *kubernetes.Clientset, namespace, serviceName string, port1, port2 int32) error { |
| | | fmt.Println("\033[1;37;40mCreating Service:", serviceName, "\033[0m") |
| | | |
| | | nodePort, err := getRandomNodePort(clientset) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | service := &apiv1.Service{ |
| | | ObjectMeta: metav1.ObjectMeta{ |
| | |
| | | { |
| | | Name: "http", |
| | | Protocol: apiv1.ProtocolTCP, |
| | | Port: port, |
| | | TargetPort: intstr.FromInt(int(port)), |
| | | NodePort: nodePort, |
| | | Port: port, // 集群内部访问端口 |
| | | TargetPort: intstr.FromInt(int(port)), // 容器对外端口 |
| | | NodePort: port1, // 外部访问端口 |
| | | }, |
| | | { |
| | | Name: "tcp", |
| | | Protocol: apiv1.ProtocolTCP, |
| | | Port: rpcPort, |
| | | TargetPort: intstr.FromInt(int(rpcPort)), |
| | | NodePort: port2, |
| | | }, |
| | | }, |
| | | }, |
| | | } |
| | | |
| | | _, err = clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{}) |
| | | _, err := clientset.CoreV1().Services(namespace).Create(context.TODO(), service, metav1.CreateOptions{}) |
| | | |
| | | if err != nil { |
| | | if !errors.IsAlreadyExists(err) { |
| | | return fmt.Errorf("failed to create Service: %v", err) |
| | | } |
| | | fmt.Printf("Service %s already exists in Namespace %s\n", serviceName, namespace) |
| | | log.Printf("Service %s already exists in Namespace %s\n", serviceName, namespace) |
| | | } else { |
| | | fmt.Printf("Service %s created in Namespace %s\n", serviceName, namespace) |
| | | log.Printf("Service %s created in Namespace %s\n", serviceName, namespace) |
| | | } |
| | | |
| | | return nil |
| | | } |
| | | |
| | | func getTwoNodePort(client *kubernetes.Clientset) (nodePort1, nodePort2 int32, err error) { |
| | | nodePort1, err = getRandomNodePort(client) |
| | | if err != nil { |
| | | return 0, 0, err |
| | | } |
| | | |
| | | for { |
| | | nodePort2, err = getRandomNodePort(client) |
| | | if err != nil { |
| | | return 0, 0, err |
| | | } |
| | | |
| | | if nodePort2 != nodePort1 { |
| | | break |
| | | } |
| | | |
| | | } |
| | | |
| | | return nodePort1, nodePort2, nil |
| | | } |
| | | |
| | | // getRandomNodePort 获取一个未使用的随机 NodePort |
| | |
| | | |
| | | // 获取第一个端口的 NodePort |
| | | if len(svc.Spec.Ports) > 0 { |
| | | return svc.Spec.Ports[0].NodePort, nil |
| | | for _, p := range svc.Spec.Ports { |
| | | // return tcp port |
| | | if p.Name == "http" { |
| | | return p.NodePort, nil |
| | | } |
| | | } |
| | | } |
| | | |
| | | return 0, fmt.Errorf("no ports defined for Service %s", serviceName) |