설치 스크립트는 go 및 Terraform CLI 도구로 작성된 실행 파일을 사용하여 빈 프로젝트를 가져오고 여기에 애플리케이션을 설치합니다. 출력은 작동하는 애플리케이션과 부하 분산 IP 주소의 URL입니다.
./main.tf
서비스 사용 설정
Google Cloud 서비스는 기본적으로 프로젝트에서 사용 중지되어 있습니다. 여기에서 솔루션을 사용하려면 다음을 활성화해야 합니다.
Compute Engine - 가상 머신 및 네트워킹
variable"gcp_service_list"{description="The list of apis necessary for the project"type=list(string)default=["compute.googleapis.com",]}resource"google_project_service""all"{for_each=toset(var.gcp_service_list)project=var.project_numberservice=each.keydisable_dependent_services=falsedisable_on_destroy=false}
방화벽 규칙 만들기
방화벽에서 포트 80을 여는 규칙을 만들고 이를 http-server 라벨의 모든 머신에 적용합니다.
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-01-14(UTC)"],[],[],null,["# NoSQL Client Server\n\n+\n\n[](./images/arch-nosql-client-server.svg)\n\nNoSQL Client Server will configure and create two Compute Engine instances:\n\n- **Server** --- which will run MongoDB\n- **Client** --- which will run a custom go application that talks to MongoDB and then exposes an API where you consume data from MongoDB\n\n*** ** * ** ***\n\nGet Started\n-----------\n\nClick on the following link to a copy of the source code in Cloud Shell. Once\nthere, a single command will spin up a working copy of the application in your\nproject..\n| **Key Point:** Clicking on the following link will open a Cloud Shell session and download code from GitHub. You will have to authorize DeployStack to run. No actions will be taken until you do so.\n\n[](https://ssh.cloud.google.com/cloudshell/editor?cloudshell_git_repo=https://github.com/googlecloudplatform/deploystack-nosql-client-server&shellonly=true&cloudshell_image=gcr.io/ds-artifacts-cloudshell/deploystack_custom_image)\n\n[View source code on GitHub](https://github.com/googlecloudplatform/deploystack-nosql-client-server)\n\n*** ** * ** ***\n\nNoSQL Client Server components\n------------------------------\n\nThe NoSQL Client Server architecture makes use of one key product. The following highlights that product, along with more information, including links to related videos, product documentation, and interactive walkthroughs.\n\n*** ** * ** ***\n\nScripts\n-------\n\nThe install script uses an executable written in `go` and Terraform CLI tools to\ntake an empty project and install the application in it. The output should be a\nworking application and a url for the load balancing IP address.\n| **Note:** The following is a breakdown of the commands that run when deploying NoSQL Client Server. *You don't need to run each command individually; Click the Open in Cloud Shell button in the Getting Started section and run all commands.*\n\n### `./main.tf`\n\n#### Enable Services\n\nGoogle Cloud Services are disabled in a project by default. In order to use any\nof the solutions here, we have to activate the following:\n\n- **Compute Engine** --- virtual machines and networking\n\n variable \"gcp_service_list\" {\n description = \"The list of apis necessary for the project\"\n type = list(string)\n default = [\n \"compute.googleapis.com\",\n ]\n }\n\n resource \"google_project_service\" \"all\" {\n for_each = toset(var.gcp_service_list)\n project = var.project_number\n service = each.key\n disable_dependent_services = false\n disable_on_destroy = false\n }\n\n#### Create Firewall rule\n\nMakes a rule that opens up port 80 on the firewall and then applies it to any machine labeled `http-server` \n\n resource \"google_compute_firewall\" \"default-allow-http\" {\n name = \"deploystack-allow-http\"\n project = var.project_number\n network = \"projects/${var.project_id}/global/networks/default\"\n\n allow {\n protocol = \"tcp\"\n ports = [\"80\"]\n }\n\n source_ranges = [\"0.0.0.0/0\"]\n\n target_tags = [\"http-server\"]\n }\n\n#### Create Server Instance\n\nSpins up a VM that will act as the server, and serve up MongoDB \n\n # Create Instances\n resource \"google_compute_instance\" \"server\" {\n name = \"server\"\n zone = var.zone\n project = var.project_id\n machine_type = \"e2-standard-2\"\n tags = [\"http-server\"]\n\n\n boot_disk {\n auto_delete = true\n device_name = \"server\"\n initialize_params {\n image = \"family/ubuntu-1804-lts\"\n size = 10\n type = \"pd-standard\"\n }\n }\n\n network_interface {\n network = \"default\"\n access_config {\n // Ephemeral public IP\n }\n }\n\n metadata_startup_script = \u003c\u003cSCRIPT\n apt-get update\n apt-get install -y mongodb\n service mongodb stop\n sed -i 's/bind_ip = 127.0.0.1/bind_ip = 0.0.0.0/' /etc/mongodb.conf\n iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 27017\n service mongodb start\n SCRIPT\n depends_on = [google_project_service.all]\n }\n\n#### Create Client Instance\n\nSpins up a VM that will act as the client, and consume data from MongoDB \n\n resource \"google_compute_instance\" \"client\" {\n name = \"client\"\n zone = var.zone\n project = var.project_id\n machine_type = \"e2-standard-2\"\n tags = [\"http-server\", \"https-server\"]\n\n boot_disk {\n auto_delete = true\n device_name = \"client\"\n initialize_params {\n image = \"family/ubuntu-1804-lts\"\n size = 10\n type = \"pd-standard\"\n }\n }\n\n network_interface {\n network = \"default\"\n\n access_config {\n // Ephemeral public IP\n }\n }\n\n metadata_startup_script = \u003c\u003cSCRIPT\n add-apt-repository ppa:longsleep/golang-backports -y && \\\n apt update -y && \\\n apt install golang-go -y\n mkdir /modcache\n mkdir /go\n mkdir /app && cd /app\n curl https://raw.githubusercontent.com/GoogleCloudPlatform/golang-samples/main/compute/quickstart/compute_quickstart_sample.go --output main.go\n go mod init exec\n GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache go mod tidy\n GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache go get -u \n sed -i 's/mongoport = \"80\"/mongoport = \"27017\"/' /app/main.go\n echo \"GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache HOST=${google_compute_instance.server.network_interface.0.network_ip} go run main.go\"\n GOPATH=/go GOMODCACHE=/modcache GOCACHE=/modcache HOST=${google_compute_instance.server.network_interface.0.network_ip} go run main.go &\n SCRIPT\n\n depends_on = [google_project_service.all]\n }\n\n*** ** * ** ***\n\nConclusion\n----------\n\nOnce run you should now have a client server api using MongoDB running on two Compute Engine instances. Additionally you should have all of the code to modify or extend this solution to fit your environment."]]