DevOps Fundamentals - Cơ bản về DevOps

DevOps là gì?

Định nghĩa

DevOps là sự kết hợp của Development (phát triển) và Operations (vận hành), là một văn hóa, phương pháp làm việc và tập hợp các practices nhằm cải thiện collaboration giữa development team và operations team.

Mục tiêu của DevOps

  1. Tăng tốc độ delivery: Đưa sản phẩm đến người dùng nhanh hơn
  2. Cải thiện chất lượng: Giảm bugs và issues trong production
  3. Tăng reliability: Đảm bảo hệ thống hoạt động ổn định
  4. Cải thiện collaboration: Phá bỏ rào cản giữa teams

DevOps Culture

graph LR
    A[Development Team] <--> B[Shared Responsibility]
    B <--> C[Operations Team]

    D[Fast Feedback] --> B
    E[Continuous Learning] --> B
    F[Automation] --> B

DevOps Lifecycle

Các giai đoạn trong DevOps

graph TD
    A[Plan] --> B[Code]
    B --> C[Build]
    C --> D[Test]
    D --> E[Release]
    E --> F[Deploy]
    F --> G[Operate]
    G --> H[Monitor]
    H --> A

1. Plan (Lập kế hoạch)

  • Định nghĩa: Xác định requirements, tính năng cần phát triển
  • Tools: Jira, Azure DevOps, Trello
  • Java Example: Sprint planning cho Java microservices
// Example: User Story cho Java application
/**
 * User Story: Người dùng có thể đăng ký tài khoản
 * Acceptance Criteria:
 * - API endpoint POST /api/users/register
 * - Validation email và password
 * - Return JWT token khi thành công
 * - Handle duplicate email error
 */
@RestController
@RequestMapping("/api/users")
public class UserController {

    @PostMapping("/register")
    public ResponseEntity<RegisterResponse> register(@Valid @RequestBody RegisterRequest request) {
        // Implementation sẽ được develop trong sprint
        return null;
    }
}

2. Code (Lập trình)

  • Định nghĩa: Developers viết code và commit vào version control
  • Tools: Git, GitHub, GitLab
  • Best Practices: Feature branches, code review, pair programming
// Example: Git workflow cho Java project
// Feature branch cho user registration
public class UserService {

    private final UserRepository userRepository;
    private final PasswordEncoder passwordEncoder;
    private final JwtService jwtService;

    public RegisterResponse registerUser(RegisterRequest request) {
        // Validate email format
        if (!EmailValidator.isValid(request.getEmail())) {
            throw new ValidationException("Invalid email format");
        }

        // Check if user exists
        if (userRepository.existsByEmail(request.getEmail())) {
            throw new UserAlreadyExistsException("Email already registered");
        }

        // Create new user
        User user = new User();
        user.setEmail(request.getEmail());
        user.setPassword(passwordEncoder.encode(request.getPassword()));

        User savedUser = userRepository.save(user);

        // Generate JWT token
        String token = jwtService.generateToken(savedUser);

        return new RegisterResponse(savedUser.getId(), token);
    }
}

3. Build (Xây dựng)

  • Định nghĩa: Compile source code thành executable artifacts
  • Tools: Maven, Gradle, Jenkins, GitHub Actions
