k8s部署-47-StatefulSet的学习

发布一下 0 0
k8s部署-47-StatefulSet的学习



StatefulSet


它主要有以下两种特性:

1、顺序性  比如说后面启动的服务,需要访问前面的服务2、持久存储区分  比如说数据库之类,不止需要顺序性,而且需要数据持久存储的需要;

创建service

在这里我们是headless-server,他是没有具体的IP地址的,是使用域名的方式来访问的,看下;

[root@node1 ~]# cd namespace/[root@node1 namespace]# mkdir statefulset[root@node1 namespace]# cd statefulset/[root@node1 statefulset]# cat headless-service.yaml apiVersion: v1kind: Servicemetadata:  name: springboot-web-svcspec:  ports:  - port: 80    targetPort: 8080    protocol: TCP  clusterIP: None  selector:    app: springboot-web [root@node1 statefulset]# [root@node1 statefulset]# kubectl apply -f headless-service.yaml service/springboot-web-svc created[root@node1 statefulset]# kubectl get serviceNAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGEkubernetes           ClusterIP   10.233.0.1   <none>        443/TCP   2m5sspringboot-web-svc   ClusterIP   None         <none>        80/TCP    7s[root@node1 statefulset]#

创建pod

在这里我们创建pod的时候,有一个参数是serviceName,这里我们要写上上面service的名字;

[root@node1 statefulset]# vim statefulset.yamlapiVersion: apps/v1kind: StatefulSetmetadata:  name: springboot-webspec:  serviceName: springboot-web-svc  replicas: 2  selector:    matchLabels:      app: springboot-web  template:    metadata:      labels:        app: springboot-web    spec:      containers:      - name: springboot-web        image: registry.cn-beijing.aliyuncs.com/yunweijia0909/springboot-web:v1        ports:        - containerPort: 8080        livenessProbe:          tcpSocket:            port: 8080          initialDelaySeconds: 20          periodSeconds: 10          failureThreshold: 3          successThreshold: 1          timeoutSeconds: 5        readinessProbe:          httpGet:            path: /hello?name=test            port: 8080            scheme: HTTP          initialDelaySeconds: 20          periodSeconds: 10          failureThreshold: 1          successThreshold: 1          timeoutSeconds: 5[root@node1 statefulset]# [root@node1 statefulset]# kubectl apply -f  statefulset.yaml statefulset.apps/springboot-web created[root@node1 statefulset]# 


然后我们使用-w参数,实施跟踪下pod的变化;

[root@node1 statefulset]# kubectl get pod -o wide -wNAME               READY   STATUS    RESTARTS   AGE   IP             NODE    NOMINATED NODE   READINESS GATESspringboot-web-0   0/1     Running   0          8s    10.200.135.6   node3   <none>           <none>springboot-web-0   1/1     Running   0          26s   10.200.135.6   node3   <none>           <none>springboot-web-1   0/1     Pending   0          0s    <none>         <none>   <none>           <none>springboot-web-1   0/1     Pending   0          0s    <none>         node3    <none>           <none>springboot-web-1   0/1     ContainerCreating   0          0s    <none>         node3    <none>           <none>springboot-web-1   0/1     ContainerCreating   0          1s    <none>         node3    <none>           <none>springboot-web-1   0/1     Running             0          1s    10.200.135.5   node3    <none>           <none>springboot-web-1   1/1     Running             0          22s   10.200.135.5   node3    <none>           <none>^C[root@node1 statefulset]# kubectl get pod -o wideNAME               READY   STATUS    RESTARTS   AGE   IP             NODE    NOMINATED NODE   READINESS GATESspringboot-web-0   1/1     Running   0          64s   10.200.135.6   node3   <none>           <none>springboot-web-1   1/1     Running   0          38s   10.200.135.5   node3   <none>           <none>[root@node1 statefulset]#


从上面可以看到,是当springboot-web-0启动完毕,且通过健康检查之后,springboot-web-1才开始启动的,这就是我们的顺序性;

小操作一下子

从上面可以看到,两个pod都运行在了node3上,然后我们登录到node3上去看看;

