10. kubernetes资源——statefulset有状态负载

kubernetes资源------statefulset有状态负载

一、statfulset有状态负载

1、无状态、有状态区别

  • 无状态负载

    支持副本、滚动更新, 适用于频繁更新的业务

    从业务角度来说,不需要客户端任何信息

  • 有状态负载

    适用于部署数据库

    从业务角度,服务端在运行期间,需要记录客户端状态信息(令牌、会话)、典型的动态网站、数据库

2、创建有状态负载

2.1 创建pv、pvc持久化
复制代码
apiVersion: v1
kind: PersistentVolume
metadata:
    name: db-pv-5g
spec:
    capacity:
        storage: 5G
    accessModes:
        - ReadWriteMany
    persistentVolumeReclaimPolicy: Recycle
    nfs:
       server: "192.168.140.13"
       path: "/db/data"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
    name: db-pvc-5g
spec:
    accessModes:
        - ReadWriteMany
    resources:
        requests:
            storage: 5G
复制代码
[root@k8s-master statefulTest]# kubectl get pv
NAME       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   VOLUMEATTRIBUTESCLASS   REASON   AGE
db-pv-5g   5G         RWX            Recycle          Bound    default/db-pvc-5g                  <unset>                          19s
[root@k8s-master statefulTest]# 
[root@k8s-master statefulTest]# kubectl get pvc
NAME        STATUS   VOLUME     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
db-pvc-5g   Bound    db-pv-5g   5G         RWX                           <unset>                 22s
2.2 创建有状态负载
复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
    name: test-mysql
spec:
    replicas: 1
    selector:
        matchLabel:
            app: mysql
    serviceName: "test-mysql"
    template:
        metadata:
            labels:
               app: mysql
        spec:
            containers:
            - name: test-mysql
              image: mysql:5.7
              imagePullPolicy: IfNotPresent
              env:
              - name: MYSQL_ROOT_PASSWORD
                value: redhat
              volumeMounts:
                 - name: mysql-data-volume
                   mountPath: /var/lib/mysql
            volumes:
              - name: mysql-data-volume
                persistentVolumeClaim:
                    claimName: db-pvc-5g
---
apiVersion: v1
kind: Service
metadata:
    name: test-mysql
spec:
    ports:
    - port: 3306
    selector:
        app: mysql
复制代码
[root@k8s-master statefulTest]# kubectl get pod
NAME           READY   STATUS    RESTARTS   AGE
test-mysql-0   1/1     Running   0          6s

[root@k8s-master statefulTest]# kubectl get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP    125d
test-mysql   ClusterIP   10.96.61.172   <none>        3306/TCP   48s
2.3 测试有状态负载

删除数据库pod

复制代码
[root@k8s-master statefulTest]# kubectl get pod 
NAME           READY   STATUS    RESTARTS   AGE
test-mysql-0   1/1     Running   0          2m17s
[root@k8s-master statefulTest]# 
[root@k8s-master statefulTest]# 
[root@k8s-master statefulTest]# kubectl delete pod test-mysql-0 
pod "test-mysql-0" deleted

k8s会自动重建、登录mysql查看数据

复制代码
// 再次查看pod,名称不变
[root@k8s-master statefulTest]# kubectl get pod
NAME           READY   STATUS    RESTARTS   AGE
test-mysql-0   1/1     Running   0          16s

// 登录数据库查看数据
[root@k8s-master statefulTest]# kubectl exec -ti test-mysql-0 bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@test-mysql-0:/# mysql -uroot -predhat
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| A                  |
| B                  |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.02 sec)

mysql> 
相关推荐
_Walli_2 小时前
k8s集群搭建(七)-------- 微服务间的调用
微服务·容器·kubernetes
马达加斯加D3 小时前
k8s --- resource: Pod, ReplicaSet and Deployment
云原生·容器·kubernetes
Candice_jy7 小时前
vscode运行ipynb文件:使用docker中的虚拟环境
服务器·ide·vscode·python·docker·容器·编辑器
CS创新实验室8 小时前
从穿孔卡片到云原生:批处理系统的不朽演进与核心思想
云原生·操作系统·批处理
檐下翻书1739 小时前
Spring Boot 深度剖析:从虚拟线程到声明式 HTTP 客户端,再到云原生最优解
spring boot·http·云原生
roman_日积跬步-终至千里9 小时前
【Docker下部署高可用】StarRocks 存算一体架构高可用部署要点
docker·容器·架构
zmjjdank1ng9 小时前
k8s问答题(二)
云原生·容器·kubernetes
卡奥斯开源社区官方9 小时前
2025 实战指南:WebAssembly 重塑云原生开发 —— 从前端加速到后端革命的全栈落地
前端·云原生·wasm
万博智云OneProCloud10 小时前
SmartX 联合万博智云发布云原生异构容灾解决方案白皮书(附下载)
云原生·云容灾·hyperbdr云容灾·灾备系统