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

创建Service

创建Service

分享
更新时间:2021/02/10 GMT+08:00

功能介绍

创建一个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.

请求消息

请求参数:

请求参数参见表101

请求示例:

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"
    }
}

响应消息

响应参数:

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

响应示例:

{
    "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 状态码

状态码

描述

201

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

更多状态码请参见状态码

分享:

    相关文档

    相关产品