GCP

GCP Cloud Run

김모우 2023. 2. 6. 20:58
728x90
반응형

 

Cloud Run 이란?

GCP의 확장 가능한 인프라에서 직접 컨테이너를 실행할 수 있게 해주는 관리형 컴퓨팅 플랫폼입니다.

Cloud Run은 Knative 에서 빌드되어 Cloud Run으로 완전 관리형으로 컨테이너를실행하거나 Cloud Run on GKE로 GKE 클러스터에서 실행하도록 선택할 수 있습니다.

Cloud Run을 통해 개발자들은 코드를 작성하는데 Focus on 할 수 있고, 해당 서비스를 활용하여 손쉽게 서비스를 구성하고 운영, 확장할 수 있습니다.

 

 

Cloud Run 활성화

- gcloud auth login
: 구글 인증 진행

- gcloud services enable run.googleapis.com
: Cloud Run API 활성화

- gcloud config set compute/region <region 정보>

 

- Location=<region 정보>
: 환경 변수 설정

 

Container 배포 및 Cloud Run 실행

- nano package.json

{
  ## 필수 필드 (소문자, 한 단어, -/_ 포함 불가)
  "name": "helloworld",
  "description": "Simple hello world sample in Node",
  ## 필수 필드 (Majon.Minor.Path 순으로 입력)
  "version": "1.0.0",
  ## 메인으로 지정한 모듈의 ID
  "main": "index.js",
  ## Command Alias를 통해 사용할 Command 미리 지정 가능
  "scripts": {
    ## 시작 시 마다 실행
    "start": "node index.js"
  },
  "author": "Google LLC",
  "license": "Apache-2.0",
  ## 의존성 // 프로젝트가 의존하고 있는 패키지 일괄 관리
  "dependencies": {
    "express": "^4.17.1"
  }
}

* package.json

- npm: Node Package Manager -> node.js를 위한 패키지 매니저 // npm은 node.js에서 사용되는 모듈들을 패키지로 만들어 관리함

- package.json 설명

: 프로젝트 운영에 필요한 패키지 리스트

: 프로젝트 버전 명시

: 다른 환경에서도 빌드를 재생 가능하게 만들어, 다른 개발자가 쉽게 사용할 수 있게함 (Why?? 프로젝트 운영에 필요한 패키지 리스트 및 버전까지 명시하므로 다른 환경에서 빌드하더라도 동일한 의존성 패키지를 통해 빌드가 이루어짐)

 

- index.js

const express = require('express');
const app = express();
const port = process.env.PORT || 8080;
app.get('/', (req, res) => {
  const name = process.env.NAME || 'World';
  res.send(`Hello ${name}!`);
});
app.listen(port, () => {
  console.log(`helloworld: listening on port ${port}`);
});

 

- Dockerfile

# Use the official lightweight Node.js 12 image.
# https://hub.docker.com/_/node
FROM node:12-slim
# Create and change to the app directory.
WORKDIR /usr/src/app
# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY package*.json ./
# Install production dependencies.
# If you add a package-lock.json, speed your build by switching to 'npm ci'.
# RUN npm ci --only=production
RUN npm install --only=production
# Copy local code to the container image.
COPY . ./
# Run the web service on container startup.
CMD [ "npm", "start" ]

 

- gcloud builds submit --tag gcr.io/$GOOGLE_CLOUD_PROJECT/helloworld

: Container Image 빌드
: gcr.io/<project 명>

 

- gcloud container images list
: Container 이미지 리스트 확인

- docker run -d -p 8080:8080 gcr.io/$GOOGLE_CLOUD_PROJECT/helloworld

: Containr 실행

 

- gcloud run deploy --image gcr.io/$GOOGLE_CLOUD_PROJECT/helloworld --allow-unauthenticated --region=$LOCATION

: Cloud Run 을 통해 Web 배포

- Web 정상 접속 확인

728x90
반응형

'GCP' 카테고리의 다른 글

Terraform Variables  (0) 2023.03.14
Terraform 시작하기  (0) 2023.02.12
Google Cloud Platform for AWS Professionals Labs  (0) 2021.04.15