k8s学习(十)创建StatefuleSet

目录


前言

Deployments 和 ReplicaSets 是为无状态服务而设计,StatefulSet 是为有状态服务而设计。

应用场景:

    稳定的持久化存储,即Pod重新调度后还是能访问到相同的持久化数据,基于PVC来实现

    稳定的网络标志,即Pod重新调度后其PodName和HostName不变,基于Headless Service(即没有Cluster IP的Service)来实现

    有序部署,有序扩展,即Pod是有顺序的,在部署或者扩展的时候要依据定义的顺序依次依次进行(即从0到N-1,在下一个Pod运行之前所有之前的Pod必须都是Running和Ready状态),基于init containers来实现

    有序收缩,有序删除(即从N-1到0)


参考之前的文章,首先安装nfs和storageclass。

创建statefulset :

1、nginx-statefulset-test.yaml

[root@k8s-master k8s]# cat nginx-statefulset-test.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-storageclass-service
  labels:
    app: nginx-service-test
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: nginx-service-test
  selector:
    app: nginx-service-test
  type: ClusterIP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx-statefuleset01
  labels:
    app: nginx-storageclass-test
spec:
  serviceName: "nginx-storageclass-service"
  replicas: 2
  selector:
    matchLabels:
      app: nginx-service-test
  template:
    metadata:
      labels:
        app: nginx-service-test
    spec:
      containers:
      - name: nginx-service-test
        image: nginx:latest
        ports:
        - containerPort: 80
          name: nginx-s-c
        volumeMounts:
        - name: web
          mountPath: /usr/share/nginx/html
      volumes:
      - name: web
        persistentVolumeClaim:
          claimName: pvc-test-01

2、创建

[root@k8s-master k8s]# kubectl create -f nginx-statefulset-test.yaml
service/nginx-storageclass-service created
statefulset.apps/nginx-statefuleset01 created

3、查看

[root@k8s-master k8s]# kubectl get svc
NAME                         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes                   ClusterIP   10.96.0.1        <none>        443/TCP        7d1h
nginx-storageclass-service   ClusterIP   10.103.227.6     <none>        80/TCP         3m50s
[root@k8s-master k8s]# kubectl get statefulset
NAME                   READY   AGE
nginx-statefuleset01   2/2     4m19s
[root@k8s-master k8s]# kubectl get rs
NAME                                     DESIRED   CURRENT   READY   AGE
nfs-client-provisioner-test-59bf4c676f   1         1         1       13h
[root@k8s-master k8s]# kubectl get pods
NAME                                           READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-test-59bf4c676f-5wdpf   1/1     Running   0          13h
nginx-statefuleset01-0                         1/1     Running   0          13h
nginx-statefuleset01-1                         1/1     Running   0          13h

4、在nfs共享目录下创建文件

 cd /data/nfsdata/default-pvc-test-01-pvc-4cdc0549-c6ce-4f77-8816-a49df5627d98/

创建 index.html

[root@k8s-master default-pvc-test-01-pvc-4cdc0549-c6ce-4f77-8816-a49df5627d98]# cat index.html
<h1>hello,nfs storageclass !</h1>

5、访问service

可以看出,访问到的是共享目录下index.html文件

[root@k8s-master k8s]# curl 10.103.227.6:80
<h1>hello,nfs storageclass !</h1>

6、查看 statefulset 详细信息

[root@k8s-master k8s]# kubectl describe statefulset nginx-statefuleset01
Name:               nginx-statefuleset01
Namespace:          default
CreationTimestamp:  Sat, 25 Dec 2021 10:59:56 -0500
Selector:           app=nginx-service-test
Labels:             app=nginx-storageclass-test
Annotations:        <none>
Replicas:           2 desired | 2 total
Update Strategy:    RollingUpdate
  Partition:        0
Pods Status:        2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx-service-test
  Containers:
   nginx-service-test:
    Image:        nginx:latest
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:
      /usr/share/nginx/html from web (rw)
  Volumes:
   web:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  pvc-test-01
    ReadOnly:   false
Volume Claims:  <none>
Events:         <none>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_lrs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值