准备

OVF模板准备

  • 安装 centos 6.8
  • 初始化
  • 部署vmware tools
  • ovf 文件制作
#!/bin/bash

mycpu=$1
mymem=$2
mydisk=$3

if $1 || $2 || $3;
then
    echo "需要输入三个参数!"
    echo "例如如果想新建一个1核心1GB20GB的ovf,输入 ./xx 1 1 20"
    exit
fi

echo $mycpu $mymem $mydisk

ovfFileName="$mycpu.$mymem.$mydisk.ovf"
mfFileName="$mycpu.$mymem.$mydisk.mf"

cp -f $mydisk.ovf $ovfFileName
sed -i "s:mycpu:$mycpu:g" $ovfFileName
sed -i "s:mymem:$mymem:g" $ovfFileName

openssl dgst -sha1 $ovfFileName > $mfFileName
openssl dgst -sha1 $mydisk.vmdk >> $mfFileName

部署

一台虚拟机作为中转

  • 资料打过去
  • dnsmasq服务器

看看哪个机器上面跑哪些虚机

#!/usr/bin/env python
#coding:utf-8

import json



mental_server_type = {
    'cpu': 42,
    'mem': 200
}

# target,初始化一百台服务器
machines = []
for i in range(50):
    a = {}
    a['cpuused'] = 0
    a['memused'] = 0
    a['yw'] = []
    a['yw2'] = []
    machines.append(a)

# IP地址编号


def main():
    with open("./info2.txt") as f:
        ip_bianhao = 10
        for line in f:
            #print line
            info = line.replace("\r", "").replace("\n", "").split(",")
            yw_name = info[0]
            yw_instance_type = info[2]
            yw_instance_count = int(info[1])
            instance_used_cpu = int(yw_instance_type.split(".")[0])
            instance_used_mem = int(yw_instance_type.split(".")[1])
            instance_used_disk = int(yw_instance_type.split(".")[2])
            for number in range(0,yw_instance_count):
                guest_name = yw_name + "+" + str(instance_used_cpu) +"." + str(instance_used_mem)+"."+ str(instance_used_disk)
                for machine in machines:
                    if machine['cpuused'] + instance_used_cpu < mental_server_type['cpu'] and machine['memused'] + instance_used_mem < mental_server_type['mem'] and guest_name not in machine['yw']:
                        machine['yw'].append(guest_name )
                        machine['yw2'].append(guest_name + "+" +str(ip_bianhao) )
                        ip_bianhao = ip_bianhao + 1
                        machine['cpuused'] += instance_used_cpu
                        machine['memused'] += instance_used_mem
                        break


main()

# 直接打印
print "*"*60
for machine in machines:
    if machine['memused'] !=0:
        print machine

# 详细打印
print "*"*60
cpu_used_total = 0
mem_used_total = 0
instance_total = 0
for machine in machines:
    if machine['memused'] !=0:
        cpu_used_total += machine['cpuused']
        mem_used_total += machine['memused']
        instance_total += 1
print """一共消耗cpu: %s, mem: %s, instnaces: %s"""%(cpu_used_total,mem_used_total,instance_total)

# 直接打印
print "*"*60
i = 1
for machine in machines:
    if machine['memused'] !=0:
        print "-"*30 + "打印第%s服务器的虚拟机列表"%i + "-"*30
        for item in machine['yw2']:
            #print item
            item2 = item.split("+")
            result = "%s-IP%s,%s"%(item2[0],item2[2],item2[1])
            print result
        i += 1

在中转机器上执行OVF部署操作

  • 批量部署脚本
  • 每台机器上面执行
#!/bin/sh

cat create.txt | while read line
do
   echo $line
   vmname=`echo $line | awk -F, '{print $1}'`
   vmsize=`echo $line | awk -F, '{print $2}'`
   echo /vmfs/volumes/datastore1/vmware-ovftool/ovftool -dm=thin -ds=datastore1 '--net:bridged=VM Network' "--name=$vmname" http://192.168.0.88:1280/ovf/${vmsize}.ovf 'vi://root:fOgLnr1D@192.168.0.216'
done

部署完成后, 获取mac地址ip地址列表

#!/bin/sh
echo '' > /tmp/dhcp.txt
vim-cmd vmsvc/getallvms | grep vmx > /tmp/vmx.txt
cat /tmp/vmx.txt | while read line
do
   vmid=`echo $line | awk '{print $1}'`
   vmname=`echo $line | awk '{print $2}'`
   vmip=`echo $vmname | awk -F\- '{print $NF}'`
   vmip=`echo $vmip | awk -FP '{print "192.168.0."$NF}'`
   vmmac=`vim-cmd vmsvc/device.getdevices $vmid | grep macAddress | awk -F\" '{print $2}'`
   echo "$vmid $vmname $vmip $vmmac"
   echo "dhcp-host=$vmmac,$vmip" >> /tmp/dhcp.txt
done

cat /tmp/dhcp.txt

打扫战场

TO BE CONTINUE

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