• Sidecar 服务注册
    • ">最小样例
    • ">重写样例
    • ">Sidecar 服务默认值
    • ">取值限制
    • ">生命周期

    Sidecar 服务注册

    Connect proxies are typically deployed as “sidecars” that run on the same node as the single service instance that they handle traffic for. They might be on the same VM or running as a separate container in the same network namespace.

    To simplify the configuration experience when deploying a sidecar for a service instance, Consul 1.3 introduced a new field in the Connect block of the service definition.

    The connect.sidecar_service field is a complete nested service definition on which almost any regular service definition field can be set. The exceptions are noted below. If used, the service definition is treated identically to another top-level service definition. The value of the nested definition is that all fields are optional with some opinionated defaults applied that make setting up a sidecar proxy much simpler.

    最小样例

    To register a service instance with a sidecar, all that’s needed is:

    1. {
    2. "name": "web",
    3. "port": 8080,
    4. "connect": { "sidecar_service": {} }
    5. }

    This will register the web service as normal, but will also register another proxy service with defaults values used.

    The above expands out to be equivalent to the following explicit service definitions:

    1. {
    2. "name": "web",
    3. "port": 8080,
    4. }
    5. {
    6. "name": "web-sidecar-proxy",
    7. "port": 20000,
    8. "kind": "connect-proxy",
    9. "checks": [
    10. {
    11. "Name": "Connect Sidecar Listening",
    12. "TCP": "127.0.0.1:20000",
    13. "Interval": "10s"
    14. },
    15. {
    16. "name": "Connect Sidecar Aliasing web",
    17. "alias_service": "web"
    18. }
    19. ],
    20. "proxy": {
    21. "destination_service_name": "web",
    22. "destination_service_id": "web",
    23. "local_service_address": "127.0.0.1",
    24. "local_service_port": 8080,
    25. }
    26. }

    Details on how the defaults are determined are documented below.

    Note: Sidecar service registrations are only a shorthand for registering multiple services. Consul will not start up or manage the actual proxy processes for you.

    重写样例

    The following example shows a service definition where some fields are overridden to customize the proxy configuration.

    1. {
    2. "name": "web",
    3. "port": 8080,
    4. "connect": {
    5. "sidecar_service": {
    6. "proxy": {
    7. "upstreams": [
    8. {
    9. "destination_name": "db",
    10. "local_bind_port": 9191
    11. }
    12. ],
    13. "config": {
    14. "handshake_timeout_ms": 1000
    15. }
    16. }
    17. }
    18. }
    19. }

    This example customizes the proxy upstreams and some built-in proxy configuration.

    Sidecar 服务默认值

    The following fields are set by default on a sidecar service registration. With the exceptions noted any field may be overridden explicitly in the connect.sidecar_service definition to customize the proxy registration. The “parent” service refers to the service definition that embeds the sidecar proxy.

    取值限制

    Almost all fields in a service definition may be set on the connect.sidecar_service except for the following:

    • id - Sidecar services get an ID assigned and it is an error to override this. This ensures the agent can correctly de-register the sidecar service later when the parent service is removed.
    • kind - Kind defaults to connect-proxy and there is currently no way to unset this to make the registration be for a regular non-connect-proxy service.
    • connect.sidecar_service - Service definitions can’t be nested recursively.
    • connect.proxy - (Deprecated) Managed proxies can’t be defined on a sidecar.
    • connect.native - Currently the kind is fixed to connect-proxy and it’s an error to register a connect-proxy that is also Connect-native.

    生命周期

    Sidecar service registration is mostly a configuration syntax helper to avoid adding lots of boiler plate for basic sidecar options, however the agent does have some specific behavior around their lifecycle that makes them easier to work with.

    The agent fixes the ID of the sidecar service to be based on the parent service’s ID. This enables the following behavior.

    • A service instance can only ever have one sidecar service registered.
    • When re-registering via API or reloading from configuration file:
      • If something changes in the nested sidecar service definition, the change will update the current sidecar registration instead of creating a new one.
      • If a service registration removes the nested sidecar_service then the previously registered sidecar for that service will be deregistered automatically.
    • When reloading the configuration files, if a service definition changes it’s ID, then a new service instance and a new sidecar instance will be registered. The old ones will be removed since they are no longer found in the config files.