在云項目開發(fā)中有時會有下面的需求:
構(gòu)建大量擁有相同配置, 相同已安裝軟件, 甚至相同已啟動進(jìn)程的虛擬機。
下面的內(nèi)容會基于1個簡單的例子:
現(xiàn)有1臺vm 名字是 tf-vpc0-subnet0-vm0 , 我在上面手動安裝了jdk11 和 tomcat 服務(wù)器, 但是一臺這樣的vm是不夠的, 我想基于這個vm 快速克隆類似已安裝好和啟動好tomcat 的服務(wù)器。
其實我們用terraform 也能很方便地大量構(gòu)建規(guī)格相同的vm, 但是它們基本上都是空機器, 如果要往里面安裝我和配置一些東西, 只靠terraform其實并不足夠。
其中1個方法 就是使用goolge cloud 中的下面兩種技術(shù)
實例模板: instance-template
自定義操作系統(tǒng)鏡像: custom-image
簡單來講, instance-template 可以讓用戶定制1個 vm 的模板
包括:
而下面會詳細(xì)介紹如何基于1個vm - instance 去創(chuàng)建1個 instance-template, 從而去復(fù)制這個vm instance
image 實際就是vm的操作系統(tǒng)的一份copy, google 本身已經(jīng)提供相當(dāng)多的系統(tǒng)鏡像, 包括debian, ubuntu, rockylinux 等。
可以用下面命令去查看
[gateman@manjaro-x13 Terraform-GCP-config]$ gcloud compute images list
NAME PROJECT FAMILY DEPRECATED STATUS
centos-7-v20231212 centos-cloud centos-7 READY
centos-stream-8-v20231212 centos-cloud centos-stream-8 READY
centos-stream-9-v20231212 centos-cloud centos-stream-9 READY
cos-101-17162-336-28 cos-cloud cos-101-lts READY
cos-105-17412-226-43 cos-cloud cos-105-lts READY
cos-109-17800-66-33 cos-cloud cos-109-lts READY
cos-97-16919-404-21 cos-cloud cos-97-lts READY
debian-10-buster-v20231212 debian-cloud debian-10 READY
debian-11-bullseye-arm-v20231212 debian-cloud debian-11-arm READY
debian-11-bullseye-v20231212 debian-cloud debian-11 READY
debian-12-bookworm-arm-v20231212 debian-cloud debian-12-arm READY
debian-12-bookworm-v20231212 debian-cloud debian-12 READY
fedora-cloud-base-gcp-34-1-2-x86- fedora-cloud fedora-cloud-34 READY
...
我們在構(gòu)建vm時必須指定一個鏡像, 實際上就是為vm選擇操作系統(tǒng)。
但是這些鏡像大部分都沒有安裝第三方軟件, 例如jdk, tomcat, k8s… docker等服務(wù)器軟件是沒有的。
如果我們要避免重復(fù)在各臺vm上安裝這些軟件, 則需要構(gòu)建自定義鏡像。
好了, 下一節(jié)我們開始實操!
vm 名字: tf-vpc0-subnet0-vm0
安裝jdk11:
sudo apt-get install openjdk-11-jdk
安裝tomcat10:
cd /home/gateman/server/
wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.17/bin/apache-tomcat-10.1.17.tar.gz
tar -xf apache-tomcat-10.1.17.tar.gz
mv apache-tomcat-10.1.17 tomcat10
rm -rf apache-tomcat-10.1.17.tar.gz
利用systemd 配置tomcat 10 自啟動
因為如果只完成上面兩步, 即使你在當(dāng)前vm啟動tomcat, 但是基于這個vm 的disk創(chuàng)建的鏡像是不包含進(jìn)程的, 所以要對操作系統(tǒng)做一些配置把tomcat10 加入自啟動的service list
詳細(xì)步驟參考:
https:///nvd11/article/details/135007086=
我們先看命令
gcloud compute images create <<image-name>> \
--source-disk=<<disk-name>> \
--source-disk-zone=<<zone-name>> \
--force
所以關(guān)鍵只有兩個參數(shù), 1個就是disk name名字, 實際上鏡像就是硬盤上文件的1個集合。 所以參數(shù)是disk的名字而不是 vm-instance的名字。
我們可以用下面兩種命令去獲得 某個vm的disk 名字
[gateman@manjaro-x13 Terraform-GCP-config]$ gcloud compute instances describe tf-vpc0-subnet0-vm0 --zone europe-west2-c --format='get(disks[].source)'
https://www.googleapis.com/compute/v1/projects/jason-hsbc/zones/europe-west2-c/disks/tf-vpc0-subnet0-vm0
[gateman@manjaro-x13 Terraform-GCP-config]$ gcloud compute disks list --filter="users:tf-vpc0-subnet0-vm0"
NAME LOCATION LOCATION_SCOPE SIZE_GB TYPE STATUS
tf-vpc0-subnet0-vm0 europe-west2-c zone 20 pd-standard READY
可以見到默認(rèn)情況下, 跟著vm創(chuàng)建的第一塊disk名字就是vm名字
=========================================
第二個參數(shù)是 source-disk-zone, 我們用模板vm的zone 就好, 我這邊是europe-west2-c
–force 的意思即使模板vm已啟動也強制制作鏡像, 默認(rèn)下只能為1個已關(guān)閉的vm 的disk制作鏡像(避免制作過程中磁盤被更新)
執(zhí)行:
[gateman@manjaro-x13 Terraform-GCP-config]$ gcloud compute images create e2-small-tomcat-image \
--source-disk=tf-vpc0-subnet0-vm0 \
--source-disk-zone=europe-west2-c \
--force
Created [https://www.googleapis.com/compute/v1/projects/jason-hsbc/global/images/e2-small-tomcat-image].
WARNING: Some requests generated warnings:
-
NAME PROJECT FAMILY DEPRECATED STATUS
e2-small-tomcat-image jason-hsbc READY
[gateman@manjaro-x13 Terraform-GCP-config]$
我們可以用下面去查看所有自定義的系統(tǒng)image
[gateman@manjaro-x13 Terraform-GCP-config]$ gcloud compute images list --project=jason-hsbc --no-standard-images
NAME PROJECT FAMILY DEPRECATED STATUS
e2-small-tomcat-image jason-hsbc READY
而這個e2-small-tomcat-image 是基于disk tf-vpc0-subnet0-vm0 創(chuàng)建的, 為了避免這個disk 跟隨它的vm刪除而被刪除, 可以執(zhí)行下面命令去取消級聯(lián)刪除關(guān)系
gcloud compute instances set-disk-auto-delete tf-vpc0-subnet0-vm0 \
--no-auto-delete \
--disk=tf-vpc0-subnet0-vm0
先看下面這個命令
gcloud compute instance-templates create e2-small-tomcat
–source-instance=tf-vpc0-subnet0-vm0
–source-instance-zone=europe-west2-c
–image=e2-small-tomcat-image
從參數(shù)來看, 貌似是可以基于1個 vm的規(guī)格去創(chuàng)建1個 instance template的, 但是經(jīng)過本人多次測試,
如果直接基于1個vm的規(guī)格, 用–source-instance 創(chuàng)建的instance template 只能應(yīng)用這個vm 創(chuàng)建時使用的image,
即使在之后創(chuàng)建vm時制定 --image參數(shù)也無效。 也就是講無法使用 基礎(chǔ)vm 預(yù)安裝的jdk11 tomcat等, 不符合需求。
具體原因不詳。
所以建議, 了解清楚tf-vpc0-subnet0-vm0的規(guī)格, 用更詳細(xì)的參數(shù)去創(chuàng)建template, 同時制定image , 如下:
gcloud compute instance-templates create e2-small-tomcat \
--image=e2-small-tomcat-image \
--machine-type=e2-small \
--network=tf-vpc0 \
--subnet=tf-vpc0-subnet0 \
--region=europe-west2 \
--no-address \
--service-account=vm-common@jason-hsbc.iam.gserviceaccount.com \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--preemptible
gcloud compute instances create tf-vpc0-subnet0-vm2 \
--source-instance-template=e2-small-tomcat \
--zone=europe-west2-c
因為創(chuàng)建instance-template時已經(jīng)指定了image, 這里就不需要–image參數(shù)了
output
[gateman@manjaro-x13 ~]$ gcloud compute instances create tf-vpc0-subnet0-vm2 \
--source-instance-template=e2-small-tomcat \
--zone=europe-west2-c
Created [https://www.googleapis.com/compute/v1/projects/jason-hsbc/zones/europe-west2-c/instances/tf-vpc0-subnet0-vm2].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
tf-vpc0-subnet0-vm2 europe-west2-c e2-small true 192.168.0.27 RUNNING
這個新建的vm就應(yīng)該具有與vm0 相同的硬盤內(nèi)容了, 例如安裝了java tomcat等。
例如下面我用同樣的template 創(chuàng)建了2個vm
[gateman@manjaro-x13 ~]$ gcloud compute instances create tf-vpc0-subnet0-vm10 --source-instance-template=e2-small-tomcat --zone=europe-west2-c
Created [https://www.googleapis.com/compute/v1/projects/jason-hsbc/zones/europe-west2-c/instances/tf-vpc0-subnet0-vm10].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
tf-vpc0-subnet0-vm10 europe-west2-c e2-small true 192.168.0.30 RUNNING
[gateman@manjaro-x13 ~]$ gcloud compute instances create tf-vpc0-subnet0-vm15 --source-instance-template=e2-small-tomcat --zone=europe-west2-c
Created [https://www.googleapis.com/compute/v1/projects/jason-hsbc/zones/europe-west2-c/instances/tf-vpc0-subnet0-vm15].
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
tf-vpc0-subnet0-vm15 europe-west2-c e2-small true 192.168.0.31 RUNNING
[gateman@manjaro-x13 ~]$
1個vm10, 1個vm15
其中vm10 我之前用它的名字創(chuàng)建過1個VM (基于基礎(chǔ)debian 鏡像), 但是已刪除
而 vm15 則之前為用過這個名字,
結(jié)果是, vm10 無法應(yīng)用vm0的鏡像內(nèi)容, 就是無預(yù)裝jdk tomcat 等
而vm15是ok的!
原因未知!
因篇幅問題不能全部顯示,請點此查看更多更全內(nèi)容
Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號-2
違法及侵權(quán)請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)