Terraform系列二腾讯云CVM进一步相关玩法

网友投稿 260 2022-09-28

Terraform系列二腾讯云CVM进一步相关玩法

背景:

紧接[Terraform系列一腾讯云CVM相关简单创建]。准备围绕着cvm先熟悉一下基本的流程。比如:系统盘扩容,挂载数据盘,帐号密钥ssh-key,绑定公网ip.研究一下官方文档体验一下!

Terraform系列二腾讯云CVM进一步相关玩法

1.关于硬盘的操作

参照:,设置system_disk_size = 100。修改系统盘为100G,并增加了数据盘data_disks配置(50G)

1. 修改cvm.tf配置文件

[root@zhangpeng terraform]# cat cvm.tf

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 100 hostname = "cvm-almalinux" data_disks { data_disk_type = "CLOUD_PREMIUM" data_disk_size = 50 encrypt = false } security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" internet_max_bandwidth_out = 10 count = 1 }

2. terraform plan

3. terraform apply

4. 验证

2. 创建公网ip并绑定cvm

公网ip打开腾讯云控制台云服务器有个公网IP的选项看了下url叫eip?官网搜索一下eip:= true开启公网IP!参照:然后绑定CVM?具体步骤如下:

1. 修改cvm.tf

cat cvm.tf

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 100 hostname = "cvm-almalinux" allocate_public_ip = true data_disks { data_disk_type = "CLOUD_PREMIUM" data_disk_size = 50 encrypt = false } security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" internet_max_bandwidth_out = 10 count = 1 }

2. terraform plan

3. terraform apply

4. 验证

3.cvm重建得到的结论

在上面步骤中cvm拥有了公网的Ip。ssh登陆服务器先看一下:系统盘 数据盘创建成功,主机名hostname也设置成功了!先随便生成一个文件,然后更改cvm. tf相关配置。确认一下在什么环境下cvm会重建!

touch zhangpeng.txt

1. 修改一下公网ip出口带宽测试一下?

将internet_max_bandwidth_out = 10修改为internet_max_bandwidth_out = 15cat cvm.tf

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 100 hostname = "cvm-almalinux" allocate_public_ip = true data_disks { data_disk_type = "CLOUD_PREMIUM" data_disk_size = 50 encrypt = false } security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" internet_max_bandwidth_out = 15 count = 1 }

2. 修改系统盘与数据盘大小

两个的测试都放在一起了,首先是修改数据盘的大小:data_disk_size = 50 修改为 data_disk_size = 100cat cvm.tf

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 100 hostname = "cvm-almalinux" allocate_public_ip = true data_disks { data_disk_type = "CLOUD_PREMIUM" data_disk_size = 100 encrypt = false } security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" internet_max_bandwidth_out = 15 count = 1 }

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 150 hostname = "cvm-almalinux" allocate_public_ip = true data_disks { data_disk_type = "CLOUD_PREMIUM" data_disk_size = 100 encrypt = false } security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" internet_max_bandwidth_out = 15 count = 1 }

3. 增加一块新的数据盘

cat cvm.tf

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 150 hostname = "cvm-almalinux" allocate_public_ip = true data_disks { data_disk_type = "CLOUD_PREMIUM" data_disk_size = 100 encrypt = false } data_disks { data_disk_type = "CLOUD_PREMIUM" data_disk_size = 50 encrypt = false } security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" internet_max_bandwidth_out = 15 count = 1 }

4.结论

貌似在进行新增或者删除相关配置的时候都会重建?找泽阳大佬确认了一下有什么方式可以避免。貌似是我理解错了:写在cvm.tf这里貌似算是修改了cvm初始化,应该最好把数据盘 负载均衡单独创建,然后将其绑定到对应cvm!

4. 特别强调

1. terraform destroy

正好顺便体验一下删除配置然后重新创建一下应用:

terraform destroy

2. 单独创建vpc subset route and cvm

保持其他配置文件(vpc subset route and cvm)不变,修改cvm.tf如下:cat cvm.tf

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 50 hostname = "cvm-almalinux" security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] lifecycle { create_before_destroy = false } vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" }

3. terraform plan and terraform apply

4. 单独增加一个eip并绑定

