Statistics
| Branch: | Tag: | Revision:

root / docs / examplesdir / server.rst @ 40ddc207

History | View | Annotate | Download (12.9 kB)

1
Creating Servers (Virtual Machines)
2
===================================
3

    
4
A `server` (also known as `virtual machine`), is created based on a registered
5
`image` and a preconfigured hardware setup (also known as `flavor`).
6

    
7
Create a virtual server
8
-----------------------
9

    
10
List available flavors
11

    
12
.. code-block:: console
13

    
14
    [kamaki]: flavor list -l
15
    1 C1R128D1drbd
16
        SNF:disk_template: drbd
17
        disk:              1
18
        id:                1
19
        links:
20
                href: https://example.com/cyclades/compute/v2.0/flavors/1
21
                rel:  bookmark
22
                . . . . . . .
23
                href: https://example.com/cyclades/compute/v2.0/flavors/1
24
                rel:  self
25
        name:              C1R128D1drbd
26
        ram:               128
27
        vcpus:             1
28
    2 C1R128D1plain
29
        SNF:disk_template: plain
30
        disk:              1
31
        id:                2
32
        links:
33
                href: https://example.com/cyclades/compute/v2.0/flavors/2
34
                rel:  bookmark
35
                . . . . . . .
36
                href: https://example.com/cyclades/compute/v2.0/flavors/2
37
                rel:  self
38
        name:             C1R128D1plain
39
        ram:              128
40
        vcpus:            1
41
    [kamaki]:
42

    
43
List available images
44

    
45
.. code-block:: console
46

    
47
    [kamaki]: image compute list
48
    f1r57-1m4g3-1d Debian Base Alpha
49
    53c0nd-1m4g3-1d Beta Debian Base
50
    [kamaki]:
51

    
52
Let's pick the `C1R128D1drbd` (id: 1) flavor and the `Debian Base Alpha` (id:
53
f1r57-1m4g3-1d) image to create a new virtual server called 'My First Server'
54

    
55
.. code-block:: console
56

    
57
    [kamaki]: server create 'My First Server' 1 f1r57-1m4g3-1d
58
    accessIPv4:
59
    accessIPv6:
60
    addresses:
61
    adminPass:       Y0uW0nt5eeMeAg4in
62
    attachments:
63
    config_drive:
64
    created:         2013-06-19T12:34:47.362078+00:00
65
    diagnostics:
66
    flavor:
67
            id:    1
68
    hostId:
69
    id:              141
70
    image:
71
            id:    f1r57-1m4g3-1d
72
    key_name:        None
73
    metadata:
74
                   os:    debian
75
                   users: root
76
    name:            My First Server
77
    progress:        0
78
    security_groups:
79
                      name: default
80
    status:          BUILD
81
    suspended:       False
82
    tenant_id:       s0m3-u53r-1d
83
    updated:         2013-06-19T12:34:48.512867+00:00
84
    user_id:         s0m3-u53r-1d
85
    [kamaki]:
86

    
87
.. note:: The adminPass field is not stored anywhere, therefore users would
88
    rather write it down and change it the first time they use the virtual server
89

    
90
Wait for the virtual server with id 141 to build (optional)
91

    
92
.. code-block:: console
93

    
94
    [kamaki]: server wait 141
95
    <bar showing build progress, until 100%>
96
    Server 141 is now in ACTIVE mode
97
    [kamaki]:
98

    
99
Destroy the virtual server (wait is still optional)
100

    
101
.. code-block:: console
102

    
103
    [kamaki]: server delete 141
104
    [kamaki]: server wait 141 ACTIVE
105
    <bar showing destruction progress, until 100%>
106
    Server 141 is now in DELETED mode
107
    [kamaki]:
