zhangqian
2023-12-26 a2667454aa39ea72b0e3660dca7dc4f468d712a2
src/k8s/create.go
@@ -31,11 +31,15 @@
   Client         *kubernetes.Clientset // Kubernetes 客户端
   Image          string                // 镜像名称
   DBHost         string                // 数据库地址
   DBName         string                //数据库名称
   HttpPort       int32                 // HTTP 端口
   RpcPort        int32                 // RPC 端口
   NameSpace      string                // Namespace
   DeploymentName string                // Deployment 名称
   ServiceName    string                // Service 名称
   ALHost         string                // 算法Host
   Host           string                // 服务Host
   NodeID         string                // Nsq节点ID
}
func create_test() {
@@ -134,12 +138,6 @@
      return err
   }
   // 创建 Namespace
   err = createNamespace(config.Client, config.NameSpace)
   if err != nil {
      return err
   }
   config.HttpPort, config.RpcPort, err = getTwoNodePort(config.Client)
   if err != nil {
      return err
@@ -161,8 +159,8 @@
   return nil
}
// createNamespace 创建指定的 Namespace
func createNamespace(clientset *kubernetes.Clientset, namespace string) error {
// CreateNamespace 创建指定的 Namespace
func CreateNamespace(clientset *kubernetes.Clientset, namespace string) error {
   log.Printf("Creating Namespace: %s\n", namespace)
   ns := &apiv1.Namespace{
@@ -186,6 +184,61 @@
func createDeployment(config Config) error {
   fmt.Println("\033[1;37;40mCreating Deployment:", config.DeploymentName, "\033[0m")
   envs := []apiv1.EnvVar{
      {
         Name:  "GRPC_PORT",
         Value: fmt.Sprint(config.RpcPort),
      },
      {
         Name: "DB_HOST",
         ValueFrom: &apiv1.EnvVarSource{
            FieldRef: &apiv1.ObjectFieldSelector{
               APIVersion: "v1",
               FieldPath:  "status.hostIP",
            },
         },
      },
      {
         Name: "HOST",
         ValueFrom: &apiv1.EnvVarSource{
            FieldRef: &apiv1.ObjectFieldSelector{
               APIVersion: "v1",
               FieldPath:  "status.hostIP",
            },
         },
      },
      {
         Name:  "DB_NAME",
         Value: config.DBName,
      },
      {
         Name:  "DB_PORT",
         Value: strconv.Itoa(6446),
      },
      {
         Name:  "DB_USER",
         Value: config.NameSpace,
      },
      {
         Name:  "DB_PASSWD",
         Value: config.NameSpace + "@Basic2023",
      },
   }
   if config.ALHost != "" {
      envs = append(envs, apiv1.EnvVar{
         Name:  "AL_HOST",
         Value: config.ALHost,
      })
   }
   if config.NodeID != "" {
      envs = append(envs, apiv1.EnvVar{
         Name:  "NODE_ID",
         Value: config.NodeID,
      })
   }
   deployment := &appsv1.Deployment{
      ObjectMeta: metav1.ObjectMeta{
@@ -219,38 +272,9 @@
               },
               Containers: []apiv1.Container{
                  {
                     Name:  config.NameSpace,
                     Image: config.Image,
                     Env: []apiv1.EnvVar{
                        {
                           Name:  "GRPC_PORT",
                           Value: fmt.Sprint(config.RpcPort),
                        },
                        {
                           Name:  "DB_HOST",
                           Value: "172.20.11.128",
                        },
                        {
                           Name:  "GRPC_HOST",
                           Value: "0.0.0.0",
                        },
                        {
                           Name:  "DB_NAME",
                           Value: config.NameSpace,
                        },
                        {
                           Name:  "DB_PORT",
                           Value: strconv.Itoa(3306),
                        },
                        {
                           Name:  "DB_USER",
                           Value: config.NameSpace,
                        },
                        {
                           Name:  "DB_PASSWD",
                           Value: config.NameSpace + "@Basic2023",
                        },
                     },
                     Name:            config.NameSpace,
                     Image:           config.Image,
                     Env:             envs,
                     ImagePullPolicy: apiv1.PullAlways, // 设置镜像拉取策略为 Always
                  },
               },