|
| 1 | +--- |
| 2 | +title: Using Sidecar Processes in Your App |
| 3 | +owner: CAPI |
| 4 | +--- |
| 5 | + |
| 6 | +This topic describes sidecar processes and how to use them in your app. |
| 7 | + |
| 8 | +## <a id="overview"></a> Overview |
| 9 | + |
| 10 | +You can run additional operating system processes in the same container as your app. These are called sidecar processes, or sidecars. An example of a sidecar is an Application Performance Monitoring (APM) tool. |
| 11 | + |
| 12 | +### <a id="use-case"></a> Use Cases |
| 13 | + |
| 14 | +You can use sidecars for processes that depend on each other or must run in the same container. |
| 15 | + |
| 16 | +Some examples include processes that need do the following: |
| 17 | + |
| 18 | +* Communicate over a Unix socket or through localhost |
| 19 | +* Share the same filesystem |
| 20 | +* Be scaled and placed together |
| 21 | +* Have fast interprocess communication |
| 22 | + |
| 23 | +### <a id="how"></a> How They Work |
| 24 | + |
| 25 | +All required code and configuration needed to run the sidecar and app are packaged together in the same droplet. This droplet is deployed in a single container on Diego and both processes within the container are health checked independently. |
| 26 | + |
| 27 | + |
| 28 | +### <a id="limitations"></a> Limitations |
| 29 | + |
| 30 | +Sidecars have the following limitations: |
| 31 | + |
| 32 | +* The start and stop order of app processes and their sidecars is undefined. |
| 33 | +* App processes and sidecars are codependent: if either crashes or exits, the other will as well. |
| 34 | +* Sidecars are currently not independently scalable and share resources with the main app process and other sidecars within the container. |
| 35 | +* Sidecars only support PID-based health checks. HTTP health-checks for sidecars are not currently supported. |
| 36 | + |
| 37 | +## <a id="create"></a> Create a Sidecar |
| 38 | + |
| 39 | +The recommended way to create sidecars for your app is with an App Manifest. |
| 40 | + |
| 41 | +name is a user defined identifier (unique per app) |
| 42 | +process_types is a list of app processes the sidecar will attach to. You can attach multiple sidecars to each process type your app uses |
| 43 | +command is the command used to start the sidecar |
| 44 | + |
| 45 | +## <a id="example"></a> Example: Push a Sample App with a Sidecar |
| 46 | + |
| 47 | +### <a id="about"></a> About the Sample App |
| 48 | + |
| 49 | +To demonstrate how sidecars work in Cloud Foundry, we will deploy a simple Ruby app that talks to a separate Golang binary called `config-server` that is deployed as a sidecar process. |
| 50 | + |
| 51 | +First, clone the capi-sidecar-samples git repository: |
| 52 | + |
| 53 | +git clone https://github.com/cloudfoundry-samples/capi-sidecar-samples.git |
| 54 | + |
| 55 | +In this repository you will find two apps: a Ruby app that serves as the “main application” called `sidecar-dependent-app` and a Golang “configuration server” sidecar called `config-server-sidecar` that produces a `config-server` binary. |
| 56 | + |
| 57 | +The `config-server` binary provides applications with their required configuration over its `/config/` endpoint and only accepts connections over localhost on the `CONFIG_SERVER_PORT` port. For example: |
| 58 | + |
| 59 | +``` |
| 60 | +vcap@f00949bd-6601-4731-6f7e-e859:~$ curl localhost:$CONFIG_SERVER_PORT/config/ |
| 61 | +{"Scope":"some-service.admin","Password":"not-a-real-p4$$w0rd"} |
| 62 | +``` |
| 63 | + |
| 64 | +Since the `config-server` sidecar process only accepts connections directly over localhost, it needs to share the same network namespace as the main app. Or, in other words, it needs to be co-located in the same container. This makes the `config-server` process a prime candidate for the sidecar pattern. For demonstration purposes, we’ve added a `/config` endpoint to the main app that simply calls out to the `config-server` sidecar and will echo back the configuration in its response: |
| 65 | + |
| 66 | +``` |
| 67 | +get '/config' do |
| 68 | +puts "Sending a request to the config-server sidecar at localhost:#{ENV['CONFIG_SERVER_PORT']}/config/" |
| 69 | +response = Typhoeus.get("localhost:#{ENV['CONFIG_SERVER_PORT']}/config/") |
| 70 | +puts "Received #{response.body} from the config-server sidecar" |
| 71 | +response.body |
| 72 | +end |
| 73 | +``` |
| 74 | + |
| 75 | +The diagram below demonstrates this architecture: |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | +### <a id="procedure"></a> Procedure |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | + |
0 commit comments