gcp-cloud shell 초기화

gcloud config list 를 확인하게되면 가끔 꼬인다.....................여러 어카운트를 가지고 관리할때 진짜 복잡해 지는데.... 이때에 필요한기능이다.

cloud shell 을 초기화해서 꼬인 연결을 초기화 해준다.

문제가 될때 초기화 하자

gcp- Google Kubernetes Engine

https://cloud.google.com/kubernetes-engine/docs/quickstart?hl=ko

먼저 cloud shell 에서 프로젝트 지정하고 리전(아님)을 지정한다. - 수정- zone 을 지정한다.

linuxer@cloudshell:~ (elated-ranger-263505)$ gcloud config set compute/zone us-central1-a
Updated property [compute/zone].

그리고 컨테이너 클러스터를 생성한다.

linuxer@cloudshell:~ (elated-ranger-263505)$ gcloud container clusters create linuxer-k8s

WARNING: Currently VPC-native is not the default mode during cluster creation. In the future, this will become the default mode and can be disabled using --no-enable-ip-ali as flag. Use --[no-]enable-ip-alias flag to suppress this warning.
WARNING: Newly created clusters and node-pools will have node auto-upgrade enabled by default. This can be disabled using the --no-enable-autoupgrade flag.
WARNING: Starting in 1.12, default node pools in new clusters will have their legacy Compute Engine instance metadata endpoints disabled by default. To create a cluster with
legacy instance metadata endpoints disabled in the default node pool, run clusters create with the flag --metadata disable-legacy-endpoints=true.
WARNING: Your Pod address range (--cluster-ipv4-cidr) can accommodate at most 1008 node(s).
This will enable the autorepair feature for nodes. Please see https://cloud.google.com/kubernetes-engine/docs/node-auto-repair for more information on node autorepairs.
ERROR: (gcloud.container.clusters.create) ResponseError: code=403, message=Kubernetes Engine API is not enabled for this project. Please ensure it is enabled in Google Cloud
Console and try again: visit https://console.cloud.google.com/apis/api/container.googleapis.com/overview?project=elated-ranger-26 to do so.

api error 는 역시나 사이트로 이동해서 허용해준다.

zone 을 지정하지 않고 리전을 자꾸 지정해서 클러스터를 생성할 수 없었다.

서태호님께서 잘못된 부분을 알려주셨다. 덕분에 진행할수 있었다.ㅜㅜ다행

gcloud container clusters get-credentials linuxer-k8s@cloudshell:~ (jth3434-197516)$ gcloud config set compute/zone us-central1-a

정상적으로 클러스터를 생성하고

@cloudshell:~ (jth3434-197516)$ gcloud container clusters get-credentials linuxer-k8s

클러스터 사용자 인증하고~

https://github.com/GoogleCloudPlatform/kubernetes-engine-samples/tree/master/hello-app

URL 참고하시고 hello-app 으로 deployment 한다.

@cloudshell:~/hello-app (jth3434-197516)$ kubectl create deployment hello-server --image=gcr.io/google-samples/hello-app:1.0

hello-app ver 1.0 이다. dockerfile 을 확인하면 컨테이너 설정을 확인할수 있다.

kubectl expose 를 이용하여 생성한 파일을 노출한다.

@cloudshell:~/hello-app (jth3434-197516)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-server-64db4d4dc7-xtrcd 1/1 Running 0 12m
@cloudshell:~/hello-app (jth3434-197516)$ kubectl get service hello-server
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-server LoadBalancer 10.35.255.80 35.223.145.93 80:30201/TCP 5m30s

그리고 get pads 으로 pads 의 상태를 확인하고 서비스를 확인해서 정상적으로 external-ip로 접속했을때 접속이 되면 정상이다.

gcp-terrafrom-2 with VPC create

이전 포스팅에서 cloud shell 을 이용해서 terraform 을 사용하는 방법을 포스팅 했다.

이번에는 VPC 를 생성하는 방법을 포스팅 하기로 하였다.

https://www.terraform.io/docs/providers/google/r/compute_subnetwork.html

다음 docs 를 참고하였다.

resource name -실제 vpc name -에 대문자가 들어가면
Error: Error creating Network: googleapi: Error 400: Invalid value for field 'resource.name'
에러가 발생한다 참고하자.

이걸 몰라서 한참..테스트를 했다.

main.tf

resource "google_compute_subnetwork" "us-central1-subnet" {
name = "${local.name_suffix}-us-central1-subnet"
ip_cidr_range = "10.2.0.0/16"
region = "us-central1"
network = google_compute_network.linuxer-VPC.self_link
}
resource "google_compute_subnetwork" "europe-west1-subnet" {
name = "${local.name_suffix}-europe-west1-subnet"
ip_cidr_range = "10.3.0.0/16"
region = "europe-west1"
network = google_compute_network.linuxer-VPC.self_link
}
resource "google_compute_network" "linuxer-VPC" {
name = "${local.name_suffix}-vpc"
auto_create_subnetworks = false
}

