Longhorn,企业级云原生容器分布式存储 - K8S 资源配置示例

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: Longhorn,企业级云原生容器分布式存储 - K8S 资源配置示例

Block Volume(块卷)



apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: longhorn-block-vol
    spec:
      accessModes:
        - ReadWriteOnce
      volumeMode: Block
      storageClassName: longhorn
      resources:
        requests:
          storage: 2Gi
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: block-volume-test
      namespace: default
    spec:
      containers:
        - name: block-volume-test
          image: nginx:stable-alpine
          imagePullPolicy: IfNotPresent
          volumeDevices:
            - devicePath: /dev/longhorn/testblk
              name: block-vol
          ports:
            - containerPort: 80
      volumes:
        - name: block-vol
          persistentVolumeClaim:
            claimName: longhorn-block-vol


CSI Persistent Volume(CSI 持久卷)



apiVersion: v1
    kind: PersistentVolume
    metadata:
      name: longhorn-vol-pv
    spec:
      capacity:
        storage: 2Gi
      volumeMode: Filesystem
      accessModes:
        - ReadWriteOnce
      persistentVolumeReclaimPolicy: Delete
      storageClassName: longhorn
      csi:
        driver: driver.longhorn.io
        fsType: ext4
        volumeAttributes:
          numberOfReplicas: '3'
          staleReplicaTimeout: '2880'
        volumeHandle: existing-longhorn-volume
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: longhorn-vol-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      resources:
        requests:
          storage: 2Gi
      volumeName: longhorn-vol-pv
      storageClassName: longhorn
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: volume-pv-test
      namespace: default
    spec:
      restartPolicy: Always
      containers:
      - name: volume-pv-test
        image: nginx:stable-alpine
        imagePullPolicy: IfNotPresent
        livenessProbe:
          exec:
            command:
              - ls
              - /data/lost+found
          initialDelaySeconds: 5
          periodSeconds: 5
          timeoutSeconds: 4
        volumeMounts:
        - name: vol
          mountPath: /data
        ports:
        - containerPort: 80
      volumes:
      - name: vol
        persistentVolumeClaim:
          claimName: longhorn-vol-pvc


Deployment(部署)



apiVersion: v1
    kind: Service
    metadata:
      name: mysql
      labels:
        app: mysql
    spec:
      ports:
        - port: 3306
      selector:
        app: mysql
      clusterIP: None
    ---
    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: mysql-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: longhorn
      resources:
        requests:
          storage: 2Gi
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mysql
      labels:
        app: mysql
    spec:
      selector:
        matchLabels:
          app: mysql # has to match .spec.template.metadata.labels
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: mysql
        spec:
          restartPolicy: Always
          containers:
          - image: mysql:5.6
            name: mysql
            livenessProbe:
              exec:
                command:
                  - ls
                  - /var/lib/mysql/lost+found
              initialDelaySeconds: 5
              periodSeconds: 5
              timeoutSeconds: 4
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: changeme
            ports:
            - containerPort: 3306
              name: mysql
            volumeMounts:
            - name: mysql-volume
              mountPath: /var/lib/mysql
            env:
            - name: MYSQL_ROOT_PASSWORD
              value: "rancher"
          volumes:
          - name: mysql-volume
            persistentVolumeClaim:
              claimName: mysql-pvc


Pod with PersistentVolumeClaim(带有持久卷声明的 Pod)



apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: longhorn-volv-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: longhorn
      resources:
        requests:
          storage: 2Gi
    ---
    apiVersion: v1
    kind: Pod
    metadata:
      name: volume-test
      namespace: default
    spec:
      restartPolicy: Always
      containers:
      - name: volume-test
        image: nginx:stable-alpine
        imagePullPolicy: IfNotPresent
        livenessProbe:
          exec:
            command:
              - ls
              - /data/lost+found
          initialDelaySeconds: 5
          periodSeconds: 5
          timeoutSeconds: 4
        volumeMounts:
        - name: volv
          mountPath: /data
        ports:
        - containerPort: 80
      volumes:
      - name: volv
        persistentVolumeClaim:
          claimName: longhorn-volv-pvc


Restore to file(恢复到文件)



