2012年11月21日 星期三

[CloudFoundry] Add Service to Cloud Foundry

最近因工作需求,大部分的時間都耗在CloundFoundry上面
但大部分的時間都在找尋他的解決方法,冏。。。

一下紀錄如何加入一個Service到CF
在開始前先名詞定義:
1. Echo Service :指Cloudfoundry上的Component,而主要又分成
Echo Node and Echo Gateway
2. Echo App :指的是在Cloudfoundry上運行的App
3. Echo Server : 指的是Echo App最後透過Echo Service連到的Deamon

[1] 環境條件


Node1 : REST
IP : 192.168.11.157

Node2 : DEA
IP : 192.168.11.158

[2] 安裝Echo Service


2.1 vcap_dev support echo_service

檔案路徑 : .../cloudfoundry/.deployments/rest/config/vcap_components.json

{"components":["router","cloud_controller","health_manager","dea","uaa",
...(略)
,"echo_node","echo_gateway"]}

2.2 確認echo_service有被註冊

檔案路徑 : .../cloudfoundry/vcap/dev_setup/lib/vcap_components.rb

## services: gateways & nodes
%w(redis mysql mongodb rabbitmq postgresql vblob neo4j memcached couchdb elasticsearch filesystem echo).each do |service|
ServiceComponent.register("#{service}_gateway")
end

%w(redis mysql mongodb rabbitmq postgresql vblob neo4j memcached couchdb elasticsearch echo).each do |service|
ServiceComponent.register("#{service}_node")
end

2.3 設定 commuincation token
檔案路徑 : .../cloudfoundry/.deployments/rest/config/cloud_controller.yml

# Services we provide, and their tokens. Avoids bootstrapping DB.
builtin_services:
redis:
token: changeredistoken
...(略)
echo:
token: changeechotoken

2.4 確認echo serivce有被configuration到nuke_service.rb
檔案路徑 : .../cloudfoundry/vcap/services/tools/misc/bin/nuke_service.rb

default_configs = {
:mongodb => File.expand_path("../../mongodb/config/mongodb_gateway.yml", __FILE__),
...(略)
:echo => File.expand_path("../../echo/config/echo_gateway.yml", __FILE__)
}
# Note : 如果想移除某個Service,則刪除相對應的設定

2.5 設定 Echo Gateway Config
原始路徑 : .../cloudfoundry/vcap/services/echo/config/echo_gateway.yml
複製到
檔案路徑 : .../cloudfoundry/.deployments/rest/config/echo_gateway.yml

---
cloud_controller_uri: api.vcap.me
service:
name: echo
version: "1.0"
description: 'Echo service'
plans: ['free']
default_plan: ['free']
tags: ['echo', 'echo-1.0', 'echobased', 'demo']
supported_versions: ['1.0']
version_aliases:
"current": "1.0"

host: localhost index: 0
token: changeechotoken
logging:
level: debug
mbus: nats://nats:nats@192.168.11.157:4222/
pid: /var/vcap/sys/run/echo_service.pid
node_timeout: 2
}

2.6 設定 Echo Node Config
原始路徑 : .../cloudfoundry/vcap/services/echo/config/echo_node.yml
複製到
檔案路徑 : .../cloudfoundry/.deployments/rest/config/echo_node.yml

---
plan: free
capacity: 100
local_db: sqlite3:/var/vcap/services/echo/echo_node.db
mbus: nats://nats:nats@192.168.11.157:4222/
base_dir: /var/vcap/services/echo/
index: 0
logging:
level: debug
pid: /var/vcap/sys/run/echo_node.pid
node_id: echo_node_0
host: 192.168.11.157
port: 5002

2.7 設定 Echo Node Config
原始路徑 : .../cloudfoundry/vcap/services/echo/config/echo_node.yml
複製到
檔案路徑 : .../cloudfoundry/.deployments/rest/config/echo_node.yml

---
plan: free
capacity: 100
local_db: sqlite3:/var/vcap/services/echo/echo_node.db
mbus: nats://nats:nats@192.168.11.157:4222/
base_dir: /var/vcap/services/echo/
index: 0
logging:
level: debug
pid: /var/vcap/sys/run/echo_node.pid
node_id: echo_node_0
host: 192.168.11.157
port: 5002

2.8 安裝 Echo Node
$ cd .../cloudfoundry/vcap/services/echo
$ source $HOME/.cloudfoundry_deployment_profile && bundle package

2.9 重新啟動
$ .../cloudfoundry/vcap/dev_setup/bin/vcap_dev restart


[3]Push Echo App


3.1 Download sample Code Path
執行檔 : .war file
原始檔 : source code


3.2 Push to Cloudfoundry
$ ...login to cloudfoundry (略)
$ mkdir echoapp ; cp testapp.war echoapp $ cd echoapp
$ vmc push

Would you like to deploy from the current directory? [Yn]: Y
Application Name: echoapp
Detected a Java Web Application, is this correct? [Yn]: Y
Application Deployed URL [echoapp.vcap.me]:
Memory reservation (128M, 256M, 512M, 1G, 2G) [512M]:
How many instances? [1]:
Create services to bind to 'echoapp'? [yN]: Y
1: echo
2: mongodb
3: mysql
4: redis
What kind of service?: 1
Specify the name of the service [echo-3a851]: myecho
Create another? [yN]:
Would you like to save this configuration? [yN]:
Creating Application: OK
Creating Service [myecho]: OK
Binding Service [myecho]: OK
Uploading Application:
Checking for available resources: OK
Processing resources: OK
Packing application: OK
Uploading (1K): OK
Push Status: OK


[4] Start Echo Server


4.1 Download sample Code Path
執行檔 : .war file
原始檔 : source code


4.2 Start Echo Server 以下的Port要對應Echo Node Config的Port
$ java -jar echo_service.jar -port 5002

[5] Test OK


[6] Note

如果發生下面的Error Message,有能是因為Echo Server沒有正常啟動
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 40
37: EchoClient client = new EchoClient(creds.getPort(),
38: creds.getHost());
39: try {
40: echoMsg = client.sendMessage(clientMsg);
41: } catch (IOException e) {
42: statusMsg = e.getMessage();
43: }
...(略)

可以透過修改SourceCode,不過以下使用較簡單方式來解決
檔案路徑 : /etc/hosts

127.0.1.1 ubuntu
改成 -->
192.168.11.157 ubuntu

沒有留言:

張貼留言