# Example: GitHub Actions build pipeline
name: Build Java Application

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout code
      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') }}

    - name: Build with Maven
      run: mvn clean compile

    - name: Package application
      run: mvn package -DskipTests

    - name: Upload build artifacts
      uses: actions/upload-artifact@v3
      with:
        name: jar-artifact
        path: target/*.jar

4. Test (Kiểm thử)

  • Định nghĩa: Thực hiện automated testing để đảm bảo code quality
  • Types: Unit tests, Integration tests, End-to-end tests
// Example: Comprehensive testing strategy
@SpringBootTest
@TestMethodOrder(OrderAnnotation.class)
class UserServiceIntegrationTest {

    @Autowired
    private UserService userService;

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    @Order(1)
    @DisplayName("Should register new user successfully")
    void shouldRegisterNewUser() {
        // Arrange
        RegisterRequest request = new RegisterRequest();
        request.setEmail("test@example.com");
        request.setPassword("StrongPassword123!");

        // Act
        RegisterResponse response = userService.registerUser(request);

        // Assert
        assertThat(response).isNotNull();
        assertThat(response.getUserId()).isNotNull();
        assertThat(response.getToken()).isNotBlank();
    }

    @Test
    @Order(2)
    @DisplayName("Should throw exception for duplicate email")
    void shouldThrowExceptionForDuplicateEmail() {
        // Arrange
        RegisterRequest request = new RegisterRequest();
        request.setEmail("test@example.com"); // Email đã tồn tại
        request.setPassword("AnotherPassword123!");

        // Act & Assert
        assertThatThrownBy(() -> userService.registerUser(request))
                .isInstanceOf(UserAlreadyExistsException.class)
                .hasMessage("Email already registered");
    }

    @Test
    @Order(3)
    @DisplayName("Should validate API endpoint")
    void shouldValidateApiEndpoint() {
        // Arrange
        RegisterRequest request = new RegisterRequest();
        request.setEmail("api-test@example.com");
        request.setPassword("ValidPassword123!");

        HttpEntity<RegisterRequest> entity = new HttpEntity<>(request);

        // Act
        ResponseEntity<RegisterResponse> response = restTemplate.postForEntity(
                "/api/users/register", entity, RegisterResponse.class);

        // Assert
        assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
        assertThat(response.getBody().getToken()).isNotBlank();
    }
}

5. Release (Phát hành)

  • Định nghĩa: Chuẩn bị code để deploy lên các environments
  • Activities: Versioning, changelog, release notes
<!-- Example: Maven release configuration -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-release-plugin</artifactId>
    <version>3.0.0-M7</version>
    <configuration>
        <tagNameFormat>v@{project.version}</tagNameFormat>
        <autoVersionSubmodules>true</autoVersionSubmodules>
        <releaseProfiles>release</releaseProfiles>
    </configuration>
</plugin>

<!-- Release profile -->
<profiles>
    <profile>
        <id>release</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

6. Deploy (Triển khai)

  • Định nghĩa: Đưa application lên target environment
  • Strategies: Blue-Green, Rolling, Canary deployment
# Example: Kubernetes deployment cho Java app
apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-service
  labels:
    app: user-service
    version: v1.0.0
spec:
  replicas: 3
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  selector:
    matchLabels:
      app: user-service
  template:
    metadata:
      labels:
        app: user-service
        version: v1.0.0
    spec:
      containers:
      - name: user-service
        image: mycompany/user-service:v1.0.0
        ports:
        - containerPort: 8080
        env:
        - name: SPRING_PROFILES_ACTIVE
          value: "production"
        - name: DB_HOST
          valueFrom:
            secretKeyRef:
              name: db-credentials
              key: host
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-credentials
              key: password
        livenessProbe:
          httpGet:
            path: /actuator/health
            port: 8080
          initialDelaySeconds: 60
          periodSeconds: 30
        readinessProbe:
          httpGet:
            path: /actuator/health/readiness
            port: 8080
          initialDelaySeconds: 30
          periodSeconds: 10
        resources:
          requests:
            memory: "512Mi"
            cpu: "500m"
          limits:
            memory: "1Gi"
            cpu: "1000m"

7. Operate (Vận hành)

  • Định nghĩa: Quản lý và maintain application trong production
  • Activities: Configuration management, scaling, incident response
// Example: Operational features trong Java app
@RestController
@RequestMapping("/admin")
public class OperationsController {

    private final ApplicationService applicationService;
    private final MetricsService metricsService;

    @GetMapping("/health")
    public ResponseEntity<Map<String, Object>> getDetailedHealth() {
        Map<String, Object> health = new HashMap<>();

        // Database connectivity
        health.put("database", checkDatabaseHealth());

        // External services
        health.put("external-api", checkExternalApiHealth());

        // Memory usage
        health.put("memory", getMemoryStatus());

        // Thread pools
        health.put("threads", getThreadPoolStatus());

        return ResponseEntity.ok(health);
    }

    @PostMapping("/cache/clear")
    public ResponseEntity<String> clearCache(@RequestParam String cacheName) {
        try {
            applicationService.clearCache(cacheName);
            return ResponseEntity.ok("Cache cleared successfully");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body("Failed to clear cache: " + e.getMessage());
        }
    }

    @GetMapping("/metrics/custom")
    public ResponseEntity<Map<String, Object>> getCustomMetrics() {
        Map<String, Object> metrics = new HashMap<>();

        metrics.put("active-users", metricsService.getActiveUserCount());
        metrics.put("requests-per-minute", metricsService.getRequestsPerMinute());
        metrics.put("error-rate", metricsService.getErrorRate());

        return ResponseEntity.ok(metrics);
    }

    private Map<String, Object> getMemoryStatus() {
        Runtime runtime = Runtime.getRuntime();
        Map<String, Object> memory = new HashMap<>();

        memory.put("max", runtime.maxMemory());
        memory.put("total", runtime.totalMemory());
        memory.put("free", runtime.freeMemory());
        memory.put("used", runtime.totalMemory() - runtime.freeMemory());

        return memory;
    }
}

8. Monitor (Giám sát)

  • Định nghĩa: Thu thập metrics, logs và alerts để đảm bảo system health
  • Tools: Prometheus, Grafana, ELK Stack, Jaeger
// Example: Monitoring integration trong Java app
@Component
public class ApplicationMetrics {

    private final Counter requestCounter;
    private final Timer responseTimer;
    private final Gauge activeSessionsGauge;

    public ApplicationMetrics(MeterRegistry meterRegistry) {
        this.requestCounter = Counter.builder("http.requests.total")
                .description("Total HTTP requests")
                .tag("application", "user-service")
                .register(meterRegistry);

        this.responseTimer = Timer.builder("http.response.time")
                .description("HTTP response time")
                .register(meterRegistry);

        this.activeSessionsGauge = Gauge.builder("sessions.active")
                .description("Number of active user sessions")
                .register(meterRegistry, this, ApplicationMetrics::getActiveSessionCount);
    }

    public void recordRequest(String endpoint, String method, int statusCode) {
        requestCounter.increment(
                Tags.of("endpoint", endpoint, "method", method, "status", String.valueOf(statusCode))
        );
    }

    public void recordResponseTime(String endpoint, Duration duration) {
        responseTimer.record(duration, Tags.of("endpoint", endpoint));
    }

    private double getActiveSessionCount() {
        // Logic to count active sessions
        return SessionManager.getActiveSessionCount();
    }
}

// Custom health indicator
@Component
public class CustomHealthIndicator implements HealthIndicator {

    private final DatabaseHealthService databaseHealth;
    private final ExternalApiHealthService externalApiHealth;

    @Override
    public Health health() {
        Map<String, Object> details = new HashMap<>();

        // Check database
        boolean dbHealthy = databaseHealth.isHealthy();
        details.put("database", dbHealthy ? "UP" : "DOWN");

        // Check external APIs
        boolean apiHealthy = externalApiHealth.isHealthy();
        details.put("external-api", apiHealthy ? "UP" : "DOWN");

        // Check memory usage
        double memoryUsage = getMemoryUsagePercentage();
        details.put("memory-usage", memoryUsage + "%");
        boolean memoryHealthy = memoryUsage < 90;

        boolean overallHealthy = dbHealthy && apiHealthy && memoryHealthy;

        return overallHealthy ? 
                Health.up().withDetails(details).build() :
                Health.down().withDetails(details).build();
    }

    private double getMemoryUsagePercentage() {
        Runtime runtime = Runtime.getRuntime();
        long maxMemory = runtime.maxMemory();
        long totalMemory = runtime.totalMemory();
        long freeMemory = runtime.freeMemory();
        long usedMemory = totalMemory - freeMemory;

        return (double) usedMemory / maxMemory * 100;
    }
}

DevOps Principles

1. Automation First

  • Định nghĩa: Tự động hóa mọi quy trình có thể
  • Lợi ích: Giảm human errors, tăng consistency, tiết kiệm thời gian

2. Continuous Everything

  • Continuous Integration: Integrate code changes thường xuyên
  • Continuous Delivery: Sẵn sàng deploy bất cứ lúc nào
  • Continuous Deployment: Tự động deploy khi tests pass
  • Continuous Monitoring: Giám sát liên tục system health

3. Infrastructure as Code (IaC)

  • Định nghĩa: Quản lý infrastructure thông qua code
  • Tools: Terraform, CloudFormation, Ansible

4. Microservices Architecture

  • Định nghĩa: Chia application thành nhiều services nhỏ, độc lập
  • Lợi ích: Independent deployment, scalability, technology diversity

5. Monitoring và Observability

  • Monitoring: Thu thập metrics và alerts
  • Logging: Ghi lại events và errors
  • Tracing: Theo dõi requests qua multiple services

DevOps Tools Ecosystem

Development

  • Version Control: Git, GitHub, GitLab
  • Code Quality: SonarQube, Checkstyle, SpotBugs
  • IDEs: IntelliJ IDEA, Eclipse, VS Code

Build và Package

  • Build Tools: Maven, Gradle
  • Artifact Repository: Nexus, Artifactory
  • Container Registry: Docker Hub, Amazon ECR

CI/CD

  • CI/CD Platforms: Jenkins, GitHub Actions, GitLab CI, Azure DevOps
  • Configuration: YAML, Groovy scripts

Testing

  • Unit Testing: JUnit, TestNG, Mockito
  • Integration Testing: Spring Boot Test, TestContainers
  • Performance Testing: JMeter, Gatling

Deployment

  • Containerization: Docker, Podman
  • Orchestration: Kubernetes, Docker Swarm
  • Cloud Platforms: AWS, Azure, GCP

Monitoring

  • Metrics: Prometheus, Micrometer
  • Visualization: Grafana
  • Logging: ELK Stack (Elasticsearch, Logstash, Kibana)
  • Tracing: Jaeger, Zipkin

Benefits của DevOps

1. Faster Time to Market

  • Deploy features nhanh hơn
  • Rapid feedback loops
  • Quick response to market changes

2. Improved Quality

  • Automated testing
  • Early bug detection
  • Consistent environments

3. Better Collaboration

  • Shared responsibilities
  • Cross-functional teams
  • Improved communication

4. Increased Reliability

  • Automated deployments
  • Consistent processes
  • Better monitoring

5. Enhanced Security

  • Security as code
  • Automated vulnerability scanning
  • Compliance automation

DevOps không chỉ là tools và technologies, mà là một cultural shift toward collaboration, automation, và continuous improvement trong software development lifecycle.