[Python] 建立連續性 vs和pool,node
#Step 01 – 使用python 建立大量 virtual
#!/usr/bin/env python3
############################################################
#!/usr/bin/python
#
# python create_pool.py 10.1.1.4 test_pool 10.1.10.10,10.1.10.11 80
# python create_node_pool1.py 10.1.1.4 test_pool_333 192.168.33.11,192.168.33.12,192.168.33.13,192.168.33.14,192.168.33.15 80
#
# 建立 performance-l4 Virtual server
# python create_node_pool_vs1_L4.py 10.1.1.4 10.100.10.10 192.168.33.11,192.168.33.12,192.168.33.13,192.168.33.14,192.168.33.15 80,443,21,9988,8080 -T Tick_numbers
#
# python create_node_pool_vs1_L4.py -T Tick_numbers
#
from f5.bigip import ManagementRoot
import pprint
import argparse
import time
pp = pprint.PrettyPrinter(indent=3)
parser = argparse.ArgumentParser(description='Script to create a pool on a BIG-IP device')
parser.add_argument("host", help="The IP/Hostname of the BIG-IP device")
parser.add_argument("VIP_name", help="The name of the VIP")
parser.add_argument("node_adds", help="A comma seperated string in the format :[,:]")
parser.add_argument("service", help="Pool port service :[,:]")
parser.add_argument("-T", "--order_tick_num", help="order_tick_nmuber", default="TNS-2020-0000")
parser.add_argument("-P", "--partition", help="The partition name", default="Common")
parser.add_argument("-u", "--username", help="The BIG-IP username", default="admin")
parser.add_argument("-p", "--password", help="The BIG-IP password", default="admin")
args = parser.parse_args()
mgmt = ManagementRoot(args.host, args.username, args.password)
print ("Start : %s" % time.ctime())
#
VIP_vs_n = "vs_%s" % args.VIP_name
################################################################
# 建立pool name
################################################################
service_list = args.service.split(',')
#
for serviceport in service_list:
Pool_vs_n = "pool_%s_%s" % (serviceport, args.VIP_name)
#
pool_path = "/%s/%s" % (args.partition, Pool_vs_n)
#
pool = mgmt.tm.ltm.pools.pool.create(partition=args.partition, name=Pool_vs_n)
#
print ("Created pool %s" % pool_path)
#
#print ("Created pool LB")
node_list = args.node_adds.split(',')
#
print ("\nCreated node For %s Pool" % pool_path)
#
for node in node_list:
nodes = mgmt.tm.ltm.nodes.node.create(name="node_%s" % node, address=node, partition='Common')
print (" Create node %s" % node)
node_a = mgmt.tm.ltm.nodes.node.load(partition=args.partition, name="node_%s" % node )
node_a.description = "%s" % (args.order_tick_num)
node_a.update()
print ("\n更新 : %s" % (args.order_tick_num))
#
################################################################
# 設定 pool 加入 poolmember
#################################################################
for serviceport2 in service_list:
print ("\nCreated pool member /Common/pool_%s_%s" % (serviceport2, args.VIP_name))
for member in node_list:
poola = mgmt.tm.ltm.pools.pool.load(name="pool_%s_%s" % (serviceport2, args.VIP_name))
pool_member = poola.members_s.members.create(partition=args.partition, name="node_%s:%s" % (member, serviceport2))
print(" Added member %s:%s" % (member, serviceport2))
#
#
#
################################################################
# 更新 pool 設定
#################################################################
#time.sleep( 5 )
for serviceport3 in service_list:
print ("\n/Common/pool_%s_%s" % (serviceport3, args.VIP_name))
pool_a = mgmt.tm.ltm.pools.pool.load(partition=args.partition, name="pool_%s_%s" % (serviceport3, args.VIP_name))
pool_a.loadBalancingMode = "least-connections-member"
pool_a.monitor = "http"
pool_a.minUpMembers = 1
pool_a.description = "%s" % (args.order_tick_num)
pool_a.update()
print ("\n設定 負載平衡方式 : least-connections-members")
#
print ("End 1: %s" % time.ctime())
################################################################
# 建立 Virtual Server
#################################################################
for serviceport4 in service_list:
pool_vs = "pool_%s_%s" % (serviceport4, args.VIP_name)
params = {'name': "vs_%s_%s" % (args.VIP_name, serviceport4),
'partition': 'Common',
'destination': "%s:%s" % (args.VIP_name, serviceport4),
'mask': '255.255.255.255',
'description': args.order_tick_num,
'source': '0.0.0.0/0',
'pool': pool_vs,
'profiles': [
{'name': 'fastL4'}
],
'ipProtocol': 'tcp',
'vlansEnabled': True,
'vlans': ['/Common/MGMT']
}
try:
vs = mgmt.tm.ltm.virtuals.virtual.create(**params)
print(vs.raw)
except Exception as e:
print(params['name'] + ' failed. ' + str(e))
#d
print ("End 2: %s" % time.ctime())
#################################################################################
# End
#################################################################################
>>> from f5.bigip import ManagementRoot
>>> mgmt = ManagementRoot('192.168.1.1', 'myuser', 'mypass')
>>> n = mgmt.tm.ltm.nodes.node.load(partition='Common', name='192.168.15.15')
>>> print n.raw
node_a = mgmt.tm.ltm.nodes.node.load(partition=args.partition, name="node_%s" % node )
#Step 02 –
#!/usr/bin/python
#
# python create_pool.py 10.1.1.4 test_pool 10.1.10.10,10.1.10.11 80
# python create_node_pool1.py 10.1.1.4 test_pool_333 192.168.33.11,192.168.33.12,192.168.33.13,192.168.33.14,192.168.33.15 80
#
# 建立 performance-l4 Virtual server
# python create_node_pool_vs1_L4.py 10.1.1.4 10.100.10.10 192.168.33.11,192.168.33.12,192.168.33.13,192.168.33.14,192.168.33.15 80
#
#
from f5.bigip import ManagementRoot
import pprint
import argparse
import time
pp = pprint.PrettyPrinter(indent=3)
parser = argparse.ArgumentParser(description='Script to create a pool on a BIG-IP device')
parser.add_argument("host", help="The IP/Hostname of the BIG-IP device")
parser.add_argument("VIP_name", help="The name of the VIP")
parser.add_argument("node_adds", help="A comma seperated string in the format :[,:]")
parser.add_argument("service", help="Pool port service :[,:]")
parser.add_argument("-T", "--order_tick_num", help="order_tick_nmuber", default="TNS-2020-0000")
parser.add_argument("-P", "--partition", help="The partition name", default="Common")
parser.add_argument("-u", "--username", help="The BIG-IP username", default="admin")
parser.add_argument("-p", "--password", help="The BIG-IP password", default="admin")
args = parser.parse_args()
mgmt = ManagementRoot(args.host, args.username, args.password)
print ("Start : %s" % time.ctime())
VIP_vs_n = "vs_%s" % args.VIP_name
#
# 建立pool name
#
service_list = args.service.split(',')
#
for serviceport in service_list:
Pool_vs_n = "pool_%s_%s" % (serviceport, args.VIP_name)
#
#pool_path = "/%s/%s" % (args.partition, args.pool_name)
pool_path = "/%s/%s" % (args.partition, Pool_vs_n)
#pool = mgmt.tm.ltm.pools.pool.create(partition=args.partition, name=args.pool_name)
pool = mgmt.tm.ltm.pools.pool.create(partition=args.partition, name=Pool_vs_n)
#
print ("Created pool %s" % pool_path)
#
#print ("Created pool LB")
node_list = args.node_adds.split(',')
#
print ("\nCreated node For %s Pool" % pool_path)
#
for node in node_list:
nodes = mgmt.tm.ltm.nodes.node.create(name="node_%s" % node, address=node, partition='Common')
print (" Create node %s" % node)
node_a = mgmt.tm.ltm.nodes.node.load(partition=args.partition, name="node_%s" % node )
node_a.description = "%s" % (args.order_tick_num)
node_a.update()
print ("\n更新 : %s" % (args.order_tick_num))
#
#print ("\nCreated pool member %s" % pool_path)
for serviceport2 in service_list:
print ("\nCreated pool member /Common/pool_%s_%s" % (serviceport2, args.VIP_name))
for member in node_list:
poola = mgmt.tm.ltm.pools.pool.load(name="pool_%s_%s" % (serviceport2, args.VIP_name))
pool_member = poola.members_s.members.create(partition=args.partition, name="node_%s:%s" % (member, serviceport2))
#pool_member = pool.members_s.members.create(partition=args.partition, name="node_%s:%s" % (member, serviceport2))
print(" Added member %s:%s" % (member, serviceport2))
#
#
#
#time.sleep( 5 )
for serviceport3 in service_list:
print ("\n/Common/pool_%s_%s" % (serviceport3, args.VIP_name))
pool_a = mgmt.tm.ltm.pools.pool.load(partition=args.partition, name="pool_%s_%s" % (serviceport3, args.VIP_name))
pool_a.loadBalancingMode = "least-connections-member"
pool_a.monitor = "http"
pool_a.minUpMembers = 1
pool_a.description = "%s" % (args.order_tick_num)
pool_a.update()
print ("\n設定 負載平衡方式 : least-connections-members")
#time.sleep( 5 )
print ("End 1: %s" % time.ctime())
################################################################
##
#################################################################
for serviceport4 in service_list:
pool_vs = "pool_%s_%s" % (serviceport4, args.VIP_name)
params = {'name': "vs_%s_%s" % (args.VIP_name, serviceport4),
'partition': 'Common',
'destination': "%s:%s" % (args.VIP_name, serviceport4),
'mask': '255.255.255.255',
'description': args.order_tick_num,
'source': '0.0.0.0/0',
'pool': pool_vs,
'profiles': [
{'name': 'fastL4'}
],
'ipProtocol': 'tcp',
'vlansEnabled': True,
'vlans': ['/Common/MGMT']
}
try:
vs = mgmt.tm.ltm.virtuals.virtual.create(**params)
vipa = mgmt.tm.ltm.virtuals.virtual.load(partition='Common', name="vs_%s_%s" % (args.VIP_name, serviceport4))
print("Create virtualserver %s" % vipa.name)
#print(vs.raw)
except Exception as e:
print(params['name'] + ' failed. ' + str(e))
#d
print ("End 2: %s" % time.ctime())
#################################################################################
# end
#################################################################################
#########################
輸出結果:
########################
Start : Thu Sep 10 20:32:34 2020
Created pool /Common/pool_80_10.100.10.10
Created pool /Common/pool_443_10.100.10.10
Created pool /Common/pool_21_10.100.10.10
Created node For /Common/pool_21_10.100.10.10 Pool
Create node 192.168.33.11
更新 : TNS-2020-3333
Create node 192.168.33.12
更新 : TNS-2020-3333
Create node 192.168.33.13
更新 : TNS-2020-3333
Create node 192.168.33.14
更新 : TNS-2020-3333
Create node 192.168.33.15
更新 : TNS-2020-3333
Created pool member /Common/pool_80_10.100.10.10
Added member 192.168.33.11:80
Added member 192.168.33.12:80
Added member 192.168.33.13:80
Added member 192.168.33.14:80
Added member 192.168.33.15:80
Created pool member /Common/pool_443_10.100.10.10
Added member 192.168.33.11:443
Added member 192.168.33.12:443
Added member 192.168.33.13:443
Added member 192.168.33.14:443
Added member 192.168.33.15:443
Created pool member /Common/pool_21_10.100.10.10
Added member 192.168.33.11:21
Added member 192.168.33.12:21
Added member 192.168.33.13:21
Added member 192.168.33.14:21
Added member 192.168.33.15:21
/Common/pool_80_10.100.10.10
設定 負載平衡方式 : least-connections-members
/Common/pool_443_10.100.10.10
設定 負載平衡方式 : least-connections-members
/Common/pool_21_10.100.10.10
設定 負載平衡方式 : least-connections-members
End 1: Thu Sep 10 20:32:35 2020
Create virtualserver vs_10.100.10.10_80
Create virtualserver vs_10.100.10.10_443
Create virtualserver vs_10.100.10.10_21
End 2: Thu Sep 10 20:32:35 2020
#Step 03 –DNS分流使用
將問到的A紀錄存到記憶體中 irule 分流 利用domain 方式
##irule_DNS_response_For_split
when RULE_INIT {
set static::maxquery 3000
set static::maxSecond 3600
}
when DNS_RESPONSE {
if {([DNS::question type] eq "A") or ([DNS::question type] eq "AAAA") } {
if {[class match [DNS::question name] contains class_foreign_outsite] } {
set rrs [DNS::answer]
foreach rr $rrs {
log local0. "[DNS::rdata $rr]"
table set -subtable "nonCNIP" [DNS::rdata $rr] [DNS::question name] $static::maxSecond $static::maxSecond
}
}
}
}
#
# 將分流IP設定到 table 裡面
when CLIENT_ACCEPTED {
set default_pool [LB::server pool]
if { [table lookup -subtable "nonCNIP" [IP::local_addr]] != "" }{
pool GW_Hinet_Internet
snat automap
#log local0. "HTTP is allow_IP_Hinet"
} else {
pool $default_pool
#log local0. "HTTP is allow_IP_OA-Client Source IP: [IP::client_addr]:[TCP::client_port]"
}
}
# 將分流IP設定到 table 裡面
when CLIENT_ACCEPTED {
set default_pool [LB::server pool]
if { [table lookup -subtable "nonKUIP" [IP::local_addr]] != "" }{
pool GW_HK_Internet
snat automap
#log local0. "HTTP is allow_IP_HK"
} else {
pool $default_pool
#log local0. "HTTP is allow_IP_OA-Client Source IP: [IP::client_addr]:[TCP::client_port]"
}
}
#Step 04 –
設定 ansible 注意事項
設定變數
##############################################################
ansible 2.9.13
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.7/dist-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0]
####################################################################
[all:vars]
ansible_python_interpreter='/usr/bin/env python3'
[webservers]
node_192.168.1.11 ansible_host=192.168.1.11
node_192.168.1.12 ansible_host=192.168.1.12
node_192.168.1.13 ansible_host=192.168.1.13
node_192.168.1.14 ansible_host=192.168.1.14
node_192.168.1.15 ansible_host=192.168.1.15
node_192.168.1.16 ansible_host=192.168.1.16
---
#
# 建立 node 方式
# node 資訊在 hosts 檔案內
# 指令方式 ansible-playbook -i hosts bigip-node.yml
#
- name: BIG-IP SETUP CREATE NODES
hosts: localhost
connection: local
gather_facts: false
vars_prompt:
- name: "private_ip"
prompt: "Enter BIG-IP ip address "
private: no
- name: "ansible_user"
prompt: "Enter BIG-IP user "
private: no
- name: "ansible_ssh_pass"
prompt: "Enter BIG-IP password "
private: yes
tasks:
- set_fact:
provider:
server: "{{private_ip}}"
user: "{{ansible_user}}"
password: "{{ansible_ssh_pass}}"
server_port: 443
validate_certs: no
- name: CREATE NODES
bigip_node:
provider: "{{provider}}"
host: "{{hostvars[item].ansible_host}}"
name: "{{hostvars[item].inventory_hostname}}"
loop: "{{ groups['webservers'] }}"
#Step 05 –
#!/usr/bin/python
import csv
import sys
import time
#input number you want to search
number = input('Enter number to find\n')
#read csv, and split on "," the line
#csv_file = csv.reader(open('hs300_399300.csv', "r"))
#csv_file = csv.reader(open('hs300_399300.csv', "r"))
with open('f5list.csv', newline='') as csvFile:
rows = csv.DictReader(csvFile)
count = 0
#loop through the csv list
for row in rows:
#if current rows 2nd value is equal to input, print that row
if number == row['function_name']:
count += 1
#print (row)
print ("Run_Step %s : %s" % (count, time.ctime()))
print("F5_Host_To : %s \n\tShow_pool : %s \n\t\tmumber : %s\n" % (row['F5_Host'], row['pool_name'], row['pool_mumbers']))
time.sleep( 3 )
#Step 06 –
from f5.bigip import ManagementRoot
import logging, ipaddress
# enable logging with timestamp
logging.basicConfig(filename="bigip_script.log",
format="%(asctime)s %(levelname)s:%(message)s",
level=logging.INFO)
try:
# Need to re-write this to store as an encrypted credential file
mgmt = ManagementRoot("192.168.1.11","admin","121278")
except Exception as e:
logging.error("Error in connecting to bigip.",e)
print(e)
exit(1)
ltm = mgmt.tm.ltm # LTM
vs = ltm.virtuals.virtual # Virtual Server
def choosePool():
index = 1 # index starts from 1.
dictPool = {} # dictionary to store the pool.name
for pool in ltm.pools.get_collection():
print(str(index) + ". " + pool.name)
dictPool[index] = pool.name
index += 1 # this index maps to the pool.name stored.
choice = input("Which pool do you want to update? ")
return dictPool[int(choice)] # user enters a digit, need to type cast choice to integer.
def vsMenu():
# Simple menu to take in user input for creating vs.
name = input("Enter virtual server name ")
destination = input("Enter ip address of virtual server ")
try:
ipaddress.ip_address(destination) # check for valid ipv4 address.
except Exception as e:
logging.error(e)
print("You have entered an invalid ipv4 address ", e)
exit(1)
port = input("Enter the service port number of {} ".format(name))
if not port:# if user did not enter a value
port = '0' # default port is any
ipProtocol = input("Enter protocol of {} ".format(name))
if not ipProtocol: # if user did not enter a value
ipProtocol = 'tcp' # default is tcp
source = input("Enter the source address (your expected visitor, if none just press enter) ")
if not source: # if user did not enter a value
source = '0.0.0.0/0' # default is any address
pool = choosePool()
print("Which persistence do you prefer?\n")
print("[1] source address\n")
print("[2] destination address\n")
persistChoice = input("Your choice (press enter to skip): ")
if persistChoice == '1':
persist = 'source_addr' # session persistence based on source address
elif persistChoice == '2':
persist = 'dest_addr' # session persistence based on destination address
else:
persist = "" # default value is none.
destination = destination + ":" + port # persist parameter accepts ip_address:port eg. 192.168.1.1:80 only.
#print(name,destination,source,ipProtocol,pool,persist)
createVS(name,destination,source,ipProtocol,pool,persist)
def createVS(name,destination,source,ipProtocol,pool,persist):
if vs.exists(partition="Common", name=name):
print("{} exists in bigip!".format(name))
try:
logging.info("Creating the Virtual Server {}".format(name))
# Calling the iControl API using http-post
vs.create(partition="Common",
name=name,
destination=destination,
source=source,
ipProtocol=ipProtocol,
pool=pool,
persist = persist)
except Exception as e:
logging.error(e)
exit(1)
def addMember():
poolobj = ltm.pools.pool.load(partition="Common", name=choosePool())
createMembers(poolobj)
def createMembers(poolObj):
members = []
while True:
nodeMember = input("Enter member ip address: ")
if not nodeMember: # Quit asking if user does not put in value
break
try:
ipaddress.ip_address(nodeMember) # validate if user has entered a valid ip address
logging.debug("Collecting {}...".format(nodeMember))
except Exception as e:
logging.error("{} is an invalid ipv4 address: ".format(nodeMember),e)
print("You have entered an invalid ip address: ",e)
exit(1)
nodeMemberPort = input("Service port of this member") # Collect server port
if not nodeMemberPort: # Quit asking when no port is provided.
break
# make sure the port number is between 0 and 65535
elif int(nodeMemberPort) >= 0 and int(nodeMemberPort) <= 65535:
# collect the ip_address:port eg. 192.168.1.1:80 to members array.
members.append(nodeMember + ":" + nodeMemberPort)
logging.info("Collecting {}:{}".format(nodeMember,nodeMemberPort))
else:
logging.info("Unknown service port, assume quitting sub-menu...")
break
for member in members:
poolObj.members_s.members.create(partition="Common", name=member)
def createPool():
poolName = input("Enter pool name eg. pool-Gwen: ")
# check if pool exists in bigip
while ltm.pools.pool.exists(partition="Common",name=poolName):
print("This pool {} is already exist! Choose another name".format(poolName))
poolName = input("Enter pool name eg. pool-Gwen: ")
if poolName: # Ask further for inputs if user has enter Pool Name
print("Select load balancing method:\n")
print("[1] Round Robin (default)\n")
print("[2] Least Connections (Member)\n")
print("[3] Least Connections (Node)\n")
print("[4] Least sessions\n")
choice = input("Enter a load balancing method: ")
if not choice or choice is '1':
lbMode = "round-robin" # default load balancing method, if user did not enter any value.
elif choice is '2':
lbMode = "least-connections-member"
elif choice is '3':
lbMode = "least-connections-node"
else:
lbMode = "least-sessions"
try:
poolObj = ltm.pools.pool.create(partition="Common", name=poolName, loadBalancingMode=lbMode)
logging.info("Creating pool {} with load balancing method as {}".format(poolName,lbMode))
except Exception as e:
logging.error("Error in creating pool:",e)
print(e)
exit(1)
choice = input("Proceed to create members? ")
if choice.lower() == 'y':
logging.info("User has selected to create pool members.")
createMembers(poolObj)
elif choice.lower() == 'n':
logging.info("User does not want to create pool members.")
else:
print("You have entered an invalid choice, only y or n.\n")
# Ugly menu in CLI
def cliMenu():
choice = 0
while choice is not '9':
print("Menu\n")
print("====\n")
print("[1]Create Pool\n")
print("[2]Add members to existing Pool\n")
print("[3]Create Virtual Server\n")
print("[9]Quit\n")
choice = input("Enter a choice: ")
if choice == '1':
createPool()
elif choice == '2':
addMember()
elif choice == '3':
vsMenu()
elif choice == '9':
print("Bye.")
else:
print("You have entered an invalid choice.")
logging.error("User has entered an invalid choice in main menu.")
if __name__ == "__main__":
cliMenu()
#Step 07 –
#Step 08 –
#Step 09 –
#Step 10 –
as
沒有留言:
張貼留言