kubernetes(5):rc副本控制器—服务发现

rc副本控制器—服务发现

rc根据labels自动发现pod,并且根据设置副本数目来增删pod

创建一个pod,lebel=nginx

[root@k8s-master k8s]# cat myweb_pod.yaml 
apiVersion: v1  #版本
kind: Pod       #资源类型
metadata:       #元数据(属性)
  name: test   #pod名字
  labels:       #标签  键值对
    app: nginx
spec:           #详细
  containers:   #容器
    - name: nginx  #名字
      image: 192.168.0.136:5000/nginx:latest #镜像
      ports:
        - containerPort: 80         #端口



[root@k8s-master k8s]# kubectl create -f myweb_pod.yaml 
pod "test" created
[root@k8s-master k8s]# kubectl get  pods
NAME      READY     STATUS    RESTARTS   AGE
test      1/1       Running   0          18s
[root@k8s-master k8s]#

 

创建一个rc,lebel=myweb

[root@k8s-master k8s]# cat myweb-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb  #rc的名字
spec:
  replicas: 3  #副本数,即集群数目
  selector:
    app: myweb #选择器,如果容器里是app-nginx归它管理
  template:    #模板
    metadata:  
      labels:  #标签
        app: myweb
    spec:
      containers:
      - name: nginx
        image: 192.168.0.136:5000/nginx:latest
        ports:
        - containerPort: 80
[root@k8s-master k8s]# kubectl create -f myweb-rc.yaml 
replicationcontroller "myweb" created
[root@k8s-master k8s]#
[root@k8s-master k8s]# kubectl get  pods -o wide
NAME          READY     STATUS    RESTARTS   AGE       IP            NODE
myweb-1p9tm   1/1       Running   0          14s       172.16.14.3   k8s-node-2
myweb-2h14p   1/1       Running   0          15s       172.16.14.2   k8s-node-2
myweb-xjrsn   1/1       Running   0          16s       172.16.73.3   k8s-node-1
test          1/1       Running   0          4m        172.16.73.2   k8s-node-1
[root@k8s-master k8s]#

 

修改pod的lebel标签Lebel=myweb

[root@k8s-master k8s]# kubectl edit pod test 
pod "test" edited
[root@k8s-master k8s]# kubectl describe pod test | grep Labels
Labels:         app=myweb
[root@k8s-master k8s]#

 

 

rc根据Lebel自动发现

[root@k8s-master k8s]# kubectl get  pods               
NAME          READY     STATUS    RESTARTS   AGE
myweb-2h14p   1/1       Running   0          3m
myweb-xjrsn   1/1       Running   0          3m
test          1/1       Running   0          6m
[root@k8s-master k8s]#

 

我们发现少了一个pod,Lebel和rc一致的pod,会被rc自动发现,并根据设置的副本数目进行删减

一般rc会认为pod存活久的比较稳定,删除新的pod。

 

小结

控制器设置最少保持3个副本

rc控制器是根据Labels 来区分组别的

test pod的标签和myweb控制器的标签一样,所以就删除了存活时间最短的容器

 

posted on 2019-08-26 09:50  光阴8023  阅读(227)  评论(0编辑  收藏  举报