Deployment in the “PAAS” Cloud...•Nodejs & python are the best in terms of performance • An...
Transcript of Deployment in the “PAAS” Cloud...•Nodejs & python are the best in terms of performance • An...
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
Jose Javier Garcia Aranda email: [email protected]
October 2014
Deployment in the “PAAS”
Cloud
Deploying….please wait
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
overview of main tools
STS (security token service)
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
Agenda
1| Introduction
2| S3 and SimpleDB
3| Users and Roles
4| programming languages
5| python on aws
6| javascript client on aws
7| nodejs on aws
8| JSON and simpleDB
9| debugging Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
4
Environment and aplication( I)
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
5
Environment and aplication( II)
1 aplicación = N environments
1 environment = 1 programming language= 1 virtual instance
Environments dont share machines
python
nodejs
one application
http://aws.amazon.com/es/
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
6
Environment platforms: language and instance type
•Nodejs & python are the best in terms of performance
• An smaller type of instance is cheaper but this strategy force to instance more machines if incoming traffic grows. So, if there is success, perhaps it is not the cheapest strategy
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
7
instances Click on EC2 pto see all your instances
Instances of the same application.
Ordered by environment. For each environment we can have N instances
One instance can not have more than one environment
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
8
Uploading software: version control
ZIP with sources
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
9
costs
750 hours free per month ( it means one machine). The rest of machines are paid per hours. The same for load balancers. Load balancers looks expensive but there are only one per environment
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
10
Costs (II): designing applications Incoming traffic to S3 from internet is free
Outgoing traffic from Services to S3 is not free (free only first 15GB)
Server processing is not free (only 750h) therefore is a good option to move processing to client side
Design is constrained by costs. A more efficient design usually is also
cheaper
http://calculator.s3.amazonaws.com/index.html
EXAMPLE: 5 instances, 5 load balancers, 10 GB/month incoming traffic and 10GB outgoing traffic. Almost all cost are related to instances, but only around 150€
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
11
Elasticity triggers At the end of
development you will determine your
thresholds by means of load tests. Depends on
type of requests, type of processing, languages, database read/writes,
etc
And of course:
INSTANCE TYPE
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
12
Agenda
1| Introduction
2| S3 and SimpleDB
3| Users and Roles
4| programming languages
5| python on aws
6| javascript client on aws
7| nodejs on aws
8| JSON and simpleDB
9| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
13
S3 Massive storage, extremely cheap
Organized in “buckets”. A directory is a “bucket” inside other “bucket”
You can create infinite buckets
Click on S3 to see all your buckets and edit permissions, etc
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
14
To upload a file or object to S3 you must use an HTTP interface
amazon libraries simplifies the process, then you use functions instead HTTP requests
to use it from a program you must have user’s credentials or role’s credentials
http://pruebaspython-env.elasticbeanstalk.com/
Ejemplo pag 14 superfacil javascript
cliente
S3
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
15
SimpleDB noSQL database for massive estorage. Extremely cheap
Schema less: create domain without fields, then at object insertion , specifies the fields
Tables are called “domains”
http://ejemplopythonsimple-env.elasticbeanstalk.com/creadomain?domain=hola
No human interface. But SimpleDB plugin for firefox.
You can create domains using :
Manually using a python window
or a server side program created by yourself
http://ejemplopythonsimple-env.elasticbeanstalk.com/borradomain?domain=hola
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
16
SimpleDB: SDBTool plugin
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
17
Import boto Import boto.sdb sdb=boto.sdb.connect_to_region('eu-west-1', aws_access_key_id='AKIAJDXPT5WBCIN6IRJQ', aws_secret_access_key='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') my_domain=sdb.create_domain("hola") domains = sdb.get_all_domains() domains My_domain = sdb.get_domain('hola') item_name = 'un_item' item_attrs = {'Artist': 'The Jackson 5', 'Genera':'Pop'} my_domain.put_attributes(item_name, item_attrs) item_name = 'otro_item' item_attrs = {'Artist': 'julio iglesias', 'Genera':'Pop'} my_domain.put_attributes(item_name, item_attrs) my_domain.get_item('un_item') sdb.get_attributes(my_domain,'un_item',attribute_names='Artist') query = 'select * from hola where Genera="Pop"' rs = my_domain.select(query) for j in rs: print j.name + "tiene como autor:" + j['Artist'] sdb.delete_domain("hola")
SimpleDB: py example
Conecta y lista los dominios
Inserta dos items
Lee un item individual usando dos formas diferentes
Hace una query basada en un campo e imprime los resultados
Borra el dominio
Usar linea de comando python, desde nuestro PC en una ventana
command Importa la librería boto de amazon para python
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
18
Agenda
1| Introduction
2| S3 and SimpleDB
3| Users and Roles
4| programming languages
5| python on aws
6| javascript client on aws
7| nodejs on aws
8| JSON and simpleDB
9| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
19
Users and Roles
amazon’s account credentials
User’s credentials
Role’s credentials
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
20
Creating amazon account credentials
Secret Key id : it is like a login
Secret Access key: it is like a password
Users and Roles
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
21
Users and roles are created separately
A user can asume a role
A user can generate temporal credentials of certain role, using STS (Security Token Service) of AWS
Users and Roles
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
22
Temporal credentials are useful to access amazon resources (like S3 ) directly from a browser instead from a server-side
Without valid credentials, S3 reject requests
Creating temporal credentials in node.js
import boto from boto.sts import STSConnection def pedirToken(): sts_connection = boto.sts.connect_to_region('eu-west-1',aws_access_key_id='AKIAJMSM5FKHYGHQOHDQ',aws_secret_access_key='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') assumedRoleObject = sts_connection.assume_role(role_arn="arn:aws:iam::866663158052:role/mmsclient",role_session_name="AssumeRoleSession1",duration_seconds=900) return assumedRoleObject
Creating temporal credentials in python
Ejemplo javascript facil pag 12
Role’s credencials MUST include: AccessKey, SecretAccessKey and SessionToken. Without sessionToken you can not connect
Users and Roles
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
23
Agenda
1| Introduction
2| S3 and SimpleDB
3| Users and Roles
4| programming languages
5| python on aws
6| javascript client on aws
7| nodejs on aws
8| JSON and simpleDB
9| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
24
Node has a monothread execution model, which allow more concurrency that nother lenguages because ( among other reasons) a lower use of RAM
Python and node offers a similar performance. Python is multithread but is comparable with node
Jetty and Resin are java engines, with similar performance than node or python but amazon uses tomcat
languages
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
25
languages
Requirements.txt Package.json
Declare dependencies
And at client side you can use javascript
Root file
import aws library
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
26
languages
Service X
Logic in Python, nodejs, java, etc
Respnses in JSON format
Request from browser
Request from javascript
light client HTML+JS and/or Jquery Service X
hello
Good practices: do not mix HTML neither javascript in server responses. Templates engine is only useful for initial client aplication download
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
27
Agenda
1| Introduction
2| S3 and SimpleDB
3| Users and Roles
4| programming languages
5| python on aws
6| javascript client on aws
7| nodejs on aws
8| JSON and simpleDB
9| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
28
http://boto.readthedocs.org/en/latest/index.html#
Python on aws
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
29
Python on aws Install python (it has editor)
Install boto library
Install a framework : Django o Flask ( both compatible with aws)
Develop locally. If you are behind firewall in intranet, perhaps you must upload services before test ( otherwise S3 or simpleDB connections could fail)
Application.py
DIR A
DIR B
fichero1.py fichero2.py
__init__.py
To upload software to amazon you must build a ZIP file with the complete application
Requirements.txt
URL
(URL routing)
1
2
3
4
5
fichero3.py
__init__.py
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
30
Server code
Client code
Server code
YOUR COMPUTER
deploy
Express server
Listening on
127.0.0.1:3000
Testing locally, and then deploy
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
31
Python on aws Application.py
otrofile.py
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
32
Python on aws This example uses boto to access simpleDB and all is included in one file (application.py)
Conectar siempre usando región europa
Credenciales de cuenta amazon o bien credenciales de un
usuario creado desde la cuenta amazon
Inicializa variables o error 500
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
33
Python on aws
Ejecutado en local. Si no hay cortafuegos, python se
conectará a simpledb desde nuestro PC
Ejecutado en amazon. Siempre va a funcionar y no
hay conexión a simpledb desde nuestro PC
flask escucha en el 5000 por defecto
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
34
Python on aws This example create a temporal credentials using STS (Security Token Service)
Credenciales de un usuario creado desde la cuenta amazon. La credenciales de cuenta amazon en este caso no valen, solo un usuario puede asumir un rol
Asumes the role “mmsclient”
Le pasa las credenciales al motor de templates
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
35
Agenda
1| Introduction
2| S3 and SimpleDB
3| Users and Roles
4| programming languages
5| python on aws
6| javascript client on aws
7| nodejs on aws
8| JSON and simpleDB
9| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
36
Javascript on aws Esta es la continuación del ejemplo anterior. Ahora vemos el lado cliente javascript
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
37
Javascript con aws
Usa las tres componentes de las credenciales temporales. El token es
necesario
Instanciamos un objeto bucket usando la librería AWS que nos conecta a S3
Cargamos la librería aws-sdk
These symbols “{{“ is interpreted by template engine and replace these
variables by their values
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
38
Agenda
1| Introducción
2| S3 y SimpleDB
3| Roles y usuarios
4| lenguajes
5| python con aws
6| javascript cliente con aws
7| nodejs con aws
8| JSON y simpleDB
9| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
39
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/frames.html
Nodejs on aws
Es la misma documentacion tanto en javascript cliente como en nodejs. Solo
cambia la forma de instalar
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
40
Nodejs on aws Install node
Install a framework : expressjs (includes templates engine “jade”)
Install editor: nodeclipse.
Develop and test locally (executing app.js). If you are behind intranet firewall perhaps you must upload to amazon before test
URL
1
2 2
4
5
App.js
DIR A
DIR B
fichero1.js fichero2.js
Package.json
(URL routing)
fichero3.js
Install aws-sdk using npm.
3 execute package json to Install aws-sdk. and express
3
To upload to amazon you must create a ZIP including all files
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
41
Package.json
Same functionality as requirements.txt of python
Nodejs on aws
Do not execute before adding this line
Usando nodeclipse
App.js: includes the mapping between URLs and functions included in other files .js
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
42
Express escucha en el puerto 3000
Nodejs on aws
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
43
Nodejs on aws
Variables son referencias a ficheros
Mapeo de Urls a funciones de esos ficheros
Fichero App.js
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
44
Fichero PruebaS3.js
Nodejs on aws
In this case you can use amazon account’s credentials instead temporal credentials because this credentials are not sent to browser. This is server-side
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
45
Fichero token.js
Nodejs con aws
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
46
Node js y simpledb Variables POST
This example insert a new item into table “playlist and then list by console all stored elements
User_id url keywords
No blocking function. Client does not wait for callback invocation
Nombre del campo valor del campo
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
47
blocking. Esta vez el cliente espera a que la función callback sea invocada
Hemos comentado la respuesta y ahora contestamos desde dentro
Nodejs con aws
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
48
Agenda
1| Introducción
2| S3 y SimpleDB
3| Roles y usuarios
4| lenguajes
5| python con aws
6| javascript cliente con aws
7| nodejs con aws
8| JSON y simpleDB
9| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
49
JSON y simpleDB Concepto JSON
JSON son strings, siempre. Y a partir de estos strings se pueden construir objetos javascript y viceversa. Pero NO son lo mismo. Un objeto no es un string
simpleDB y JSON
JSON son strings y por tanto podemos hacer queries de tipo “select campo1 from tabla where campo2 like ‘valor%’ de este modo podemos encontrar tuplas bajo el criterio de que un atributo de un objeto que se almacena en campo1 tenga cierto valor
Supongamos que recibimos unos campos por POST y queremos unirlos en un objeto que almacenaremos en base de datos. Recogemos los datos que llegan por post usando req.body.<nombre-de-inputfield> var input_keywords=req.body.keywords;
input_keywords=input_keywords.toUpperCase();
var split_keywords= input_keywords.split(",");
var keywords=JSON.stringify(split_keywords);
Ahora keywords es : ["MUSICA"," CINE"," OCIO"]
lo cual está en formato JSON, listo para ser almacenado. Al leerlo invocaremos a JSON.parse( cadena) y asi podremos acceder los 3 valores en forma de objeto array
Mi objeto[0] tomará el valor “MUSICA”
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
50
JSON y simpleDB Creamos una tupla en la que el valor del campo keywords es un string en formato JSON, convertible a objeto
Ahora consultamos la tabla e imprimimos
Insertamos la tupla
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
51
JSON y simpleDB usamos JSON.parse para convertir la cadena en objeto
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
52
Supongamos que queremos almacenar un perfil de usuario como una lista de palabras con pesos. El formato JSON es
Keywords = [ { “key”: “música”, “peso”: 5}, {“key”: “deportes”, “peso”: 7} ]
Para acceder, construiremos un objeto asi: var keywords=JSON.parse(keywords);. Y luego accedemos a keywords[0].key , o a keywords[0].peso Si lo que tenemos es una lista de palabras sin pesos:
Keywords = [ { “key”: “música” }, {“key”: “deportes” } ] como solo hay un campo , pues podemos eliminar dicho campo y asi queda:
Keywords = [ “música” , “deportes” ] Y asi accedermos a los valores como elementos de un array Claves= JSON.parse(keywords) Claves[0] es musica
Otros ejemplos sencillos de uso de JSON JSON y simpleDB
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
53
Agenda
1| Introducción
2| S3 y SimpleDB
3| Roles y usuarios
4| lenguajes
5| python con aws
6| javascript cliente con aws
7| nodejs con aws
8| JSON y simpleDB
8| debugging: Logs & CORS
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
54
logs
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
55
logs
Console.log (“ this is a message readable only checking logs”)
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
56
CORS: Cross Origin Resource Sharing
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
57
CORS: Cross Origin Resource Sharing
COPYRIGHT © 2012 ALCATEL-LUCENT. ALL RIGHTS RESERVED.
58
CORS: solution while developing
All Rights Reserved © Alcatel-Lucent 2010