108

    
109
Inject ssh keys to a debian server
110
----------------------------------
111

    
112
Assume that the servers build from the image `Debian Base Alpha` accept ssh
113
connections. We need to build servers that can log us as root without a
114
password. This can be achieved if the `/root/.ssh/authorized_keys` file exists
115
and contains the public key of the current user.
116

    
117
Assume that the public key file of the current user is located at
118
`/home/someuser/.ssh/id_rsa.pub` . We need a method of injecting this file as
119
`/root/.ssh/authorized_keys` while creating the virtual server.
120

    
121
Luckily, Synnefo fully supports the OpenStack suggestion for file injections on
122
virtual servers and kamaki allows it by using the **-p** argument (p stands for
123
`PERSONALITY` and is the term used in the
124
`respective OpenStack <http://docs.openstack.org/api/openstack-compute/2/content/CreateServers.html>`_ description).
125

    
126
The syntax of the -p argument is something called "the personlity string"::
127

    
128
    -p <local file path>[,<remote path>[,<remote owner>[,<remote group>[,<mode>]]]]
129

    
130
    e.g.,
131

    
132
    -p /home/someuser/.ssh/id_rsa.pub,/root/.ssh/authorized_keys,root,root,0777
133

    
134
.. note:: In case of omitting an optional part of the personality string, the
135
    default behavior depends on the remote server, e.g., for a debian image we
136
    expect the file to have root ownership, if the ownership is not specified.
137

    
138
Create a virtual server while injecting current user public key to root account
139

    
140
.. code-block:: console
141

    
142
    [kamaki]: server
143
    [server]: create 'NoPassword Server' 1 f1r57-1m4g3-1d -p /home/someuser/.ssh/id_rsa.pub,/root/.ssh/authorized_keys
144
    accessIPv4:
145
    accessIPv6:
146
    addresses:
147
    adminPass:       Th1s1s4U5elessTh1ngN0w
148
    attachments:
149
    config_drive:
150
    created:         2013-06-19T12:34:47.362078+00:00
151
    diagnostics:
152
    flavor:
153
            id:    1
154
    hostId:
155
    id:              142
156
    image:
157
            id:     f1r57-1m4g3-1d
158
    key_name:        None
159
    metadata:
160
                    os:    debian
161
                    users: root
162
    name:           No Password Server
163
    progress:        0
164
    security_groups:
165
                    name: default
166
    status:          BUILD
167
    suspended:       False
168
    tenant_id:       s0m3-u53r-1d
169
    updated:         2013-06-19T12:34:48.512867+00:00
170
    user_id:         s0m3-u53r-1d
171
    [server]:
172

    
173
When the virtual server is ready, get the virtual servers external IP from the web UI. Let's assume the
174
IP is 123.456.78.90 .
175

    
176
.. code-block:: console
177

    
178
    [server]: /exit
179
    $ ssh 123.456.78.90
180
    Linux remote-virtual server-4241 2.6.32-5-amd64 #1 SMP XXXX x86_64
181

    
182
    The programs included with the Debian GNU/Linux system are free software;
183
    the exact distribution terms for each program are described in the
