Skip to content

Commit 5680350

Browse files
Merge pull request #23 from devdattakulkarni/Containers
Added Docker installation script; Also greetings example
2 parents be0ae7b + 2294dbd commit 5680350

12 files changed

Lines changed: 413 additions & 107 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM ubuntu:14.04
2+
RUN apt-get update -y \
3+
&& apt-get install -y python-setuptools python-pip python-mysqldb
4+
ADD requirements.txt /src/requirements.txt
5+
RUN cd /src; pip install -r requirements.txt
6+
ADD . /src
7+
EXPOSE 5000
8+
CMD ["python", "/src/application.py"]
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
Greetings
2+
----------
3+
4+
This example shows deployment of a web application that uses MySQL backend.
5+
6+
Several environment definition files are available:
7+
8+
1) environment-local.yaml: Environment definition containing MySQL container platform element
9+
2) environment-rds-local.yaml: Environment definition containing RDS resource. The RDS instance
10+
is open so as to allow connecting application container running locally on your machine
11+
3) environment-rds-ecs.yaml: Environment definition containing RDS and ECS cluster resource.
12+
4) environment-cloudsql-gke.yaml: Environment definition containing Cloud SQL and GKE cluster resource.
13+
14+
Several application definition files are available:
15+
16+
1) app-local.yaml: Application definition for local deployment of application
17+
2) app-aws.yaml: Application definition for AWS ECS deployment
18+
3) app-gcloud.yaml: Application definition for Google GKE deployment
19+
4) greetings-pod.yaml: Kubernetes Pod definition for Google GKE deployment
20+
21+
22+
23+
===============
24+
Deploy Locally
25+
===============
26+
27+
Deploy application locally binding to local MySQL container
28+
29+
$ cld env create env-local environment-local.yaml
30+
31+
$ cld container create cont1 local
32+
33+
Edit app-local.yaml to include image id obtained from output of command:
34+
35+
$ cld container show cont1
36+
37+
$ cld app deploy greetings-local env-local app-local.yaml
38+
39+
40+
==================
41+
Deploy on AWS ECS
42+
==================
43+
44+
Deploy application on ECS binding to a RDS instance
45+
46+
$ cld env create env-aws environment-rds-ecs.yaml
47+
48+
$ cld container create cont2 ecr
49+
50+
Edit app-aws.yaml to include image url obtained from output of command:
51+
52+
$ cld container show cont2
53+
54+
$ cld app deploy greetings-aws env-aws app-aws.yaml
55+
56+
57+
======================================
58+
Deploy on Google GKE - using app yaml
59+
======================================
60+
61+
Deploy application on GKE binding to a Cloud SQL instance
62+
63+
$ cld env create env-gcloud environment-cloudsql-gke.yaml
64+
65+
$ cld container create cont3 gcr
66+
67+
Edit app-gcloud.yaml to include image url obtained from output of command:
68+
69+
$ cld container show cont3
70+
71+
$ cld app deploy greetings-gke env-gcloud app-gcloud.yaml
72+
73+
74+
======================================
75+
Deploy on Google GKE - using Pod yaml
76+
======================================
77+
78+
Deploy application on GKE binding to a Cloud SQL instance
79+
80+
$ cld env create env-gcloud environment-cloudsql-gke.yaml
81+
82+
$ cld container create cont3 gcr
83+
84+
Edit greetings-pod.yaml to include image url obtained from output of command:
85+
86+
$ cld container show cont3
87+
88+
$ cld app deploy greetings-gke env-gcloud greetings-pod.yaml
89+
90+
91+
Track / Debug:
92+
---------------
93+
94+
$ cld env show <env-name>
95+
96+
$ cld app show <app-name>
97+
98+
$ cld app logs <app-name>
99+
100+
$ cld env shell <env-name>
101+
102+
103+
Verify:
104+
-------
105+
106+
$ cld app show <app-name>
107+
108+
$ cld app list
109+
110+
$ cld env show <env-name>
111+
112+
$ cld env list
113+
114+
115+
Cleanup:
116+
--------
117+
118+
$ cld app delete <app-name>
119+
120+
$ cld env delete <env-name>
121+
122+
$ cld container delete <container-name>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
import os
2+
import time
3+
4+
from flask import request
5+
from flask import Flask, render_template
6+
import mysql.connector
7+
from mysql.connector import errorcode
8+
9+
10+
application = Flask(__name__)
11+
app = application
12+
13+
14+
def get_db_creds():
15+
db = os.environ.get("DB", None) or os.environ.get("database", None)
16+
username = os.environ.get("USER", None) or os.environ.get("username", None)
17+
password = os.environ.get("PASSWORD", None) or os.environ.get("password", None)
18+
hostname = os.environ.get("HOST", None) or os.environ.get("dbhost", None)
19+
return db, username, password, hostname
20+
21+
22+
def create_table():
23+
# Check if table exists or not. Create and populate it only if it does not exist.
24+
db, username, password, hostname = get_db_creds()
25+
table_ddl = 'CREATE TABLE message(id INT UNSIGNED NOT NULL AUTO_INCREMENT, greeting TEXT, PRIMARY KEY (id))'
26+
27+
cnx = ''
28+
try:
29+
cnx = mysql.connector.connect(user=username, password=password,
30+
host=hostname,
31+
database=db)
32+
except Exception as exp:
33+
print(exp)
34+
import MySQLdb
35+
#try:
36+
cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db)
37+
#except Exception as exp1:
38+
# print(exp1)
39+
40+
cur = cnx.cursor()
41+
42+
try:
43+
cur.execute(table_ddl)
44+
cnx.commit()
45+
populate_data()
46+
except mysql.connector.Error as err:
47+
if err.errno == errorcode.ER_TABLE_EXISTS_ERROR:
48+
print("already exists.")
49+
else:
50+
print(err.msg)
51+
52+
53+
def populate_data():
54+
55+
db, username, password, hostname = get_db_creds()
56+
57+
print("Inside populate_data")
58+
print("DB: %s" % db)
59+
print("Username: %s" % username)
60+
print("Password: %s" % password)
61+
print("Hostname: %s" % hostname)
62+
63+
cnx = ''
64+
try:
65+
cnx = mysql.connector.connect(user=username, password=password,
66+
host=hostname,
67+
database=db)
68+
except Exception as exp:
69+
print(exp)
70+
import MySQLdb
71+
cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db)
72+
73+
cur = cnx.cursor()
74+
cur.execute("INSERT INTO message (greeting) values ('Hello, World!')")
75+
cnx.commit()
76+
print("Returning from populate_data")
77+
78+
79+
def query_data():
80+
81+
db, username, password, hostname = get_db_creds()
82+
83+
print("Inside query_data")
84+
print("DB: %s" % db)
85+
print("Username: %s" % username)
86+
print("Password: %s" % password)
87+
print("Hostname: %s" % hostname)
88+
89+
cnx = ''
90+
try:
91+
cnx = mysql.connector.connect(user=username, password=password,
92+
host=hostname,
93+
database=db)
94+
except Exception as exp:
95+
print(exp)
96+
import MySQLdb
97+
cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db)
98+
99+
cur = cnx.cursor()
100+
101+
cur.execute("SELECT greeting FROM message")
102+
entries = [dict(greeting=row[0]) for row in cur.fetchall()]
103+
return entries
104+
105+
try:
106+
print("---------" + time.strftime('%a %H:%M:%S'))
107+
print("Before create_table global")
108+
create_table()
109+
print("After create_data global")
110+
except Exception as exp:
111+
print("Got exception %s" % exp)
112+
conn = None
113+
114+
115+
@app.route('/add_to_db', methods=['POST'])
116+
def add_to_db():
117+
print("Received request.")
118+
print(request.form['message'])
119+
msg = request.form['message']
120+
121+
db, username, password, hostname = get_db_creds()
122+
123+
cnx = ''
124+
try:
125+
cnx = mysql.connector.connect(user=username, password=password,
126+
host=hostname,
127+
database=db)
128+
except Exception as exp:
129+
print(exp)
130+
import MySQLdb
131+
cnx = MySQLdb.connect(unix_socket=hostname, user=username, passwd=password, db=db)
132+
133+
cur = cnx.cursor()
134+
cur.execute("INSERT INTO message (greeting) values ('" + msg + "')")
135+
cnx.commit()
136+
return hello()
137+
138+
139+
@app.route("/")
140+
def hello():
141+
print("Inside hello")
142+
print("Printing available environment variables")
143+
print(os.environ)
144+
print("Before displaying index.html")
145+
entries = query_data()
146+
print("Entries: %s" % entries)
147+
return render_template('index.html', entries=entries)
148+
149+
150+
if __name__ == "__main__":
151+
app.debug = True
152+
app.run(host='0.0.0.0')
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apiVersion: apps/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: greetings-deployment
5+
labels:
6+
app: greetings
7+
spec:
8+
replicas: 1
9+
selector:
10+
matchLabels:
11+
app: greetings
12+
template:
13+
metadata:
14+
labels:
15+
app: greetings
16+
spec:
17+
containers:
18+
- name: greetings1
19+
image: gcr.io/cloudark-test-gke/greetings:v1
20+
ports:
21+
- containerPort: 5000
22+
env:
23+
- name: "PASSWORD"
24+
value: testpass123!@#
25+
- name: "DB"
26+
value: testdb
27+
- name: "HOST"
28+
value: 35.225.61.225
29+
- name: "USER"
30+
value: root
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: greetings1
5+
labels:
6+
name: greetings1
7+
spec:
8+
containers:
9+
- image: us.gcr.io/cloudark-test-gke/greetpod:1512924392448
10+
name: wordpress
11+
ports:
12+
- containerPort: 5000
13+
env:
14+
- name: "PASSWORD"
15+
value: $CLOUDARK_CLOUDSQL_Password
16+
- name: "DB"
17+
value: $CLOUDARK_CLOUDSQL_DBName
18+
- name: "HOST"
19+
value: $CLOUDARK_CLOUDSQL_Address
20+
- name: "USER"
21+
value: $CLOUDARK_CLOUDSQL_Username
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
kind: Service
2+
apiVersion: v1
3+
metadata:
4+
name: greeting-service
5+
spec:
6+
selector:
7+
app: greetings
8+
ports:
9+
- protocol: TCP
10+
port: 80
11+
targetPort: 5000
12+
type: LoadBalancer
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
flask
2+
https://cdn.mysql.com/Downloads/Connector-Python/mysql-connector-python-1.0.12.tar.gz
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<html>
2+
<head>
3+
<style>
4+
.bodycenter {
5+
margin: auto;
6+
width: 60%;
7+
padding: 10px;
8+
}
9+
10+
.footer{
11+
text-align: center;
12+
border: 1px solid #73AD21;
13+
}
14+
</style>
15+
</head>
16+
<body>
17+
<div class="bodycenter">
18+
19+
<form action="add_to_db" method="post">
20+
<p>Sample Web Application that uses a managed relational database service such as Google CloudSQL or Amazon RDS.</p>
21+
<br>
22+
Input a greeting: <input type="text" name="message"><br>
23+
<input type="submit" value="Add greeting to database">
24+
</form>
25+
26+
<p>Greetings stored in database</p>
27+
<br>
28+
{% for entry in entries %}
29+
<div>
30+
<div>{{ entry.greeting }}</div>
31+
</div>
32+
{% endfor %}
33+
34+
<div class="footer">
35+
</div>
36+
</div>
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)