2장. Terraform을 사용하여 ROSA 클러스터 생성
2.1. Terraform을 사용하여 기본 ROSA 클러스터 생성 링크 복사링크가 클립보드에 복사되었습니다!
기본 클러스터 옵션으로 구성된 Terraform 클러스터 템플릿을 사용하여 AWS(ROSA)에서 Red Hat OpenShift Service를 빠르게 생성합니다.
아래에 설명된 클러스터 생성 프로세스에서는 다음 리소스를 사용하여 HCP 클러스터로 ROSA를 준비하는 Terraform 구성을 사용합니다.
-
관리
oidc-config구성이 있는 OIDC 공급자 - AWS Managed ROSA 정책과 관련된 사전 요구 사항 IAM Operator 역할
- AWS Managed ROSA 정책이 연결된 IAM 계정 역할
- STS 클러스터를 사용하여 ROSA를 생성하는 데 필요한 기타 모든 AWS 리소스
2.1.1. Terraform 개요 링크 복사링크가 클립보드에 복사되었습니다!
Terraform은 리소스를 한 번 구성하고 해당 리소스를 원하는 대로 복제하는 방법을 제공하는 코드 기반 도구입니다. Terraform은 선언적 언어를 사용하여 생성 작업을 수행합니다. 인프라 리소스의 최종 상태를 선언하고 Terraform은 이러한 리소스를 사양에 맞게 생성합니다.
사전 요구 사항
Terraform 구성 내에서 Red Hat Cloud Services 공급자 를 사용하려면 다음 사전 요구 사항을 충족해야 합니다.
- AWS(ROSA) 명령줄 인터페이스(CLI) 툴에 Red Hat OpenShift Service를 설치했습니다.
- 오프라인 Red Hat OpenShift Cluster Manager 토큰이 있습니다.
- Terraform 버전 1.4.6 이상이 설치되어 있습니다.
AWS 계정 전체 IAM 역할을 생성했습니다.
특정 계정 전체 IAM 역할 및 정책은 ROSA 지원, 설치, 컨트롤 플레인 및 컴퓨팅 기능에 필요한 STS 권한을 제공합니다. 여기에는 계정 전체 Operator 정책이 포함됩니다. AWS 계정 역할에 대한 자세한 내용은 추가 리소스를 참조하십시오.
- AWS 계정 및 관련 인증 정보가 있어 리소스를 생성할 수 있습니다. 인증 정보는 AWS 공급자용으로 구성됩니다. AWS Terraform 공급자 설명서의 인증 및 구성 섹션을 참조하십시오.
최소한 Terraform이 작동하는 AWS IAM 역할 정책에서 다음 권한이 있습니다. AWS 콘솔에서 이러한 권한을 확인합니다.
예 2.1. Terraform에 대한 최소 AWS 권한
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": [ "iam:GetPolicyVersion", "iam:DeletePolicyVersion", "iam:CreatePolicyVersion", "iam:UpdateAssumeRolePolicy", "secretsmanager:DescribeSecret", "iam:ListRoleTags", "secretsmanager:PutSecretValue", "secretsmanager:CreateSecret", "iam:TagRole", "secretsmanager:DeleteSecret", "iam:UpdateOpenIDConnectProviderThumbprint", "iam:DeletePolicy", "iam:CreateRole", "iam:AttachRolePolicy", "iam:ListInstanceProfilesForRole", "secretsmanager:GetSecretValue", "iam:DetachRolePolicy", "iam:ListAttachedRolePolicies", "iam:ListPolicyTags", "iam:ListRolePolicies", "iam:DeleteOpenIDConnectProvider", "iam:DeleteInstanceProfile", "iam:GetRole", "iam:GetPolicy", "iam:ListEntitiesForPolicy", "iam:DeleteRole", "iam:TagPolicy", "iam:CreateOpenIDConnectProvider", "iam:CreatePolicy", "secretsmanager:GetResourcePolicy", "iam:ListPolicyVersions", "iam:UpdateRole", "iam:GetOpenIDConnectProvider", "iam:TagOpenIDConnectProvider", "secretsmanager:TagResource", "sts:AssumeRoleWithWebIdentity", "iam:ListRoles" ], "Resource": [ "arn:aws:secretsmanager:*:<ACCOUNT_ID>:secret:*", "arn:aws:iam::<ACCOUNT_ID>:instance-profile/*", "arn:aws:iam::<ACCOUNT_ID>:role/*", "arn:aws:iam::<ACCOUNT_ID>:oidc-provider/*", "arn:aws:iam::<ACCOUNT_ID>:policy/*" ] }, { "Sid": "VisualEditor1", "Effect": "Allow", "Action": [ "s3:*" ], "Resource": "*" } ] }
Terraform 사용 시 고려 사항
일반적으로 Terraform을 사용하여 클라우드 리소스 관리는 Terraform 방법론을 사용하여 변경 사항을 수행해야 합니다. AWS 콘솔 또는 Red Hat 콘솔과 같은 Terraform 외부의 툴을 사용하여 Terraform이 생성한 클라우드 리소스를 수정할 때는 주의하십시오. Terraform 이외의 툴을 사용하여 Terraform에서 이미 관리하는 클라우드 리소스를 관리하면 선언된 Terraform 구성의 구성 드리프트가 도입되었습니다.
예를 들어 Red Hat Hybrid Cloud Console 을 사용하여 Terraform이 생성된 클러스터를 업그레이드하는 경우 향후 구성 변경 사항을 적용하기 전에 Terraform 상태를 조정해야 합니다. 자세한 내용은 HashiCorp 개발자 설명서의 Terraform 상태에서 리소스 관리를 참조하십시오.
2.1.2. 기본 클러스터 사양 개요 링크 복사링크가 클립보드에 복사되었습니다!
| 구성 요소 | 기본 사양 |
|---|---|
| 계정 및 역할 |
|
| 클러스터 설정 |
|
| 암호화 |
|
| 컨트롤 플레인 노드 구성 |
|
| 인프라 노드 구성 |
|
| 컴퓨팅 노드 시스템 풀 |
|
| 네트워킹 구성 |
|
| CIDR(Classless Inter-Domain Routing) 범위 |
|
| 클러스터 역할 및 정책 |
|
| 스토리지 |
|
| 클러스터 업데이트 전략 |
|
2.1.3. Terraform을 사용하여 기본 ROSA 클러스터 생성 링크 복사링크가 클립보드에 복사되었습니다!
아래에 설명된 클러스터 생성 프로세스는 Terraform을 사용하여 계정 전체 IAM 역할 및 관리 OIDC 구성으로 ROSA 클러스터를 생성하는 방법을 보여줍니다.
2.1.3.1. Terraform을 위한 환경 준비 링크 복사링크가 클립보드에 복사되었습니다!
Terraform을 사용하여 AWS 클러스터에서 Red Hat OpenShift Service를 생성하려면 오프라인 Red Hat OpenShift Cluster Manager 토큰 을 내보내야 합니다.
절차
선택 사항: 이 절차 중에 Terraform 파일이 현재 디렉터리에 생성되므로 새 디렉터리를 생성하여 이러한 파일을 저장하고 다음 명령을 실행하여 이 파일로 이동할 수 있습니다.
$ mkdir terraform-cluster && cd terraform-cluster- 오프라인 Red Hat OpenShift Cluster Manager 토큰을 사용하여 계정에 권한을 부여합니다.
오프라인 토큰을 복사하고 다음 명령을 실행하여 토큰을 환경 변수로 설정합니다.
$ export RHCS_TOKEN=<your_offline_token>참고이 환경 변수는 시스템을 다시 시작하거나 터미널을 닫는 등 각 세션이 끝날 때 재설정됩니다.
검증
토큰을 내보낸 후 다음 명령을 실행하여 값을 확인합니다.
$ echo $RHCS_TOKEN
2.1.3.2. Terraform 파일을 로컬에서 생성 링크 복사링크가 클립보드에 복사되었습니다!
오프라인 Red Hat OpenShift Cluster Manager 토큰 을 설정한 후 클러스터를 빌드하려면 Terraform 파일을 로컬로 생성해야 합니다. 다음 코드 템플릿을 사용하여 이러한 파일을 생성할 수 있습니다.
절차
다음 명령을 실행하여
main.tf파일을 생성합니다.$ cat<<-EOF>main.tf # # Copyright (c) 2023 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # terraform { required_providers { aws = { source = "hashicorp/aws" version = ">= 4.20.0" } rhcs = { version = ">= 1.6.3" source = "terraform-redhat/rhcs" } } } # Export token using the RHCS_TOKEN environment variable provider "rhcs" {} provider "aws" { region = var.aws_region ignore_tags { key_prefixes = ["kubernetes.io/"] } default_tags { tags = var.default_aws_tags } } data "aws_availability_zones" "available" {} locals { # Extract availability zone names for the specified region, limit it to 3 if multi az or 1 if single region_azs = var.multi_az ? slice([for zone in data.aws_availability_zones.available.names : format("%s", zone)], 0, 3) : slice([for zone in data.aws_availability_zones.available.names : format("%s", zone)], 0, 1) } resource "random_string" "random_name" { length = 6 special = false upper = false } locals { worker_node_replicas = var.multi_az ? 3 : 2 # If cluster_name is not null, use that, otherwise generate a random cluster name cluster_name = coalesce(var.cluster_name, "rosa-\${random_string.random_name.result}") } # The network validator requires an additional 60 seconds to validate Terraform clusters. resource "time_sleep" "wait_60_seconds" { count = var.create_vpc ? 1 : 0 depends_on = [module.vpc] create_duration = "60s" } module "rosa-hcp" { source = "terraform-redhat/rosa-hcp/rhcs" version = "1.6.3" cluster_name = local.cluster_name openshift_version = var.openshift_version account_role_prefix = local.cluster_name operator_role_prefix = local.cluster_name replicas = local.worker_node_replicas aws_availability_zones = local.region_azs create_oidc = true private = var.private_cluster aws_subnet_ids = var.create_vpc ? var.private_cluster ? module.vpc[0].private_subnets : concat(module.vpc[0].public_subnets, module.vpc[0].private_subnets) : var.aws_subnet_ids create_account_roles = true create_operator_roles = true # Optional: Configure a cluster administrator user \1 # # Option 1: Default cluster-admin user # Create an administrator user (cluster-admin) and automatically # generate a password by uncommenting the following parameter: # create_admin_user = true # Generated administrator credentials are displayed in terminal output. # # Option 2: Specify administrator username and password # Create an administrator user and define your own password # by uncommenting and editing the values of the following parameters: # admin_credentials_username = <username> # admin_credentials_password = <password> depends_on = [time_sleep.wait_60_seconds] } EOF- 1
- 선택 사항: 적절한 매개변수의 주석을 제거하고 필요한 경우 값을 편집하여 클러스터 생성 중에 관리자를 생성합니다.
다음 명령을 실행하여
variables.tf파일을 생성합니다.참고클러스터를 빌드하도록 명령을 실행하기 전에 이 파일을 복사하고 편집합니다.
$ cat<<-EOF>variables.tf # # Copyright (c) 2023 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # variable "openshift_version" { type = string default = "4.14.20" description = "Desired version of OpenShift for the cluster, for example '4.14.20'. If version is greater than the currently running version, an upgrade will be scheduled." } variable "create_vpc" { type = bool description = "If you would like to create a new VPC, set this value to 'true'. If you do not want to create a new VPC, set this value to 'false'." } # ROSA Cluster info variable "cluster_name" { default = null type = string description = "The name of the ROSA cluster to create" } variable "additional_tags" { default = { Terraform = "true" Environment = "dev" } description = "Additional AWS resource tags" type = map(string) } variable "multi_az" { type = bool description = "Multi AZ Cluster for High Availability" default = true } variable "worker_node_replicas" { default = 3 description = "Number of worker nodes to provision. Single zone clusters need at least 2 nodes, multizone clusters need at least 3 nodes" type = number } variable "aws_subnet_ids" { type = list(any) description = "A list of either the public or public + private subnet IDs to use for the cluster blocks to use for the cluster" default = ["subnet-01234567890abcdef", "subnet-01234567890abcdef", "subnet-01234567890abcdef"] } variable "private_cluster" { type = bool description = "If you want to create a private cluster, set this value to 'true'. If you want a publicly available cluster, set this value to 'false'." } #VPC Info variable "vpc_name" { type = string description = "VPC Name" default = "tf-qs-vpc" } variable "vpc_cidr_block" { type = string description = "value of the CIDR block to use for the VPC" default = "10.0.0.0/16" } variable "private_subnet_cidrs" { type = list(any) description = "The CIDR blocks to use for the private subnets" default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] } variable "public_subnet_cidrs" { type = list(any) description = "The CIDR blocks to use for the public subnets" default = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] } variable "single_nat_gateway" { type = bool description = "Single NAT or per NAT for subnet" default = false } #AWS Info variable "aws_region" { type = string default = "us-east-2" } variable "default_aws_tags" { type = map(string) description = "Default tags for AWS" default = {} } EOF다음 명령을 실행하여
vpc.tf파일을 생성합니다.$ cat<<-EOF>vpc.tf # # Copyright (c) 2023 Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "5.1.2" count = var.create_vpc ? 1 : 0 name = var.vpc_name cidr = var.vpc_cidr_block azs = local.region_azs private_subnets = var.multi_az ? var.private_subnet_cidrs : [var.private_subnet_cidrs[0]] public_subnets = var.multi_az ? var.public_subnet_cidrs : [var.public_subnet_cidrs[0]] enable_nat_gateway = true single_nat_gateway = var.single_nat_gateway enable_dns_hostnames = true enable_dns_support = true tags = var.additional_tags } EOFTerraform을 시작할 준비가 되어 있습니다.
2.1.3.3. Terraform을 사용하여 ROSA 클러스터 생성 링크 복사링크가 클립보드에 복사되었습니다!
Terraform 파일을 생성한 후 필요한 모든 종속 항목을 제공하려면 Terraform을 시작해야 합니다. 그런 다음 Terraform 계획을 적용합니다.
Terraform 상태 파일을 수정하지 마십시오. 자세한 내용은 Terraform 사용 시 고려 사항을참조하십시오.
절차
Terraform 파일을 기반으로 리소스를 생성하려면 다음 명령을 실행합니다.
$ terraform init선택 사항: 복사한 Terraform이 다음 명령을 실행하여 올바른지 확인합니다.
$ terraform validate출력 예
Success! The configuration is valid.다음 명령을 실행하여 Terraform으로 클러스터를 생성합니다.
$ terraform applyTerraform 인터페이스는 다음과 같이 클러스터를 생성하도록 두 가지 질문을 합니다.
출력 예
var.create_vpc If you would like to create a new VPC, set this value to 'true'. If you do not want to create a new VPC, set this value to 'false'. Enter a value: var.private_cluster If you want to create a private cluster, set this value to 'true'. If you want a publicly available cluster, set this value to 'false'. Enter a value:Terraform 인터페이스에 생성 또는 변경할 리소스가 나열되고 확인 메시지가 표시되면
yes를 입력하여 취소하십시오.출력 예
Plan: 63 to add, 0 to change, 0 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve.yes를 입력하면 Terraform 계획이 시작되어 AWS 계정 역할, Operator 역할 및 ROSA Classic 클러스터를 생성합니다.
검증
다음 명령을 실행하여 클러스터가 생성되었는지 확인합니다.
$ rosa list clusters클러스터 ID, 이름 및 상태를 보여주는 출력 예
ID NAME STATE TOPOLOGY 27c3snjsupa9obua74ba8se5kcj11269 rosa-tf-demo ready Classic (STS)다음 명령을 실행하여 계정 역할이 생성되었는지 확인합니다.
$ rosa list account-roles출력 예
I: Fetching account roles ROLE NAME ROLE TYPE ROLE ARN OPENSHIFT VERSION AWS Managed ROSA-demo-Installer-Role Installer arn:aws:iam::<ID>:role/ROSA-demo-Installer-Role 4.14 No ROSA-demo-Support-Role Support arn:aws:iam::<ID>:role/ROSA-demo-Support-Role 4.14 No ROSA-demo-Worker-Role Worker arn:aws:iam::<ID>:role/ROSA-demo-Worker-Role 4.14 No다음 명령을 실행하여 Operator 역할이 생성되었는지 확인합니다.
$ rosa list operator-rolesTerraform-created Operator 역할을 표시하는 출력 예
I: Fetching operator roles ROLE PREFIX AMOUNT IN BUNDLE rosa-demo 8
2.1.3.4. Terraform을 사용하여 ROSA 클러스터 삭제 링크 복사링크가 클립보드에 복사되었습니다!
terraform destroy 명령을 사용하여 terraform apply 명령으로 생성된 모든 리소스를 제거합니다.
리소스를 제거하기 전에 Terraform .tf 파일을 수정하지 마십시오. 이러한 변수는 삭제할 리소스와 일치합니다.
절차
terraform apply명령을 실행하여 클러스터를 생성한 디렉터리에서 다음 명령을 실행하여 클러스터를 삭제합니다.$ terraform destroyTerraform 인터페이스에서 두 가지 변수를 입력하라는 메시지를 표시합니다. 이는 클러스터를 생성할 때 제공한 응답과 일치해야 합니다.
var.create_vpc If you would like to create a new VPC, set this value to 'true.' If you do not want to create a new VPC, set this value to 'false.' Enter a value: var.private_cluster If you want to create a private cluster, set this value to 'true.' If you want a publicly available cluster, set this value to 'false.' Enter a value:yes를 입력하여 역할을 시작하고 클러스터 삭제를 시작합니다.출력 예
Plan: 0 to add, 0 to change, 63 to destroy. Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes
검증
다음 명령을 실행하여 클러스터가 삭제되었는지 확인합니다.
$ rosa list clusters클러스터 없이 표시되는 출력 예
I: No clusters available다음 명령을 실행하여 계정 역할이 삭제되었는지 확인합니다.
$ rosa list account-rolesTerraform-created 계정 역할이 표시되지 않는 출력 예
I: Fetching account roles I: No account roles available다음 명령을 실행하여 Operator 역할이 삭제되었는지 확인합니다.
$ rosa list operator-rolesTerraform-created Operator 역할이 표시되지 않는 출력 예
I: Fetching operator roles I: No operator roles available