k8s java-client 创建service

yaml创建

实践中发现,必须得有selector,且与deployment的标签一致,如都是"app: nginx",否则不能用创建出来的service来访问nginx

apiVersion: v1
kind: Service
metadata:
  name: nginx-service	#Service 的名称
  labels:     	#Service 自己的标签
    app: nginx	#为该 Service 设置 key 为 app,value 为 nginx 的标签
spec:	    #这是关于该 Service 的定义,描述了 Service 如何选择 Pod,如何被访问
  selector:	    #标签选择器
    app: nginx	#选择包含标签 app:nginx 的 Pod
  ports:
  - name: nginx-port	#端口的名字
    protocol: TCP	    #协议类型 TCP/UDP
    port: 80	        #集群内的其他容器组可通过 80 端口访问 Service
    nodePort: 32600   #通过任意节点的 32600 端口访问 Service
    targetPort: 80	#将请求转发到匹配 Pod 的 80 端口
  type: NodePort	#Serive的类型,ClusterIP/NodePort/LoaderBalancer

k8s api创建
@Data
public class ServiceDTO {
    /**
     * apiVersion: v1
     * kind: Service
     * metadata:
     *   name: nginx-service	#Service 的名称
     *   labels:     	#Service 自己的标签
     *     app: nginx	#为该 Service 设置 key 为 app,value 为 nginx 的标签
     * spec:	    #这是关于该 Service 的定义,描述了 Service 如何选择 Pod,如何被访问
     *   selector:	    #标签选择器
     *     app: nginx	#选择包含标签 app:nginx 的 Pod
     *   ports:
     *   - name: nginx-port	#端口的名字
     *     protocol: TCP	    #协议类型 TCP/UDP
     *     port: 80	        #集群内的其他容器组可通过 80 端口访问 Service
     *     nodePort: 32600   #通过任意节点的 32600 端口访问 Service
     *     targetPort: 80	#将请求转发到匹配 Pod 的 80 端口
     *   type: NodePort	#Serive的类型,ClusterIP/NodePort/LoaderBalancer
     */
    private String metadataName;
    private String metadataLabelsApp;
    private String specSelectorApp;
    private String specPortsName;
    private String specPortsProtocol;
    private Integer specPortsPort;
    private Integer specPortsNodePort;
    private IntOrString specPortsTargetPort;
    private String specType;

}

    /**
     * 创建一个service
     * @param namespace
     * @param serviceDTO
     * @return
     */
    @PostMapping("/createNamespacedService/{namespace}")
    public ResultUtil createNamespacedService(@PathVariable String namespace,
                                              @RequestBody ServiceDTO serviceDTO) {
        Gson gson = new Gson();
        CoreV1Api apiInstance = new CoreV1Api(k8sInit.getConnection());
        V1Service result;
        V1Service body = serviceService.createV1Service(serviceDTO);
        try {
            result = apiInstance.createNamespacedService(namespace, body, "true", null, null);
            System.out.println(result);
        } catch (ApiException e) {
            return ResultUtil.error(String.valueOf(e.getCode()), e.getMessage());
        }
        return ResultUtil.success(gson.toJson(result));
    }

postman

POST http://localhost:8080/api/v1/service/createNamespacedService/default

{
	"metadataName":"nginx-service1",
	"metadataLabelsApp":"nginx",
    "specSelectorApp":"nginx",
    "specPortsName":"nginx-port",
    "specPortsProtocol":"TCP",
    "specPortsPort": 80,
    "specPortsNodePort": 32601,
    "specPortsTargetPort": 80,
    "specType": "NodePort"
}

kuboard上查看
在这里插入图片描述
创建的是一个nginx的service,
访问入口http://192.168.2.3:32601/
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值