StatefulSet

StatefulSet:

1、稳点且唯一的网络标识符

2、稳点且持久的存储

3、有序、平滑的部署和扩展

4、有序、平滑的删除和终止

5、有序的滚动更新

 

三个组件组成:headless(无头服务)    service、StatefuSet、voluneClaimTemplate(存储卷申请模板)

 

 

##需要建好pv存储


apiVersion: v1
kind: Service
metadata:
name: myapp-svc
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: myapp
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: my-statefulset
spec:
serviceName: myapp-svc
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: mystat-pod
image: ikubernetes/myapp:v1
ports:
- containerPort: 80
name: web
volumeMounts:
- name: myappdata
mountPath: /usr/share/nginx/html
volumeClaimTemplates: ##会动态创建pvc
- metadata:
name: myappdata
spec:
accessModes: ["ReadWriteOnce"]
# storageClassName: "gluster-dybanic"
resources:
requests:
storage: 2Gi


[root@k8s-m ~]# kubectl apply -f srtateful-demo.yaml
service/myapp-svc created
statefulset.apps/my-statefulset created

 

##查看

[root@k8s-m ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv01 2Gi RWO,RWX Retain Bound default/myappdata-my-statefulset-0 2d
pv02 5Gi RWO,RWX Retain Available 2d
pv03 10Gi RWO,RWX Retain Bound default/mypvc 2d
pv04 20Gi RWO,RWX Retain Available 2d
[root@k8s-m ~]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
myappdata-my-statefulset-0 Bound pv01 2Gi RWO,RWX 3m
mypvc Bound pv03 10Gi RWO,RWX 2d
[root@k8s-m ~]# kubectl get sts
NAME DESIRED CURRENT AGE
my-statefulset 1 1 3m

##删除pod的话,pvc不会被删除

 

 

 

#上面粘贴的格式有误

 

构建pv与StatefulSet

#创建目录
mkdir  /data/{v1,v2,v3,v4,v5} -p
#安装nfs
 yum install -y nfs-utils
#更改所有者
chown  -R nfsnobody.nfsnobody  /data/
#配置nfs文件
[root@k8s-m yaml]# cat  /etc/exports
/data/v1  172.31.0.0/20(rw,sync)
/data/v2  172.31.0.0/20(rw,sync)
/data/v3  172.31.0.0/20(rw,sync)
/data/v4  172.31.0.0/20(rw,sync)
/data/v5  172.31.0.0/20(rw,sync)
#启动nfs
systemctl  start nfs
#查看挂载资源
[root@k8s-m yaml]# showmount  -e 127.0.0.1
Export list for 127.0.0.1:
/data/v5 172.31.0.0/20
/data/v4 172.31.0.0/20
/data/v3 172.31.0.0/20
/data/v2 172.31.0.0/20
/data/v1 172.31.0.0/20




#创建pv
[root@k8s-m yaml]# cat mypv.yaml 
apiVersion: v1
kind: PersistentVolume
metadata:
   name: pv01
spec:
  nfs:
    path: /data/v1
    server: k8s-m
  accessModes: ["ReadWriteMany","ReadWriteOnce"] 
  capacity:
    storage: 2Gi
---
apiVersion: v1
kind: PersistentVolume
metadata:
   name: pv02
spec:
  nfs:
    path: /data/v2
    server: k8s-m
  accessModes: ["ReadWriteMany"] 
  capacity:
    storage: 2Gi

#导入
kubectl apply -f  mypv.yaml
#创建statefulset
[root@k8s-m yaml]#  cat mystatefulset.yaml 
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: web
spec:
  serviceName: "nginx"
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 2Gi

#导入
kubectl apply -f  mystatefulset.yaml
#查看
[root@k8s-m yaml]# kubectl  get pvc
NAME        STATUS   VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Bound    pv01     2Gi        RWO,RWX                       5s
[root@k8s-m yaml]# kubectl  get statefulset
NAME   READY   AGE
web    1/1     20s
[root@k8s-m yaml]# kubectl  get pod
NAME                            READY   STATUS    RESTARTS   AGE
myapp-deploy-59fddd9797-6px2d   1/1     Running   0          117m
myapp-deploy-59fddd9797-scvdl   1/1     Running   0          117m
myapp-deploy-59fddd9797-zrk95   1/1     Running   0          117m
web-0                           1/1     Running   0          24s

  

 

posted @ 2018-09-08 13:11  巽逸  阅读(234)  评论(0编辑  收藏  举报