全球大流行的冠状病毒 COVID-19(也称为 SARS-CoV-2)已经影响了全世界数百万人。 COVID-19 Web 应用程序是用 Python 和 Flask 开发的云应用程序原型,可提供受 COVID-19 影响的病例的全球统计数据。该应用程序利用 REST API 服务来获取 COVID-19 的每日统计数据,并且该应用程序部署在 AWS EC2 实例中。
获取所有国家和全球统计数据的摘要。
获取所有国家/地区的列表以及名称和国家/地区代码详细信息。
这将使用外部 API“https://api.covid19api.com/summary”加载包含 COVID 19 国家/地区统计数据的数据库
主页显示了 COVID 19 的全球统计数据以及用于查看国家/地区统计数据以及与外部 REST API 交互的链接。可以使用curl命令:
从数据库中获取所有国家的统计数据。可以使用以下curl命令执行:
curl -i -k https://0.0.0.0/summary/country
从数据库中获取全局统计数据。可以使用以下curl命令执行:
curl -i -k https://0.0.0.0/summary/global
从数据库中获取特定国家/地区的统计数据。可以使用以下curl命令执行:
curl -i -k https://0.0.0.0/summary/country/TestCountry
将新国家/地区添加到数据库中。用户必须提供以下信息:
这是一个POST请求,可以使用以下curl命令来执行:
curl -i -k -H "Content-Type: application/json" -X POST -d '{"NewConfirmed":2,"TotalConfirmed":3,"NewDeaths":3,"TotalDeaths":3,"NewRecovered":3,"TotalRecovered":3,"Country":"TestCountry"}' https://0.0.0.0/summary/country
将现有国家/地区更新到数据库。用户必须提供以下信息:
这是一个 PUT 请求,可以使用以下curl 命令执行:
curl -i -k -H "Content-Type: application/json" -X PUT -d '{"NewConfirmed":999,"TotalConfirmed":3,"NewDeaths":3,"TotalDeaths":3,"NewRecovered":3,"TotalRecovered":3,"Country":"TestCountry"}' https://0.0.0.0/summary/country
从数据库中删除国家/地区记录。用户必须提供以下信息:
这是一个 DELETE 请求,可以使用以下curl命令执行:
curl -i -k -H "Content-Type: application/json" -X DELETE -d '{"Country":"TestCountry"}' https://0.0.0.0/summary/country
将全局统计信息添加到数据库中。用户必须提供以下信息:
这里的“key”指的是值“Global”。这是一个 POST 请求,可以使用以下命令执行:
curl -i -k -H "Content-Type: application/json" -X POST -d '{"NewConfirmed":9,"TotalConfirmed":3,"NewDeaths":3,"TotalDeaths":3,"NewRecovered":3,"TotalRecovered":3,"Key":"TestGlobal"}' https://0.0.0.0/summary/global
将全局统计数据更新到数据库。用户必须提供以下信息:
这里的“key”指的是值“Global”。这是一个 PUT 请求,可以使用以下命令执行:
curl -i -k -H "Content-Type: application/json" -X PUT -d '{"NewConfirmed":1,"TotalConfirmed":10,"NewDeaths":3,"TotalDeaths":3,"NewRecovered":3,"TotalRecovered":3,"Key":"TestGlobal"}' https://0.0.0.0/summary/global
从数据库中删除全局统计记录。用户必须提供以下信息:
*钥匙
这里的“key”指的是值“Global”。这是一个 DELETE 请求,可以使用以下命令执行:
curl -i -k -H "Content-Type: application/json" -X DELETE -d '{"Key":"TestGlobal"}' https://0.0.0.0/summary/global
sudo apt update
sudo apt install docker.io
sudo docker pull cassandra:latest
sudo docker run --name cassandra-test -p 9042:9042 -d cassandra:latest
wget https://raw.githubusercontent.com/niranjanganesan/ECS781P_Cloud_Computing_Mini_Project/master/country.csv
wget https://raw.githubusercontent.com/niranjanganesan/ECS781P_Cloud_Computing_Mini_Project/master/global.csv
sudo docker cp country.csv cassandra-test:/home/country.csv
sudo docker cp global.csv cassandra-test:/home/global.csv
sudo docker exec -it cassandra-test cqlsh
cqlsh> CREATE KEYSPACE COVID19 WITH REPLICATION = {'class': 'SimpleStrategy', 'replication_factor': '1'};
cqlsh> CREATE TABLE COVID19.summary (Country text PRIMARY KEY,NewConfirmed int,
TotalConfirmed int, NewDeaths int, TotalDeaths int, NewRecovered int,
TotalRecovered int);
cqlsh> CREATE TABLE COVID19.global (Key text PRIMARY KEY,NewConfirmed int,
TotalConfirmed int, NewDeaths int, TotalDeaths int, NewRecovered int,
TotalRecovered int, TimeStamp text);
cqlsh> COPY COVID19.summary(Country, NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered)
FROM '/home/country.csv' WITH HEADER=TRUE;
cqlsh> COPY COVID19.global(Key, NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered)
FROM '/home/global.csv' WITH HEADER=TRUE;
该应用程序通过设置自签名证书通过 https 提供服务,如下所示:
$ openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
Generating a RSA private key
.....................................++++
....................................................................................++++
writing new private key to 'key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:UK
State or Province Name (full name) [Some-State]:England
Locality Name (eg, city) []:London
Organization Name (eg, company) [Internet Widgits Pty Ltd]:QMUL
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:localhost
Email Address []:[email protected]
if __name__ == "__main__":
context = ('cert.pem','key.pem')
app.run(host='0.0.0.0',port=443,ssl_context=context)
以下是在 kubernetes 中构建 docker 镜像和部署应用程序的步骤
sudo snap install microk8s --classic
sudo microk8s enable registry
apiVersion: apps/v1
kind: Deployment
metadata:
name: covidapp-deployment
labels:
app: covidapp
spec:
selector:
matchLabels:
app: covidapp
replicas: 3
template:
metadata:
labels:
app: covidapp
spec:
containers:
- name: covidapp
image: localhost:32000/covidapp:registry
ports:
- containerPort: 443
sudo docker build . -t localhost:32000/covidapp:registry
sudo docker push localhost:32000/covidapp:registry
{
"insecure-registries" : ["localhost:32000"]
}
注意:此步骤是可选的,只有在 docker Push 失败时才必须执行。
sudo systemectl restart docker
sudo docker start cassandra-test
sudo microk8s.kubectl apply -f ./deployment.yaml
we应用程序现已部署在kubernetes中
sudo microk8s.kubectl get deployment
sudo microk8s.kubectl get pods
sudo microk8s.kubectl expose deployment covidapp-deployment --port=443 --type=LoadBalancer
sudo microk8s.kubectl get services
以下是服务的状态示例:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/covidappv3-deployment LoadBalancer 10.152.183.77 <pending> 443:30873/TCP 17h
service/kubernetes ClusterIP 10.152.183.1 <none> 443/TCP 9d
在这里我们可以看到部署的 Web 应用程序暴露给 Nodeport“30873”。默认情况下,kubernetes 服务分配“30000-32767”范围内的端口。
最后,使用 AWS EC2 帐户的公共 DNS 以及以“30xxx”开头的 Nodeport 在浏览器中查看 Web 应用程序。
sudo microk8s.kubectl delete deployment covidapp-deployment
sudo microk8s.kubectl delete service covidapp-deployment
sudo docker rm cassandra-test