修改haproxy配置文件
生活随笔
收集整理的這篇文章主要介紹了
修改haproxy配置文件
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
HAproxy配置文件操作:
1. 根據(jù)用戶輸入輸出對(duì)應(yīng)的backend下的server信息
2. 可添加backend 和sever信息
3. 可修改backend 和sever信息
4. 可刪除backend 和sever信息
5. 操作配置文件前進(jìn)行備份
6 添加server信息時(shí),如果ip已經(jīng)存在則修改;如果backend不存在則創(chuàng)建;若信息與已有信息重復(fù)則不操作
配置文件 參考 http://www.cnblogs.com/alex3714/articles/5717620.html
配置文件如下:
globallog 127.0.0.1 local2
daemon
maxconn 256
log 127.0.0.1 local2 info
defaults
log global
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option dontlognull
listen stats :8888
stats enable
stats uri /admin
stats auth admin:1234
frontend oldboy.org
bind 0.0.0.0:80
option httplog
option httpclose
option forwardfor
log global
acl www hdr_reg(host) -i www.oldboy.org
use_backend www.oldboy.org if www
backend www.oldboy.org
server 100.1.7.9 weight 20 maxconn 3000
代碼如下: f = open("haproxy","r",encoding="gbk") f_new = open("haproxy_new","w",encoding="gbk") #將文件內(nèi)容轉(zhuǎn)換為一個(gè)列表,并定義一個(gè)變量。 b = f.readlines() #定義三個(gè)選項(xiàng): option = ("1、請(qǐng)輸入搜索的地址:","2、請(qǐng)輸入增加的內(nèi)容:","3、請(qǐng)輸入刪除的內(nèi)容:") #打印三個(gè)選項(xiàng): for i in option:print(i) choice = input("請(qǐng)選擇操作方式:序列號(hào)>>>") #如果選擇1,則進(jìn)入查找選項(xiàng)。 if choice == "1" :data = input("請(qǐng)輸入搜索地址:")#利用字符串拼接,定義一個(gè)變量,-----backend所在的行。address = "backend %s\n"%dataif address in b:#利用列表的下標(biāo)來定位位置,然后通過下標(biāo)打印內(nèi)容。index_add = b.index(address)print(b[index_add],b[index_add+1])if address not in b:print("您查找的內(nèi)容不存在") #如果選擇2,則進(jìn)入增加內(nèi)容選項(xiàng): if choice =="2":data = input("輸入增加內(nèi)容:")data_eval = eval(data)#通過列表計(jì)數(shù)器來判斷輸入的內(nèi)容是否在列表中存在,如果計(jì)數(shù)器為0則不存在,如果計(jì)數(shù)器不為0則存在。#不存在則添加,存在則不添加。f_find = b.count("backend %s\n"%data_eval["backend"])#如果backend后的內(nèi)容在文件中不能找到則新增。if f_find == 0:for line in b:f_new.write(line)# for f_line2 in f:# #將輸入的內(nèi)容通過字符串拼接的形式寫入文件中f_new.write("\nbackend ")f_new.write(data_eval["backend"])f_new.write("\n server %s weight %s maxconn %s"\%(data_eval["record"]["server"],data_eval["record"]["weight"]\,data_eval["record"]["maxconn"]))#如果backend后的內(nèi)容在文件中能找到則不執(zhí)行任何操作。if f_find != 0:print("您添加的內(nèi)容已經(jīng)存在") #如果選擇3,則進(jìn)入刪除內(nèi)容選項(xiàng): if choice == "3":data = input("輸入刪除內(nèi)容:")data_eval = eval(data)#通過列表計(jì)數(shù)器來判斷輸入的內(nèi)容是否在列表中存在,如果計(jì)數(shù)器為0則不存在,如果計(jì)數(shù)器不為0則存在。#不存在則添加,存在則不添加。f_find = b.count("backend %s\n"%data_eval["backend"])#如果backend后的內(nèi)容在文件中不能找到則新增。if f_find != 0:b_index=b.index("backend www.oldboy.org\n")b.pop(b_index)b.pop(b_index)for line in b:f_new.write(line)#如果backend后的內(nèi)容在文件中不能找到則不執(zhí)行任何操作。if f_find == 0:print("您輸入的內(nèi)容文件中不存在") f.close() f_new.close()
?
轉(zhuǎn)載于:https://www.cnblogs.com/1dreams/p/6880205.html
總結(jié)
以上是生活随笔為你收集整理的修改haproxy配置文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么不再使用三棱军刺?
- 下一篇: centos7 安装配置openstac