Debian Codegraph @

docs/deploy/koyeb.rst

raw
1 Deploy to Koyeb
2 ================
3
4 This guide describes how to deploy a websockets server to Koyeb_.
5
6 .. _Koyeb: https://www.koyeb.com
7
8 .. admonition:: The free tier of Koyeb is sufficient for trying this guide.
9 :class: tip
10
11 The `free tier`__ include one web service, which this guide uses.
12
13 __ https://www.koyeb.com/pricing
14
15 We’re going to deploy a very simple app. The process would be identical to a
16 more realistic app.
17
18 Create repository
19 -----------------
20
21 Koyeb supports multiple deployment methods. Its quick start guides recommend
22 git-driven deployment as the first option. Let's initialize a git repository:
23
24 .. code-block:: console
25
26 $ mkdir websockets-echo
27 $ cd websockets-echo
28 $ git init -b main
29 Initialized empty Git repository in websockets-echo/.git/
30 $ git commit --allow-empty -m "Initial commit."
31 [main (root-commit) 740f699] Initial commit.
32
33 Render requires the git repository to be hosted at GitHub.
34
35 Sign up or log in to GitHub. Create a new repository named ``websockets-echo``.
36 Don't enable any of the initialization options offered by GitHub. Then, follow
37 instructions for pushing an existing repository from the command line.
38
39 After pushing, refresh your repository's homepage on GitHub. You should see an
40 empty repository with an empty initial commit.
41
42 Create application
43 ------------------
44
45 Here’s the implementation of the app, an echo server. Save it in a file
46 called ``app.py``:
47
48 .. literalinclude:: ../../example/deployment/koyeb/app.py
49 :language: python
50
51 This app implements typical requirements for running on a Platform as a Service:
52
53 * it listens on the port provided in the ``$PORT`` environment variable;
54 * it provides a health check at ``/healthz``;
55 * it closes connections and exits cleanly when it receives a ``SIGTERM`` signal;
56 while not documented, this is how Koyeb terminates apps.
57
58 Create a ``requirements.txt`` file containing this line to declare a dependency
59 on websockets:
60
61 .. literalinclude:: ../../example/deployment/koyeb/requirements.txt
62 :language: text
63
64 Create a ``Procfile`` to tell Koyeb how to run the app.
65
66 .. literalinclude:: ../../example/deployment/koyeb/Procfile
67
68 Confirm that you created the correct files and commit them to git:
69
70 .. code-block:: console
71
72 $ ls
73 Procfile app.py requirements.txt
74 $ git add .
75 $ git commit -m "Initial implementation."
76 [main f634b8b] Initial implementation.
77  3 files changed, 39 insertions(+)
78  create mode 100644 Procfile
79  create mode 100644 app.py
80  create mode 100644 requirements.txt
81
82 The app is ready. Let's deploy it!
83
84 Deploy application
85 ------------------
86
87 Sign up or log in to Koyeb.
88
89 In the Koyeb control panel, create a web service with GitHub as the deployment
90 method. Install and authorize Koyeb's GitHub app if you haven't done that yet.
91
92 Follow the steps to create a new service:
93
94 1. Select the ``websockets-echo`` repository in the list of your repositories.
95 2. Confirm that the **Free** instance type is selected. Click **Next**.
96 3. Configure health checks: change the protocol from TCP to HTTP and set the
97 path to ``/healthz``. Review other settings; defaults should be correct.
98 Click **Deploy**.
99
100 Koyeb builds the app, deploys it, verifies that the health checks passes, and
101 makes the deployment active.
102
103 Validate deployment
104 -------------------
105
106 Let's confirm that your application is running as expected.
107
108 Since it's a WebSocket server, you need a WebSocket client, such as the
109 interactive client that comes with websockets.
110
111 If you're currently building a websockets server, perhaps you're already in a
112 virtualenv where websockets is installed. If not, you can install it in a new
113 virtualenv as follows:
114
115 .. code-block:: console
116
117 $ python -m venv websockets-client
118 $ . websockets-client/bin/activate
119 $ pip install websockets
120
121 Look for the URL of your app in the Koyeb control panel. It looks like
122 ``https://<app>-<user>-<id>.koyeb.app/``. Connect the
123 interactive client — you must replace ``https`` with ``wss`` in the URL:
124
125 .. code-block:: console
126
127 $ websockets wss://<app>-<user>-<id>.koyeb.app/
128 Connected to wss://<app>-<user>-<id>.koyeb.app/.
129 >
130
131 Great! Your app is running!
132
133 Once you're connected, you can send any message and the server will echo it,
134 or press Ctrl-D to terminate the connection:
135
136 .. code-block:: console
137
138 > Hello!
139 < Hello!
140 Connection closed: 1000 (OK).
141
142 You can also confirm that your application shuts down gracefully.
143
144 Connect an interactive client again:
145
146 .. code-block:: console
147
148 $ websockets wss://<app>-<user>-<id>.koyeb.app/
149 Connected to wss://<app>-<user>-<id>.koyeb.app/.
150 >
151
152 In the Koyeb control panel, go to the **Settings** tab, click **Pause**, and
153 confirm.
154
155 Eventually, the connection gets closed with code 1001 (going away).
156
157 .. code-block:: console
158
159 $ websockets wss://<app>-<user>-<id>.koyeb.app/
160 Connected to wss://<app>-<user>-<id>.koyeb.app/.
161 Connection closed: 1001 (going away).
162
163 If graceful shutdown wasn't working, the server wouldn't perform a closing
164 handshake and the connection would be closed with code 1006 (abnormal closure).