在兩台虛擬機上各裝一個glassfish與測試application

事先放一個測試servlet /test/hello在n1, n2兩台機器上
裝glassfish要先裝java jdk並設環境變數

需要程式:

httpd, mod_proxy, mod_ssl, mod_proxy_balancer

  1. 安裝httpd

    1
    2
    3
    4
    yum install httpd
    或http://www.rpmfind.net/找rpm
    安裝完後會在下列路徑找到/etc/httpd
    log在/var/log/httpd/error_log
  2. 到/etc/httpd/modules找找有無mod_proxy, mod_ssl
    沒有的話去http://www.rpmfind.net/找rpm安裝
    注意裝的版本要跟httpd完全一致
    CentOS中通常只會缺mod_ssl

  3. 停止 SELinux
    編輯 /etc/sysconfig/selinux 這個檔案
    將 SELINUX=enforcing 改成 SELINUX=disabled
    執行setenforce 0

  4. 設定mod_proxy
    /etc/httpd/conf.d/中加入mod_proxy.conf
    其中n1是machine1的domain, n2是machine2的domain

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    Listen 9090
    Listen 9191
    NameVirtualHost *:9090
    NameVirtualHost *:9191
    <VirtualHost *:9090>
    ProxyRequests off
    ServerName n1
    <Proxy balancer://mycluster>
    # WebHead1
    BalancerMember http://n1:8080
    # WebHead2
    BalancerMember http://n2:8080
    # Security "technically we aren't blocking
    # anyone but this the place to make those
    # chages
    Order Deny,Allow
    Deny from none
    Allow from all
    # Load Balancer Settings
    # We will be configuring a simple Round
    # Robin style load balancer. This means
    # that all webheads take an equal share of
    # of the load.
    ProxySet lbmethod=byrequests
    </Proxy>
    # balancer-manager
    # This tool is built into the mod_proxy_balancer
    # module and will allow you to do some simple
    # modifications to the balanced group via a gui
    # web interface.
    <Location /balancer-manager>
    SetHandler balancer-manager
    # I recommend locking this one down to your
    # your office
    Order deny,allow
    Allow from all
    </Location>
    # Point of Balance
    # This setting will allow to explicitly name the
    # the location in the site that we want to be
    # balanced, in this example we will balance "/"
    # or everything in the site.
    ProxyPass /balancer-manager !
    ProxyPass / balancer://mycluster/
    </VirtualHost>
    <VirtualHost *:9191>
    ProxyRequests off
    SSLEngine on
    ServerName n1
    SSLCertificateFile /etc/pki/tls/certs/localhost.crt
    SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
    <Proxy balancer://mycluster>
    # WebHead1
    BalancerMember http://n1:8080
    # WebHead2
    BalancerMember http://n2:8080
    # Security "technically we aren't blocking
    # anyone but this the place to make those
    # chages
    Order Deny,Allow
    Deny from none
    Allow from all
    # Load Balancer Settings
    # We will be configuring a simple Round
    # Robin style load balancer. This means
    # that all webheads take an equal share of
    # of the load.
    ProxySet lbmethod=byrequests
    </Proxy>
    # balancer-manager
    # This tool is built into the mod_proxy_balancer
    # module and will allow you to do some simple
    # modifications to the balanced group via a gui
    # web interface.
    <Location /balancer-manager>
    SetHandler balancer-manager
    # I recommend locking this one down to your
    # your office
    Order deny,allow
    Allow from all
    </Location>
    # Point of Balance
    # This setting will allow to explicitly name the
    # the location in the site that we want to be
    # balanced, in this example we will balance "/"
    # or everything in the site.
    ProxyPass /balancer-manager !
    ProxyPass / balancer://mycluster/
    </VirtualHost>
  5. 關閉iptables並啟動httpd
    service stop iptalbes
    service start httpd

  6. 將httpd設定為開機即啟動,iptables設定成不要開機啟動
    chkconfig –levels 235 httpd on
    chkconfig iptables off

  7. http://localhost:9090/balancer-manager
    apache proxy_balancer簡單管理頁面

  8. http://n1:9090/test/hello並觀察balancer-manager頁面

  9. 可以將/etc/httpd/conf/httpd.conf的KeepAlive改為On