| 1 |
Deploy to Fly |
| 2 |
============= |
| 3 |
|
| 4 |
This guide describes how to deploy a websockets server to Fly_. |
| 5 |
|
| 6 |
.. _Fly: https://fly.io/ |
| 7 |
|
| 8 |
.. admonition:: The free tier of Fly is sufficient for trying this guide. |
| 9 |
:class: tip |
| 10 |
|
| 11 |
The `free tier`__ include up to three small VMs. This guide uses only one. |
| 12 |
|
| 13 |
__ https://fly.io/docs/about/pricing/ |
| 14 |
|
| 15 |
We're going to deploy a very simple app. The process would be identical for a |
| 16 |
more realistic app. |
| 17 |
|
| 18 |
Create application |
| 19 |
------------------ |
| 20 |
|
| 21 |
Here's the implementation of the app, an echo server. Save it in a file called |
| 22 |
``app.py``: |
| 23 |
|
| 24 |
.. literalinclude:: ../../example/deployment/fly/app.py |
| 25 |
:language: python |
| 26 |
|
| 27 |
This app implements typical requirements for running on a Platform as a Service: |
| 28 |
|
| 29 |
* it provides a health check at ``/healthz``; |
| 30 |
* it closes connections and exits cleanly when it receives a ``SIGTERM`` signal. |
| 31 |
|
| 32 |
Create a ``requirements.txt`` file containing this line to declare a dependency |
| 33 |
on websockets: |
| 34 |
|
| 35 |
.. literalinclude:: ../../example/deployment/fly/requirements.txt |
| 36 |
:language: text |
| 37 |
|
| 38 |
The app is ready. Let's deploy it! |
| 39 |
|
| 40 |
Deploy application |
| 41 |
------------------ |
| 42 |
|
| 43 |
Follow the instructions__ to install the Fly CLI, if you haven't done that yet. |
| 44 |
|
| 45 |
__ https://fly.io/docs/hands-on/install-flyctl/ |
| 46 |
|
| 47 |
Sign up or log in to Fly. |
| 48 |
|
| 49 |
Launch the app — you'll have to pick a different name because I'm already using |
| 50 |
``websockets-echo``: |
| 51 |
|
| 52 |
.. code-block:: console |
| 53 |
|
| 54 |
$ fly launch |
| 55 |
Creating app in ... |
| 56 |
Scanning source code |
| 57 |
Detected a Python app |
| 58 |
Using the following build configuration: |
| 59 |
Builder: paketobuildpacks/builder:base |
| 60 |
? App Name (leave blank to use an auto-generated name): websockets-echo |
| 61 |
? Select organization: ... |
| 62 |
? Select region: ... |
| 63 |
Created app websockets-echo in organization ... |
| 64 |
Wrote config file fly.toml |
| 65 |
? Would you like to set up a Postgresql database now? No |
| 66 |
We have generated a simple Procfile for you. Modify it to fit your needs and run "fly deploy" to deploy your application. |
| 67 |
|
| 68 |
.. admonition:: This will build the image with a generic buildpack. |
| 69 |
:class: tip |
| 70 |
|
| 71 |
Fly can `build images`__ with a Dockerfile or a buildpack. Here, ``fly |
| 72 |
launch`` configures a generic Paketo buildpack. |
| 73 |
|
| 74 |
If you'd rather package the app with a Dockerfile, check out the guide to |
| 75 |
:ref:`containerize an application <containerize-application>`. |
| 76 |
|
| 77 |
__ https://fly.io/docs/reference/builders/ |
| 78 |
|
| 79 |
Replace the auto-generated ``fly.toml`` with: |
| 80 |
|
| 81 |
.. literalinclude:: ../../example/deployment/fly/fly.toml |
| 82 |
:language: toml |
| 83 |
|
| 84 |
This configuration: |
| 85 |
|
| 86 |
* listens on port 443, terminates TLS, and forwards to the app on port 8080; |
| 87 |
* declares a health check at ``/healthz``; |
| 88 |
* requests a ``SIGTERM`` for terminating the app. |
| 89 |
|
| 90 |
Replace the auto-generated ``Procfile`` with: |
| 91 |
|
| 92 |
.. literalinclude:: ../../example/deployment/fly/Procfile |
| 93 |
:language: text |
| 94 |
|
| 95 |
This tells Fly how to run the app. |
| 96 |
|
| 97 |
Now you can deploy it: |
| 98 |
|
| 99 |
.. code-block:: console |
| 100 |
|
| 101 |
$ fly deploy |
| 102 |
|
| 103 |
... lots of output... |
| 104 |
|
| 105 |
==> Monitoring deployment |
| 106 |
|
| 107 |
1 desired, 1 placed, 1 healthy, 0 unhealthy [health checks: 1 total, 1 passing] |
| 108 |
--> v0 deployed successfully |
| 109 |
|
| 110 |
Validate deployment |
| 111 |
------------------- |
| 112 |
|
| 113 |
Let's confirm that your application is running as expected. |
| 114 |
|
| 115 |
Since it's a WebSocket server, you need a WebSocket client, such as the |
| 116 |
interactive client that comes with websockets. |
| 117 |
|
| 118 |
If you're currently building a websockets server, perhaps you're already in a |
| 119 |
virtualenv where websockets is installed. If not, you can install it in a new |
| 120 |
virtualenv as follows: |
| 121 |
|
| 122 |
.. code-block:: console |
| 123 |
|
| 124 |
$ python -m venv websockets-client |
| 125 |
$ . websockets-client/bin/activate |
| 126 |
$ pip install websockets |
| 127 |
|
| 128 |
Connect the interactive client — you must replace ``websockets-echo`` with the |
| 129 |
name of your Fly app in this command: |
| 130 |
|
| 131 |
.. code-block:: console |
| 132 |
|
| 133 |
$ websockets wss://websockets-echo.fly.dev/ |
| 134 |
Connected to wss://websockets-echo.fly.dev/. |
| 135 |
> |
| 136 |
|
| 137 |
Great! Your app is running! |
| 138 |
|
| 139 |
Once you're connected, you can send any message and the server will echo it, |
| 140 |
or press Ctrl-D to terminate the connection: |
| 141 |
|
| 142 |
.. code-block:: console |
| 143 |
|
| 144 |
> Hello! |
| 145 |
< Hello! |
| 146 |
Connection closed: 1000 (OK). |
| 147 |
|
| 148 |
You can also confirm that your application shuts down gracefully. |
| 149 |
|
| 150 |
Connect an interactive client again — remember to replace ``websockets-echo`` |
| 151 |
with your app: |
| 152 |
|
| 153 |
.. code-block:: console |
| 154 |
|
| 155 |
$ websockets wss://websockets-echo.fly.dev/ |
| 156 |
Connected to wss://websockets-echo.fly.dev/. |
| 157 |
> |
| 158 |
|
| 159 |
In another shell, restart the app — again, replace ``websockets-echo`` with your |
| 160 |
app: |
| 161 |
|
| 162 |
.. code-block:: console |
| 163 |
|
| 164 |
$ fly restart websockets-echo |
| 165 |
websockets-echo is being restarted |
| 166 |
|
| 167 |
Go back to the first shell. The connection is closed with code 1001 (going |
| 168 |
away). |
| 169 |
|
| 170 |
.. code-block:: console |
| 171 |
|
| 172 |
$ websockets wss://websockets-echo.fly.dev/ |
| 173 |
Connected to wss://websockets-echo.fly.dev/. |
| 174 |
Connection closed: 1001 (going away). |
| 175 |
|
| 176 |
If graceful shutdown wasn't working, the server wouldn't perform a closing |
| 177 |
handshake and the connection would be closed with code 1006 (abnormal closure). |