首頁  >  文章  >  後端開發  >  Kubernetes 作為資料庫?關於 CRD 您需要了解的內容

Kubernetes 作為資料庫?關於 CRD 您需要了解的內容

WBOY
WBOY原創
2024-07-19 12:32:22219瀏覽

Kubernetes as a Database? What You Need to Know About CRDs

在快速發展的雲端原生技術領域,Kubernetes 已成為容器化應用編排的有力工具。但在開發人員和 IT 專家中,「Kubernetes 是資料庫嗎?」是一個常見問題。這篇文章探討了這個問題,並對自訂資源定義 (CRD) 進行了全面的描述,重點介紹了它們在 Kubernetes 生態系統中的使用。我們希望透過完整的程式碼範例和實際應用來闡明這些想法,並說明 Kubernetes 在有狀態應用管理方面的適應性和強大功能。

Kubernetes 簡介

Kubernetes,通常縮寫為 K8s,是一個開源平台,旨在自動化應用程式容器的部署、擴展和操作。 Kubernetes 最初由 Google 開發,在充滿活力的社群以及廣泛的工具和擴充功能的支持下,已成為容器編排事實上的標準。

Kubernetes 核心概念

在深入了解 CRD 的細節以及 Kubernetes 作為資料庫的問題之前,有必要先了解一些核心概念:

Pod:Kubernetes 中最小的可部署單元,代表叢集中正在執行的進程的單一實例。

節點:Kubernetes 中的工作機器,可以是虛擬的,也可以是實體的。

叢集:由 Kubernetes master 控制的一組節點。

服務:定義一組邏輯 Pod 以及存取它們的策略的抽象。

Kubernetes 作為資料庫:神話還是現實?

Kubernetes 本身不是資料庫。它是一個編排平台,可以管理容器化應用程序,包括資料庫。然而,由於 Kubernetes 可以用來有效地部署和管理資料庫應用程序,因此經常會出現混亂。

了解自訂資源定義 (CRD)

自訂資源定義 (CRD) 擴充了 Kubernetes API,允許使用者管理自己的特定於應用程式的自訂資源。此功能使 Kubernetes 具有高度可擴展性和可自訂性,以適應各種用例。

什麼是 CRD?

CRD 使用戶能夠定義行為類似於內建 Kubernetes 資源的自訂物件。例如,雖然 Kubernetes 具有 Pod、服務和部署等內建資源,但您可以建立「MySQLCluster」或「PostgreSQLBackup」等自訂資源。

建立 CRD

要建立 CRD,您需要在 YAML 檔案中定義它並將其套用到您的 Kubernetes 叢集。這是一個基本範例:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myresources.example.com
spec:
  group: example.com
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    plural: myresources
    singular: myresource
    kind: MyResource
    shortNames:
      - mr

使用 kubectl apply -f myresource-crd.yaml 應用此 YAML 檔案將在叢集中建立自訂資源定義。

管理自訂資源

建立 CRD 後,您就可以開始管理自訂資源,就像管理本機 Kubernetes 資源一樣。以下是自訂資源實例的範例:

apiVersion: example.com/v1
kind: MyResource
metadata:
  name: myresource-sample
spec:
  foo: bar
  count: 10

您可以使用以下方式建立此自訂資源:

kubectl apply -f myresource-instance.yaml

將 CRD 用於有狀態應用

自訂資源定義對於管理有狀態應用程式(包括資料庫)特別有用。它們允許您定義資料庫叢集的所需狀態、備份策略和其他自​​訂行為。

範例:使用 CRD 管理 MySQL 叢集

考慮一個場景,您需要使用 Kubernetes 管理 MySQL 叢集。您可以定義自訂資源來表示 MySQL 叢集配置:

apiVersion: example.com/v1
kind: MySQLCluster
metadata:
  name: my-mysql-cluster
spec:
  replicas: 3
  version: "5.7"
  storage:
    size: 100Gi
    class: standard

借助此 CRD,您可以使用標準 Kubernetes 命令建立、更新和刪除 MySQL 集群,從而使資料庫管理更加簡單並與基礎架構的其餘部分集成。

進階 CRD 功能

CRD 提供了多種高級功能,可增強其功能以及與 Kubernetes 生態系統的整合。

驗證模式

您可以為自訂資源定義驗證架構,以確保僅接受有效的配置。以下是向 MySQLCluster CRD 新增驗證架構的範例:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: mysqlclusters.example.com
spec:
  group: example.com
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                replicas:
                  type: integer
                  minimum: 1
                version:
                  type: string
                storage:
                  type: object
                  properties:
                    size:
                      type: string
                    class:
                      type: string
  scope: Namespaced
  names:
    plural: mysqlclusters
    singular: mysqlcluster
    kind: MySQLCluster
    shortNames:
      - mc

自訂控制器

要自動化管理自訂資源,您可以編寫自訂控制器。這些控制器會監視自訂資源的更改,並採取措施使實際狀態與所需狀態保持一致。

以下概述如何為 MySQLCluster 資源編寫控制器:

package main

import (
    "context"
    "log"

    mysqlv1 "example.com/mysql-operator/api/v1"
    "k8s.io/apimachinery/pkg/runtime"
    "k8s.io/client-go/kubernetes/scheme"
    ctrl "sigs.k8s.io/controller-runtime"
    "sigs.k8s.io/controller-runtime/pkg/client"
    "sigs.k8s.io/controller-runtime/pkg/manager"
)

