From ceae117b21803bb5193eb3f77647e0505b647372 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Wed, 23 Jul 2025 08:39:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A8=E7=90=86=E4=BB=BB=E5=8A=A1=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cloud/container/option.go | 17 +++++------ cloud/service/service_test.go | 28 +++++++++++------- participant/k8s/k8s.go | 9 ++++-- participant/k8s/model/container.go | 8 +++--- participant/k8s/service/container.go | 41 ++++++++++++-------------- participant/k8s/service/pvc.go | 2 +- participant/k8s/service/svc.go | 43 ++++++++++++++++++++++++++++ 7 files changed, 100 insertions(+), 48 deletions(-) create mode 100644 participant/k8s/service/svc.go diff --git a/cloud/container/option.go b/cloud/container/option.go index d3c28df..ef430f6 100644 --- a/cloud/container/option.go +++ b/cloud/container/option.go @@ -1,14 +1,15 @@ package container type CreateParam struct { - ContainerGroupName string `json:"containerGroupName"` - Name string `json:"name"` - Image string `json:"image"` - Cpu string `json:"cpu,omitempty"` - Memory string `json:"memory,omitempty"` - Port int32 `json:"port,omitempty"` - HostPort int32 `json:"hostPort,omitempty"` - MountPath string `json:"mountPath,omitempty"` + ContainerGroupName string `json:"containerGroupName"` + Name string `json:"name"` + Image string `json:"image"` + Cpu string `json:"cpu,omitempty"` + Memory string `json:"memory,omitempty"` + Port int32 `json:"port,omitempty"` + NodePort int32 `json:"nodePort,omitempty"` + MountPath string `json:"mountPath,omitempty"` + Args []string `json:"args,omitempty"` Envs []struct { Name string `json:"name,omitempty"` Value string `json:"value,omitempty"` diff --git a/cloud/service/service_test.go b/cloud/service/service_test.go index 02503a5..9ace0a8 100644 --- a/cloud/service/service_test.go +++ b/cloud/service/service_test.go @@ -32,13 +32,18 @@ func TestCreateService(t *testing.T) { convey.Convey("all train algorithms", func() { param := container.CreateParam{ - ContainerGroupName: "hello-nginx", - Image: "nginx:latest", - Name: "hello-nginx", - Cpu: "1.0", - Memory: "1.0", + ContainerGroupName: "hello-llama", + Image: "ghcr.io/ggml-org/llama.cpp:server", + //Image: "nginx:latest", + Name: "hello-llama", + Args: []string{"-m", "/models/ERNIE-4.5-0.3B-PT-GGUF/ernie-4.5-0.3b-pt-q4_k_m.gguf", "--port", "8000", "--host", "0.0.0.0", "-n", "512"}, + MountPath: "/models", + Port: 8000, + NodePort: 30000, + Cpu: "100m", + Memory: "256Mi", } - ctn, err := svc.CreateContainer(ctx, 456, ¶m) + ctn, err := svc.CreateContainer(ctx, 123, ¶m) if err != nil { fmt.Println(err) } @@ -72,12 +77,13 @@ func TestDeleteService(t *testing.T) { convey.Convey("all train algorithms", func() { param := container.DeleteParam{ - DeleteParameter: &container.EciDeleteParam{ - RegionId: "cn-hangzhou", - ContainerGroupId: "eci-bp1f6qix5wkkeqhzoc77", - }, + //DeleteParameter: &container.EciDeleteParam{ + //RegionId: "cn-hangzhou", + //ContainerGroupId: "eci-bp1f6qix5wkkeqhzoc77", + //}, + Name: "hello-llama", } - err := svc.DeleteContainer(ctx, 456, ¶m) + err := svc.DeleteContainer(ctx, 123, ¶m) if err != nil { fmt.Println(err) } diff --git a/participant/k8s/k8s.go b/participant/k8s/k8s.go index 55afb00..25977ab 100644 --- a/participant/k8s/k8s.go +++ b/participant/k8s/k8s.go @@ -93,6 +93,7 @@ func (c *Container) Create(ctx context.Context, param *container.CreateParam) (i Container: model.Container{ Name: param.Name, Image: param.Image, + Args: param.Args, Limits: struct { Cpu string `json:"cpu,omitempty"` Memory string `json:"memory,omitempty"` @@ -102,12 +103,16 @@ func (c *Container) Create(ctx context.Context, param *container.CreateParam) (i }, ContainerPort: struct { Port int32 `json:"port,omitempty"` - HostPort int32 `json:"hostPort,omitempty"` - }{}, + NodePort int32 `json:"nodePort,omitempty"` + }{ + Port: param.Port, + NodePort: param.NodePort, + }, Envs: nil, Command: nil, }, ContainerGroupName: param.ContainerGroupName, + MountPath: param.MountPath, } err := service.CreateContainer(c.opt.ClientSet, &cParam) if err != nil { diff --git a/participant/k8s/model/container.go b/participant/k8s/model/container.go index 43252b9..2e3dfa7 100644 --- a/participant/k8s/model/container.go +++ b/participant/k8s/model/container.go @@ -4,18 +4,18 @@ type CreateContainerParam struct { ContainerGroupName string `json:"containerGroupName"` Container Container `json:"Container"` MountPath string `json:"mountPath,omitempty"` - PvcName string `json:"pvcName,omitempty"` } type Container struct { - Name string `json:"name"` - Image string `json:"image"` + Name string `json:"name"` + Image string `json:"image"` + Args []string `json:"args,omitempty"` Limits struct { Cpu string `json:"cpu,omitempty"` Memory string `json:"memory,omitempty"` } `json:"limits,omitempty"` ContainerPort struct { Port int32 `json:"port,omitempty"` - HostPort int32 `json:"hostPort,omitempty"` + NodePort int32 `json:"nodePort,omitempty"` } `json:"containerPorts,omitempty"` Envs []struct { Name string `json:"name,omitempty"` diff --git a/participant/k8s/service/container.go b/participant/k8s/service/container.go index 8c9d628..74a4095 100644 --- a/participant/k8s/service/container.go +++ b/participant/k8s/service/container.go @@ -27,25 +27,30 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP if !errors.IsNotFound(err) { return err } - // 创建pvc - if param.MountPath != "" { - pvcName, err := CreatePvc(client) + // 创建svc + if param.Container.ContainerPort.Port != 0 { + _, err := CreateSvc(client, param) if err != nil { return err } - param.PvcName = pvcName } // 创建 Pod 对象 pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Name: param.ContainerGroupName, // Pod 名称(必选) + Labels: map[string]string{ + "app": param.ContainerGroupName, + }, }, + Spec: v1.PodSpec{ + HostNetwork: true, Containers: []v1.Container{ // 至少一个容器(必选) { ImagePullPolicy: "Never", Name: param.Container.Name, Image: param.Container.Image, + Args: param.Container.Args, Resources: v1.ResourceRequirements{ Limits: v1.ResourceList{ v1.ResourceCPU: resource.MustParse(param.Container.Limits.Cpu), @@ -61,28 +66,20 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP // 挂载pvc if param.MountPath != "" { pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, v1.VolumeMount{ - Name: param.PvcName, + Name: "11mvgt1jh-pvc", MountPath: param.MountPath, }) pod.Spec.Volumes = append(pod.Spec.Volumes, v1.Volume{ - Name: param.PvcName, + Name: "11mvgt1jh-pvc", VolumeSource: v1.VolumeSource{ PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ - ClaimName: param.PvcName, + ClaimName: "11mvgt1jh-pvc", ReadOnly: false, }, }, }) } - // 设置端口 - for i := range param.Container.ContainerPort.Port { - pod.Spec.Containers[0].Ports[i] = v1.ContainerPort{ - ContainerPort: param.Container.ContainerPort.Port, - HostPort: param.Container.ContainerPort.HostPort, - } - } - // 设置环境变量 for i := range param.Container.Envs { pod.Spec.Containers[0].Env[i] = v1.EnvVar{ @@ -99,7 +96,7 @@ func CreateContainer(client *kubernetes.Clientset, param *model.CreateContainerP if err != nil { fmt.Printf(err.Error()) // 删除pvc - client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{}) + //client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{}) return err } @@ -119,12 +116,12 @@ func DeleteContainer(client *kubernetes.Clientset, param *model.DeleteContainerP return err } // 删除pvc - if pod.Spec.Volumes[0].PersistentVolumeClaim != nil { - err = client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{}) - if err != nil { - return err - } - } + //if pod.Spec.Volumes[0].PersistentVolumeClaim != nil { + // err = client.CoreV1().PersistentVolumeClaims("default").Delete(context.TODO(), pod.Spec.Volumes[0].PersistentVolumeClaim.ClaimName, metav1.DeleteOptions{}) + // if err != nil { + // return err + // } + //} return nil } diff --git a/participant/k8s/service/pvc.go b/participant/k8s/service/pvc.go index d3cfc0c..0e45d82 100644 --- a/participant/k8s/service/pvc.go +++ b/participant/k8s/service/pvc.go @@ -31,7 +31,7 @@ func CreatePvc(client *kubernetes.Clientset) (string, error) { }, Resources: v1.VolumeResourceRequirements{ Requests: v1.ResourceList{ - v1.ResourceStorage: resource.MustParse("10Gi"), + v1.ResourceStorage: resource.MustParse("1Gi"), }, }, StorageClassName: &sc, diff --git a/participant/k8s/service/svc.go b/participant/k8s/service/svc.go new file mode 100644 index 0000000..e61dc45 --- /dev/null +++ b/participant/k8s/service/svc.go @@ -0,0 +1,43 @@ +package service + +import ( + "context" + "gitlink.org.cn/JointCloud/pcm-participant-k8s/model" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/intstr" + "k8s.io/client-go/kubernetes" +) + +func CreateSvc(client *kubernetes.Clientset, param *model.CreateContainerParam) (string, error) { + + svc := v1.Service{ + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{ + Name: param.ContainerGroupName + "-service", + }, + + Spec: v1.ServiceSpec{ + Type: "NodePort", + Selector: map[string]string{ + "app": param.ContainerGroupName, + }, + Ports: []v1.ServicePort{ + { + Port: param.Container.ContainerPort.Port, + TargetPort: intstr.IntOrString{ + Type: intstr.Int, + IntVal: param.Container.ContainerPort.Port, + }, + NodePort: param.Container.ContainerPort.NodePort, + }, + }, + }, + } + _, err := client.CoreV1().Services("default").Create(context.Background(), &svc, metav1.CreateOptions{}) + if err != nil { + return "", err + } + return svc.Name, nil + +}