salt haproxy 부하 균형 배치 실현

8031 단어
설정 환경:
호스트 이름
ip
복무
server1
172.25.1.1
salt-manster
server2
172.25.1.2
salt-minion , haproxy
server3
172.25.1.3
salt-minion , apache
server4
172.25.1.4
salt-minion ,nginx
환경 에서 nginx 와 apache 배치 스 크 립 트 에 접근 합 니 다. 이 글 은 haproxy 기능 만 수행 합 니 다.https://blog.csdn.net/u010489158/article/details/81744757
haproxy 자동 배치 실현
(1) yum 소스 확장
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[salt]
name=salt
baseurl=http://172.25.1.250/rhel6
enabled=1
gpgcheck=0

[HighAvailability]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6.5/HighAvailability
enabled=1
gpgcheck=1

[LoadBalancer]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6.5/LoadBalancer
enabled=1
gpgcheck=1

[ResilientStorage]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6.5/ResilientStorage
enabled=1
gpgcheck=1

[ScalableFileSystem]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6.5/ScalableFileSystem
enabled=1
gpgcheck=1

(2) haproxy 스 크 립 트 저장 위치 만 들 기
[root@server1 ~]# cd /srv/salt/  
[root@server1 salt]# mkdir haproxy   #    haproxy       
[root@server1 salt]# cd haproxy/

(3) yum 원본 프로필 의 푸 시 스 크 립 트
[root@server1 haproxy]# vim yum.sls    #    yum    
yum-install:
  file.managed:                 #       
    - name: /etc/yum.repos.d/rhel-source.repo   # source      name   
    - source: salt://haproxy/file/rhel-source.repo
  cmd.run:    #  shell  
    - name: yum clean all

(4) haproxy 의 소스 코드 설치 스 크 립 트
[root@server1 haproxy]# vim make.sls    #haproxy     
include:
  - haproxy.yum        #  haproxy    yum.sls  
haproxy-install:       #  haproxy   
  pkg.installed:       #  yum      
    - pkgs:   #     
      - rpm-build
  file.managed:
    - name: /root/haproxy-1.6.11.tar.gz
    - source: salt://haproxy/file/haproxy-1.6.11.tar.gz
  cmd.run:      #  shell  
    - name: yum install rpm-build && cd /root && rpmbuild -tb haproxy-1.6.11.tar.gz && tar zxf haproxy-1.6.11.tar.gz && cd /rpmbuild/RPMS/x86_64/ && rpm -ivh haproxy-1.6.11-1.x86_64.rpm
    - create: /etc/haproxy     #             


(5) haproxy 사용자 의 스 크 립 트 만 들 기
[root@server1 users]# cd /srv/salt/users
[root@server1 users]# vim haproxy.sls  #  haproxy  
haproxy-group:   #      
  group.present:
    - name: haproxy
    - gid: 200

haproxy-user:  #     
  user.present:
    - name: haproxy
    - uid: 200
    - gid: 200
    - shell: /sbin/nologin
    - createhome: False
    - home: /usr/local/nginx


(6) haproxy 서비스의 시작 과 설정 파일 스 크 립 트
[root@server1 haproxy]# vim service.sls   #         
include:   #  haproxy  make.sls    users  haproxy.sls
  - haproxy.make
  - users.haproxy
/etc/haproxy/haproxy.cfg:     #      id, source        ID  
  file.managed:
    - source: salt://haproxy/file/haproxy.cfg
haproxy-service:   #  haproxy     
  service.running:
    - enable: True   #      
    - name: haproxy   #haproxy  
    - reload: True   #  watch           reload
    - watch:    #  file    
      - file: /etc/haproxy/haproxy.cfg
    - require:
      - user: haproxy


(7) file 디 렉 터 리 의 내용
[root@server1 file]# ls    #file      haproxy    ,haproxy     ,  yum      
haproxy-1.6.11.tar.gz  haproxy.cfg  rhel-source.repo

< 1 > haproxy. cfg 파일
[root@server1 file]# cat haproxy.cfg 
# This is a sample configuration. It illustrates how to separate static objects
# traffic from dynamic traffic, and how to dynamically regulate the server load.
#
# It listens on 192.168.1.10:80, and directs all requests for Host 'img' or
# URIs starting with /img or /css to a dedicated group of servers. URIs
# starting with /admin/stats deliver the stats page.
#
 
global
        maxconn         10000
        stats socket    /var/run/haproxy.stat mode 600 level admin
        log             127.0.0.1 local0
        uid             200
        gid             200
        chroot          /var/empty
        daemon
 
# The public 'www' address in the DMZ
frontend public
        bind            *:80 name clear
        #bind            192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
        mode            http
        log             global
        option          httplog
        option          dontlognull
        monitor-uri     /monitoruri
        maxconn         8000
        timeout client  30s
 
        stats uri       /admin/stats
        #use_backend     static if { hdr_beg(host) -i img }
        #use_backend     static if { path_beg /img /css   }
        default_backend static  #        static       
 
# The static backend backend for 'Host: img', /img and /css.
backend static
        mode            http
        balance         roundrobin
        option prefer-last-server
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  5s
        #option httpchk  HEAD /favicon.ico
        server          statsrv1 172.25.1.3:80 check inter 1000  #        
        server          statsrv2 172.25.1.4:80 check inter 1000
 
# the application servers go here
backend dynamic
        mode            http
        balance         roundrobin
        retries         2
        option redispatch
        timeout connect 5s
        timeout server  30s
        timeout queue   30s
        option httpchk  HEAD /login.php
        cookie          DYNSRV insert indirect nocache
        fullconn        4000 # the servers will be used at full load above this number of connections
        server          dynsrv1 192.168.1.1:80 minconn 50 maxconn 500 cookie s1 check inter 1000
        server          dynsrv2 192.168.1.2:80 minconn 50 maxconn 500 cookie s2 check inter 1000
        server          dynsrv3 192.168.1.3:80 minconn 50 maxconn 500 cookie s3 check inter 1000
        server          dynsrv4 192.168.1.4:80 minconn 50 maxconn 500 cookie s4 check inter 1000



< 2 > rhel - source. repo 파일
[root@server1 file]# cat rhel-source.repo 
[rhel-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6.5
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release


[salt-source]
name=Red Hat Enterprise Linux $releasever - $basearch - Source
baseurl=http://172.25.1.250/rhel6
enabled=1
gpgcheck=0

(8) 전역 푸 시 파일 을 작성 하고 모든 노드 를 원 클릭 으로 푸 시 합 니 다.
[root@server1 ~]# cd /srv/salt/
[root@server1 salt]# vim top.sls 
base:
  'server2':
    - haproxy.service
  'server3':
    - apache.install
  'server4':
    - nginx.service

[root@server1 salt]# salt '*' state.highstate   #    

(9) 테스트
< 1 > server 1 은 server 2 (haproxy) 를 방문 하여 httpd 와 nginx 의 폴 링 을 실현 합 니 다.
[root@server1 ~]# curl 172.25.1.2
this is apache
[root@server1 ~]# curl 172.25.1.2
this is nginx!!!!
[root@server1 ~]# curl 172.25.1.2
this is apache
[root@server1 ~]# curl 172.25.1.2
this is nginx!!!!

< 2 > 각 노드 의 서비스 자동 오픈
[root@server2 ~]# netstat  -nutlp |grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7535/haproxy        
[root@server3 ~]# netstat  -nutlp |grep 80
tcp        0      0 :::80                       :::*                        LISTEN      2650/httpd  
[root@server4 mnt]# netstat  -nutlp |grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      10438/nginx    

좋은 웹페이지 즐겨찾기