Oracle雲遠程大量建立虛擬機器

來源:互聯網
上載者:User

標籤:####   base   man   stand   tps   oop   bae   ima   dir   

前言

由於公司業務需求,我們需要大量建立虛擬機器執行任務,任務運行完後需要銷毀,以節省資源。
我們大量建立虛擬方式是通過Oracle雲提供的bash介面,所以我們第一步就是要建立bash環境

一. 在Linux機器上配置Command Line Interface。1. Python版本號碼大於等於2.7

[[email protected] ~]$ python --version

2. 安裝的使用者要有sudo許可權

[[email protected] ~]# visudo

符合條件

2. 下載並安裝CLI

[[email protected] ~]$ curl -L "https://raw.githubusercontent.com/oracle/oci-cli/master/scripts/install/install.sh" | bash

查看

3. 自動組建組態CLI配製檔案

此設定檔用於認證

擷取使用者OCID:

擷取租戶OCID

4. 更新使用者密鑰

查看密鑰放置的位置:

查看金鑰產製原料

添加密鑰

4. 測試是否配置成功

二. 批量部署節點機指令碼
#!/bin/bash# Description: batch create Oracle Cloud Virtual Instance.# Date: 2017/11/22# Author: lirou<[email protected]># Version: 1.0.1##### set some variables.Error_Create=2Error_No_Instance=3iError_Parameter=4#node host global variablesoci_path=/root/y/ocicompartment_id=ocid1.tenancy.oc1tenancy_id=ocid1.tenancy.oc1subnet_id=ocid1.subnet.oc1.phximage_id=ocid1.image.oc1.phxshape="VM.Standard1.1"#record node host create and delete variables.file_of_alived_node=/var/lib/oracle/alived.nodesfile_of_ip_number=/var/lib/oracle/ip.txtfile_of_create_node_log=/var/log/oracle/create.logfile_of_delete_node_log=/var/log/oracle/delete.log### make sure file is existence.[ ! -d $(dirname $file_of_alived_node) ] && mkdir $(dirname $file_of_alived_node) >>/dev/null[ ! -d $(dirname $file_of_ip_number) ] && mkdir $(dirname $file_of_ip_number) >>/dev/nulltouch $file_of_ip_number[ ! -d $(dirname $file_of_create_node_log) ] && mkdir $(dirname $file_of_create_node_log) >>/dev/null[ ! -d $(dirname $file_of_delete_node_log) ] && mkdir $(dirname $file_of_delete_node_log) >>/dev/null#### create node host### Usage: Create_Node instance_display_name instance_private_ip volume_display_name volume_size_in_mbs attachment_display_namefunction Create_Node {          #Create instance    instance_id=$($oci_path compute instance launch --availability-domain $avail_domain -c $compartment_id --image-id $image_id --shape $shape --display-name $1 --subnet-id $subnet_id --private-ip $2 | grep "\"id\"" | cut -d "\"" -f 4)    if [[ -z $instance_id ]];then        echo "[$(date +‘%F %T‘)] [instance] [$1:$2] [create failure] [exit...]" >> $file_of_create_node_log        exit $ERROR_Create    else        echo "[$(date +‘%F %T‘)] [instance] [$1:$instance_id:$2] [create success]" >> $file_of_create_node_log    fi    # Create Volume    volume_id=$($oci_path bv volume create --availability-domain $avail_domain -c $compartment_id --display-name $3 --size-in-mbs $4  | grep "\"id\"" | cut -d "\"" -f 4)    if [[ -z $volume_id ]];then        echo "[$(date +‘%F %T‘)] [volume] [$3] [create failure] [exit...]" >> $file_of_create_node_log        exit $ERROR_Create    else        echo "[$(date +‘%F %T‘)] [volume] [$3:$volume_id:$4] [create success]" >> $file_of_create_node_log    fi    # Attach Volume to Instance    while true;do        instance_state=$($oci_path compute instance get --instance-id $instance_id | grep "lifecycle-state" |cut -d "\"" -f 4)        volume_state=$($oci_path bv volume get --volume-id $volume_id | grep "lifecycle-state" |cut -d "\"" -f 4)        if [[ $instance_state == "RUNNING" ]] && [[ $volume_state == "AVAILABLE" ]];then            volume_attached_id=$($oci_path compute volume-attachment attach --display-name $5 --instance-id $instance_id --type iscsi --volume-id $volume_id | grep "\"id\"" | cut -d "\"" -f 4)             if [[ -z $volume_attached_id ]];then                echo "[$(date +‘%F %T‘)] [volume_attached] [$5] [create failure] [exit...]" >> $file_of_create_node_log                exit $ERROR_Create            else                echo "[$(date +‘%F %T‘)] [volume_attached] [$5:$volume_attached_id] [create success]" >> $file_of_create_node_log            fi            break        fi        sleep 5    done    echo "\"$(date +‘%F %T‘)\" \"$instance_id\" \"$volume_id\" \"$volume_attached_id\"" >> $file_of_alived_node}if [ $# -le 3 ];then    echo "Error: Usage $(basename $0) {create|delete} number"    exit $Error_Parameterficase $1 in     create)        start_ip_number=5        # file_of_ip_number restore have been create maximal ip         . $file_of_ip_number        create_instance_number=0        # Loop create node host        while [[ $create_instance_number -lt $2 ]] && [[ $start_ip_number -le 250 ]];do              instance_display_name=iGB$(printf "%03d" $start_ip_number)            instance_private_ip=10.40.1.$start_ip_number            echo $instance_private_ip            volume_display_name=vGB$(printf "%03d" $start_ip_number)            volume_size_mbs=51200            attachment_display_name=${instance_display_name}_attached_${volume_display_name}            Create_Node $instance_display_name $instance_private_ip $volume_display_name $volume_size_mbs $attachment_display_name            # alter create maximal ip            start_ip_number=$((start_ip_number+1))            echo "start_ip_number=$start_ip_number" >$file_of_ip_number              create_instance_number=$((create_instance_number+1))        done        echo "create $create_instance_number instance."        ;;    delete)        . $file_of_ip_number        #end_delete_ip_number=$((start_create_ip_number-1))        delete_instance_number=0        while [ $delete_instance_number -lt $2 ];do            instance_id=$(tail -1 $file_of_alived_node | cut -d "\"" -f 4)            if [[ -z $instance_id ]];then                echo "no have more instance"                echo "delete $delete_instance_number instance."                exit $Error_No_Instance                 fi            volume_id=$(tail -1 $file_of_alived_node | cut -d "\"" -f 6)            $oci_path compute instance terminate --force --instance-id $instance_id            while true;do                instance_state=$($oci_path compute instance get --instance-id $instance_id |grep "lifecycle-state" |cut -d "\"" -f 4)                 if [[ $instance_state == "TERMINATED" ]] || [[ -z $instance_state ]];then                    break                fi                sleep 5            done            $oci_path bv volume delete --force --volume-id $volume_id            delete_instance_number=$((delete_instance_number+1))            echo "\"$instance_id\" \"$volume_id\"" >> $file_of_delete_node_log            # alter alived hosts            sed -i "/$instance_id/d" $file_of_alived_node             echo "start_ip_number=$((start_ip_number-1))" >$file_of_ip_number          done        echo "delete $delete_instance_number instance."        ;;    *)        echo "Usage: $(basename $0) {create|delete} number"esac

注意 :

  • 指令碼使用
    • 建立節點機:./nodes_ocvh.sh create 3
    • 刪除節點:./nodes_ocvh.sh delete 2

Oracle雲遠程大量建立虛擬機器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.