一、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> 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