一、StatefluSet控制器
-
StatefluSet最主要的是用来部署有状态应用
-
StatefulSet部署是有状态应用,拥有统一的网络,并且有启动顺序
1.使用StatefluSet部署nginx测试启动顺序
#1.创建存放目录
[root@k8s-master1 ~]# mkdir statefluset
[root@k8s-master1 ~]# cd statefluset/
#2.创建配置清单
[root@k8s-master1 statefluset]# vim test.yaml
apiVersion: v1
kind: Service
metadata:
name: statefulset-test
spec:
ports:
- name: http
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: statefulset-test
spec: # StatefulSet控制器的spec里必须包含三个(serviceName、template、selector)
serviceName: statefulset-test
template: #模板
metadata: #元数据
labels: #标签
app: statefulset
spec:
containers:
- name: nginx
image: nginx
selector: #选择
matchExpressions: #模糊选择
- key: app #关键字app
operator: In #操作符 in是包含的意思
values:
- statefulset #指app标签中包含statefulset就可以(如果values这里包含多个值,它们之间是或的关系)
# - aaa
# - xxx
#3.部署
[root@k8s-master1 statefluset]# kubectl apply -f test.yaml
service/statefulset-test unchanged
statefulset.apps/statefulset-test created
#4.测试扩容启动顺序
[root@k8s-master1 statefluset]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
statefulset-test ClusterIP 10.100.63.1 <none> 80/TCP 8m51s
[root@k8s-master1 statefluset]# kubectl edit statefulset #编辑扩容到5个
statefulset.apps/statefulset-test edited
[root@k8s-master1 statefluset]# kubectl get pods -o wide -w #监控可观察到是先创建statefulset-test-0,等statefulset-test-0 状态为running后再创建statefulset-test-1,以此类推....
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
statefulset-test-0 1/1 Running 0 11m 10.244.1.27 gdx2 <none> <none>
statefulset-test-1 1/1 Running 0 52s 10.244.2.42 gdx3 <none> <none>
statefulset-test-2 1/1 Running 0 47s 10.244.1.28 gdx2 <none> <none>
statefulset-test-3 1/1 Running 0 43s 10.244.2.43 gdx3 <none> <none>
statefulset-test-4 1/1 Running 0 38s 10.244.1.29 gdx2 <none> <none>
#5.测试缩容启动顺序
[root@k8s-master1 statefluset]# kubectl edit statefulset #编辑缩容到1个
statefulset.apps/statefulset-test edited
[root@k8s-master1 statefluset]# kubectl get pods -o wide -w #监控可得是先结束掉statefulset-test-4,等statefulset-test-4状态为 Terminating后,再结束掉statefulset-test-3,以此类推....
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
statefulset-test-4 1/1 Terminating 0 37s 10.244.2.46 gdx3 <none> <none>
statefulset-test-4 0/1 Terminating 0 38s 10.244.2.46 gdx3 <none> <none>
statefulset-test-4 0/1 Terminating 0 44s 10.244.2.46 gdx3 <none> <none>
statefulset-test-4 0/1 Terminating 0 44s 10.244.2.46 gdx3 <none> <none>
statefulset-test-3 1/1 Terminating 0 49s 10.244.1.30 gdx2 <none> <none>
statefulset-test-3 0/1 Terminating 0 50s 10.244.1.30 gdx2 <none> <none>
statefulset-test-3 0/1 Terminating 0 62s 10.244.1.30 gdx2 <none> <none>
statefulset-test-3 0/1 Terminating 0 62s 10.244.1.30 gdx2 <none> <none>
statefulset-test-2 1/1 Terminating 0 67s 10.244.2.45 gdx3 <none> <none>
statefulset-test-2 0/1 Terminating 0 67s 10.244.2.45 gdx3 <none> <none>
statefulset-test-2 0/1 Terminating 0 68s 10.244.2.45 gdx3 <none> <none>
statefulset-test-2 0/1 Terminating 0 68s 10.244.2.45 gdx3 <none> <none>
statefulset-test-1 1/1 Terminating 0 73s 10.244.2.44 gdx3 <none> <none>
statefulset-test-1 0/1 Terminating 0 74s 10.244.2.44 gdx3 <none> <none>
statefulset-test-1 0/1 Terminating 0 75s 10.244.2.44 gdx3 <none> <none>
statefulset-test-1 0/1 Terminating 0 75s 10.244.2.44 gdx3 <none> <none>
[root@k8s-master1 statefluset]# kubectl get pods -o wide #查看所容后pod个数
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
statefulset-test-0 1/1 Running 0 20m 10.244.1.27 gdx2 <none> <none>
2.使用StatefluSet部署word press
#1.创建配置清单
[root@k8s-master1 wordpress]# vim wordpress.yaml
apiVersion: v1
kind: Service
metadata:
name: wordpress-test
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: wordpress-test
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: wordpress-test
spec:
serviceName: wordpress-test
selector:
matchLabels:
app: wordpress-test
template:
metadata:
labels:
app: wordpress-test
spec:
containers:
- name: php
image: elaina0808/lnmp-php:v6
- name: nginx
image: elaina0808/lnmp-nginx:v9
#2.部署word press
[root@k8s-master1 wordpress]# kubectl apply -f wordpress.yaml
#3.查看集群内部IP
[root@k8s-master1 wordpress]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress-test ClusterIP 10.96.102.98 <none> 80/TCP 15m
#4.内网访问
[root@k8s-master1 wordpress]# curl 10.96.102.98
<!DOCTYPE html>
<html dir='ltr'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width">
<title>WordPress › Error</title>
<style type="text/css">
html {
background: #f1f1f1;
}
#5.修改成nodeport生成外网IP
[root@k8s-master1 wordpress]# kubectl edit svc wordpress-test
service/wordpress-test edited
【type:NodePort】#把原来的clusterIP改成nodeport
#6.查看分配的IP
[root@k8s-master1 wordpress]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
wordpress-test NodePort 10.96.102.98 <none> 80:31895/TCP 18m
#7.浏览器访问
192.168.12.11:31895
二、健康检查
- 怎样保证pod中的容器正常启动?
- 怎样保证pod中容器能够正常对外提供服务?
- 只有容器启动了并且能够正常对外提供服务了,才能放到负载均衡上供给用户访问
- Kubernetes提供了健康检查服务,对于检测到故障服务会被及时自动下线,以及通过重启服务的方式使服务自动恢复。
1.存活性检查 (LivenessProbe)
- pod中所有容器的status=Running时,Pod的状态才会是Running状态。
- 判断容器是否存活,即Pod是否为running状态,如果LivenessProbe探针探测到容器不健康,则kubelet将kill掉容器,并根据容器的重启策略判断按照那种方式重启,如果一个容器不包含LivenessProbe探针,则Kubelet认为容器的LivenessProbe探针的返回值永远成功。
- 当存活性检查检测失败的时候,kebulet会删除容器,重新启动一个新的容器。继续检查。
- 存活性探测支持的方法有三种:ExecAction,TCPSocketAction,HTTPGetAction。
#1.ExecAction
kind: Service
apiVersion: v1
metadata:
name: name-mysql
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: alvinos/django:v1
livenessProbe: #存活性检查
exec:
command:
- cat
- /root/test/manage.py
#2.HTTPGetAction
kind: Service
apiVersion: v1
metadata:
name: name-mysql
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: alvinos/django:v1
livenessProbe: #存活性检查
httpGet:
port: 80
path: /index
#3.TcpSocket 相当于 ping
kind: Service
apiVersion: v1
metadata:
name: name-mysql
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: alvinos/django:v1
livenessProbe: #存活性检查
tcpSocket:
port: 80
2.健康检查参数
#1.检查失败最少次数,默认:3次
delay=10s : 探测延时时间initialDelaySeconds
timeout=1s :探测的超时时间
period=10s :探测的频率
success=1 :成功多少次才算成功
failure=1 :失败多少次才算失败
failureThreshold:最少连续几次探测失败的次数,满足该次数则认为fail
initialDelaySeconds:容器启动之后开始进行存活性探测的秒数。不填立即进行
periodSeconds:执行探测的频率(秒)。默认为10秒。最小值为1。
successThreshold:探测失败后,最少连续探测成功多少次才被认定为成功,满足该次数则认为success。(但是如果是liveness则必须是 1。最小值是 1。)
timeoutSeconds:每次执行探测的超时时间,默认1秒,最小1秒。
#2.实例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: name-mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: alvinos/django:v1
livenessProbe:
exec:
command:
- cat
- /root/test/manage.py
failureThreshold:1
initialDelaySeconds:1
periodSeconds:1
3.就绪性探测
- 就绪性探测的特点是探测失败,立即移出负载均衡(endprints —> NotReadyAddresses)
- 如果ReadinessProbe探测失败,则容器的Ready将设置为False,控制器将此Pod的Endpoint从对应的service的Endpoint列表中移除,从此不再将任何请求调度此Pod上,直到下次探测成功。
#实例:
kind: Service
apiVersion: v1
metadata:
name: readnessprobe
spec:
ports:
- name: http
port: 80
targetPort: 80
selector:
app: readnessprobe
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: readnessprobe
spec:
selector:
matchLabels:
app: readnessprobe
template:
metadata:
labels:
app: readnessprobe
spec:
containers:
- name: readnessprobe
image: alvinos/django:v1
readnessProbe:
exec:
command:
- cat
- /root/test/manage.py
4.总结
- 存活性探测:探测失败,立即删除容器
- 就绪性探测:探测失败,立即移除负载均衡