type MySQLClusterReconciler struct {
    client.Client
    Scheme *runtime.Scheme
}

func (r *MySQLClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    var mysqlCluster mysqlv1.MySQLCluster
    if err := r.Get(ctx, req.NamespacedName, &mysqlCluster); err != nil {
        return ctrl.Result{}, client.IgnoreNotFound(err)
    }

    // Reconciliation logic goes here

    return ctrl.Result{}, nil
}

func main() {
    mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
        Scheme: scheme.Scheme,
    })
    if err != nil {
        log.Fatalf("unable to start manager: %v", err)
    }

    if err := (&MySQLClusterReconciler{
        Client: mgr.GetClient(),
        Scheme: mgr.GetScheme(),
    }).SetupWithManager(mgr); err != nil {
        log.Fatalf("unable to create controller: %v", err)
    }

    if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
        log.Fatalf("unable to run manager: %v", err)
    }
}

版本控制

CRD 支援版本控制,可讓您管理自訂資源的不同版本並在它們之間順利遷移。這對於保持向後相容性和隨著時間的推移不斷發展 API 至關重要。

案例研究:資料庫的 Kubernetes 運算子

Kubernetes Operators leverage CRDs and custom controllers to automate the management of complex stateful applications like databases. Let's explore a real-world example: the MySQL Operator.

The MySQL Operator

The MySQL Operator simplifies the deployment and management of MySQL clusters on Kubernetes. It uses CRDs to define the desired state of the MySQL cluster and custom controllers to handle tasks like provisioning, scaling, and backups.

Defining the MySQLCluster CRD

The MySQL Operator starts by defining a CRD for the MySQLCluster resource, as shown earlier. This CRD includes fields for specifying the number of replicas, MySQL version, storage requirements, and more.

Writing the MySQLCluster Controller

The controller for the MySQLCluster resource continuously watches for changes to MySQLCluster objects and reconciles the actual state with the desired state. For example, if the number of replicas is increased, the controller will create new MySQL instances and configure them to join the cluster.

Code Example: Scaling a MySQL Cluster

Here’s a simplified version of the controller logic for scaling a MySQL cluster:

func (r *MySQLClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    var mysqlCluster mysqlv1.MySQLCluster
    if err := r.Get(ctx, req.NamespacedName, &mysqlCluster); err != nil {
        return ctrl.Result{}, client.IgnoreNotFound(err)
    }

    // Fetch the current number of MySQL pods
    var pods corev1.PodList
    if err := r.List(ctx, &pods, client.InNamespace(req.Namespace), client.MatchingLabels{
        "app":  "mysql",
        "role": "master",
    }); err != nil {
        return ctrl.Result{}, err
    }

    currentReplicas := len(pods.Items)
    desiredReplicas := mysqlCluster.Spec.Replicas

    if currentReplicas < desiredReplicas {
        // Scale up
        for i := currentReplicas; i < desiredReplicas; i++ {
            newPod := corev1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                    Name:      fmt.Sprintf("mysql-%d", i),
                    Namespace: req.Namespace,
                    Labels: map[string

]string{
                        "app":  "mysql",
                        "role": "master",
                    },
                },
                Spec: corev1.PodSpec{
                    Containers: []corev1.Container{
                        {
                            Name:  "mysql",
                            Image: "mysql:5.7",
                            Ports: []corev1.ContainerPort{
                                {
                                    ContainerPort: 3306,
                                },
                            },
                        },
                    },
                },
            }
            if err := r.Create(ctx, &newPod); err != nil {
                return ctrl.Result{}, err
            }
        }
    } else if currentReplicas > desiredReplicas {
        // Scale down
        for i := desiredReplicas; i < currentReplicas; i++ {
            podName := fmt.Sprintf("mysql-%d", i)
            pod := &corev1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                    Name:      podName,
                    Namespace: req.Namespace,
                },
            }
            if err := r.Delete(ctx, pod); err != nil {
                return ctrl.Result{}, err
            }
        }
    }

    return ctrl.Result{}, nil
}

Benefits of Using Kubernetes Operators

Kubernetes Operators, built on CRDs and custom controllers, provide several benefits for managing stateful applications:

Automation: Operators automate routine tasks such as scaling, backups, and failover, reducing the operational burden on administrators.

Consistency: By encapsulating best practices and operational knowledge, Operators ensure that applications are managed consistently and reliably.

Extensibility: Operators can be extended to support custom requirements and integrate with other tools and systems.

Conclusion

Although Kubernetes is not a database in and of itself, it offers a strong framework for installing and administering database applications. A strong addition to the Kubernetes API, bespoke Resource Definitions (CRDs) allow users to design and manage bespoke resources that are suited to their particular requirements.

You may build Kubernetes Operators that automate the administration of intricate stateful applications, such as databases, by utilizing CRDs and custom controllers. This method offers more flexibility and customization, improves consistency, and streamlines processes.

This article has covered the foundations of CRDs, shown comprehensive code examples, and shown how stateful applications may be efficiently managed with CRDs. To fully utilize Kubernetes, you must comprehend and make use of CRDs, regardless of whether you are implementing databases or other sophisticated services on this potent orchestration platform.

As Kubernetes develops further, its adaptability to a variety of use cases and needs is ensured by its expansion through CRDs and Operators. Keep investigating and experimenting with these functionalities as you progress with Kubernetes to open up new avenues for your infrastructure and apps.

以上是Kubernetes 作為資料庫?關於 CRD 您需要了解的內容的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn