Replication Controller

 1# replica.yaml
 2---
 3apiVersion: v1
 4kind: ReplicationController
 5metadata:
 6  name: first-replic
 7spec:
 8  replicas: 3
 9  template:
10    metadata:
11      name: simple-pod
12      labels:
13        app: simple-pod
14    spec:
15      containers:
16        - name: timemachine
17          image: lukelau/rest-docker:0.0.1 
18          args:
19            - -server.addr=0.0.0.0:8000

以上配置会保证Pod的数量稳定为3个。当我们删除一个Pod之后,Replication Controller就会创建出一个新的Pod来维持Pod的数量。

RC之所以会发现Pod已经挂掉了,是因为探针(Container probes)的存在。在K8s中, kubelet会通过指定的探针方式去探测容器是否存活。

三种探针方式

三种类型的handler

  • ExecAction: Executes a specified sommand inside the container. The diagnostic is considered successful if the command exits with a status code of 0.
  • CPSocketAction: Performs a TCP check against the container’s IP adress on a specified port. The diagnostic is consider successful if the port is open.
  • TPGetAction: Performs and HTTP GET request against the Container’s IP adderess on a specified port and path. The diagnostic is considered successful if the response has a status code grater than or equal to 200 and less than 400.

当你的Pod的健康探针探测发现Pod的不健康次数超过设定的次数的时候,那么RC就会将这个有问题的Pod删除(没有restart操作),然后再创建出一个新的来。RC还会检测Pod的当前数量,如果不足则会创建,如果小于则会关掉一些Pod。

ReplicaSet

ReplicaSet 的Selector会比ReplicationController强大一些。

 1---
 2apiVersion: apps/v1
 3kind: ReplicaSet
 4metadata:
 5  name: first-replic-set
 6spec:
 7  selector:
 8    matchLabels:
 9      app: simple-pod-set
10  replicas: 3
11
12  template:
13    metadata:
14      name: simple-pod-set
15      labels:
16        app: simple-pod-set
17    spec:
18      containers:
19        - name: timemachine
20          image: lukelau/rest-docker:0.0.1 
21          args:
22            - -server.addr=0.0.0.0:8000

selector 变得复杂了,除了matchLabels之外,还支持matchExpressions。

Deployment已经将内置的replica集从ReplicationController转成ReplicaSet了。