Class Service

  • All Implemented Interfaces:
    IResource, software.amazon.jsii.JsiiSerializable, software.constructs.IConstruct

    @Generated(value="jsii-pacmak/1.52.1 (build 5ccc8f6)",
               date="2022-01-14T19:05:22.144Z")
    @Stability(Stable)
    public class Service
    extends Resource
    An abstract way to expose an application running on a set of Pods as a network service.

    With Kubernetes you don't need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them.

    For example, consider a stateless image-processing backend which is running with 3 replicas. Those replicas are fungible—frontends do not care which backend they use. While the actual Pods that compose the backend set may change, the frontend clients should not need to be aware of that, nor should they need to keep track of the set of backends themselves. The Service abstraction enables this decoupling.

    If you're able to use Kubernetes APIs for service discovery in your application, you can query the API server for Endpoints, that get updated whenever the set of Pods in a Service changes. For non-native applications, Kubernetes offers ways to place a network port or load balancer in between your application and the backend Pods.

    • Constructor Detail

      • Service

        protected Service​(software.amazon.jsii.JsiiObjectRef objRef)
      • Service

        protected Service​(software.amazon.jsii.JsiiObject.InitializationMode initializationMode)
      • Service

        @Stability(Stable)
        public Service​(@NotNull
                       software.constructs.Construct scope,
                       @NotNull
                       String id,
                       @Nullable
                       ServiceProps props)
        Parameters:
        scope - This parameter is required.
        id - This parameter is required.
        props -
      • Service

        @Stability(Stable)
        public Service​(@NotNull
                       software.constructs.Construct scope,
                       @NotNull
                       String id)
        Parameters:
        scope - This parameter is required.
        id - This parameter is required.
    • Method Detail

      • addDeployment

        @Stability(Stable)
        public void addDeployment​(@NotNull
                                  Deployment deployment,
                                  @Nullable
                                  AddDeploymentOptions options)
        Associate a deployment to this service.

        If not targetPort is specific in the portOptions, then requests will be routed to the port exposed by the first container in the deployment's pods. The deployment's labelSelector will be used to select pods.

        Parameters:
        deployment - The deployment to expose. This parameter is required.
        options - Optional settings for the port.
      • addDeployment

        @Stability(Stable)
        public void addDeployment​(@NotNull
                                  Deployment deployment)
        Associate a deployment to this service.

        If not targetPort is specific in the portOptions, then requests will be routed to the port exposed by the first container in the deployment's pods. The deployment's labelSelector will be used to select pods.

        Parameters:
        deployment - The deployment to expose. This parameter is required.
      • addSelector

        @Stability(Stable)
        public void addSelector​(@NotNull
                                String label,
                                @NotNull
                                String value)
        Services defined using this spec will select pods according the provided label.

        Parameters:
        label - The label key. This parameter is required.
        value - The label value. This parameter is required.
      • exposeViaIngress

        @Stability(Stable)
        @NotNull
        public IngressV1Beta1 exposeViaIngress​(@NotNull
                                               String path,
                                               @Nullable
                                               ExposeServiceViaIngressOptions options)
        Expose a service via an ingress using the specified path.

        Parameters:
        path - The path to expose the service under. This parameter is required.
        options - Additional options.
        Returns:
        The `Ingress` resource that was used.
      • exposeViaIngress

        @Stability(Stable)
        @NotNull
        public IngressV1Beta1 exposeViaIngress​(@NotNull
                                               String path)
        Expose a service via an ingress using the specified path.

        Parameters:
        path - The path to expose the service under. This parameter is required.
        Returns:
        The `Ingress` resource that was used.
      • serve

        @Stability(Stable)
        public void serve​(@NotNull
                          Number port,
                          @Nullable
                          ServicePortOptions options)
        Configure a port the service will bind to.

        This method can be called multiple times.

        Parameters:
        port - The port definition. This parameter is required.
        options -
      • serve

        @Stability(Stable)
        public void serve​(@NotNull
                          Number port)
        Configure a port the service will bind to.

        This method can be called multiple times.

        Parameters:
        port - The port definition. This parameter is required.
      • getApiObject

        @Stability(Stable)
        @NotNull
        protected org.cdk8s.ApiObject getApiObject()
        The underlying cdk8s API object.

        Specified by:
        getApiObject in class Resource
        See Also:
        base.Resource.apiObject
      • getPorts

        @Stability(Stable)
        @NotNull
        public List<ServicePort> getPorts()
        Ports for this service.

        Use serve() to expose additional service ports.

      • getSelector

        @Stability(Stable)
        @NotNull
        public Map<String,​String> getSelector()
        Returns the labels which are used to select pods for this service.
      • getType

        @Stability(Stable)
        @NotNull
        public ServiceType getType()
        Determines how the Service is exposed.
      • getClusterIP

        @Stability(Stable)
        @Nullable
        public String getClusterIP()
        The IP address of the service and is usually assigned randomly by the master.
      • getExternalName

        @Stability(Stable)
        @Nullable
        public String getExternalName()
        The externalName to be used for EXTERNAL_NAME types.