k8s入门_RC、RS、Deployment

RC

什么是RC:

Replication Controller(副本控制器),RC能够保证pod在任意时间运行的副本数量,能够保证pod总是可用的。
RC控制的pod的多个副本,每个副本都有独立的ip,并且支持pod副本数量的扩、缩容。
 

RC定义文件格式:

这里还是以nginx为例,认识最简单的rc配置文件。 每一行配置都有详细的解释
#api版本
apiVersion: v1
#对象资源类型 RC
kind: ReplicationController
#RC元数据
metadata:
    #对象资源名称
    name: nginx
#RC的详细描述
spec:
    #维持pod的共数量
    replicas: 3
    #RC选择器,指定对哪个Pod使用rc
    selector:
        #label 标签,选择有此 label 的 Pod
        app: nginx 
    # 定义创建 Pod 实例的模板
    template:
        metadata:
            name: nginx 
            # Pod 的 label,对应上面 rc 的 selector 
            labels:
                app: nginx
        spec:
            containers: 
            # 定义 Pod 中的容器
            - name: nginx
              image: nginx
              ports:
              - containerPort: 80
View Code

 

RC常用基本操作

创建rc,其中rc_demo.yml是上面rc的定义文件

[root@k8s-01 pod_demo]# kubectl create -f rc_demo.yml 
replicationcontroller/nginx created

查询创建的rc以及rc对应的pod、container