apiVersion: v1
    kind: Pod
    metadata:
      name: restore-to-file
      namespace: longhorn-system
    spec:
      nodeName: <NODE_NAME>
      containers:
      - name: restore-to-file
        command:
        # set restore-to-file arguments here
        - /bin/sh
        - -c
        - longhorn backup restore-to-file
          '<BACKUP_URL>'
          --output-file '/tmp/restore/<OUTPUT_FILE>'
          --output-format <OUTPUT_FORMAT>
        # the version of longhorn engine should be v0.4.1 or higher
        image: longhorn/longhorn-engine:v0.4.1
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
        volumeMounts:
        - name: disk-directory
          mountPath: /tmp/restore  # the argument <output-file> should be in this directory
        env:
        # set Backup Target Credential Secret here.
        - name: AWS_ACCESS_KEY_ID
          valueFrom:
            secretKeyRef:
              name: <S3_SECRET_NAME>
              key: AWS_ACCESS_KEY_ID
        - name: AWS_SECRET_ACCESS_KEY
          valueFrom:
            secretKeyRef:
              name: <S3_SECRET_NAME>
              key: AWS_SECRET_ACCESS_KEY
        - name: AWS_ENDPOINTS
          valueFrom:
            secretKeyRef:
              name: <S3_SECRET_NAME>
              key: AWS_ENDPOINTS
      volumes:
        # the output file can be found on this host path
        - name: disk-directory
          hostPath:
            path: /tmp/restore
      restartPolicy: Never


Simple Pod(简单 Pod)



apiVersion: v1
    kind: Pod
    metadata:
      name: longhorn-simple-pod
      namespace: default
    spec:
      restartPolicy: Always
      containers:
        - name: volume-test
          image: nginx:stable-alpine
          imagePullPolicy: IfNotPresent
          livenessProbe:
            exec:
              command:
                - ls
                - /data/lost+found
            initialDelaySeconds: 5
            periodSeconds: 5
            timeoutSeconds: 4
          volumeMounts:
            - name: volv
              mountPath: /data
          ports:
            - containerPort: 80
      volumes:
        - name: volv
          persistentVolumeClaim:
            claimName: longhorn-simple-pvc


Simple PersistentVolumeClaim(简单持久卷声明)



apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
      name: longhorn-simple-pvc
    spec:
      accessModes:
        - ReadWriteOnce
      storageClassName: longhorn
      resources:
        requests:
          storage: 1Gi


StatefulSet



apiVersion: v1
    kind: Service
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      ports:
      - port: 80
        name: web
      selector:
        app: nginx
      type: NodePort
    ---
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
      name: web
    spec:
      selector:
        matchLabels:
          app: nginx # has to match .spec.template.metadata.labels
      serviceName: "nginx"
      replicas: 2 # by default is 1
      template:
        metadata:
          labels:
            app: nginx # has to match .spec.selector.matchLabels
        spec:
          restartPolicy: Always
          terminationGracePeriodSeconds: 10
          containers:
          - name: nginx
            image: k8s.gcr.io/nginx-slim:0.8
            livenessProbe:
              exec:
                command:
                  - ls
                  - /usr/share/nginx/html/lost+found
              initialDelaySeconds: 5
              periodSeconds: 5
              timeoutSeconds: 4
            ports:
            - containerPort: 80
              name: web
            volumeMounts:
            - name: www
              mountPath: /usr/share/nginx/html
      volumeClaimTemplates:
      - metadata:
          name: www
        spec:
          accessModes: [ "ReadWriteOnce" ]
          storageClassName: "longhorn"
          resources:
            requests:
              storage: 1Gi


StorageClass



kind: StorageClass
    apiVersion: storage.k8s.io/v1
    metadata:
      name: longhorn
    provisioner: driver.longhorn.io
    allowVolumeExpansion: true
    parameters:
      numberOfReplicas: "3"
      staleReplicaTimeout: "2880" # 48 hours in minutes
      fromBackup: ""
    #  diskSelector: "ssd,fast"
    #  nodeSelector: "storage,fast"
    #  fsType: "ext4"
    #  recurringJobs: '[
    #   {
    #     "name":"snap",
    #     "task":"snapshot",
    #     "cron":"*/1 * * * *",
    #     "retain":1
    #   },
    #   {
    #     "name":"backup",
    #     "task":"backup",
    #     "cron":"*/2 * * * *",
    #     "retain":1,
    #     "labels": {
    #       "interval":"2m"
    #      }
    #   }
    #  ]'


请注意,只有 ext4 文件系统支持在卷意外分离后(detached unexpectedly)自动重新挂载。

