1. GCP - CLI
1.1 vpc, vm 만들기
# mkdir gcp_cli && cd $_
# tee -a /etc/yum.repos.d/google-cloud-sdk.repo << EOM
[google-cloud-cli]
name=Google Cloud CLI
baseurl=https://packages.cloud.google.com/yum/repos/cloud-sdk-el8-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOM
yum install -y google-cloud-cli
gcloud --version
gcloud init --console-only
asia-northeast3-a





- gcloud init --console-only을 실행한후 나온 링크를 복사하여 이동
- 이동한 링크에서 키를 발급 받은 후 cli에서 그 키를 입력하여 인증

- 생성한 프로젝트 선택
* vpc 생성
gcloud compute networks create test-vpc
gcloud compute networks subnets create test-subnet --network=test-vpc --range=192.168.0.0/16 --region=asia-northeast3
- 서브넷 대역대를 설정하면 알아서 서브넷이 만들어진다.
gcloud compute firewall-rules list ## 보안그룹 느낌의 방화벽
gcloud compute firewall-rules create test-vpc-allow-ssh --allow=tcp:22 --description="Allow incoming traffic on TCP port 22" --direction=INGRESS --network=test-vpc --source-ranges 106.253.56.124/32
gcloud compute firewall-rules create test-vpc-allow-http --allow=tcp:80 --description="Allow incoming traffic on TCP port 80" --direction=INGRESS --network=test-vpc --source-ranges 0.0.0.0/0
gcloud compute firewall-rules create test-vpc-allow-icmp --allow=icmp --description="Allow incoming traffic on ICMP" --direction=INGRESS --network=test-vpc --source-ranges 0.0.0.0/0
- 22번 포트, 80번 포트, icmp를 열어준다.
gcloud compute images list # 이미들의 리스트를 볼 수 있다
gcloud compute images describe centos-7-v20230509 \ # 해당 이미지 버전의 정보를 볼 수 있음
--project=centos-cloud
gcloud compute machine-types list --filter="zone:( asia-northeast3-a )"
- 이미지의 목록들을 확인할 수 있다
- describe를 통해 특정 이미지의 세부정보 확인가능
# vi httpd-gcp.txt
#!/bin/bash
setenforce 0
yum install -y httpd wget
systemctl enable --now httpd
cd /tmp
wget https://s3.ap-northeast-2.amazonaws.com/seoul.yunhyeong.shop/food.tar
tar xvf food.tar -C /var/www/html
- 사용자 데이터 정의
* VM 만들기
gcloud compute instances create foodwagon \
--image=centos-7-v20230509 \
--image-project=centos-cloud \
--machine-type=e2-micro \
--network=test-vpc \
--subnet=test-subnet \
--tags http-server,https-server \ ## 방화벽에 태그를 생성해서 적용(http, https)
--zone=asia-northeast3-a \
--metadata-from-file=startup-script=httpd-gcp.txt
* 키생성

ssh-keygen -t rsa -f /root/.ssh/yunhyeong -C yunhyeong -b 2048 # 키 생성 후 이름 지정
- 2차 암호를 입력하라는 말이 나오는데 그냥 엔터 눌러서 스킵
# vi /root/.ssh/yunhyeong.pub
yunhyeong:ssh-rsa
# gcloud compute os-login ssh-keys add \
--key-file=/root/.ssh/yunhyeong.pub \
--project=terraform-02 \ # 프로젝트 ID 입력
--ttl=365d

- public key 앞에 key의 이름과 ":"문자를 넣어준다.
gcloud compute instances add-metadata foodwagon --metadata-from-file ssh-keys=/root/.ssh/yunhyeong.pub
- 메타데이터를 올리는 명령어
gcloud compute instances describe foodwagon
- 올려진 메타데이터나 foodwagon Vm의 정보를 확인
ssh -i /root/.ssh/yunhyeong yunhyeong@34.64.88.179
- 키를 활용하여 만들어진 VM에 접근
1.2 정리하기(삭제)
gcloud compute instances delete foodwagon
gcloud compute firewall-rules list
gcloud compute firewall-rules delete test-vpc-allow-http
gcloud compute firewall-rules delete test-vpc-allow-ssh
gcloud compute networks subnets delete test-subnet
gcloud compute networks delete test-vpc
- VM부터 vpc까지 순서대로 지운다
'Cloud Solution Architect > Terraform, Ansible' 카테고리의 다른 글
GCP - Terraform, Ansible (0) | 2023.05.19 |
---|