[root@node3 ~]# crictl ps | grep springboot-webe0019775cca37       8ad32427177e4       5 minutes ago       Running             springboot-web             0                   add1689b2e708d11c9c74bd1ba       8ad32427177e4       5 minutes ago       Running             springboot-web             0                   7748dbde4cd47[root@node3 ~]# crictl exec -it e0019775cca37 bashroot@springboot-web-1:/# hostnamespringboot-web-1root@springboot-web-1:/# exit[root@node3 ~]# crictl exec -it d11c9c74bd1ba bashroot@springboot-web-0:/# hostnamespringboot-web-0root@springboot-web-0:/# exit[root@node3 ~]#


可以看到主机名,一个是0,一个是1;

那么,我们如何访问到另一个pod呢?规则是如下:

PodName.ServiceName.Namespace


例如:

root@springboot-web-0:/# curl springboot-web-1.springboot-web-svc.default:8080/hello?name=yunweijia;echo ""Hello yunweijia! I'm springboot-web-demo controller!root@springboot-web-0:/#


亦或者,自己访问自己;

root@springboot-web-0:/# curl springboot-web-0.springboot-web-svc.default:8080/hello?name=yunweijia;echo ""Hello yunweijia! I'm springboot-web-demo controller!root@springboot-web-0:/#


当然了,由于我们的命名空间是default,那么忽略不写也是可以的,如下:

root@springboot-web-0:/# curl springboot-web-1.springboot-web-svc:8080/hello?name=yunweijia;echo ""Hello yunweijia! I'm springboot-web-demo controller!root@springboot-web-0:/# curl springboot-web-0.springboot-web-svc:8080/hello?name=yunweijia;echo ""Hello yunweijia! I'm springboot-web-demo controller!root@springboot-web-0:/#

持久存储

首先我们要声明,上一篇中,由于系统资源的不足,我们没有成功创建GlusterFS,但是这里持久化存储要用到GlusterFS,所以只写过程,结果就不展示了;

# 先把上面的资源删除[root@node1 statefulset]# kubectl delete -f statefulset.yaml statefulset.apps "springboot-web" deleted[root@node1 statefulset]## 然后创建内容如下[root@node1 statefulset]# vim statefulset-volume.yaml apiVersion: apps/v1kind: StatefulSetmetadata:  name: springboot-webspec:  serviceName: springboot-web-svc  replicas: 2  selector:    matchLabels:      app: springboot-web  template:    metadata:      labels:        app: springboot-web    spec:      containers:      - name: springboot-web        image: registry.cn-beijing.aliyuncs.com/yunweijia0909/springboot-web:v1        ports:        - containerPort: 8080        livenessProbe:          tcpSocket:            port: 8080          initialDelaySeconds: 20          periodSeconds: 10          failureThreshold: 3          successThreshold: 1          timeoutSeconds: 5        readinessProbe:          httpGet:            path: /hello?name=test            port: 8080            scheme: HTTP          initialDelaySeconds: 20          periodSeconds: 10          failureThreshold: 1          successThreshold: 1          timeoutSeconds: 5        volumeMounts:        - name: data          mountPath: /yunweijia-data  volumeClaimTemplates:  - metadata:      name: data    spec:      accessModes:      - ReadWriteOnce      storageClassName: glusterfs-storage-class      resources:        requests:          storage: 1Gi[root@node1 statefulset]#


这里的不同之处是volumeClaimTemplates,他会自动创建pvc,需要注意一下;

然后运行一下;

剩余内容请转至VX公众号 “运维家” ,回复 “153” 查看。

------ 以下内容为防伪内容,忽略即可 ------

------ 以下内容为防伪内容,忽略即可 ------

------ 以下内容为防伪内容,忽略即可 ------

it运维工程师怎么样运维工程师日常工作表高级运维工程师工资杭州安全运维工程师要怎么学北京运维工程师驻场开发多少钱机场运维工程师工作机房运维工程师认证华数传媒运维工程师实施运维工程师总结与计划手游运维工程师一天都干嘛运维检查工程师运维工程师相关专业长沙空气站运维工程师北京中关新才运维工程师培训旺采网运维工程师it运维工程师评级it系统维运工程师招聘华为机房运维工程师阿里高级运维工程师面试云计算运维工程师是程序员吗

版权声明:内容来源于互联网和用户投稿 如有侵权请联系删除

本文地址:http://0561fc.cn/97879.html