相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
相关文章
|
3天前
|
Cloud Native Serverless 数据中心
阿里云ACK One:注册集群支持ACS算力——云原生时代的计算新引擎
ACK One注册集群已正式支持ACS(容器计算服务)算力,为企业的容器化工作负载提供更多选择和更强大的计算能力。
|
1天前
|
人工智能 Kubernetes 异构计算
大道至简-基于ACK的Deepseek满血版分布式推理部署实战
本教程演示如何在ACK中多机分布式部署DeepSeek R1满血版。
|
2月前
|
存储 Kubernetes 开发者
容器化时代的领航者:Docker 和 Kubernetes 云原生时代的黄金搭档
Docker 是一种开源的应用容器引擎,允许开发者将应用程序及其依赖打包成可移植的镜像,并在任何支持 Docker 的平台上运行。其核心概念包括镜像、容器和仓库。镜像是只读的文件系统,容器是镜像的运行实例,仓库用于存储和分发镜像。Kubernetes(k8s)则是容器集群管理系统,提供自动化部署、扩展和维护等功能,支持服务发现、负载均衡、自动伸缩等特性。两者结合使用,可以实现高效的容器化应用管理和运维。Docker 主要用于单主机上的容器管理,而 Kubernetes 则专注于跨多主机的容器编排与调度。尽管 k8s 逐渐减少了对 Docker 作为容器运行时的支持,但 Doc
170 5
容器化时代的领航者:Docker 和 Kubernetes 云原生时代的黄金搭档
|
1月前
|
关系型数据库 分布式数据库 PolarDB
[PolarDB实操课] 02.使用云起实验室资源快速体验PolarDB分布式版
本次课程由阿里云PolarDB开源架构师黄心雨分享,重点介绍如何使用云起实验室资源快速体验PolarDB分布式版。主要内容包括: 1. **PolarDB-X的四种安装方法**:Docker、PXD工具、Kubernetes和源码编译。 2. **容器技术简介**:解释容器在云原生环境中的作用,解决代码跨环境迁移问题。 3. **云起实验室实操**:通过云起实验室提供的零门槛平台,快速部署PolarDB-X,体验其主要功能。 4. **课程小结**:总结PolarDB-X的安装方式及实际操作步骤,并展望后续课程内容。
|
2月前
|
运维 Kubernetes 调度
阿里云容器服务 ACK One 分布式云容器企业落地实践
阿里云容器服务ACK提供强大的产品能力,支持弹性、调度、可观测、成本治理和安全合规。针对拥有IDC或三方资源的企业,ACK One分布式云容器平台能够有效解决资源管理、多云多集群管理及边缘计算等挑战,实现云上云下统一管理,提升业务效率与稳定性。
|
3月前
|
Kubernetes Cloud Native 微服务
云原生入门与实践:Kubernetes的简易部署
云原生技术正改变着现代应用的开发和部署方式。本文将引导你了解云原生的基础概念,并重点介绍如何使用Kubernetes进行容器编排。我们将通过一个简易的示例来展示如何快速启动一个Kubernetes集群,并在其上运行一个简单的应用。无论你是云原生新手还是希望扩展现有知识,本文都将为你提供实用的信息和启发性的见解。
|
3月前
|
Kubernetes Cloud Native 开发者
云原生入门:Kubernetes的简易指南
【10月更文挑战第41天】本文将带你进入云原生的世界,特别是Kubernetes——一个强大的容器编排平台。我们将一起探索它的基本概念和操作,让你能够轻松管理和部署应用。无论你是新手还是有经验的开发者,这篇文章都能让你对Kubernetes有更深入的理解。
|
3月前
|
运维 Kubernetes Cloud Native
云原生技术入门:Kubernetes和Docker的协同工作
【10月更文挑战第43天】在云计算时代,云原生技术成为推动现代软件部署和运行的关键力量。本篇文章将带你了解云原生的基本概念,重点探讨Kubernetes和Docker如何协同工作以支持容器化应用的生命周期管理。通过实际代码示例,我们将展示如何在Kubernetes集群中部署和管理Docker容器,从而为初学者提供一条清晰的学习路径。
|
3月前
|
Kubernetes 负载均衡 Cloud Native
探索Kubernetes:云原生应用的基石
探索Kubernetes:云原生应用的基石
|
3月前
|
Kubernetes 监控 负载均衡
深入云原生:Kubernetes 集群部署与管理实践
【10月更文挑战第37天】在数字化转型的浪潮中,云原生技术以其弹性、可扩展性成为企业IT架构的首选。本文将引导你了解如何部署和管理一个Kubernetes集群,包括环境准备、安装步骤和日常维护技巧。我们将通过实际代码示例,探索云原生世界的秘密,并分享如何高效运用这一技术以适应快速变化的业务需求。
85 1

相关产品

  • 容器服务Kubernetes版