k8s攻略之有状态服务StatefulSet
有状态服务:比较常见的mongo复制集,redis cluster,rabbitmq cluster等等。使用StatefulSet 进行创建。
无状态服务:相互之前都是独立的,很适合用横向扩充来增加服务的资源量。通常使用Deployment、DaemonSet进行创建。很多无状态应用也通过 StatefulSet 来管理。
创建pv
root@node1:~# cat web-pv.yaml
# mkdir -p /nfs_dir/{web-pv0,web-pv1}
apiVersion: v1
kind: PersistentVolume
metadata:
name: web-pv0
labels:
type: web-pv0
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: my-storage-class
nfs:
path: /nfs_dir/web-pv0
server: 10.0.1.201
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: web-pv1
labels:
type: web-pv1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: my-storage-class
nfs:
path: /nfs_dir/web-pv1
server: 10.0.1.201
[root@node-1 wgw-tmp]# kubectl apply -f web-pv01.yaml
persistentvolume/web-pv0 created
persistentvolume/web-pv1 created
[root@node-1 wgw-tmp]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 1Gi RWO Recycle Bound default/pvc1 nfs 20h
web-pv0 1Gi RWO Retain Available my-storage-class 10s
web-pv1 1Gi RWO Retain Available my-storage-class 10s
2、创建pvc
PVC_name = volumeClaimTemplates_name + "-" + pod_name
www-web-0 = www + "-" + web-0
www-web-1 = www + "-" + web-1
~# cat web-pvc.yaml :
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: www-web-0
spec:
accessModes:
ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: my-storage-class
selector:
matchLabels:
type: web-pv0
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: www-web-1
spec:
accessModes:
ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: my-storage-class
selector:
matchLabels:
type: web-pv1
创建pvc
[root@node-1 wgw-tmp]# kubectl apply -f web-pvc.yaml
persistentvolumeclaim/www-web-0 created
persistentvolumeclaim/www-web-1 created
[root@node-1 wgw-tmp]#
[root@node-1 wgw-tmp]#
[root@node-1 wgw-tmp]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pvc-sc Pending nfs-boge 20h
pvc1 Bound pv1 1Gi RWO nfs 20h
www-web-0 Bound web-pv0 1Gi RWO my-storage-class 15s
www-web-1 Bound web-pv1 1Gi RWO my-storage-class 15s
3、创建Service 和 StatefulSet
~# cat web.yaml :
apiVersion: v1
kind: Service
metadata:
name: web-headless
labels:
app: nginx
spec:
ports:
port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: v1
kind: Service
metadata:
name: web
labels:
app: nginx
spec:
ports:
port: 80
name: web
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 2 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
containerPort: 80
name: web
volumeMounts:
name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
kubectl apply -f web05.yaml
service/web-headless unchanged
service/web created
statefulset.apps/web created
kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.68.0.1 <none> 443/TCP 114d
mysvc ClusterIP 10.68.164.5 <none> 80/TCP 10d
nginx ClusterIP 10.68.50.26 <none> 80/TCP 7d22h
web ClusterIP 10.68.55.99 <none> 80/TCP 96s
web-headless ClusterIP None <none> 80/TCP 2m19s
[root@node-1 wgw-tmp]# kubectl get statefulsets.apps -o wide
NAME READY AGE CONTAINERS IMAGES
web 2/2 2m20s nginx nginx
# kubectl get pod -o wide
web-0 1/1 Running 0 3m10s 172.20.139.79 10.0.1.203 <none> <none>
web-1 1/1 Running 0 3m8s 172.20.139.78 10.0.1.203 <none> <none>
校验
[ ]
[ ]
[ ]
this is web-1
[ ]
this is web-0
[ ]
this is web-0
[ ]
wgw frist comeing
[ ]
this is web-1