K8S对象-StatefulSet
目录
前言:
本文从一个简单示例开始介绍statefulSet的创建和基本用法,最后提供一个mongo集群的生产示例。
1. 概念
1.1 功能:
- 维护、管理pod的生命周期
- 运行的pod都是有状态的,切有启动顺序
1.2 一般应用
Mysql、Mongo 等有状态的服务
1.3 比较
对象 | 是否有状态 | POD数量 |
---|---|---|
Deployment | 无 | 按副本数 |
DaemonSet | 无 | (适合节点)每节点一个 |
StatefulSet | 有 | 按副本数 |
2. 简单示例基本用法
说明:nginx本身是没有状态的,这里用nginx仅是便于后边的伸缩演示,对实际生产并没有意义。
- 已有条件:
集群中已经创建了默认StorageClass。(pv会默认自动创建) - 需要创建:
HeadLessService
StatefulSet
2.1 yml文件
- HeadLessService
创建一个test.yml文件,内容如下:
apiVersion: v1
kind: Service
metadata:
name: headless-nginx
namespace: test
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
说明:headless和普通service的区别是设置 clusterIP: None,不让其获取ClusterIP , DNS解析的时候直接走pod的ip地址。
- 在test.yml文件添加如下内容(和前边service用--- 分开)
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
namespace: test
spec:
serviceName: "nginx"
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: harborcto.xxx.com.cn/public/nginx
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
说明:
volumeClaimTemplates 是创建 pvc 的样例。和pod的样例 template 是平级的。
2.2 启动和查看
[root@DoM01 statfulset]# kubectl create -f test.yml
service/headless-nginx created
statefulset.apps/web created
[root@DoM01 statfulset]# kubectl get all -n test
NAME READY STATUS RESTARTS AGE
pod/web-0 1/1 Running 0 28s
pod/web-1 1/1 Running 0 22s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/headless-nginx ClusterIP None <none> 80/TCP 25s
NAME READY AGE
statefulset.apps/web 2/2 25s
可以看到:
1)没有使用Deployment时的 replicaset来管理pod。
2)有web-0 和web-1 两个pod启动了。
3)headless-service 没有分配ip地址。
2.3 伸缩
- 增加到5个pod
[root@DoM01 ~]# kubectl scale statefulset -n test --replicas=5 web
statefulset.apps/web scaled
[root@DoM01 ~]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 20m
web-1 1/1 Running 0 20m
web-2 1/1 Running 0 18s
web-3 1/1 Running 0 13s
web-4 1/1 Running 0 7s
从时间可见,pod在按序号依次启动。
- 减少到两个
[root@DoM01 ~]# kubectl scale statefulset -n test --replicas=2 web
statefulset.apps/web scaled
[root@DoM01 ~]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 26m
web-1 1/1 Running 0 26m
web-2 1/1 Running 0 6m53s
web-3 1/1 Running 0 6m48s
web-4 0/1 Terminating 0 6m42s
[root@DoM01 ~]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
web-0 1/1 Running 0 27m
web-1 1/1 Running 0 26m
如上可见,pod按序号从后往前依次关闭。
2.4 删除
[root@DoM01 ~]# kubectl delete -n test statefulsets web
statefulset.apps "web" deleted
[root@DoM01 ~]# kubectl get pod -n test
No resources found.
[root@DoM01 ~]# kubectl get pvc -n test
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
www-web-0 Bound pvc-9dd04363-b482-45c6-a789-cfad170c1372 1Gi RWO nfs-client 37m
www-web-1 Bound pvc-ef7a4f64-1279-4640-813d-4a2707d61258 1Gi RWO nfs-client 37m
www-web-2 Bound pvc-0b6a3249-d2aa-4d8a-82a6-1fe78ce86999 1Gi RWO nfs-client 17m
www-web-3 Bound pvc-bdbf66e9-0a86-4e29-807c-44c634ad23f7 1Gi RWO nfs-client 17m
www-web-4 Bound pvc-9bb3fc9f-923e-46f9-b82c-66ec7414ea04 1Gi RWO nfs-client 17m
如上可见:
删除statefulset后,默认配置下pod会被删除。但是pvc是不会被删除的,需要我们手动删除。
3. mongdb示例
3.1 HeadLessService
- yml文件
创建service.yml文件如下:
apiVersion: v1
kind: Service
metadata:
name: headless-mongo
namespace: test
labels:
name: mongo
spec:
selector:
role: mongo
ports:
- port: 27017
targetPort: 27017
clusterIP: None
- 创建service
[root@DoM01 statfulset]# kubectl create -f service.yml
[root@DoM01 statfulset]# kubectl get service -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
headless-mongo ClusterIP None <none> 27017/TCP 32m
3.2 StatefulSet
- yml文件
创建 statefulset.yml文件如下:
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: mongo
namespace: test
spec:
serviceName: "mongo"
replicas: 3
template:
metadata:
labels:
role: mongo
environment: test
spec:
terminationGracePeriodSeconds: 10
containers:
- name: mongo
image: harbocto.boe.com.cn/public/mongo:3.4.4
command:
- mongod
- "--replSet"
- rs0
#- "--smallfiles"
#- "--noprelloc"
resources:
limits:
cpu: "1"
memory: 2Gi
requests:
cpu: "0.2"
memory: 100Mi
ports:
- containerPort: 27017
volumeMounts:
- name: mongo-persistent-storage
mountPath: /data/db
- name: mongo-sidecar
image: harbocto.boe.com.cn/public/mongo-k8s-sidecar:latest
resources:
limits:
cpu: "1"
memory: 2Gi
requests:
cpu: "0.2"
memory: 100Mi
env:
- name: MONGO_SIDECAR_POD_LABELS
value: "role=mongo,environment=test"
- name: KUBERNETES_MONGO_SERVICE_NAME
value: "mongo"
volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
- 启动
[root@DoM01 statfulset]# kubectl create -f statefulset.yml
[root@DoM01 statfulset]# kubectl get pod -n test
NAME READY STATUS RESTARTS AGE
mongo-0 2/2 Running 0 68s
mongo-1 2/2 Running 0 62s
mongo-2 2/2 Running 0 39s
如上,可见3个节点依次启动。