backing_file.tf - provider 에서 리전을 지정하지 않아도 된다고 하는데 지정해주었다.

locals {
name_suffix = "linuxer"
}
provider "google" {
region = "us-central1"
zone = "us-central1-c"
}

VPC : linuxer-vpc
linuxer-us-central1-subnet us-central1 / 10.2.0.0/16
linuxer-europe-west1-subnet europe-west1 / 10.3.0.0/16

dellpa34@cloudshell:~/docs-examples/subnetwork_basic$ terraform plan
Error: Error locking state: Error acquiring the state lock: resource temporarily unavailable
Lock Info:
ID: 640725d0-1fad-c74e-7cff-35baf4c72937
Path: terraform.tfstate
Operation: OperationTypeApply
Who: dellpa34@cs-6000-devshell-vm-3de0c123-93a9-4a2f-a584-d94918d8801a
Version: 0.12.18
Created: 2020-01-04 12:07:10.301086954 +0000 UTC
Info:
Terraform acquires a state lock to protect the state from being written
by multiple users at the same time. Please resolve the issue above and try
again. For most commands, you can disable locking with the "-lock=false"
flag, but this is not recommended.

테스트중에 캔슬 한번했더니 프로세스가 종료되지 않아서 자꾸 -lock=false 옵션을 주라고 떳다. 귀찮아서 그냥 죽였다. kill -9 4120

dellpa34 284 0.0 0.3 23080 6800 pts/1 S<s 18:01 0:00 _ -bash
dellpa34 4120 0.0 1.7 151504 29856 pts/1 T<l 21:07 0:00 _ terraform destroy
dellpa34 4123 0.1 3.0 153232 52824 pts/1 T<l 21:07 0:01 | _ /usr/local/bin/terraform destroy
dellpa34 4179 0.0 2.1 153020 37296 pts/1 T<l 21:07 0:00 | _ /home/dellpa34/docs-examples/subnetwork_basic/.terraform/plugins/linux_amd64/terraform-provider-google_v3.3.0_x5
dellpa34 4186 0.0 1.3 124688 22536 pts/1 T<l 21:07 0:00 | _ /home/dellpa34/docs-examples/subnetwork_basic/.terraform/plugins/linux_amd64/terraform-provider-random_v2.2.1_x4
dellpa34 4462 0.0 0.1 38304 3200 pts/1 R<+ 21:17 0:00 _ ps afxuwww
dellpa34@cloudshell:~/docs-examples/subnetwork_basic$ kill -9 4120

프로세스를 죽이고 apply 하여 정상적으로 생성되는것을 확인하였다.

대문자..-_-;;

일단 테라폼에서 vpc 생성할때 대문자는 안된다.

GUI에서도 대문자 사용은 불가하네..좋은걸 알았다..

내두시간..!

gcp-terrafrom-1 with google cloud shell

gcp 스터디를 위해서 테라폼의 사용을 익히려고 한다.
그러기 위해선 먼저 테라폼을 gcp cloudshell 에서 사용하는것이 우선이라 생각했다.

테라폼으로 aws내 에선 테스트 경험이 있으므로 gcp 의 네트워크 구성과 환경에 맞춰서 테라폼을 설정하는 방법을 익혀야 했다.

gcp 에서 테라폼을 사용하려면 cloudshell 을 사용하는 방법과 인스턴스를 생성하여 사용하는 방법 아니면 클라이언트 PC에서 사용하는 방법 이렇게 3가지가 있는데 나는 cloudshell 을 매우 좋아하므로 cloudshell 로 진행 할것이다.

먼저 cloud shell 에서 테라폼을 사용하려면 몇가지 단계를 거쳐야 했다.

1 terrafrom install
2 gcp api setup
3 config setting

이 단계를 간단하게 줄여주는 페이지가 있어서 먼저 테스트 해봤다.

https://www.hashicorp.com/blog/kickstart-terraform-on-gcp-with-google-cloud-shell/  

https://github.com/terraform-google-modules/docs-examples

두개의 URL 을 참고하시기 바란다.

URL 을 따라서 진행하던중 terraform init 에서 에러가 발생하였다.

Provider "registry.terraform.io/-/google" v1.19.1 is not compatible with Terraform 0.12.18.
Provider version 2.5.0 is the earliest compatible version. Select it with
the following version constraint:

version = "~> 2.5"
Terraform checked all of the plugin versions matching the given constraint:
~> 1.19.0
Consult the documentation for this provider for more information on
compatibility between provider and Terraform versions.
Downloading plugin for provider "random" (hashicorp/random) 2.2.1…
Error: incompatible provider version

