# DevOps культура ## Навигация - [🏠 Главная страница курса](../../../README.md) - [📚 Модуль 9](../README.md) --- ## Принципы DevOps ### 1. Collaboration - Тесное взаимодействие между разработкой и операциями - Общие цели и метрики - Культура взаимопомощи ### 2. Automation - Автоматизация рутинных процессов - Infrastructure as Code - Continuous Integration/Continuous Deployment ### 3. Monitoring - Непрерывный мониторинг системы - Проактивное выявление проблем - Быстрое восстановление ### 4. Feedback - Быстрая обратная связь - Непрерывное улучшение - Обучение на ошибках ## DevOps Practices ### 1. Continuous Integration (CI) ```yaml # GitHub Actions CI Pipeline name: CI/CD Pipeline on: push: branches: [ main, develop ] pull_request: branches: [ main ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up JDK 17 uses: actions/setup-java@v3 with: java-version: '17' distribution: 'temurin' - name: Cache Maven dependencies uses: actions/cache@v3 with: path: ~/.m2 key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} restore-keys: ${{ runner.os }}-m2 - name: Run tests run: mvn clean test - name: Generate test report uses: dorny/test-reporter@v1 if: success() || failure() with: name: Maven Tests path: target/surefire-reports/*.xml reporter: java-junit build: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Set up JDK 17 uses: actions/setup-java@v3 with: java-version: '17' distribution: 'temurin' - name: Build with Maven run: mvn clean package -DskipTests - name: Build Docker image run: docker build -t myapp:${{ github.sha }} . - name: Push to registry run: | echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin docker push myapp:${{ github.sha }} deploy: needs: build runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - name: Deploy to production run: | echo "Deploying to production..." # Deployment logic here ``` ### 2. Continuous Deployment (CD) ```yaml # Kubernetes Deployment apiVersion: apps/v1 kind: Deployment metadata: name: myapp labels: app: myapp spec: replicas: 3 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: myapp image: myapp:latest ports: - containerPort: 8080 env: - name: SPRING_PROFILES_ACTIVE value: "production" - name: DATABASE_URL valueFrom: secretKeyRef: name: myapp-secrets key: database-url resources: requests: memory: "256Mi" cpu: "250m" limits: memory: "512Mi" cpu: "500m" livenessProbe: httpGet: path: /actuator/health port: 8080 initialDelaySeconds: 30 periodSeconds: 10 readinessProbe: httpGet: path: /actuator/health/readiness port: 8080 initialDelaySeconds: 5 periodSeconds: 5 --- apiVersion: v1 kind: Service metadata: name: myapp-service spec: selector: app: myapp ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer ``` ### 3. Infrastructure as Code (IaC) ```hcl # Terraform configuration terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = var.aws_region } # VPC resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" enable_dns_hostnames = true enable_dns_support = true tags = { Name = "${var.project_name}-vpc" } } # Subnets resource "aws_subnet" "public" { count = length(var.availability_zones) vpc_id = aws_vpc.main.id cidr_block = "10.0.${count.index + 1}.0/24" availability_zone = var.availability_zones[count.index] map_public_ip_on_launch = true tags = { Name = "${var.project_name}-public-subnet-${count.index + 1}" } } resource "aws_subnet" "private" { count = length(var.availability_zones) vpc_id = aws_vpc.main.id cidr_block = "10.0.${count.index + 10}.0/24" availability_zone = var.availability_zones[count.index] tags = { Name = "${var.project_name}-private-subnet-${count.index + 1}" } } # Security Groups resource "aws_security_group" "web" { name_prefix = "${var.project_name}-web-" vpc_id = aws_vpc.main.id ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { from_port = 443 to_port = 443 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "${var.project_name}-web-sg" } } # RDS Database resource "aws_db_subnet_group" "main" { name = "${var.project_name}-db-subnet-group" subnet_ids = aws_subnet.private[*].id tags = { Name = "${var.project_name}-db-subnet-group" } } resource "aws_db_instance" "main" { identifier = "${var.project_name}-db" engine = "postgres" engine_version = "14.7" instance_class = var.db_instance_class allocated_storage = 20 max_allocated_storage = 100 storage_type = "gp2" storage_encrypted = true db_name = var.db_name username = var.db_username password = var.db_password vpc_security_group_ids = [aws_security_group.db.id] db_subnet_group_name = aws_db_subnet_group.main.name backup_retention_period = 7 backup_window = "03:00-04:00" maintenance_window = "sun:04:00-sun:05:00" skip_final_snapshot = var.environment != "production" tags = { Name = "${var.project_name}-db" } } ``` ### 4. Monitoring and Logging ```yaml # Prometheus configuration global: scrape_interval: 15s evaluation_interval: 15s rule_files: - "alert_rules.yml" scrape_configs: - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'myapp' static_configs: - targets: ['myapp:8080'] metrics_path: '/actuator/prometheus' scrape_interval: 5s - job_name: 'node-exporter' static_configs: - targets: ['node-exporter:9100'] alerting: alertmanagers: - static_configs: - targets: - alertmanager:9093 --- # Grafana dashboard apiVersion: v1 kind: ConfigMap metadata: name: grafana-dashboard data: dashboard.json: | { "dashboard": { "title": "MyApp Dashboard", "panels": [ { "title": "Request Rate", "type": "graph", "targets": [ { "expr": "rate(http_requests_total[5m])", "legendFormat": "{{method}} {{endpoint}}" } ] }, { "title": "Response Time", "type": "graph", "targets": [ { "expr": "histogram_quantile(0.95, rate(http_request_duration_seconds_bucket[5m]))", "legendFormat": "95th percentile" } ] }, { "title": "Error Rate", "type": "graph", "targets": [ { "expr": "rate(http_requests_total{status=~\"5..\"}[5m])", "legendFormat": "5xx errors" } ] } ] } } ``` ## DevOps Tools ### 1. Version Control - Git для управления кодом - GitFlow для управления ветками - Pull Request для code review ### 2. CI/CD - GitHub Actions - GitLab CI/CD - Jenkins - Azure DevOps ### 3. Containerization - Docker для контейнеризации - Kubernetes для оркестрации - Helm для управления пакетами ### 4. Monitoring - Prometheus для метрик - Grafana для визуализации - ELK Stack для логирования - Jaeger для трассировки ## Best Practices ### 1. Culture - Создание культуры доверия - Общие цели и метрики - Непрерывное обучение ### 2. Automation - Автоматизация всех процессов - Infrastructure as Code - Self-healing systems ### 3. Monitoring - Comprehensive monitoring - Proactive alerting - Fast incident response ### 4. Security - Security by design - Automated security testing - Regular security audits