AIOps 一场颠覆传统运维的盛筵
748
2022-11-04
k8s部署-47-StatefulSet的学习
1StatefulSet
它主要有以下两种特性:
1、顺序性 比如说后面启动的服务,需要访问前面的服务2、持久存储区分 比如说数据库之类,不止需要顺序性,而且需要数据持久存储的需要;
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
3创建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
从上面可以看到,是当springboot-web-0启动完毕,且通过健康检查之后,springboot-web-1才开始启动的,这就是我们的顺序性;
4小操作一下子
从上面可以看到,两个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:/#
发表评论
暂时没有评论,来抢沙发吧~