在写yaml文件的时候,如果kind:Pod, 那么删除运行的pod后,这个pod服务就会消失,这是自主的pod,而如果创建pod控制器,可以在删除pod后自主恢复。
Replicaset
deployment是比replicaset更高级的管理方式,deployment不直接管理pod对象,而是由deployment管理replicaset,再由replicaset管理pod对象。
replicaset主要由三部分组成:
- 用户期望的副本数。
- 标签选择器。
- pod资源模板。
从下面的yaml文件就可以看出,template指出将新创建的pod都打上frontend的标签就可以加入该replicaset的管理。
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
namespace: default
labels:
app: guestbook
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
tier1: frontend1
template:
metadata:
labels:
tier1: frontend1
spec:
containers:
- name: php-redis
image: docker.io/yecc/gcr.io-google_samples-gb-frontend:v3
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
livenessProbe:
httpGet:
scheme: HTTP
port: 80
path: /
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 10
sucessThreshold:
failureThreshold:
startupProbe:
httpGet:
scheme: HTTP
port: 80
path: /
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 10
sucessThreshold:
failureThreshold:
readinessProbe:
httpGet:
scheme: HTTP
port: 80
path: /
initialDelaySeconds: 20
periodSeconds: 5
timeoutSeconds: 10
sucessThreshold:
failureThreshold:
此时删掉一个pod,就会有一个pod自动补上,所以要删除所有的pod,需要删除这个replicaset。
修改pod副本数量
现在三个pod副本正在运行,如果我想修改pod数量,那么我修改replicaset的yaml文件,设置副本数为2,是会删掉三个pod,新建两个,还是随机删掉目前的一个pod呢。
记录下目前三个pod的id
kubectl get pods -owide
>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
frontend-bkpjv 1/1 Running 0 44s 10.244.104.16 node2 <none> <none>
frontend-kn2vn 1/1 Running 0 44s 10.244.166.157 node1 <none> <none>
frontend-zj7r4 1/1 Running 0 44s 10.244.166.158 node1 <none> <none>
修改yaml文件后,apply,查看结果,发现随机删除了一个pod
kubectl get pods -owide
>
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
frontend-kn2vn 1/1 Running 0 11m 10.244.166.157 node1 <none> <none>
frontend-zj7r4 1/1 Running 0 11m 10.244.166.158 node1 <none> <none>
设置反亲和性
以上我们看到3个副本有两个在同一个节点上,这不太好,我们修改yaml文件,设置反亲和性以及容忍污点。
在yaml文件中修改增加以下部分:
template:
metadata:
labels:
app: frontend1
tier1: frontend1
spec:
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values: ["frontend1"]
topologyKey: kubernetes.io/hostname
然后就发现有一个副本出现在master节点上
ubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
frontend-pcxd7 1/1 Running 0 12m 10.244.166.159 node1 <none> <none>
frontend-tck5p 1/1 Running 0 12m 10.244.104.18 node2 <none> <none>
frontend-vdxt4 1/1 Running 0 12m 10.244.219.67 master <none> <none>