StatefulSet是为了管理有状态服务的问题而设计的
kubectl explain statefulset
KIND: StatefulSet
VERSION: apps/v1
DESCRIPTION:
StatefulSet represents a set of pods with consistent identities. Identities
are defined as:
- Network: A single stable DNS and hostname.
- Storage: As many VolumeClaims as requested. The StatefulSet guarantees
that a given network identity will always map to the same storage identity.
FIELDS:
apiVersion <string> #定义statefulset资源需要使用的api版本
kind <string> #定义的资源类型
metadata <Object> #元数据
spec <Object> #定义容器相关的信息
kubectl explain statefulset.spec
KIND: StatefulSet
VERSION: apps/v1
RESOURCE: spec <Object>
DESCRIPTION:
Spec defines the desired identities of pods in this set.
A StatefulSetSpec is the specification of a StatefulSet.
FIELDS:
podManagementPolicy <string> #pod管理策略
replicas <integer> #副本数
revisionHistoryLimit <integer> #保留的历史版本
selector <Object> -required- #标签选择器,选择它所关联的pod
serviceName <string> -required- #headless service的名字
template <Object> -required- #生成pod的模板
updateStrategy <Object> #更新策略
volumeClaimTemplates <[]Object> #存储卷申请模板
对于template而言,其内部定义的就是pod,pod模板是一个独立的对象
kubectl explain statefulset.spec.template
KIND: StatefulSet
VERSION: apps/v1
RESOURCE: template <Object>
DESCRIPTION:
template is the object that describes the pod that will be created if
insufficient replicas are detected. Each pod stamped out by the StatefulSet
will fulfill this Template, but have a unique identity from the rest of the
StatefulSet.
PodTemplateSpec describes the data a pod should have when created from a
template
FIELDS:
metadata <Object>
spec <Object> #定义容器属性的
通过上面可以看到,statefulset资源中有两个spec字段。
第一个spec声明的是statefulset定义多少个Pod副本(默认将仅部署1个Pod)、匹配Pod标签的选择器、创建pod的模板、存储卷申请模板
第二个spec是spec.template.spec:主要用于Pod里的容器属性等配置。
.spec.template里的内容是声明Pod对象时要定义的各种属性,所以这部分也叫做PodTemplate(Pod模板)。
还有一个值得注意的地方是:在.spec.selector中定义的标签选择器必须能够匹配到spec.template.metadata.labels里定义的Pod标签,否则Kubernetes将不允许创建statefulset。
编写一个Statefulset资源清单文件
cat statefulset.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
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: "nfs"
resources:
requests:
storage: 1Gi
kubectl apply -f statefulset.yaml
kubectl get statefulset
NAME READY AGE
web 2/2 42s
kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 2m17s
web-1 1/1 Running 0 115s
查看headless service
kubectl get svc -l app=nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx ClusterIP None <none> 80/TCP 3m19s
查看pvc
kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pvc-39a9755f-3248-49ff-8f9e-5b068b609c8f 1Gi RWO,RWX nfs-web 7m45s
www-web-1 Bound pvc-be93d4a3-1aca-44cc-802f-ddeb38c05018 1Gi RWO,RWX nfs-web 7m41s
查看pv
kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-39a9755f-3248-49ff-8f9e-5b068b609c8f 1Gi RWO,RWX Delete Bound default/www-web-0 nfs-web 8m3s
pvc-be93d4a3-1aca-44cc-802f-ddeb38c05018 1Gi RWO,RWX Delete Bound default/www-web-1 nfs-web 7m59s
查看pod主机名
for i in 0 1; do kubectl exec web-$i -- sh -c 'hostname';done
web-0
web-1
Service_NAME.NameSpace_NAME.Domain.LTD.
Domain.LTD.=svc.cluster.local. #这是默认k8s集群的域名
FQDN 全称 Fully Qualified Domain Name
即全限定域名:同时带有主机名和域名的名称
FQDN = Hostname + DomainName
主机名是 hahaha
域名是 baidu.com
FQDN= hahaha.baidu.com
StatefulSet会为关联的Pod保持一个不变的Pod Name
statefulset中Pod的名字格式为$(StatefulSet name)-$(pod序号)
StatefulSet会为关联的Pod分配一个dnsName
$<Pod Name>.$<service name>.$<namespace name>.svc.cluster.local
对于有状态应用都会用到持久化存储,比如mysql主从,由于主从数据库的数据是不能存放在一个目录下的,每个mysql节点都需要有自己独立的存储空间。而在deployment中创建的存储卷是一个共享的存储卷,多个pod使用同一个存储卷,它们数据是同步的,而statefulset定义中的每一个pod都不能使用同一个存储卷,这就需要使用volumeClainTemplate,当在使用statefulset创建pod时,volumeClainTemplate会自动生成一个PVC,从而请求绑定一个PV,每一个pod都有自己专用的存储卷。Pod、PVC和PV对应的关系图如下:
使用kubectl run运行一个提供nslookup命令的容器的,这个命令来自于dnsutils包,通过对pod主机名执行nslookup,可以检查它们在集群内部的DNS地址:
kubectl run busybox --image docker.io/library/busybox:1.28 --image-pull-policy=IfNotPresent --restart=Never --rm -it busybox -- sh
root@web-1:/# nslookup web-0.nginx.default.svc.cluster.local
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: web-0.nginx.default.svc.cluster.local
Address: 10.244.209.154 #解析的是pod的ip地址
root@web-1:/# nslookup nginx.default.svc.cluster.local
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: nginx.default.svc.cluster.local #查询service dns,会把对应的pod ip解析出来
Address: 10.244.209.139
Name: nginx.default.svc.cluster.local
Address: 10.244.209.140
dig -t A nginx.default.svc.cluster.local @10.96.0.10
格式如下:
@来指定域名服务器
A 为解析类型 ,A记录
-t 指定要解析的类型
A记录:
A记录是解析域名到IP
通过deployment创建pod,pod前端创建一个service
cat deploy-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
type: ClusterIP
ports:
- port: 80 #service的端口,暴露给k8s集群内部服务访问
protocol: TCP
targetPort: 80 #pod容器中定义的端口
selector:
run: my-nginx #选择拥有run=my-nginx标签的pod
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: busybox
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
command:
- sleep
- "3600"
kubectl apply -f deploy-service.yaml
kubectl get svc -l run=my-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S)
my-nginx ClusterIP 10.100.89.90 <none> 80/TCP
kubectl get pods -l run=my-nginx
NAME READY STATUS RESTARTS AGE
my-nginx-58f74fc5b6-jzbvk 1/1 Running 0 70s
my-nginx-58f74fc5b6-n9lqv 1/1 Running 0 53s
#通过上面可以看到deployment创建的pod是随机生成的
#进入到web-1的pod
kubectl exec -it web-1 -- /bin/bash
root@web-1:/# nslookup my-nginx.default.svc.cluster.local
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: my-nginx.default.svc.cluster.local
Address: 10.100.89.90 #解析的是service的ip地址
如果我们觉得两个副本太少了,想要增加,只需要修改配置文件statefulset.yaml里的replicas的值即可,原来replicas: 2,现在变成replicaset: 3,修改之后,执行如下命令更新:
vim statefulset.yaml
kubectl apply -f statefulset.yaml
kubectl get sts
NAME READY AGE
web 3/3 60m
kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 61m
web-1 1/1 Running 0 60m
web-2 1/1 Running 0 79s
也可以直接编辑控制器实现扩容
kubectl edit sts web
#这个是我们把请求提交给了apiserver,实时修改
把上面的spec下的replicas 后面的值改成4,保存退出
kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 62m
web-1 1/1 Running 0 62m
web-2 1/1 Running 0 3m13s
web-3 1/1 Running 0 26s
如果我们觉得4个Pod副本太多了,想要减少,只需要修改配置文件statefulset.yaml里的replicas的值即可,把replicaset:4变成replicas: 2,修改之后,执行如下命令更新:
vim statefulset.yaml
kubectl apply -f statefulset.yaml
kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 64m
web-1 1/1 Running 0 64m
kubectl explain sts.spec.updateStrategy
vim statefulset.yaml
在一个终端动态查看pod
kubectl get pods -l app=nginx -w
另一个终端执行如下命令:
kubectl apply -f statefulset.yaml
kubectl get pods -l app=nginx -w
出现的结果如下:
web-0 1/1 Running 0 10m
web-1 1/1 Running 0 10m
web-1 1/1 Terminating 0 10m
web-1 1/1 Terminating 0 10m
web-1 0/1 Terminating 0 10m
web-1 0/1 Terminating 0 10m
web-1 0/1 Terminating 0 10m
web-1 0/1 Pending 0 0s
web-1 0/1 Pending 0 0s
web-1 0/1 ContainerCreating 0 0s
web-1 0/1 ContainerCreating 0 1s
web-1 1/1 Running 0 2s
web-1 1/1 Running 0 11s
从上面结果可以看出来,pod在更新的时候,只是更新了web-1这个pod, partition: 1表示更新的时候会把pod序号大于等于1的进行更新
如果更新策略是OnDelete,那不会自动更新pod,需要手动删除,重新常见的pod才会实现更新