184
    individual files in /usr/share/doc/*/copyright.
185

    
186
    Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
187
    permitted by applicable law.
188
    root@remote-virtual server-4241:~# ls -l .ssh/
189
    total 4
190
    -rw-r--r-- 1 root root 399 Jun 19 12:34 authorized_keys
191
    root@remote-virtual server-4241:~#
192

    
193
You can now log to your remote virtual server as root, without a password. Well done!
194

    
195
.. note:: There is no reason to limit injections to ssh keys. Users with an
196
    adequate understanding of the remote OS are encouraged to prepare and
197
    inject all kinds of useful files, e.g., **lists of package sources**,
198
    **default user profiles**, **device mount configurations**, etc.
199

    
200
Clusters of virtual servers
201
---------------------------
202

    
203
A cluster of virtual servers can be created and deleted using special
204
arguments.
205

    
206
A convention is necessary: all servers belonging to the same cluster will have
207
names with a common prefix e.g., *cluster1*, *cluster2*, etc. This prefix
208
acts as the cluster name or the cluster key. Still, users must be careful not to
209
confuse cluster servers with other servers that coincidentally have the same
210
prefix (e.g., *cluster_of_stars*).
211

    
212
First, let's create a cluster of 4 servers. Each server will run the image with
213
id *f1r57-1m4g3-1d* on the hardware specified by the flavor with id *1*. The
214
prefix of the cluster will be "my cluster "
215

    
216
.. code-block:: console
217

    
218
    $ kamaki
219
    [kamaki]: server
220
    [server]: create "my cluster " 1 f1r57-1m4g3-1d --cluster-size=4 --wait
221
    ... <omitted for clarity>
222
    adminPass:       S0mePassw0rd0n3
223
    created:         2013-06-19T12:34:49.362078+00:00
224
    flavor:
225
            id:    1
226
    id:              322
227
    image:
228
            id:    f1r57-1m4g3-1d
229
    name:            my cluster 1
230
    [progress bar waiting server to build]
231
    Server 321: status is now ACTIVE
232

    
233
    ... <omitted for clarity>
234
    adminPass:       S0mePassw0rdTwo
235
    created:         2013-06-19T12:34:47.362078+00:00
236
    flavor:
237
            id:    1
238
    id:              321
239
    image:
240
            id:    f1r57-1m4g3-1d
241
    name:            my cluster 2
242
    [progress bar waiting server to build]
243
    Server 322: status is now ACTIVE
244

    
245
    ... <omitted for clarity>
246
    adminPass:       S0mePassw0rdThree
247
    created:         2013-06-19T12:34:55.362078+00:00
248
    flavor:
249
            id:    1
250
    id:              323
251
    image:
252
            id:    f1r57-1m4g3-1d
253
    name:            my cluster 3
254
    [progress bar waiting server to build]
255
    Server 323: status is now ACTIVE
256

    
257
    ... <omitted for clarity>
258
    adminPass:       S0mePassw0rdFour
259
    created:         2013-06-19T12:34:59.362078+00:00
260
    flavor:
261
            id:    1
262
    id:              324
263
    image:
264
            id:    f1r57-1m4g3-1d
265
    name:            my cluster 4
266
    [progress bar waiting server to build]
267
    Server 324: status is now ACTIVE
268

    
269
.. note:: The creation dates are similar but not ordered. This is because the
270
    servers are created asynchronously. To deactivate asynchronous operations
271
    in kamaki, set max_theads to 1
272

    
273
    .. code-block:: console
274

    
275
        # Deactivate multithreading
276

    
277
        [server]: /config set max_theads 1
278

    
279
.. note:: the *- - wait* argument is optional, but if not used, the *create*
280
    call will terminate as long as the servers are spawned, even if they are
281
    not built yet.
282

    
283
.. warning:: The server details (password, etc.) are printed in
284
    **standard output** while the progress bar and notification messages are
285
    printed in **standard error**
286

    
287
Now, let's see our clusters:
288

    
289
.. code-block:: console
290

    
291
    [server]: list --name-prefix "my cluster "
292
    321 my cluster 2
293
    322 my cluster 1
294
    323 my cluster 3
295
    324 my cluster 4
296

    
297
For demonstration purposes, let's suppose that the maximum resource limit is
298
reached if we create 2 more servers. We will attempt to expand "my cluster" by
299
4 servers, expecting kamaki to raise a quota-related error.
300

    
301
.. code-block:: console
302

    
303
    $ kamaki
304
    [kamaki]: server
305
    [server]: create "my cluster " 1 f1r57-1m4g3-1d --cluster-size=4 --wait
306
    Failed to build 4 servers
307
    Found 2 matching servers:
308
    325 my cluster 1
309
    326 my cluster 2
310
    Check if any of these servers should be removed
311

    
312
    (413) REQUEST ENTITY TOO LARGE overLimit (Resource Limit Exceeded for your
313
    account.)
314
    |  Limit for resource 'Virtual Machine' exceeded for your account.
315
    Available: 0, Requested: 1
316

    
317
The cluster expansion has failed, but 2 of the attempted 4 servers are being
318
created right now. It's up to the users judgment to destroy them or keep them.
319

    
320
First, we need to list all servers:
321

    
322
.. code-block:: console
323

    
324
    [server] list --name-prefix="my cluster "
325
    321 my cluster 2
326
    322 my cluster 1
327
    323 my cluster 3
328
    324 my cluster 4
329
    325 my cluster 1
330
    326 my cluster 2
331

    
332
.. warning:: Kamaki will always create clusters by attaching an increment at
333
    the right of the prefix. The increments always start from 1.
334

    
335
Now, our cluster seems messed up. Let's destroy it and rebuilt it.
336

    
337
.. code-block:: console
338

    
339
    [server]: delete "my cluster " --cluster --wait
340
    [progress bar waiting server to be deleted]
341
    Server 321: status is now DELETED
342

    
343
    [progress bar waiting server to be deleted]
344
    Server 322: status is now DELETED
345

    
346
    [progress bar waiting server to be deleted]
347
    Server 323: status is now DELETED
348

    
349
    [progress bar waiting server to be deleted]
350
    Server 324: status is now DELETED
351

    
352
    [progress bar waiting server to be deleted]
353
    Server 325: status is now DELETED
354

    
355
    [progress bar waiting server to be deleted]
356
    Server 326: status is now DELETED
357

    
358
.. note:: *delete* performs a single deletion if feeded with a server id, but
359
    it performs a mass deletion, based on the name, if called with --cluster
360

    
361
While creating the first cluster, we had to note down all passwords 
362

    
363
The passwords for each server are printed on the console while creating them.
364
It would be far more convenient, though, if we could massively inject an ssh
365
key into all of them. Let's do that!
366

    
367
.. code-block:: console
368

    
369
    [server]: create "my new cluster " 1 f1r57-1m4g3-1d --cluster-size=4 --wait --personality /home/someuser/.ssh/id_rsa.pub,/root/.ssh/authorized_keys,root,root,0777
370

    
371
    ... <output omitted for clarity>
372

    
373
Now, let's check if the cluster has been created.
374

    
375
.. code-block:: console
376

    
377
    [server]: list --name-prefix="my new cluster "
378
    321 my new cluster 1
379
    322 my new cluster 2
380
    323 my new cluster 3
381
    324 my new cluster 4
382

    
383
We now have a cluster of 4 virtual servers and we can ssh in all of them
384
without a password.
385

    
386
Here is a bash script for creating clusters:
387

    
388
.. code-block:: bash
389

    
390
    #!/bin/bash
391

    
392
    CL_PREFIX="cluster"
393
    CL_SIZE=4
394

    
395
    PUB_KEYS="${HOME}/.ssh/id_rsa.pub"
396
    OUT="cl_servers.txt"
397

    
398
    CLOUD=`kamaki config get default_cloud`
399
    FLAVOR_ID=1
400
    IMAGE_ID="f1r57-1m4g3-1d"
401

    
402
    echo "Clean up cluster \"${CL_PREFIX}\""
403
    kamaki --cloud=${CLOUD} server delete ${CL_PREFIX} --cluster --wait
404
    echo "Cluster \"${CL_PREFIX}\"" > ${OUT}
405

    
406
    echo "Create cluster \"${CL_PREFIX}\" of size ${CL_SIZE}"
407
    kamaki --cloud=${CLOUD} server create ${CL_PREFIX} ${FLAVOR_ID} ${IMAGE_ID}
408
        --cluster-size=${CL_SIZE} --wait
409
        --personality ${PUB_KEYS},/root/.ssh/authorized_keys,root,root >>${OUT}
410

    
411
    echo "A list of created servers can be found at ${OUT}"