美文网首页k8s那点事儿Kubernetes
kubernetes Pod控制器 ReplicaSet

kubernetes Pod控制器 ReplicaSet

作者: baiyongjie | 来源:发表于2019-01-30 11:03 被阅读10次

    ReplicaSet控制器

    ReplicationController用来确保容器应用的副本数始终保持在用户定义的副本数,即如果有容器异常退出,会自动创建新的Pod来替代;而如果异常多出来的容器也会自动回收,属于旧版本的控制器,涉及面太复杂.

    在新版本的Kubernetes中建议使用ReplicaSet来取代ReplicationController。ReplicaSet跟ReplicationController没有本质的不同,只是名字不一样,并且ReplicaSet支持集合式的selector。

    虽然ReplicaSet可以独立使用,但一般还是建议使用 Deployment 来自动管理ReplicaSet,这样就无需担心跟其他机制的不兼容问题(比如ReplicaSet不支持rolling-update但Deployment支持),而且Deployment支持滚动升级及回滚

    命令行查看ReplicaSet清单定义规则

    kubectl explain replicaSet
    kubectl explain replicaSet.spec
    

    ReplicaSet资源清单

    [root@k8s-master01 learning]# vim replicaSet-demo.yaml 
    apiVersion: apps/v1 #api版本定义
    kind: ReplicaSet    #定义资源类型为ReplicaSet
    metadata:           #元数据定义
      name: rs-demo
      namespace: learning
    spec:               #ReplicaSet的规格定义
      replicas: 2       #定义副本数量为2个
      selector:         #标签选择器,定义匹配pod的标签
        matchLabels:
          app: rs-demo
          tag: learn-rs
      template:         #pod的模板定义
        metadata:       #pod的元数据定义
          name: rs-demo #自定义pod的名称 
          labels:       #定义pod的标签,和上面保持一致或多余上面
            app: rs-demo
            tag: learn-rs
        spec:           #pod的规格定义  
          containers:   #容器定义
            - name: rs-demo       #容器名称
              image: nginx:1.10   #容器镜像
              ports:              #暴露端口
                - name: http
                  containerPort: 80
    
    [root@k8s-master01 learning]# kubectl create namespace learning
    namespace/learning created
    
    [root@k8s-master01 learning]# kubectl apply -f replicaSet-demo.yaml  
    replicaset.apps/rs-demo created
    

    ReplicaSet常用命令

    查看资源

    # 查看现有rs
    [root@k8s-master01 learning]# kubectl get replicasets.apps  -n learning 
    NAME      DESIRED   CURRENT   READY   AGE
    rs-demo   2         2         2       4m44s
    
    # 查看详细信息
    [root@k8s-master01 learning]# kubectl describe replicasets. -n learning  rs-demo 
    Name:         rs-demo
    Namespace:    learning
    Selector:     app=rs-demo,tag=learn-rs
    Labels:       <none>
    Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                    {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"name":"rs-demo","namespace":"learning"},"spec":{"replicas":5,"se...
    Replicas:     5 current / 5 desired
    Pods Status:  5 Running / 0 Waiting / 0 Succeeded / 0 Failed
    Pod Template:
      Labels:  app=rs-demo
               tag=learn-rs
      Containers:
       rs-demo:
        Image:        nginx:1.11
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Events:
      Type    Reason            Age   From                   Message
      ----    ------            ----  ----                   -------
      Normal  SuccessfulCreate  22s   replicaset-controller  Created pod: rs-demo-5pd54
      Normal  SuccessfulCreate  22s   replicaset-controller  Created pod: rs-demo-gndz6
      Normal  SuccessfulCreate  22s   replicaset-controller  Created pod: rs-demo-p479c
      Normal  SuccessfulCreate  22s   replicaset-controller  Created pod: rs-demo-bkc28
      Normal  SuccessfulCreate  22s   replicaset-controller  Created pod: rs-demo-x57kj
    

    修改Pod数量

    # 修改pod的副本数量
    # (1) 通过配置文件修改
    [root@k8s-master01 learning]# sed -i 's/  replicas: 2/  replicas: 5/g' replicaSet-demo.yaml 
    [root@k8s-master01 learning]# kubectl apply -f replicaSet-demo.yaml 
    replicaset.apps/rs-demo configured
    [root@k8s-master01 learning]# kubectl get rs -n learning 
    NAME      DESIRED   CURRENT   READY   AGE
    rs-demo   5         5         5       7m37s
    [root@k8s-master01 learning]# kubectl get pods -n learning 
    NAME            READY   STATUS    RESTARTS   AGE
    rs-demo-h29s7   1/1     Running   0          10s
    rs-demo-k778n   1/1     Running   0          7m5s
    rs-demo-mmvpz   1/1     Running   0          10s
    rs-demo-pw9l8   1/1     Running   0          10s
    rs-demo-s7q75   1/1     Running   0          7m5s
    
    # (2) edit直接编辑修改
    [root@k8s-master01 learning]# kubectl edit -n learning replicasets.apps rs-demo
    直接修改replicas字段为想要的pod个数, wq保存退出
    [root@k8s-master01 learning]# kubectl -n learning  get replicasets.
    NAME      DESIRED   CURRENT   READY   AGE
    rs-demo   3         3         3       12m
    

    修改镜像版本

    # 查看现有的
    [root@k8s-master01 learning]# kubectl -n learning  get replicasets. -o wide
    NAME      DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
    rs-demo   3         3         3       13m   rs-demo      nginx:1.10   app=rs-demo,tag=learn-rs
    
    # (1) 通过配置文件修改
    [root@k8s-master01 learning]# sed -i 's/          image: nginx:1.10/          image: nginx:1.11/g' replicaSet-demo.yaml
    [root@k8s-master01 learning]# kubectl apply -f replicaSet-demo.yaml 
    replicaset.apps/rs-demo configured
    [root@k8s-master01 learning]# kubectl -n learning  get replicasets. -o wide
    NAME      DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
    rs-demo   5         5         5       29m   rs-demo      nginx:1.11   app=rs-demo,tag=learn-rs
    
    # (2) edit直接编辑修改
    [root@k8s-master01 learning]# kubectl edit -n learning replicasets.apps rs-demo
    直接修改image字段为想要的pod个数, wq保存退出
    [root@k8s-master01 learning]# kubectl -n learning  get replicasets. -o wide
    NAME      DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
    rs-demo   5         5         5       38m   rs-demo      nginx:1.12   app=rs-demo,tag=learn-rs
    

    相关文章

      网友评论

        本文标题:kubernetes Pod控制器 ReplicaSet

        本文链接:https://www.haomeiwen.com/subject/iovqsqtx.html