文档首页 > > API参考> Kubernetes API> Service> 创建Service

创建Service

分享
更新时间: 2019/08/30 16:02

功能介绍

创建一个Service。

URI

POST /api/v1/namespaces/{namespace}/services

表1 Path参数

参数

描述

namespace

Object name and auth scope, such as for teams and projects.

表2 Query参数

参数

是否必选

描述

pretty

No

If 'true', then the output is pretty printed.

请求消息

请求参数:

请求参数参见表102

请求示例:

ClusterIP类型示例。

{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "redis",
        "labels": {
            "app": "redis"
        }
    },
    "spec": {
        "selector": {
            "app": "redis"
        },
        "ports": [
            {
                "name": "service0",
                "targetPort": 80,
                "port": 8080,
                "protocol": "TCP"
            }
        ],
        "type": "ClusterIP"
    }
}

LoadBalancer类型示例。

LoadBalancer类型Service需要在metadata.annotations自定中添加elb实例ID(kubernetes.io/elb.id)、项目ID(tenant.kubernetes.io/project-id)和账号ID(tenant.kubernetes.io/domain-id)

{
  "apiVersion": "v1",
  "kind": "Service",
  "metadata": {
    "name": "nginx",
    "annotations": {
      "kubernetes.io/elb.id": "77e6246c-a091-xxxx-xxxx-789baa571280",
      "tenant.kubernetes.io/project-id": "a9cab8xxxxxxxxxxxxxxxx41c0aeb",
      "tenant.kubernetes.io/domain-id": "65382xxxxxxxxxxxxxxxxxe684b"
    }
  },
  "spec": {
    "selector": {
      "app": "nginx"
    },
    "ports": [
      {
        "name": "service0",
        "targetPort": 80,
        "port": 8080,
        "protocol": "TCP"
      }
    ],
    "type": "LoadBalancer"
  }
}

响应消息

响应参数:

响应参数的详细描述请参见表102

响应示例:

{
  "kind": "Service",
  "apiVersion": "v1",
  "metadata": {
    "name": "redis",
    "namespace": "namespace-test",
    "selfLink": "/api/v1/namespaces/namespace-test/services/redis",
    "uid": "d6a1ce79-afdb-11e8-b6ef-f898ef6c78b4",
    "resourceVersion": "5146412",
    "creationTimestamp": "2018-09-04T00:45:36Z",
    "labels": {
      "app": "redis"
    },
    "enable": true
  },
  "spec": {
    "ports": [
      {
        "name": "service0",
        "protocol": "TCP",
        "port": 8080,
        "targetPort": 80
      }
    ],
    "selector": {
      "app": "redis"
    },
    "clusterIP": "10.247.212.210",
    "type": "ClusterIP",
    "sessionAffinity": "None"
  },
  "status": {
    "loadBalancer": {

    }
  }
}

状态码

表3描述API的状态码。

表3 状态码

状态码

描述

200

The request has been fulfilled, resulting in the creation of a new resource.

分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!

非常感谢您的反馈,我们会继续努力做到更好!

反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

跳转到云社区