环境:OCP 3.4
1. 选择 MongoDB (Ephemeral) 模板,输入如下参数值:
2. 为 nationalparks 的 DeploymentConfig 增加环境变量
oc env dc nationalparks -e MONGODB_USER=mongodb -e MONGODB_PASSWORD=mongodb -e MONGODB_DATABASE=mongodb -e MONGODB_SERVER_HOST=mongodb
确认环境变量设置成功
oc env dc/nationalparks --list
输出如下:
# deploymentconfigs nationalparks, container nationalparks
MONGODB_USER=mongodb
MONGODB_PASSWORD=mongodb
MONGODB_DATABASE=mongodb
MONGODB_SERVER_HOST=mongodb
由于修改了 DC 的环境变量,nationalparks 会自动重新发布。
3. 为数据库增加数据
查询数据
http://nationalparks-applications.apps.example.com/ws/data/all
输出如下:
[]
加载数据
http://nationalparks-applications.apps.example.com/ws/data/load
输出如下:
Items inserted in database: 2740
再次查询数据,这下有很多数据了。
nationalparks 是怎么连上数据库呢?
cd ~/mygit
vim nationalparks/src/main/java/com/openshift/evg/roadshow/parks/db/MongoDBConnection.java
@PostConstruct
public void initConnection() {
String mongoHost = env.getProperty("mongodb.server.host", "127.0.0.1"); // env var MONGODB_SERVER_HOST takes precedence
String mongoPort = env.getProperty("mongodb.server.port", "27017"); // env var MONGODB_SERVER_PORT takes precedence
String mongoUser = env.getProperty("mongodb.user", "mongodb"); // env var MONGODB_USER takes precedence
String mongoPassword = env.getProperty("mongodb.password", "mongodb"); // env var MONGODB_PASSWORD takes precedence
String mongoDBName = env.getProperty("mongodb.database", "mongodb"); // env var MONGODB_DATABASE takes precedence
try {
String mongoURI = "mongodb://" + mongoUser + ":" + mongoPassword + "@" + mongoHost + ":" + mongoPort + "/" + mongoDBName;
System.out.println("[INFO] Connection string: " + mongoURI);
MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURI));
mongoDB = mongoClient.getDatabase(mongoDBName);
} catch (Exception e) {
System.out.println("[ERROR] Creating the mongoDB. " + e.getMessage());
mongoDB = null;
}
}
4. 为 nationalparks service 增加 label:type=parksmap-backend
注意,教材里说要为 nationalparks route 增加 label:type=parksmap-backend 是错误的,应该是为 nationalparks service 增加 label:type=parksmap-backend。
oc label svc nationalparks type=parksmap-backend
确认 label 添加成功
oc describe svc nationalparks
5. 重新查看 http://parksmap-applications.apps.example.com/
6. 疑问
重启所有机器后,发现只能显示 mlbparks,感觉好像只能选择一个 parksmap-backend。
具体原因有待研究。
不重启修改后,可以同时显示 mlbparks 和 nationalparks。
oc describe route nationalparks
oc describe svc nationalparks
oc label route nationalparks type-
oc label route nationalparks type-
oc label route nationalparks type=parksmap-backend
oc label svc nationalparks type=parksmap-backend
oc describe route mlbparks
oc describe svc mlbparks
oc label route mlbparks type-
oc label svc mlbparks type-
oc label route mlbparks type=parksmap-backend
oc label svc mlbparks type=parksmap-backend
参考文献:
1. http://labs.openshift3roadshow.com/roadshow/
1. 选择 MongoDB (Ephemeral) 模板,输入如下参数值:
2. 为 nationalparks 的 DeploymentConfig 增加环境变量
oc env dc nationalparks -e MONGODB_USER=mongodb -e MONGODB_PASSWORD=mongodb -e MONGODB_DATABASE=mongodb -e MONGODB_SERVER_HOST=mongodb
确认环境变量设置成功
oc env dc/nationalparks --list
输出如下:
# deploymentconfigs nationalparks, container nationalparks
MONGODB_USER=mongodb
MONGODB_PASSWORD=mongodb
MONGODB_DATABASE=mongodb
MONGODB_SERVER_HOST=mongodb
由于修改了 DC 的环境变量,nationalparks 会自动重新发布。
3. 为数据库增加数据
查询数据
http://nationalparks-applications.apps.example.com/ws/data/all
输出如下:
[]
加载数据
http://nationalparks-applications.apps.example.com/ws/data/load
输出如下:
Items inserted in database: 2740
再次查询数据,这下有很多数据了。
nationalparks 是怎么连上数据库呢?
cd ~/mygit
vim nationalparks/src/main/java/com/openshift/evg/roadshow/parks/db/MongoDBConnection.java
@PostConstruct
public void initConnection() {
String mongoHost = env.getProperty("mongodb.server.host", "127.0.0.1"); // env var MONGODB_SERVER_HOST takes precedence
String mongoPort = env.getProperty("mongodb.server.port", "27017"); // env var MONGODB_SERVER_PORT takes precedence
String mongoUser = env.getProperty("mongodb.user", "mongodb"); // env var MONGODB_USER takes precedence
String mongoPassword = env.getProperty("mongodb.password", "mongodb"); // env var MONGODB_PASSWORD takes precedence
String mongoDBName = env.getProperty("mongodb.database", "mongodb"); // env var MONGODB_DATABASE takes precedence
try {
String mongoURI = "mongodb://" + mongoUser + ":" + mongoPassword + "@" + mongoHost + ":" + mongoPort + "/" + mongoDBName;
System.out.println("[INFO] Connection string: " + mongoURI);
MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURI));
mongoDB = mongoClient.getDatabase(mongoDBName);
} catch (Exception e) {
System.out.println("[ERROR] Creating the mongoDB. " + e.getMessage());
mongoDB = null;
}
}
4. 为 nationalparks service 增加 label:type=parksmap-backend
注意,教材里说要为 nationalparks route 增加 label:type=parksmap-backend 是错误的,应该是为 nationalparks service 增加 label:type=parksmap-backend。
oc label svc nationalparks type=parksmap-backend
确认 label 添加成功
oc describe svc nationalparks
输出如下:
Name: nationalparks
Namespace: applications
Labels: app=nationalparks
type=parksmap-backend
Selector: deploymentconfig=nationalparks
Type: ClusterIP
IP: 172.30.69.152
Port: 8080-tcp 8080/TCP
Endpoints: 10.129.0.76:8080
Session Affinity: None
No events.
Namespace: applications
Labels: app=nationalparks
type=parksmap-backend
Selector: deploymentconfig=nationalparks
Type: ClusterIP
IP: 172.30.69.152
Port: 8080-tcp 8080/TCP
Endpoints: 10.129.0.76:8080
Session Affinity: None
No events.
6. 疑问
重启所有机器后,发现只能显示 mlbparks,感觉好像只能选择一个 parksmap-backend。
具体原因有待研究。
不重启修改后,可以同时显示 mlbparks 和 nationalparks。
oc describe route nationalparks
oc describe svc nationalparks
oc label route nationalparks type-
oc label route nationalparks type-
oc label route nationalparks type=parksmap-backend
oc label svc nationalparks type=parksmap-backend
oc describe route mlbparks
oc describe svc mlbparks
oc label route mlbparks type-
oc label svc mlbparks type-
oc label route mlbparks type=parksmap-backend
oc label svc mlbparks type=parksmap-backend
参考文献:
1. http://labs.openshift3roadshow.com/roadshow/
没有评论:
发表评论