[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
nginx 3 3 3 9m56s nginx nginx app=nginx

[root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx
nginx-bwpbx                        1/1     Running   0          7m13s   10.244.2.33   k8s-03   <none>           <none>
nginx-qgj22                        1/1     Running   0          7m13s   10.244.1.56   k8s-02   <none>           <none>
nginx-vpz8h                        1/1     Running   0          7m13s   10.244.2.34   k8s-03   <none>           <none>
[root@k8s-01 pod_demo]# kubectl describe pod nginx-bwpbx
Name:           nginx-bwpbx

删除rc,下面两种删除rc的方式都可以

[root@k8s-01 pod_demo]# kubectl get rc -o wide    
NAME    DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
nginx   3         3         3       9m56s   nginx        nginx    app=nginx
[root@k8s-01 pod_demo]# kubectl delete rc nginx
replicationcontroller "nginx" deleted
[root@k8s-01 pod_demo]# kubectl get rc -o wide 
No resources found.
[root@k8s-01 pod_demo]# kubectl delete -f rc_demo.yml  

rc副本扩、缩容。将原始副本状态为3的rc扩容到4再缩容到2

[root@k8s-01 pod_demo]# kubectl get rc -o wide        
NAME    DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES   SELECTOR
nginx   3         3         0       5s    nginx        nginx    app=nginx
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME    DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES   SELECTOR
nginx   3         3         3       77s   nginx        nginx    app=nginx
[root@k8s-01 pod_demo]# kubectl scale rc nginx --replicas=4
replicationcontroller/nginx scaled
[root@k8s-01 pod_demo]# kubectl get rc -o wide             
NAME    DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES   SELECTOR
nginx   4         4         3       2m9s   nginx        nginx    app=nginx
[root@k8s-01 pod_demo]# kubectl get rc -o wide
NAME    DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
nginx   4         4         4       2m43s   nginx        nginx    app=nginx
[root@k8s-01 pod_demo]# kubectl scale rc nginx --replicas=2
replicationcontroller/nginx scaled
[root@k8s-01 pod_demo]# kubectl get rc -o wide             
NAME    DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
nginx   2         2         2       2m50s   nginx        nginx    app=nginx

修改文件配置重新应用也能够修改副本数量

[root@k8s-01 pod_demo]# kubectl get rc -o wide        
NAME    DESIRED   CURRENT   READY   AGE     CONTAINERS   IMAGES   SELECTOR
nginx   2         2         2       4m56s   nginx        nginx    app=nginx
[root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx
nginx-bp8rd                        1/1     Running   0          5m7s   10.244.1.57   k8s-02   <none>           <none>
nginx-k2r5j                        1/1     Running   0          5m7s   10.244.2.35   k8s-03   <none>           <none>
[root@k8s-01 pod_demo]# cat rc_demo.yml|grep replicas      
    replicas: 3
[root@k8s-01 pod_demo]# kubectl apply -f rc_demo.yml       
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
replicationcontroller/nginx configured
[root@k8s-01 pod_demo]# kubectl get pods -o wide|grep nginx
nginx-bp8rd                        1/1     Running   0          6m1s   10.244.1.57   k8s-02   <none>           <none>
nginx-k2r5j                        1/1     Running   0          6m1s   10.244.2.35   k8s-03   <none>           <none>
nginx-lrlw2                        1/1     Running   0          42s    10.244.2.37   k8s-03   <none>           <none>
[root@k8s-01 pod_demo]# kubectl get rc -o wide              
NAME    DESIRED   CURRENT   READY   AGE    CONTAINERS   IMAGES   SELECTOR
nginx   3         3         3       6m9s   nginx        nginx    app=nginx

 

RS

RS(Replication Set)和RC的功能基本一致,目前唯一的一个区别就是RC只支持基于等式的selector(env=dev或environment!=qa),但RS还支持基于集合的selector(version in (v1.0, v2.0)),这对复杂的运维管理就非常方便了。Label后面的随笔会介绍,简单的说就是为资源打标签,然后通过标签查询,比如:一个 【长得帅】、【有钱】、【单身】的人。

使用上面RC的定义文件改造的RS定义文件

#api版本,注意这里和RC不一样
apiVersion: apps/v1
#对象资源类型 RC
kind: ReplicaSet
#RC元数据
metadata:
    #对象资源名称
    name: nginx-rs
#RC的详细描述
spec:
    #维持pod的共数量
    replicas: 3
    #RS选择器,指定对哪个Pod
    selector:
        #相比RC,选择器这里多了一层逻辑,可以满足更复杂的选择器场景
        matchLabels:
            app: nginx 
    # 定义创建 Pod 实例的模板
    template:
        metadata:
            name: nginx 
            # Pod 的 label,对应上面 rs 的 selector 
            labels:
                app: nginx
        spec:
            containers: 
            # 定义 Pod 中的容器
            - name: nginx
              image: nginx
              ports:
              - containerPort: 80

RS常用操作命令

#创建、查询RS
[root@k8s-01 ~]# kubectl create -f RS_demo.yml 
replicaset.apps/nginx-rs created
[root@k8s-01 ~]# kubectl get rs -o wide 
NAME                         DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES      SELECTOR
nginx-rs                     3         3         3       55s   nginx        nginx       app=nginx
#查询RS详细内容,包含其关联的pod副本详细信息
[root@k8s-01 ~]# kubectl describe rs nginx-rs
Name:         nginx-rs
Namespace:    default
Selector:     app=nginx
Labels:       <none>
Annotations:  <none>
Replicas:     3 current / 3 desired
Pods Status:  3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  2m57s  replicaset-controller  Created pod: nginx-rs-l6cnq
  Normal  SuccessfulCreate  2m57s  replicaset-controller  Created pod: nginx-rs-vvqh7
  Normal  SuccessfulCreate  2m57s  replicaset-controller  Created pod: nginx-rs-9jmcj
#删除一个pod模拟pod故障RS自愈。
[root@k8s-01 ~]# kubectl delete pod nginx-rs-9jmcj
pod "nginx-rs-9jmcj" deleted
[root@k8s-01 ~]# kubectl describe rs nginx-rs|grep nginx-rs
Name:         nginx-rs
  Normal  SuccessfulCreate  5m49s  replicaset-controller  Created pod: nginx-rs-l6cnq
  Normal  SuccessfulCreate  5m49s  replicaset-controller  Created pod: nginx-rs-vvqh7
  Normal  SuccessfulCreate  5m49s  replicaset-controller  Created pod: nginx-rs-9jmcj
  Normal  SuccessfulCreate  33s    replicaset-controller  Created pod: nginx-rs-9spdp
[root@k8s-01 ~]# kubectl get pod|grep nginx-rs
nginx-rs-9spdp                     1/1     Running   0          59s
nginx-rs-l6cnq                     1/1     Running   0          6m15s
nginx-rs-vvqh7                     1/1     Running   0          6m15s
#RS的Pod副本扩缩容,两种方式:1、kubectl命令如下  2、修改文件kubectl apply -f yml
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
nginx-rs                     3         3         3       8m32s   nginx        nginx       app=nginx
[root@k8s-01 ~]# kubectl scale rs nginx-rs --replicas=4
replicaset.extensions/nginx-rs scaled
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs  
nginx-rs                     4         4         3       9m10s   nginx        nginx       app=nginx
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
nginx-rs                     4         4         4       12m   nginx        nginx       app=nginx
#删除rs
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs
nginx-rs                     4         4         4       12m   nginx        nginx       app=nginx
[root@k8s-01 ~]# kubectl delete rs nginx-rs
replicaset.extensions "nginx-rs" deleted
[root@k8s-01 ~]# kubectl get rs -o wide|grep nginx-rs

Deployment

相比于RS,Deployment增加了升级方式的定义,并且实际生产环境也多使用Deployment。

根据上面RS定义文件修改的Deployment定义文件

#api版本
apiVersion: apps/v1
#对象资源类型 Deployment
kind: Deployment
#元数据
metadata:
    #对象资源名称
    name: nginx-d
    #deployment的labels
    labels:
        app: nginx
#RC的详细描述
spec:
    #维持pod的共数量
    replicas: 3
    #RC选择器,指定对哪个Pod使用rc
    selector:
        #label 标签,选择有此 label的为app: nginx-p的Pod
        matchLabels:
            app: nginx 
    #定义deployment的升级策略
    strategy:
        #表示升级时是将源所有pod删除后使用新的template信息创建pod
        type: Recreate
        #滚动升级配置,升级时使用template新建一个pod然后将旧Pod挑选一个停服删除,持续滚动,直到所有pod更新完毕
        #type: rollintUpdate
            #maxSurge: 1
            #maxUnavailable: 1
    # 定义创建 Pod 实例的模板
    template:
        metadata:
            name: nginx 
            # Pod 的 label,对应上面 rc 的 selector 
            labels:
                app: nginx
        spec:
            containers: 
            # 定义 Pod 中的容器
            - name: nginx
              image: nginx:1.18
              ports:
              - containerPort: 80

deployment常用基本操作

#创建、查询deployment
[root@k8s-01 ~]# kubectl create -f deployment_demo.yml
deployment.apps/nginx-d created
[root@k8s-01 ~]# kubectl get deployment -o wide
NAME              READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES       SELECTOR
nginx-d           3/3     3            3           4m23s   nginx        nginx:1.18   app=nginx
#kubectl replace -f filename,如果存在同名deployment则进行替换

#deployment详情查看其关联的RS
[root@k8s-01 ~]# kubectl describe deployment nginx-d
Name:               nginx-d
Namespace:          default
CreationTimestamp:  Fri, 01 Oct 2021 22:04:26 +0800
Labels:             app=nginx
Annotations:        deployment.kubernetes.io/revision: 1
Selector:           app=nginx
Replicas:           3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:       Recreate
MinReadySeconds:    0
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.18
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-d-659bf7c684 (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  8m16s  deployment-controller  Scaled up replica set nginx-d-659bf7c684 to 3

#查看deployment对应的pod信息
[root@k8s-01 ~]# kubectl get rs nginx-d-659bf7c684 -o wide
NAME                 DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES       SELECTOR
nginx-d-659bf7c684   3         3         3       10m   nginx        nginx:1.18   app=nginx,pod-template-hash=659bf7c684
[root@k8s-01 ~]# kubectl describe rs nginx-d-659bf7c684    
Name:           nginx-d-659bf7c684
Namespace:      default
Selector:       app=nginx,pod-template-hash=659bf7c684
Labels:         app=nginx
                pod-template-hash=659bf7c684
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 3
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/nginx-d
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
           pod-template-hash=659bf7c684
  Containers:
   nginx:
    Image:        nginx:1.18
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  10m   replicaset-controller  Created pod: nginx-d-659bf7c684-qj5gh
  Normal  SuccessfulCreate  10m   replicaset-controller  Created pod: nginx-d-659bf7c684-sqkjr
  Normal  SuccessfulCreate  10m   replicaset-controller  Created pod: nginx-d-659bf7c684-cxwsc
[root@k8s-01 ~]# 

#删除pod构造deployment自愈
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d
nginx-d-659bf7c684-cxwsc           1/1     Running   0          11m   10.244.2.46   k8s-03   <none>           <none>
nginx-d-659bf7c684-qj5gh           1/1     Running   0          11m   10.244.2.45   k8s-03   <none>           <none>
nginx-d-659bf7c684-sqkjr           1/1     Running   0          11m   10.244.1.66   k8s-02   <none>           <none>
[root@k8s-01 ~]# kubectl delete pod nginx-d-659bf7c684-cxwsc
pod "nginx-d-659bf7c684-cxwsc" deleted
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d       
nginx-d-659bf7c684-qj5gh           1/1     Running   0          14m    10.244.2.45   k8s-03   <none>           <none>
nginx-d-659bf7c684-sqkjr           1/1     Running   0          14m    10.244.1.66   k8s-02   <none>           <none>
nginx-d-659bf7c684-z4jjz           1/1     Running   0          119s   10.244.2.47   k8s-03   <none>           <none>

#pod副本数量扩、缩容。还是有两种方法,这里只演示命令行方式
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d       
nginx-d-659bf7c684-qj5gh           1/1     Running   0          14m    10.244.2.45   k8s-03   <none>           <none>
nginx-d-659bf7c684-sqkjr           1/1     Running   0          14m    10.244.1.66   k8s-02   <none>           <none>
nginx-d-659bf7c684-z4jjz           1/1     Running   0          119s   10.244.2.47   k8s-03   <none>           <none>
[root@k8s-01 ~]# 
[root@k8s-01 ~]# kubectl scale deployment nginx-d --replicas=4
deployment.extensions/nginx-d scaled
[root@k8s-01 ~]# kubectl get pod -o wide|grep nginx-d         
nginx-d-659bf7c684-d6qhr           1/1     Running   0          18s     10.244.1.67   k8s-02   <none>           <none>
nginx-d-659bf7c684-qj5gh           1/1     Running   0          16m     10.244.2.45   k8s-03   <none>           <none>
nginx-d-659bf7c684-sqkjr           1/1     Running   0          16m     10.244.1.66   k8s-02   <none>           <none>
nginx-d-659bf7c684-z4jjz           1/1     Running   0          4m16s   10.244.2.47   k8s-03   <none>           <none>

#image版本升降级。升降级也有两种方式,最好通过方式2更新。将镜像从1.1.8升级到latest版本
#方式1:kubectl set image deployment [dName] [imageNmae]=[新的镜像]
#方式2:kubectl apply -f [deployment 新文件] --record 
[root@k8s-01 ~]# kubectl get deployment -o wide
NAME              READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES       SELECTOR
nginx-d           4/4     4            4           21m   nginx        nginx:1.18   app=nginx
[root@k8s-01 ~]# kubectl apply -f deployment_demo.yml --record 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/nginx-d configured
[root@k8s-01 ~]# kubectl get deployment -o wide               
NAME              READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES      SELECTOR
nginx-d           3/3     3            3           22m   nginx        nginx       app=nginx

#查看deployment的升级记录
[root@k8s-01 ~]# kubectl rollout history deployment nginx-d
deployment.extensions/nginx-d 
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl apply --filename=deployment_demo.yml --record=true

#升级回滚,回滚到上一个版本,nginx有变回1.18版本
[root@k8s-01 ~]# kubectl rollout undo deployment nginx-d
deployment.extensions/nginx-d rolled back
[root@k8s-01 ~]# kubectl get deployment -o wide            
NAME              READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES       SELECTOR
nginx-d           3/3     3            3           25m   nginx        nginx:1.18   app=nginx

 

posted @ 2021-10-01 22:31  flag_HW  阅读(1204)  评论(0编辑  收藏  举报