kubernetes快速入门10-StatefulSet控制器
2021-01-02 05:27
标签:ports 组件 appdata set 声明 creat 格式 sts ide 有状态副本集。 使用要求: StatefulSet由三个组件组成: StatefulSet帮助信息,statefulset可简写为sts 先准备存储和pv,存储利用NFS实现 准备配置清单 每一个pod的名称是固定的,删除一个pod后重建后的名称也是不会发生变化,pvc的名称里隐含了pod的名称,如:myappdata-myapp-0,所以可持续的为同一个pod提供存储卷服务。 StatefulSet资源也可以进行扩容和缩容,只要有空闲的pv就可进行扩容。 先设置升级分区边界 在实际生产环境中把有状态的应用迁移到k8s要慎重,可以多参考google上已实现方案,这里有个redis的事例:https://github.com/CommercialTribe/kube-redis kubernetes快速入门10-StatefulSet控制器 标签:ports 组件 appdata set 声明 creat 格式 sts ide 原文地址:https://blog.51cto.com/zhaochj/2533339
KIND: StatefulSet
VERSION: apps/v1
spec
k8s@node01:~/my_manifests/volumes$ cat pv-nfs.yaml
apiVersion: v1
kind: PersistentVolume
metadata: # PV资源不设置namespace
name: pv001
labels:
name: pv001
speed: slow
spec:
nfs:
path: /data/nfs/volume/v1
server: node01.k8s.com
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata: # PV资源不设置namespace
name: pv002
labels:
name: pv002
speed: medium
spec:
nfs:
path: /data/nfs/volume/v2
server: node01.k8s.com
accessModes: ["ReadWriteMany","ReadWriteOnce"]
capacity:
storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata: # PV资源不设置namespace
name: pv003
labels:
name: pv003
speed: medium
spec:
nfs:
path: /data/nfs/volume/v3
server: node01.k8s.com
accessModes: ["ReadWriteOnce"]
capacity:
storage: 2Gi
k8s@node01:~/my_manifests/volumes$ kubectl apply -f pv-nfs.yaml
persistentvolume/pv001 created
persistentvolume/pv002 created
persistentvolume/pv003 created
k8s@node01:~/my_manifests/volumes$ kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv001 2Gi RWO,RWX Retain Available 3s
pv002 2Gi RWO,RWX Retain Available 3s
pv003 2Gi RWO Retain Available 3s
k8s@node01:~/my_manifests/statefullset$ cat sts-myapp-pods.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
namespace: default
labels:
app: myapp-svc
spec:
clusterIP: None # 无头service,使用selector对应的标签选择相应的pods
selector:
app: myapp-pods
ports:
- name: http
port: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: myapp
spec:
replicas: 2
serviceName: myapp-svc
selector:
matchLabels:
app: myapp-pods
template:
metadata:
labels:
app: myapp-pods # pods使用的标签
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: web
containerPort: 80
volumeMounts:
- name: myappdata # pod中挂载卷的名称,与volumeClaimTemplates中的名称对应
mountPath: /usr/share/nginx/html/
volumeClaimTemplates: # 卷申请模板,当pod启动时会以此模板创建相应的pvc
- metadata:
name: myappdata
spec:
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 2Gi
k8s@node01:~/my_manifests/statefullset$ kubectl apply -f sts-myapp-pods.yaml
service/myapp-svc created
statefulset.apps/myapp created
# pod的名称为配置清单中“pod名称+编号”
k8s@node01:~/my_manifests/statefullset$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-0 1/1 Running 0 4m28s 10.244.2.105 node03
# 两种方式都可以实现扩容和缩容
k8s@node01:~$ kubectl scale sts myapp --replicas=3
k8s@node01:~$ kubectl patch sts myapp -p ‘{"spec":{"replicases":2}}‘
StatefulSet中pod更新
k8s@node01:~$ kubectl get sts
NAME READY AGE
myapp 3/3 10h
k8s@node01:~$ kubectl describe sts myapp
Name: myapp
Namespace: default
CreationTimestamp: Wed, 29 Jul 2020 22:34:06 +0800
Selector: app=myapp-pods
Labels:
下一篇:Http请求中如何保持状态?
文章标题:kubernetes快速入门10-StatefulSet控制器
文章链接:http://soscw.com/index.php/essay/39506.html