Creating a Service

Function

This API is used to create a Service.

URI

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

Table 1 Path parameter

Parameter

Description

namespace

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

Table 2 Query parameter

Parameter

Mandatory

Description

pretty

No

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

Request

Request parameters

For the description about request parameters, see Table 101.

Example request

ClusterIP type example:

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

LoadBalance type example:

For the LoadBalancer type, the kubernetes.io/elb.id, tenant.kubernetes.io/project-id, and tenant.kubernetes.io/domain-id parameters must be added to the metadata.annotations field.

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

Response

Response parameters

For the description about response parameters, see Table 101.

Example response

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

Status Code

Table 3 describes the status code of this API.

Table 3 Status code

Status Code

Description

201

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

For more status codes, see Status Codes.