error 는 backing_file.tf 파일에서 발생하고 있었다.
간단하게 version 차이..

cloud shell 의 terrafrom version 은

dellpa34@cloudshell:~/docs-examples/oics-blog$ terraform -version
Terraform v0.12.18
provider.google v2.20.1
provider.random v2.2.1

provider.google v2.20.1 로 1.19보다 많이 높은 상태였다. 일단 진행해 보기로 했으니.. backing_file.tf 수정

provider "google" {
version = "~> 1.19.0"
region = "us-central1"
zone = "us-central1-c"
}

provider "google" {
version = "~> 2.5"
region = "us-central1"
zone = "us-central1-c"
}

수정후에 다시 terraform init 을 실행 하였다.

Initializing provider plugins…
Checking for available provider plugins…
Downloading plugin for provider "google" (hashicorp/google) 2.20.1…
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

정상적으로 실행 되는것을 확인하였다.

init 후에 apply 하니 다시 Warning 과 함께 error 가 발생하였다.

Warning: Interpolation-only expressions are deprecated
on main.tf line 9, in resource "google_compute_instance" "instance":
9: image = "${data.google_compute_image.debian_image.self_link}"
Error: Error loading zone 'us-central1-a': googleapi: Error 403: Access Not Configured. Compute Engine API has not been used in project 45002 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/compute.googleapis.com/overview?project=304102002 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry., accessNotConfigured
on main.tf line 1, in resource "google_compute_instance" "instance":
1: resource "google_compute_instance" "instance" {

이건 이전에도 겪은 케이스 같다...functions api 셋팅할때 발생한 거였는데...googleapi 관련 에러다. cloudshell 에서 컴퓨팅쪽의 api를 사용할 수 없어서 발생하는 에러로 로그에 보이는 페이지로 이동해서 그냥 허용해 준다.

Enter a value: yes
google_compute_instance.instance: Creating…
google_compute_instance.instance: Still creating… [10s elapsed]
google_compute_instance.instance: Creation complete after 10s [id=vm-instance-optimum-badger]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

허용후에 다시 terraform apply 를 치면 정상적으로 실행이 된다. 그럼 확인해 보자.

인스턴스가 생성이 됬다. 그렇다면 이젠 간단히 생성하는것 까지 완료했으므로. 이젠 기본 템플릿을 이용한 구성을 만들것이다. 생성한 테라폼은 terraform destroy 명령어로 삭제 했다.

google_compute_instance.instance: Destroying… [id=vm-instance-optimum-badger]
google_compute_instance.instance: Still destroying… [id=vm-instance-optimum-badger, 10s elapsed]
google_compute_instance.instance: Still destroying… [id=vm-instance-optimum-badger, 20s elapsed]
google_compute_instance.instance: Still destroying… [id=vm-instance-optimum-badger, 30s elapsed]
google_compute_instance.instance: Destruction complete after 38s
random_pet.suffix: Destroying… [id=optimum-badger]
random_pet.suffix: Destruction complete after 0s

정상적으로 삭제되는것을 확인할수 있었다.

gcp cloud shell 에서 terraform 을 사용하는 방법을 테스트해 보았다.
다음엔 VPC 구성과 인스턴스 그룹 생성 로드벨런서 구성까지 한방에 진행할것이다.

여담으로 cloud shell은 진짜 강력한 도구이다.

스크린샷과 같이 shell을 지원하면서 동시에 에디터도 지원한다.

vi 에 익숙한 나같은경우에는 그냥 vi 로 작업하지만 익숙하지 않은 사용자의 경우에는 우와할정도다..

쓸수록 감탄하는 cloudshell...........

GCP Professional Cloud Architect 합격 후기

AWS sap 취득 다음 순번 자격증이 GCP PCA 였다.

사실 정리를 하는 편은 아니구.. 해서 참조한 URL 을 나열 하겠다.

이수진-님의 합격후기다.

https://brunch.co.kr/@topasvga/728

서태호-님의 자격증 가이드다.

다른 사람후기는 한섭님의..첫번째 어드바이스가 있었다.

시험은 영어로 진행되서 많이 고민을 했다. 케이스를 책읽듯이 거의 외웠다.
케이스 에서 뭘 원하는지 스스로 그렸다.

이공부 덕분에 케이스 문제는 한번도 막힘없이 완벽하게 다풀었다.

그렇게 케이스 공부가 완료되고 서비스간의 연계와 사용방법을 gcp docs 를 위주로 봤다. aws sap 를 취득하면서 요령이 생긴터라 docs 를 보면서 공부하는건 어렵지 않았다.

udemy를 주로봤고 인터넷에 올라와있는 사이트들에서 문제를 많이 풀었다.

자격증 취득에 도움을 주신 서태호님,한섭님,이수진님께 감사를 드린다.