본문 바로가기

클라우드/GCP 자원 테라폼으로 관리해보기

(16) GCP DNS 등록 및 무료 SSL 인증서 발급 및 등록

반응형

 

GCP DNS 등록도 테라폼을 통해 관리하도록 하였다. 

추후에 모듈화를 할지 고민 중이다.

 

(15) GCP Cloud DNS 등록(https://fanic2022.tistory.com/65 ) 작업을 선행으로 진행해야 한다.

 

나는 아래 3개의 도메인을 등록했다.

 

resource "google_dns_record_set" "jenkins-hrc0303-store" {
  name         = "jenkins.hrc0303.store."
  type         = "A"
  ttl          = 300
  managed_zone = google_dns_managed_zone.hrc0303-store.name

  rrdatas = ["34.64.227.240"]
}

resource "google_dns_record_set" "nginx-hrc0303-store" {
  name         = "nginx.hrc0303.store."
  type         = "A"
  ttl          = 300
  managed_zone = google_dns_managed_zone.hrc0303-store.name

  rrdatas = ["34.64.227.240"]
}

resource "google_dns_record_set" "sonarqube-hrc0303-store" {
  name         = "sonarqube.hrc0303.store."
  type         = "A"
  ttl          = 300
  managed_zone = google_dns_managed_zone.hrc0303-store.name

  rrdatas = ["34.64.227.240"]
}

 

Jenkins, nginx, sonarqube 목적으로  사용할 도메인을 등록하였다.

A 레코드로 rrdatas 값은 GKE Ingress controller 서비스의 IP로 설정했다.

 

name: 도메인명

type: 레코드 타입

ttl :  DNS 레코드의 변경사항이 적용될 때까지 걸리는 시간(초 단위)

managed_zone : 이전에 생성한 영역rrdatas = 레코드 타입에 따라 달라짐  

 

rrdatas에 들어갈 값 리스트

A 레코드: 도메인 이름을 IPv4 주소로 매핑

AAAA 레코드: 도메인 이름을 IPv6 주소로 매핑

CNAME 레코드: 도메인 이름을 다른 도메인 이름으로 매핑

MX 레코드: 메일 서버의 우선 순위와 도메인 이름을 지정

TXT 레코드: 도메인 이름에 대해 텍스트 정보를 저장

SRV 레코드: 특정 서비스를 제공하는 서버에 대한 정보를 지정

NS 레코드: 도메인 이름의 네임 서버를 지정

SOA 레코드: 도메인 이름의 시작 권한을 지정

PTR 레코드: IP 주소를 도메인 이름으로 매핑

 

 

나는 SSL 인증서를 적용해서 사용할거라 무료로 아스타리카(*) 도메인 인증서를 생성할 수 있는 letsencrypt를 사용하려고 한다. 

 

letsencrypt 홈페이지 링크

https://letsencrypt.org/ko/

 

Let's Encrypt - 무료 SSL/TLS 인증서

2024. 6. 24. NTP is critical to how TLS works, and now it’s memory safe at Let’s Encrypt. 더보기 2024. 5. 30. Increasing defense against BGP attacks thanks to support from the Open Technology Fund. 더보기 2024. 5. 1. Takeaways from Tailscale’s

letsencrypt.org

 

 

사용 방법은 다음과 같다.

WSL에 설치하는 기준으로 아래 명령어를 통해 설치를 해준다.

 

sudo apt-get install letsencrypt -y

 

설치 이후 해당 명령어를 통해 인증서를 생성해준다.

sudo certbot certonly --manual -d *.hrc0303.store -d hrc0303.store --preferred-challenges dns

 

 

 

 

해당명령어 실행시 TXT 레코드로 등록할 이름과 값이 나오는데 이 값을 테라폼을 통해 넣어주었다.

ex) 이미 생성한 후 작성한 글이라 도메인이 다른 hrc0302.store로 예시를 들었다.

 

resource "google_dns_record_set" "hrc0303-store-txt" {
  name         = "_acme-challenge.hrc0302.store."
  type         = "TXT"
  ttl          = 300
  managed_zone = google_dns_managed_zone.hrc0303-store.name

  rrdatas = ["fnuVQDZLu2dT-mGtz3zKB6aj-3hj4YBA_RUC4rGdXp0"]
}

 

 

해당 작업을 하게 되면 /etc/letsencrypt/live/도메인명 경로에 인증서 파일이 생성되게 된다.

 

 

해당 인증서를 GCP 에 사설인증서로 등록할 때 테라폼을 사용하는 방법이다.

 

인증서 관리자 API 활성화후 인증서를 등록해준다.

resource "google_project_service" "certificatemanager" {
  service = "certificatemanager.googleapis.com"
  project        = "pjt-an3-shard-vpc-2"
}

 

# 사설 인증서 등록 방법
resource "google_compute_ssl_certificate" "hrc0303-ssl-cert" {
  name        = "hrc0303-ssl-cert"
  certificate = file("../ssl/cert.pem")
  private_key = file("../ssl/privkey.pem")
}

 

파일의 경로는 위에서 생성한 파일을 받아 테라폼을 관리하고 있는 부분에 ssl 폴더를 생성하고 넣어주었다.

 

 

 

반응형