1. 创建eip 公网ip

参照:terraform]# cat eip.tf

resource "tencentcloud_eip" "cvm_almalinux_eip" { name = "cvm_almalinux_eip" internet_max_bandwidth_out = 10 internet_service_provider = "BGP" type = "EIP" internet_charge_type = "TRAFFIC_POSTPAID_BY_HOUR" }

2. eip绑定cvm

参照:terraform]# cat eip_association.tf

resource "tencentcloud_eip_association" "cvm_almalinux_association" { eip_id = "${tencentcloud_eip.cvm_almalinux_eip.id}" instance_id = "${tencentcloud_instance.cvm_almalinux.id}" }

3. terraform plan and terraform apply

这里截图就忽略了!直接看结果!!

[root@zhangpeng terraform]# ssh root@xxx.xxx.xxx.xxx kex_exchange_identification: Connection closed by remote host [root@zhangpeng terraform]# ssh root@xxx.xxx.xxx.xxx ssh: connect to host root@xxx.xxx.xxx.xxx port 22: Connection timed out [root@zhangpeng terraform]# ssh root@xxx.xxx.xxx.xxx

5. 继续体验一下单独创建数据盘绑定cvm

1. 创建数据盘

参照:terraform]# cat cbs.tf

resource "tencentcloud_cbs_storage" "cvm_almalinux_storage" { storage_name = "cvm_almalinux" storage_type = "CLOUD_PREMIUM" storage_size = 100 availability_zone = "ap-beijing-2" project_id = 0 encrypt = false tags = { abc = "tf" } }

2. 数据盘绑定cvm

参照:terraform]# cat cbs_attachment.tf

resource "tencentcloud_cbs_storage_attachment" "cvm_almalinux_attachment" { storage_id = "${tencentcloud_cbs_storage.cvm_almalinux_storage.id}" instance_id = "${tencentcloud_instance.cvm_almalinux.id}" }

3. terraform plan and terraform apply

5.进一步的体验绑定ssh-key密钥方式登陆服务器

有了前面的失败案例,现在准备单独创建一个密钥文件然后绑定CVM参照:创建key_pair

resource "tencentcloud_key_pair" "ssh-key" { key_name = "ssh-key" public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDjd8fTnp7Dcuj4mLaQxf9Zs/ORgUL9fQxRCNKkPgP1paTy1I513maMX126i36Lxxl3+FUB52oVbo/FgwlIfX8hyCnv8MCxqnuSDozf1CD0/wRYHcTWAtgHQHBPCC2nJtod6cVC3kB18KeV4U7zsxmwFeBIxojMOOmcOBuh7+trRw==" }

注意:我这里使用了我本地环境的id_rsa.pub!以上为官网例子

2. cvm增加key_pair配置

增加key_name配置![root@zhangpeng terraform]# cat cvm.tf

resource "tencentcloud_instance" "cvm_almalinux" { instance_name = "cvm-almalinux" availability_zone = "ap-beijing-2" image_id = "img-q95tlc25" instance_type = "S2.MEDIUM2" system_disk_type = "CLOUD_PREMIUM" system_disk_size = 50 hostname = "cvm-almalinux" security_groups = [ "${tencentcloud_security_group.sg_bj.id}" ] lifecycle { create_before_destroy = false } key_name= "${tencentcloud_key_pair.ssh_key.id}" vpc_id = "${tencentcloud_vpc.vpc_bj.id}" subnet_id = "${tencentcloud_subnet.subnet_bj_02.id}" }

3. terraform plan and terraform apply

题外话:

总结一下:

公网ip还是在创建CVM的时候直接设置allocate_public_ip = true比较方便 数据盘的添加 还有如果需要额外公网ip的绑定。可以单独创建组件,然后参照attachment相关将其绑定到cvm。 ssh-key的绑定服务器不会重建 下一步的计划 配置文件如何管理的更优雅? 使用Terraform在cvm中安装软件管理CVM Terraform体验管理其他应用

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微服务间调用Retrofit在Spring Cloud Alibaba中的使用
下一篇:用于 Azure VM 的临时 OS 磁盘
相关文章

 发表评论

暂时没有评论,来抢沙发吧~