From 4b717f9a0ed341163f4891f3007f990f658262d3 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 11 一月 2024 20:12:14 +0800
Subject: [PATCH] 创建clusterIP service
---
src/k8s/create.go | 18 +++--
src/k8s/create_wms.go | 45 +++++++++++++--
src/k8s/create_crm.go | 45 +++++++++++++--
src/k8s/create_srm.go | 45 +++++++++++++--
4 files changed, 127 insertions(+), 26 deletions(-)
diff --git a/src/k8s/create.go b/src/k8s/create.go
index 488c600..b08e0d9 100644
--- a/src/k8s/create.go
+++ b/src/k8s/create.go
@@ -48,6 +48,8 @@
ALHost string // 绠楁硶Host
Host string // 鏈嶅姟Host
NodeID string // Nsq鑺傜偣ID
+ HttpNodePort int32 // HTTP nodePort绔彛
+ RpcNodePort int32 // RPC nodePort绔彛
}
func create_test() {
@@ -298,7 +300,7 @@
return err
}
- config.HttpPort, config.RpcPort, err = getTwoNodePort(config.Client)
+ config.HttpNodePort, config.RpcNodePort, err = getTwoNodePort(config.Client)
if err != nil {
return err
}
@@ -350,7 +352,7 @@
},
{
Name: "GRPC_NODE_PORT",
- Value: strconv.Itoa(int(config.RpcPort)),
+ Value: strconv.Itoa(int(config.RpcNodePort)),
},
{
Name: "NODE_ID",
@@ -459,16 +461,16 @@
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
- Port: port, // 闆嗙兢鍐呴儴璁块棶绔彛
- TargetPort: intstr.FromInt(int(port)), // 瀹瑰櫒瀵瑰绔彛
- NodePort: config.HttpPort, // 澶栭儴璁块棶绔彛
+ Port: config.HttpPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.HttpPort)), // 瀹瑰櫒瀵瑰绔彛
+ NodePort: config.HttpNodePort, // 澶栭儴璁块棶绔彛
},
{
Name: "tcp",
Protocol: apiv1.ProtocolTCP,
- Port: rpcPort,
- TargetPort: intstr.FromInt(int(rpcPort)),
- NodePort: config.RpcPort,
+ Port: config.RpcPort,
+ TargetPort: intstr.FromInt(int(config.RpcPort)),
+ NodePort: config.RpcNodePort,
},
},
SessionAffinity: apiv1.ServiceAffinityClientIP,
diff --git a/src/k8s/create_crm.go b/src/k8s/create_crm.go
index 46c967c..02e2600 100644
--- a/src/k8s/create_crm.go
+++ b/src/k8s/create_crm.go
@@ -23,7 +23,7 @@
return err
}
- config.HttpPort, err = getRandomNodePort(config.Client)
+ config.HttpNodePort, err = getRandomNodePort(config.Client)
if err != nil {
return err
}
@@ -138,11 +138,12 @@
// createService 鍒涘缓鎸囧畾鐨� Service
func (c *CrmCreate) CreateService(config Config) error {
- fmt.Println("\033[1;37;40mCreating Service:", config.ServiceName, "\033[0m")
+ httpServiceName := config.ServiceName + "-http"
+ fmt.Println("\033[1;37;40mCreating Service:", httpServiceName, "\033[0m")
service := &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
- Name: config.ServiceName,
+ Name: httpServiceName,
},
Spec: apiv1.ServiceSpec{
Selector: map[string]string{
@@ -153,9 +154,9 @@
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
- Port: port, // 闆嗙兢鍐呴儴璁块棶绔彛
- TargetPort: intstr.FromInt(int(port)), // 瀹瑰櫒瀵瑰绔彛
- NodePort: config.HttpPort, // 澶栭儴璁块棶绔彛
+ Port: config.HttpPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.HttpPort)), // 瀹瑰櫒瀵瑰绔彛
+ NodePort: config.HttpNodePort, // 澶栭儴璁块棶绔彛
},
},
SessionAffinity: apiv1.ServiceAffinityClientIP,
@@ -168,6 +169,38 @@
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create Service: %v", err)
}
+ log.Printf("Service %s already exists in Namespace %s\n", httpServiceName, config.NameSpace)
+ } else {
+ log.Printf("Service %s created in Namespace %s\n", httpServiceName, config.NameSpace)
+ }
+
+ clusterIPService := &apiv1.Service{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: config.ServiceName,
+ },
+ Spec: apiv1.ServiceSpec{
+ Selector: map[string]string{
+ "cid": c.GetCid(config),
+ },
+ Type: apiv1.ServiceTypeClusterIP,
+ Ports: []apiv1.ServicePort{
+ {
+ Name: "grpc",
+ Protocol: apiv1.ProtocolTCP,
+ Port: config.RpcPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.RpcPort)), // 瀹瑰櫒瀵瑰绔彛
+ },
+ },
+ SessionAffinity: apiv1.ServiceAffinityClientIP,
+ },
+ }
+
+ _, err = config.Client.CoreV1().Services(config.NameSpace).Create(context.TODO(), clusterIPService, metav1.CreateOptions{})
+
+ if err != nil {
+ if !errors.IsAlreadyExists(err) {
+ return fmt.Errorf("failed to create clusterIP Service: %v", err)
+ }
log.Printf("Service %s already exists in Namespace %s\n", config.ServiceName, config.NameSpace)
} else {
log.Printf("Service %s created in Namespace %s\n", config.ServiceName, config.NameSpace)
diff --git a/src/k8s/create_srm.go b/src/k8s/create_srm.go
index a9c9dce..98b886d 100644
--- a/src/k8s/create_srm.go
+++ b/src/k8s/create_srm.go
@@ -23,7 +23,7 @@
return err
}
- config.HttpPort, err = getRandomNodePort(config.Client)
+ config.HttpNodePort, err = getRandomNodePort(config.Client)
if err != nil {
return err
}
@@ -138,11 +138,12 @@
// createService 鍒涘缓鎸囧畾鐨� Service
func (c *SrmCreate) CreateService(config Config) error {
- fmt.Println("\033[1;37;40mCreating Service:", config.ServiceName, "\033[0m")
+ httpServiceName := config.ServiceName + "-http"
+ fmt.Println("\033[1;37;40mCreating Service:", httpServiceName, "\033[0m")
service := &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
- Name: config.ServiceName,
+ Name: httpServiceName,
},
Spec: apiv1.ServiceSpec{
Selector: map[string]string{
@@ -153,9 +154,9 @@
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
- Port: port, // 闆嗙兢鍐呴儴璁块棶绔彛
- TargetPort: intstr.FromInt(int(port)), // 瀹瑰櫒瀵瑰绔彛
- NodePort: config.HttpPort, // 澶栭儴璁块棶绔彛
+ Port: config.HttpPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.HttpPort)), // 瀹瑰櫒瀵瑰绔彛
+ NodePort: config.HttpNodePort, // 澶栭儴璁块棶绔彛
},
},
SessionAffinity: apiv1.ServiceAffinityClientIP,
@@ -168,6 +169,38 @@
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create Service: %v", err)
}
+ log.Printf("Service %s already exists in Namespace %s\n", httpServiceName, config.NameSpace)
+ } else {
+ log.Printf("Service %s created in Namespace %s\n", httpServiceName, config.NameSpace)
+ }
+
+ clusterIPService := &apiv1.Service{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: config.ServiceName,
+ },
+ Spec: apiv1.ServiceSpec{
+ Selector: map[string]string{
+ "cid": c.GetCid(config),
+ },
+ Type: apiv1.ServiceTypeClusterIP,
+ Ports: []apiv1.ServicePort{
+ {
+ Name: "grpc",
+ Protocol: apiv1.ProtocolTCP,
+ Port: config.RpcPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.RpcPort)), // 瀹瑰櫒瀵瑰绔彛
+ },
+ },
+ SessionAffinity: apiv1.ServiceAffinityClientIP,
+ },
+ }
+
+ _, err = config.Client.CoreV1().Services(config.NameSpace).Create(context.TODO(), clusterIPService, metav1.CreateOptions{})
+
+ if err != nil {
+ if !errors.IsAlreadyExists(err) {
+ return fmt.Errorf("failed to create clusterIP Service: %v", err)
+ }
log.Printf("Service %s already exists in Namespace %s\n", config.ServiceName, config.NameSpace)
} else {
log.Printf("Service %s created in Namespace %s\n", config.ServiceName, config.NameSpace)
diff --git a/src/k8s/create_wms.go b/src/k8s/create_wms.go
index 14abb26..399e6d7 100644
--- a/src/k8s/create_wms.go
+++ b/src/k8s/create_wms.go
@@ -23,7 +23,7 @@
return err
}
- config.HttpPort, err = getRandomNodePort(config.Client)
+ config.HttpNodePort, err = getRandomNodePort(config.Client)
if err != nil {
return err
}
@@ -138,11 +138,12 @@
// createService 鍒涘缓鎸囧畾鐨� Service
func (c *WmsCreate) CreateService(config Config) error {
- fmt.Println("\033[1;37;40mCreating Service:", config.ServiceName, "\033[0m")
+ httpServiceName := config.ServiceName + "-http"
+ fmt.Println("\033[1;37;40mCreating Service:", httpServiceName, "\033[0m")
service := &apiv1.Service{
ObjectMeta: metav1.ObjectMeta{
- Name: config.ServiceName,
+ Name: httpServiceName,
},
Spec: apiv1.ServiceSpec{
Selector: map[string]string{
@@ -153,9 +154,9 @@
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
- Port: port, // 闆嗙兢鍐呴儴璁块棶绔彛
- TargetPort: intstr.FromInt(int(port)), // 瀹瑰櫒瀵瑰绔彛
- NodePort: config.HttpPort, // 澶栭儴璁块棶绔彛
+ Port: config.HttpPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.HttpPort)), // 瀹瑰櫒瀵瑰绔彛
+ NodePort: config.HttpNodePort, // 澶栭儴璁块棶绔彛
},
},
SessionAffinity: apiv1.ServiceAffinityClientIP,
@@ -168,6 +169,38 @@
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("failed to create Service: %v", err)
}
+ log.Printf("Service %s already exists in Namespace %s\n", httpServiceName, config.NameSpace)
+ } else {
+ log.Printf("Service %s created in Namespace %s\n", httpServiceName, config.NameSpace)
+ }
+
+ clusterIPService := &apiv1.Service{
+ ObjectMeta: metav1.ObjectMeta{
+ Name: config.ServiceName,
+ },
+ Spec: apiv1.ServiceSpec{
+ Selector: map[string]string{
+ "cid": c.GetCid(config),
+ },
+ Type: apiv1.ServiceTypeClusterIP,
+ Ports: []apiv1.ServicePort{
+ {
+ Name: "grpc",
+ Protocol: apiv1.ProtocolTCP,
+ Port: config.RpcPort, // 闆嗙兢鍐呴儴璁块棶绔彛
+ TargetPort: intstr.FromInt(int(config.RpcPort)), // 瀹瑰櫒瀵瑰绔彛
+ },
+ },
+ SessionAffinity: apiv1.ServiceAffinityClientIP,
+ },
+ }
+
+ _, err = config.Client.CoreV1().Services(config.NameSpace).Create(context.TODO(), clusterIPService, metav1.CreateOptions{})
+
+ if err != nil {
+ if !errors.IsAlreadyExists(err) {
+ return fmt.Errorf("failed to create clusterIP Service: %v", err)
+ }
log.Printf("Service %s already exists in Namespace %s\n", config.ServiceName, config.NameSpace)
} else {
log.Printf("Service %s created in Namespace %s\n", config.ServiceName, config.NameSpace)
--
Gitblit v1.8.0