Search This Blog

Tuesday, 19 December 2017

Terminating a specific application instance using it's index number in Pivotal Cloud Foundry

I was recently asked how to terminate a specific application instance rather then terminate all instances using "cf delete".

We can easily using the CF REST API or even easier the CF CLI "cf curl" command which makes it straight forward to make REST based calls into cloud foundry as shown below.

CF REST API Docs

https://apidocs.cloudfoundry.org/280/

Below assumes you already logged into PCF using the CF CLI

1. First find an application that has multiple instances

pasapicella@pas-macbook:~$ cf app pas-cf-manifest
Showing health and status for app pas-cf-manifest in org apples-pivotal-org / space development as papicella@pivotal.io...

name:              pas-cf-manifest
requested state:   started
instances:         2/2
usage:             756M x 2 instances
routes:            pas-cf-manifest.cfapps.io
last uploaded:     Sun 19 Nov 21:26:26 AEDT 2017
stack:             cflinuxfs2
buildpack:         client-certificate-mapper=1.2.0_RELEASE container-security-provider=1.8.0_RELEASE java-buildpack=v4.5-offline-https://github.com/cloudfoundry/java-buildpack.git#ffeefb9 java-main
                   java-opts jvmkill-agent=1.10.0_RELEASE open-jdk-like-jre=1.8.0_1...

     state     since                  cpu    memory           disk           details
#0   running   2017-12-16T00:11:27Z   0.0%   241.5M of 756M   139.9M of 1G
#1   running   2017-12-17T10:39:09Z   0.3%   221.3M of 756M   139.9M of 1G

2. Use a "cf curl" curl which uses the application GUID to determine which application to check all application instances and their current state

pasapicella@pas-macbook:~$ cf curl /v2/apps/`cf app pas-cf-manifest --guid`/instances
{
   "0": {
      "state": "RUNNING",
      "uptime": 293653,
      "since": 1513383087
   },
   "1": {
      "state": "RUNNING",
      "uptime": 169591,
      "since": 1513507149
   }
}

3. Now let's delete instance with index "1". Don't forget that PCF will determine the current desired state of the application is not the current state and will re-start the application instance very quickly

pasapicella@pas-macbook:~$ cf curl /v2/apps/`cf app pas-cf-manifest --guid`/instances/1 -X DELETE

Note: You won't get any output BUT you can verify it has done what you asked for by running the command at step #2 again

pasapicella@pas-macbook:~$ cf curl /v2/apps/`cf app pas-cf-manifest --guid`/instances
{
   "0": {
      "state": "RUNNING",
      "uptime": 293852,
      "since": 1513383087
   },
   "1": {
      "state": "DOWN",
      "uptime": 0
   }
}

If you run it again say 30 seconds later you should see your application instance re-started as shown below

pasapicella@pas-macbook:~$ cf curl /v2/apps/`cf app pas-cf-manifest --guid`/instances
{
   "0": {
      "state": "RUNNING",
      "uptime": 293870,
      "since": 1513383087
   },
   "1": {
      "state": "STARTING",
      "uptime": 11,
      "since": 1513676947
   }
}

pasapicella@pas-macbook:~$ cf curl /v2/apps/`cf app pas-cf-manifest --guid`/instances
{
   "0": {
      "state": "RUNNING",
      "uptime": 293924,
      "since": 1513383087
   },
   "1": {
      "state": "RUNNING",
      "uptime": 45,
      "since": 1513676965
   }
}

More Information

pasapicella@pas-macbook:~$ cf curl --help
NAME:
   curl - Executes a request to the targeted API endpoint

USAGE:
   cf curl PATH [-iv] [-X METHOD] [-H HEADER] [-d DATA] [--output FILE]

   By default 'cf curl' will perform a GET to the specified PATH. If data
   is provided via -d, a POST will be performed instead, and the Content-Type
   will be set to application/json. You may override headers with -H and the
   request method with -X.

   For API documentation, please visit http://apidocs.cloudfoundry.org.

EXAMPLES:
   cf curl "/v2/apps" -X GET -H "Content-Type: application/x-www-form-urlencoded" -d 'q=name:myapp'
   cf curl "/v2/apps" -d @/path/to/file

OPTIONS:
   -H            Custom headers to include in the request, flag can be specified multiple times
   -X            HTTP method (GET,POST,PUT,DELETE,etc)
   -d            HTTP data to include in the request body, or '@' followed by a file name to read the data from
   -i            Include response headers in the output
   --output      Write curl body to FILE instead of stdout

No comments: