설명
이전에는 서버에 직접 설치하는 방법을 사용했을 때
DB를 직접 들어가서 설치를 해야했지만
init-script를 사용하면 편하게 할 수 있습니다.
Init-script
서버를 가동할 때 단 한번만 작동하는 코드입니다.
bash, python, perl 언어를 사용 할 수 있지만
저는 bash를 기준으로 하겠습니다.
이것을 사용해서 서버를 구성하면서 미들웨어까지 같이 설치해서
빠르게 구성할 수 있습니다.
Init-script 사용법
variable "subnet_no" {}
resource "ncloud_init_script" "init" {
name = "ls-script"
content = "#!/usr/bin/env\nls -al"
}
resource "ncloud_server" "server" {
subnet_no = var.subnet_no
server_image_product_code = "SW.VSVR.OS.LNX64.CNTOS.0703.B050"
init_script_no = ncloud_init_script.init.id
}
Init-script 공식 설명
https://registry.terraform.io/providers/NaverCloudPlatform/ncloud/latest/docs/resources/init_script
Terraform Registry
registry.terraform.io
방법
mysql 설치 스크립트 입니다.
#!/bin/bash
sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm
sudo yum install mysql-server
sudo systemctl start mysqld
bash 선언 후 명령어 들을 적으면 작동하는 방식입니다.
그러나 한줄에 다 적어야 합니다.
resource "ncloud_init_script" "init" {
name = "ls-script"
content = "#!/bin/bash \n sudo yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-11.noarch.rpm \n sudo yum install mysql-server \n sudo systemctl start mysqld"
}
Cloud for MySQL
네이버클라우드 테라폼 version 3.0.0이 2024년 1월 달에 업데이트가 되어서
테라폼에서 Cloud for MySQL이 사용할 수 있습니다.
Cloud for MySQL 생성
resource "ncloud_vpc" "test" {
ipv4_cidr_block = "10.0.0.0/16"
}
resource "ncloud_subnet" "test" {
vpc_no = ncloud_vpc.test.vpc_no
subnet = cidrsubnet(ncloud_vpc.test.ipv4_cidr_block, 8, 1)
zone = "KR-2"
network_acl_no = ncloud_vpc.test.default_network_acl_no
subnet_type = "PUBLIC"
}
resource "ncloud_mysql" "mysql" {
subnet_no = ncloud_subnet.test.id
service_name = "my-tf-mysql"
server_name_prefix = "name-prefix"
user_name = "username"
user_password = "password1!"
host_ip = "192.168.0.1"
database_name = "db_name"
}
공식 설명
https://registry.terraform.io/providers/NaverCloudPlatform/ncloud/latest/docs/resources/mysql
Terraform Registry
registry.terraform.io
is_ha가 true가 기본이여서 is_ha = false
로 잡아야 비용이 많이나오는 걸 줄일 수 있다.
#db_server
#db server
resource "ncloud_mysql" "create_mysql" {
subnet_no = ncloud_subnet.create_db_subnet[0].id
service_name = "bo20cy-mysql"
server_name_prefix = bo20cy
user_name = bo20cy
user_password = 1q2w3e4r!@
host_ip = "%"
database_name = bo20cy-db
is_ha = true
is_multi_zone = true
standby_master_subnet_no = ncloud_subnet.create_db_subnet[1].id
}
이런식으로 고가용성을 확보 할 수 있다.
다음 장에 쓸 도메인을 땡길 때
is_ha = ture
라면
ncloud_mysql.create_mysql.mysql_server_list[0].private_domain
이 것을 써서 가져올 수 있다.
아래것을 참조하면 다른 것도 가져올 수 있다.
[Attributes Reference]
In addition to all arguments above, the following attributes are exported
id
- MySQL Instance Number.engine_version_code
- MySQL Engine version code.region_code
- Region code.vpc_no
- The ID of the associated Vpc.access_control_group_no_list
- The ID list of the associated Access Control Group.mysql_config_list
- The list of config.mysql_server_list
- The list of the MySQL server.server_instance_no
- Server instance number.server_name
- Server name.server_role
- Server role code. ex) M(Master), H(Standby Master)zone_code
- Zone code.subnet_no
- The ID of the associated Subnet.product_code
- Product code.is_public_subnet
- Public subnet status.private_domain
- Private domain.public_domain
- Public domain.memory_size
- Available memory size.cpu_count
- CPU count.data_storage_size
- Storage size.used_data_storage_size
- Size of data storage in use.uptime
- Running start time.create_date
- Server create date.
'네이버클라우드플랫폼(Ncloud) > 테라폼' 카테고리의 다른 글
테라폼 Tomcat - MySQL 연동하기(NCP) (1) | 2024.04.04 |
---|---|
테라폼 NGINX - Tomcat연동 (1) | 2024.03.29 |
테라폼 Auto Scaling Group 생성(+ 서버이미지 적용) (2) | 2024.03.01 |
테라폼 ACG 설정하기(+dynamic) (2) | 2024.02.04 |
테라폼 Loadbalancer 구축하기(+count) (2) | 2024.01.29 |