Post

GCP - GKE 시작하기

Overview

GCP를 이용하면 간단히 Kuberenetes cluster를 구축하고 이를 통해 cloud를 쉽게 활요하는 것이 가능하다. 이하에서는 GCP를 사용할 수 있는 기본 환경 설정 및 GKE(google kubernetes engine)을 사용하는 방법에 대해 알아보도록 하겠다.

전반적인 과정은 다음과 같다. 우선 GCP를 CLI환경에서 설정할 수 있도록 gcloud를 설치하고, 이후 gcloud 또는 web UI로 kubernetes cluster를 배포한다.

다음으로 locoal 환경에서 kubectl을 사용할 수 있게 설정한다. 마지막으로 bation VM을 설정해 bastion VM을 통해 항상 같은 환경에서 kubectl을 사용할 수 있게 만든다.

만약 GKE를 다른 account로 재설정 했을 경우 가장 하단에 “GKE 만료 후 다시 cloud 설정하기”를 참조하면 된다.

💡 GCP region 정보
asia-northeast3-a 이 seoul region이다.

gcloud 설치

gcloud 설치 (윈도우)

윈도우에서 powershell을 통해 손쉽게 gcloud를 설치할 수 있다. gcloud는 python 3.8+ 부터 설정이 가능한데, SDKInstnsallter에 python이 자동으로 포함돼 설치된다.

설치가 완료되면 gcloud 명령을 console에서 사용할 수 있게 된다.

1
2
3
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")

& $env:Temp\GoogleCloudSDKInstaller.exe

plugin 설치

window 환경에서 GKE를 사용하려면 다음 plguin이 설치돼야 한다.

1
gcloud components install gke-gcloud-auth-plugin

gcloud 설치 (리눅스 - 우분투 20.04 기준)

https://cloud.google.com/sdk/docs/install?hl=ko

package 설치

1
2
3
4
5
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates gnupg curl sudo
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
sudo apt-get update && sudo apt-get install google-cloud-cli

plugin 설치

1
sudo apt-get install google-cloud-cli-gke-gcloud-auth-plugin

gcloud 기본 설정

1
gcloud init

Kubectl 설치하기

1
2
3
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod 775 kubectl
sudo mv kubectl /usr/local/bin/

https://kubernetes.io/ko/docs/tasks/tools/install-kubectl-linux/

윈도우나 맥 환경에서 kubectl은 다음을 참조하자

window: https://kubernetes.io/ko/docs/tasks/tools/install-kubectl-windows/

mac: https://kubernetes.io/ko/docs/tasks/tools/install-kubectl-macos/

GKE 설치

GCP에서 제공하는 무료 credit을 가지고 GKE(Google Kubernetes Engine)을 실습 할 수 있다.

Web UI를 통해 kubernetes cluster 배포하기 (매우 간단)

GCP console에 들어가서 Kubernetes Engine 메뉴로 들어가 enable을 수행한 뒤, Web UI 메뉴를 통해 Kubernetes cluster를 설정할 수 있다.

0

1

이후 UI에서 제공하는 연결 버튼을 클릭해 명령줄 엑세스를 참조하면 kube config가 자동으로 설정된다. (.kube/config)

2

1
gcloud container clusters get-credentials core-kubernetes --zone asia-northeast3-a --project secret-imprint-412501

이후 powershell 환경에서 kubectl을 통해 GKE을 control 할 수 있다.

CLI환경에서 gcloud를 통해 kubernetes cluster 배포하기 (추천)

CLI명령을 통해 UI보다 더 직관적이고 편리하게 k8s cluster를 구축할 수 있다.

클러스터 생성하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
CLUSTER_NAME=core-kubernetes
REGION=asia-northeast3-a

gcloud components update
gcloud config set compute/zone $REGION

gcloud container clusters create $CLUSTER_NAME \
    --enable-autoscaling \
    --min-nodes=1 \
    --num-nodes=1 \
    --max-nodes=3 \
    --node-locations=$REGION \
    --machine-type=n1-standard-4

# 클러스터 확인
kubectl get node
# NAME                                             STATUS   ROLES    AGE     VERSION# gke-core-kubernetes-default-pool-b5dfd3f2-4f84   Ready    <none>   6m38s   v1.15.12-gke.2

이후 Compute Engine에 들어가면 node pool이 추가되는 것을 확인할 수 있다.

3


Bastion VM 생성

  • usage undefined- example undefined bastion vm 생성하면 새로운 service account가 자동 생성되는데, 해당 SA에는 GKE와 연결할 수 있는 권한이 부여돼 있지 않다. 그러므로 gcloud init에서 new configuration으로 기존 brower에 login 돼 있는 account의 권한이 사용될 수 있게 설정한다.

(참고) Service account 생성 및 권한 부여 with gcloud

Service account 만들기

  • usage: gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
  • example: gcloud iam service-accounts create krnomad-adm

Service account에 role 부여하기

  • usage undefined- example undefined

    Trouble shooting

bastion VM Service accont role binding 이슈 (WIP)

bastion vm 생성 시 연결된 service account는 GKE 권한이 없기 때문에 별도 SA를 생성하고 권한을 부여한 후 gcloud configuration을 변경해줘야 한다.

💡 gcp에서 bastion vm을 구성하고 GKE를 사용하려고 한다. bastion vm은 생성됐지만 gcloud에 연결되 service account 계정에 권한이 없어 gcloud container clusters get-credentials 명령 실행시 오류가 발생한다.
bastion vm에 binding 돼 있는 SA에 권한을 부여하거나 bastion vm이 다른 SA를 사용하도록 변경해야할 것 같은데, 어떻게 해야할까?

1
gcloud projects add-iam-policy-binding  white-rune-415922 --member="serviceAccount:234721658196-compute@developer.gserviceaccount.com" --role="roles/owner"
1
gcloud projects add-iam-policy-binding  white-rune-415922 --member="serviceAccount:234721658196-compute@developer.gserviceaccount.com" --role=roles/container.developer

GKE 만료 후 다시 cloud 설정하기 (WIP)

무료 credit이 종료돼 다시 설정이 필요할 경우, 다음 순서로 GKE를 재설정 할 수 있다.

  1. 신규 google id 생성
  2. GCP 가입 및 무료 credit 받기
  3. gcloud init
This post is licensed under CC BY 4.0 by the author.