Kubernetes 1.20.5实验记录–StatefulSet控制器
1、配置NFS服务器:
略
2、Worker节点安装nfs-utils:
yum -y install nfs-utils
3、创建PV:
文件pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsstor
server: 192.168.0.100
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsstor
server: 192.168.0.100
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv2
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsstor
server: 192.168.0.100
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv3
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsstor
server: 192.168.0.100
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv4
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsstor
server: 192.168.0.100
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv5
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
storageClassName: nfs
nfs:
path: /nfsstor
server: 192.168.0.100
kubectl apply -f pv.yaml
4、查看PV状态:
kubectl get pv
创建6个PV,分别给6个Pod使用
5、创建StatefulSet:
文件statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
run: statefulset
name: statefulset
spec:
serviceName: service
replicas: 2
selector:
matchLabels:
run: statefulset
template:
metadata:
labels:
run: statefulset
spec:
terminationGracePeriodSeconds: 10
containers:
- name: statefulset
image: nginx
ports:
- containerPort: 80
name: port
volumeMounts:
- name: pvc
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
kubectl apply -f statefulset.yaml
6、查看StatefulSet状态:
kubectl get statefulset
7、查看StatefulSet详细信息:
kubectl describe statefulset statefulset
8、查看Pod状态:
kubectl get pod -o wide
Pod被顺序创建,并有唯一序号标识
9、查看PVC状态:
kubectl get pvc
10、创建Service:
文件service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: service
name: service
spec:
ports:
- port: 80
name: service-port
selector:
run: statefulset
type: ClusterIP
kubectl apply -f service.yaml
11、查看Service状态:
kubectl get service
12、创建网页文件:
13、测试Service:
curl 10.103.177.124
14、对StatefulSet进行Scale Up:
文件statefulset-scaleup.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
run: statefulset
name: statefulset
spec:
serviceName: service
replicas: 6
selector:
matchLabels:
run: statefulset
template:
metadata:
labels:
run: statefulset
spec:
terminationGracePeriodSeconds: 10
containers:
- name: statefulset
image: nginx
ports:
- containerPort: 80
name: port
volumeMounts:
- name: pvc
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
kubectl apply -f statefulset-scaleup.yaml
15、查看Pod状态:
kubectl get pod -o wide
Pod被顺序创建,并有唯一序号标识
16、查看PVC状态:
kubectl get pvc
17、对StatefulSet进行Rolling Update:
文件statefulset-rollingupdate.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
run: statefulset
name: statefulset
spec:
serviceName: service
replicas: 6
selector:
matchLabels:
run: statefulset
template:
metadata:
labels:
run: statefulset
spec:
terminationGracePeriodSeconds: 10
containers:
- name: statefulset
image: nginx:1.18
ports:
- containerPort: 80
name: port
volumeMounts:
- name: pvc
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
kubectl apply -f statefulset-rollingupdate.yaml
kubectl get pod -w
Pod被倒序删除及创建,唯一序号保持不变
18、删除Service:
kubectl delete -f service.yaml
19、删除StatefulSet:
kubectl delete -f statefulset.yaml
20、查看PVC状态:
kubectl get pvc
21、删除PVC:
kubectl delete pvc pvc-statefulset-0
kubectl delete pvc pvc-statefulset-1
kubectl delete pvc pvc-statefulset-2
kubectl delete pvc pvc-statefulset-3
kubectl delete pvc pvc-statefulset-4
kubectl delete pvc pvc-statefulset-5
22、查看PV状态:
kubectl get pv
23、删除PV:
kubectl delete -f pv.yaml