- 1. Introduction
- 2. Before You Start
- 3. Downloading the Project
- 4. Deploy the app to WISE-PaaS
- 5. Application Introduce
- 6. Kubernetes Config
- 7. Docker
- 8. Deployment Application Steps
This sample code shows how to deploy an application to the EnSaaS 4.0 environment and connect to the the database (MongoDB) and message broker (RabbitMQ) services provided by the platform.
- Create a Docker Account
- Development Environment
- Install Docker
- Install kubectl
- Install MongoDB-Server
- Install Robo-3T
git clone https://github.com/WISE-PaaS/example-py-docker-iothub-mongodb.git
WISE-PaaS has 2 types of data centers
SA DataCenter:https://portal-mp-ensaas.sa.wise-paas.com
- Cluster:eks004
- Workspace:adv-training
- Namespace:level2
- Workspace:adv-training
HZ DataCenter:https://portal-mp-ensaas.hz.wise-paas.com.cn
- Cluster:eks006
- Workspace:advtraining
- Namespace:level2
- Workspace:advtraining
Simply backend appliaction。
app = Flask(__name__)
# port from cloud environment variable or localhost:3000
port = int(os.getenv("PORT", 3000))
@app.route('/', methods=['GET'])
def root():
if(port == 3000):
return 'py-docker-iothub-mongodb successful'
elif(port == int(os.getenv("PORT"))):
return render_template('index.html')
This is the MQTT and MongoDB connect config code,ENSAAS_SERVICESS
can get the application environment in WISE-PaaS。
ENSAAS_SERVICES = os.getenv('ENSAAS_SERVICES')
ENSAAS_SERVICES_js = json.loads(ENSAAS_SERVICES)
service_name = 'p-rabbitmq'
DB_SERVICE_NAME = 'mongodb'
#mqtt
broker = ENSAAS_SERVICES_js[service_name][0]['credentials']['protocols']['mqtt']['host']
username = ENSAAS_SERVICES_js[service_name][0]['credentials']['protocols']['mqtt']['username'].strip()
password = ENSAAS_SERVICES_js[service_name][0]['credentials']['protocols']['mqtt']['password'].strip()
mqtt_port = ENSAAS_SERVICES_js[service_name][0]['credentials']['protocols']['mqtt']['port']
The temp
is collection name and you can name by yourself
#mongodb
uri = ENSAAS_SERVICES_js[DB_SERVICE_NAME][0]['credentials']['uri']
app.config['MONGO_URI'] = uri
mongo = PyMongo(app)
collection = mongo.db.temp
Retrieve the secret, which iothub the secret contains
# List all secrets in the namespace
$ kubectl get secret --namespace=level2
# Output the secret content
$ kubectl get secret {secret_name} --namespace=level2 -o yaml
Copy the decoded content and paste it into the editor, such as Visual Studio Code, and let the plugin prettify it. You can now inspect the structure and start to construct your code.
# Decoding the secret
$ kubectl get secret {secret_name} --namespace=level2 -o jsonpath="{.data.ENSAAS_SERVICES}" | base64 --decode; echo
Copy the decoded content to vscode and Save as json format
Notice:the DB_SERVICE_NAME
and IOTHUB_SERVICE_NAME
need to be same name in decode content instance name。, Not the instance name in portal-service
This code can connect to IohHub,if it connect successful, on_connect
will print successful result and subscribe topic /hello
,you can define topic by yourself,and when we receive message on_message
will save data to the mongodb and get the time immediate。
#mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("/hello")
print('subscribe on /hello')
def on_message(client, userdata, msg):
print(msg.topic+','+msg.payload.decode())
ti = datetime.datetime.now()
topic = msg.topic
data = msg.payload.decode()
temp_id = collection.insert({'date': ti, 'topic': topic, 'data': data})
new_temp = collection.find_one({'_id': temp_id})
output = {'date': new_temp['date'],
'topic': new_temp['topic'], 'data': new_temp['data']}
print(output)
client = mqtt.Client()
client.username_pw_set(username, password)
client.on_connect = on_connect
client.on_message = on_message
client.connect(broker, mqtt_port, 60)
client.loop_start()
This file can help us publish message to topic。
Edit the publisher.py broker、port、username、password
you can find in ENSAAS_SERVICES
- bokrer:"ENSAAS_SERVICES => p-rabbitmq => externalHosts"
- port :"ENSAAS_SERVICES => p-rabbitmq => mqtt => port"
- username :"ENSAAS_SERVICES => p-rabbitmq => mqtt => username"
- password: "ENSAAS_SERVICES => p-rabbitmq => mqtt => password"
Each user needs to adjust the variables for certification, as follows:
- metadata >> name:py-docker-iothub-mongodb-{user_name}
- student:{user_name}
- image:{docker_account} / py-docker-iothub-mongodb:latest
- containerPort:listen 3000
- env >> valueFrom >> secretRef >> name:need same name in Portal-service {secret name}
Notice:In Portal-Services secret name
Each user needs to adjust the variables for certification, as follows:
- metadata >> name:py-docker-iothub-mongodb-{user_name}
- host:py-docker-iothub-mongodb-{user_name} . {namespace_name} . {cluster_name}.en.internal
- serviceName:need to be same name in Service.yaml {metadata name}
- servicePort:same port in Service.yaml
Each user needs to adjust the variables for certification, as follows:
- metadata >> name:py-docker-iothub-mongodb-{user_name}
- student:{user_name}
- port:same {port} in ingress.yaml
- targetPort:same {port} in deployment.yaml {containerPort}
We first download the python:3.6 and copy this application to /app
,and install library define in requirements.txt
FROM python:3.6-slim
WORKDIR /app
ADD . /app
RUN pip3 install -r requirements.txt
EXPOSE 3000
CMD ["python", "-u", "index.py"]
Adjust to your docker account
$ docker build -t {docker_account / py-docker-iothub-mongodb:latest} .
$ docker push {docker_account / py-docker-iothub-mongodb:latest}
$ kubectl apply -f k8s/
# grep can quickly find key words
$ kubectl get all --namespace=level2 | grep mongodb-sk-chen
Open two terminal first.
# 1. View the log of the container
kubectl logs -f pod/{pod_name} --namespace=level2
# 2. Send message to application in WISE-PaaS
python publisher.py
You can use Robo-3T to check our your insert data,so you need to go to Portal-service or decode data to get your config
Robo 3T create server (File => connect => Create)
- address => ENSAAS_SERVICES => mongodb-innoworks => 0 => external_host
- Database => ENSAAS_SERVICES => mongodb-innoworks => 0 => credentials => database
- Username => ENSAAS_SERVICES => mongodb-innoworks => 0 => credentials => username
- Password => ENSAAS_SERVICES => mongodb-innoworks => 0 => credentials => password