Files
sa-lessons/modules/module8/sections/04-gcp-services.md
T
Alexander Abakumov 15e744da49 Initial commit: Complete Software Architecture course with 11 modules
- Added comprehensive course structure with 11 modules
- Included theoretical content, practical examples, and tasks
- Multi-language code examples (Java, Rust, Node.js, Python)
- Detailed analysis of architectural approaches and trade-offs
- Complete caching strategies documentation
- Architecture examples from real-world systems (Twitter, Netflix, Uber)
- Structured navigation and learning path
- Ready for educational use
2025-09-09 22:08:07 +03:00

508 lines
13 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# GCP (Google Cloud Platform)
## Навигация
- [🏠 Главная страница курса](../../../README.md)
- [📚 Модуль 8](../README.md)
---
## Основные сервисы GCP
### 1. Compute Services
#### Compute Engine
- Виртуальные машины в облаке
- Различные типы машин (e2, n2, c2, m2, etc.)
- Автомасштабирование с Managed Instance Groups
- Preemptible VMs для экономии
```yaml
# GCP VM Instance
resources:
- name: my-vm
type: compute.v1.instance
properties:
zone: us-central1-a
machineType: zones/us-central1-a/machineTypes/e2-micro
disks:
- deviceName: boot
type: PERSISTENT
boot: true
autoDelete: true
initializeParams:
sourceImage: projects/ubuntu-os-cloud/global/images/family/ubuntu-2004-lts
diskSizeGb: 20
networkInterfaces:
- network: global/networks/default
accessConfigs:
- name: External NAT
type: ONE_TO_ONE_NAT
metadata:
items:
- key: startup-script
value: |
#!/bin/bash
apt-get update
apt-get install -y docker.io
systemctl start docker
systemctl enable docker
usermod -a -G docker $USER
docker run -d -p 80:80 nginx
tags:
items:
- web-server
```
#### Cloud Functions
- Serverless вычисления
- Поддержка различных языков
- Интеграция с другими GCP сервисами
```javascript
// Cloud Function Example
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
exports.processOrder = async (req, res) => {
try {
const order = req.body;
// Валидация заказа
if (!order || !order.customerId || !order.items) {
res.status(400).json({ error: 'Invalid order data' });
return;
}
// Обработка заказа
const orderId = await processOrder(order);
// Сохранение в Cloud Storage
const bucket = storage.bucket('my-orders-bucket');
const file = bucket.file(`orders/${orderId}.json`);
await file.save(JSON.stringify(order));
// Отправка уведомления через Pub/Sub
const { PubSub } = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('order-notifications');
await topic.publishMessage({
data: Buffer.from(JSON.stringify({
orderId: orderId,
status: 'processed',
timestamp: new Date().toISOString()
}))
});
res.status(200).json({
orderId: orderId,
status: 'processed'
});
} catch (error) {
console.error('Error processing order:', error);
res.status(500).json({ error: 'Internal server error' });
}
};
async function processOrder(order) {
// Логика обработки заказа
return `ORD-${order.customerId}-${Date.now()}`;
}
```
#### App Engine
- Платформа для веб-приложений
- Автоматическое масштабирование
- Поддержка различных языков
```yaml
# app.yaml
runtime: nodejs18
service: my-service
instance_class: F2
automatic_scaling:
min_instances: 1
max_instances: 10
target_cpu_utilization: 0.6
env_variables:
NODE_ENV: production
DATABASE_URL: "postgresql://user:password@/dbname?host=/cloudsql/project:region:instance"
handlers:
- url: /.*
script: auto
secure: always
```
### 2. Storage Services
#### Cloud Storage
- Объектное хранилище для файлов
- Различные классы хранения (Standard, Nearline, Coldline, Archive)
- Интеграция с CDN
```yaml
# GCP Cloud Storage Bucket
resources:
- name: my-bucket
type: storage.v1.bucket
properties:
location: US
storageClass: STANDARD
versioning:
enabled: true
lifecycle:
rule:
- action:
type: SetStorageClass
storageClass: NEARLINE
condition:
age: 30
- action:
type: SetStorageClass
storageClass: COLDLINE
condition:
age: 90
- action:
type: Delete
condition:
age: 365
cors:
- origin: ["https://example.com"]
method: ["GET", "POST"]
responseHeader: ["Content-Type"]
maxAgeSeconds: 3600
```
#### Persistent Disk
- Блочное хранилище для VM
- Различные типы (SSD, HDD)
- Автоматические снимки
```yaml
# Persistent Disk
resources:
- name: my-disk
type: compute.v1.disk
properties:
zone: us-central1-a
sizeGb: 100
type: zones/us-central1-a/diskTypes/pd-ssd
sourceImage: projects/ubuntu-os-cloud/global/images/family/ubuntu-2004-lts
```
### 3. Database Services
#### Cloud SQL
- Управляемые реляционные БД
- Поддержка MySQL, PostgreSQL, SQL Server
- Автоматические бэкапы и патчи
```yaml
# GCP Cloud SQL
resources:
- name: my-database
type: sqladmin.v1beta4.instance
properties:
name: my-database
region: us-central1
settings:
tier: db-f1-micro
dataDiskType: PD_SSD
dataDiskSizeGb: 20
backupConfiguration:
enabled: true
startTime: "03:00"
binaryLogEnabled: true
ipConfiguration:
ipv4Enabled: true
authorizedNetworks:
- value: "0.0.0.0/0"
name: "all"
locationPreference:
zone: us-central1-a
maintenanceWindow:
day: 7
hour: 3
databaseVersion: POSTGRES_13
rootPassword: !Ref DatabasePassword
```
#### Firestore
- NoSQL база данных
- Автоматическое масштабирование
- Offline поддержка
```yaml
# Firestore Database
resources:
- name: my-firestore
type: firestore.googleapis.com/Database
properties:
locationId: us-central1
type: FIRESTORE_NATIVE
concurrencyMode: OPTIMISTIC
```
#### BigQuery
- Data warehouse для аналитики
- Columnar storage
- SQL queries
```yaml
# BigQuery Dataset
resources:
- name: my-dataset
type: bigquery.v2.dataset
properties:
datasetReference:
datasetId: my_dataset
location: US
description: My BigQuery dataset
```
### 4. Networking Services
#### Virtual Private Cloud (VPC)
- Изолированная виртуальная сеть
- Подсети и маршрутизация
- Интеграция с on-premises
```yaml
# VPC Network
resources:
- name: my-vpc
type: compute.v1.network
properties:
autoCreateSubnetworks: false
routingConfig:
routingMode: REGIONAL
- name: my-subnet
type: compute.v1.subnetwork
properties:
network: !Ref my-vpc
ipCidrRange: 10.0.0.0/24
region: us-central1
privateIpGoogleAccess: true
```
#### Cloud Load Balancing
- Распределение нагрузки
- Health checks
- SSL termination
```yaml
# HTTP Load Balancer
resources:
- name: my-load-balancer
type: compute.v1.urlMap
properties:
name: my-load-balancer
defaultService: !Ref backend-service
hostRules:
- hosts: ["example.com"]
pathMatcher: path-matcher
pathMatchers:
- name: path-matcher
defaultService: !Ref backend-service
pathRules:
- paths: ["/api/*"]
service: !Ref api-backend-service
- name: backend-service
type: compute.v1.backendService
properties:
name: backend-service
backends:
- group: !Ref instance-group
healthChecks:
- !Ref health-check
loadBalancingScheme: EXTERNAL
protocol: HTTP
portName: http
```
### 5. Security Services
#### Cloud Identity and Access Management (IAM)
- Управление пользователями и доступом
- Роли и политики
- Service accounts
```yaml
# IAM Service Account
resources:
- name: my-service-account
type: iam.v1.serviceAccount
properties:
accountId: my-service-account
displayName: My Service Account
description: Service account for my application
- name: my-service-account-key
type: iam.v1.serviceAccountKey
properties:
name: !Ref my-service-account
keyAlgorithm: KEY_ALG_RSA_1024
privateKeyType: TYPE_GOOGLE_CREDENTIALS_FILE
```
#### Secret Manager
- Управление секретами
- Шифрование данных
- Аудит доступа
```yaml
# Secret Manager Secret
resources:
- name: my-secret
type: secretmanager.v1.secret
properties:
secretId: my-secret
replication:
automatic: {}
```
### 6. Monitoring and Logging
#### Cloud Monitoring
- Мониторинг ресурсов и приложений
- Логи и метрики
- Алерты и автоматические действия
```yaml
# Monitoring Alert Policy
resources:
- name: my-alert-policy
type: monitoring.v3.alertPolicy
properties:
displayName: High CPU Usage
conditions:
- displayName: CPU usage is high
conditionThreshold:
filter: 'resource.type="gce_instance" AND metric.type="compute.googleapis.com/instance/cpu/utilization"'
comparison: COMPARISON_GREATER_THAN
thresholdValue: 0.8
duration: 300s
alertStrategy:
autoClose: 604800s
enabled: true
notificationChannels:
- !Ref notification-channel
```
#### Cloud Logging
- Централизованное логирование
- Структурированные логи
- Интеграция с мониторингом
### 7. Application Services
#### Cloud Endpoints
- Управление API
- Аутентификация и авторизация
- Rate limiting
```yaml
# Cloud Endpoints Service
resources:
- name: my-endpoints-service
type: endpoints.googleapis.com/Service
properties:
serviceName: my-api.endpoints.my-project.cloud.goog
openapi: |
swagger: "2.0"
info:
title: My API
version: "1.0.0"
paths:
/api/v1/orders:
post:
summary: Create order
operationId: createOrder
parameters:
- name: order
in: body
required: true
schema:
$ref: "#/definitions/Order"
responses:
200:
description: Order created
schema:
$ref: "#/definitions/OrderResponse"
definitions:
Order:
type: object
properties:
customerId:
type: string
items:
type: array
items:
type: string
OrderResponse:
type: object
properties:
orderId:
type: string
status:
type: string
```
#### Pub/Sub
- Очереди сообщений
- Topics и Subscriptions
- Dead letter topics
```yaml
# Pub/Sub Topic
resources:
- name: my-topic
type: pubsub.v1.topic
properties:
name: my-topic
messageRetentionDuration: 604800s
- name: my-subscription
type: pubsub.v1.subscription
properties:
name: my-subscription
topic: !Ref my-topic
ackDeadlineSeconds: 60
messageRetentionDuration: 604800s
deadLetterPolicy:
deadLetterTopic: !Ref dead-letter-topic
maxDeliveryAttempts: 5
```
## Лучшие практики GCP
### 1. Project Organization
- Используйте проекты для изоляции ресурсов
- Применяйте labels для категоризации
- Используйте folders для иерархии
### 2. Security
- Используйте IAM для управления доступом
- Включайте шифрование для всех данных
- Настройте VPC firewall rules
- Используйте Private Google Access
### 3. Monitoring
- Настройте Cloud Monitoring алерты
- Используйте Cloud Logging для централизованного логирования
- Мониторьте затраты с помощью Billing API
- Настройте автоматическое масштабирование
### 4. Backup and Recovery
- Включите автоматические снимки дисков
- Используйте Cloud Storage для долгосрочного хранения
- Тестируйте процедуры восстановления
- Документируйте disaster recovery планы
### 5. Networking
- Используйте приватные подсети для баз данных
- Настройте Cloud NAT для исходящего трафика
- Используйте Private Google Access для сервисов
- Настройте firewall rules с минимальными правами