官方文档: https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/
服务类型 | 服务 | 应用场景 |
---|---|---|
无状态服务 | deployment | web服务 |
有状态服务 | StatefulSet | mysql、redis等 |
创建StatefulSet
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
StatefulSet错误排查
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 10s (x4 over 2m27s) default-scheduler pod has unbound immediate PersistentVolumeClaims
#查看pv的名字
kubectl get pv,pvc,pod
#查看具体错误
kubectl describe persistentvolumeclaim/www-web-0
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal FailedBinding 49s (x263 over 65m) persistentvolume-controller no persistent volumes available for this claim and no storage class is set
StatefulSet如何管理pods
删除StatefulSet
扩缩容StatefulSet
升级StatefulSet的pod