1、RC(Replication Controller)副本控制器,Replication Controller的作用。

  应用托管在kubernetes之后,kubernetes需要保证应用能够持续运行,这是RC的工作内容,它会确保任何时间kubernetes中都有指定数量的Pod在运行。在此基础上,RC还提供了一些更高级的特性,比如滚动升级,升级回滚等等。

  通俗的理解就是,当将应用通过K8s运行起来之后,我们的k8s是需要保证容器一直处于持续运行,保证它的高可用,那么我们的RC就可以确保容器的高可用,RC的工作原理就是,RC是会一直监控我们的K8S容器,也就是说POD资源它的运行状态,一旦发现这个Pod资源有异常了,那么我们的RC就会控制k8s在其他的Node节点上启动一个新的Pod,以此来保证这个业务的高可用运行。RC除了保证Pod高可用之外,还提供了更高级的特性,比如滚动升级,升级回滚等等。

2、首先,查看你的k8s各个节点状态是否正常运行,然后创建一个rc的目录,用于存放RC(Replication Controller)的yaml配置文件。

 1 [root@k8s-master ~]# kubectl get nods2 the server doesn't have a resource type "nods"3 [root@k8s-master ~]# kubectl get node 4 NAME         STATUS    AGE5 k8s-master   Ready     6d6 k8s-node2    Ready     6d7 k8s-node3    Ready     6d8 [root@k8s-master ~]# kubectl get nodes9 NAME         STATUS    AGE
10 k8s-master   Ready     6d
11 k8s-node2    Ready     6d
12 k8s-node3    Ready     6d
13 [root@k8s-master ~]# clear
14 [root@k8s-master ~]# kubectl get componentstatus
15 NAME                 STATUS    MESSAGE             ERROR
16 scheduler            Healthy   ok
17 controller-manager   Healthy   ok
18 etcd-0               Healthy   {"health":"true"}
19 [root@k8s-master ~]# clear
20 [root@k8s-master ~]# cd k8s/
21 [root@k8s-master k8s]# ls
22 pod
23 [root@k8s-master k8s]# mkdir rc
24 [root@k8s-master k8s]# cd rc/
25 [root@k8s-master rc]# ls
26 [root@k8s-master rc]# vim nginx_rc_yaml
27 [root@k8s-master rc]# kubectl create -f nginx_rc_yaml
28 replicationcontroller "myweb" created
29 [root@k8s-master rc]# kubectl get rc
30 NAME      DESIRED   CURRENT   READY     AGE
31 myweb     2         2         1         13s
32 [root@k8s-master rc]# 

创建nginx_rc_yaml配置文件,配置内容,如下所示。

 1 # 声明api的版本。2 apiVersion: v13 # kind代表资源的类型,资源是ReplicationController。4 kind: ReplicationController5 # 资源叫什么名字,是在其属性metadata里面的。6 metadata:7   # 第一个属性name的值是myweb,即ReplicationController的名字就叫做myweb。8   name: myweb9 # spec是详细,详细里面定义了一个容器。
10 spec:
11   # 声明副本数量是2,代表了RC会启动两个相同的Pod。
12   replicas: 2
13   # 选择器。
14   selector:
15     app: myweb
16   # Pod的启动模板,和Pod的yaml配置信息基本差不多的,几乎一样,但是这里没有名称,是因为两个Pod名称不能完全一样的。
17   # 没有指定名称,RC会随机生成一个名称。
18   template:
19     # 资源叫什么名字,是在其属性metadata里面的。但是这里让RC随机生成指定数量的名称。
20     metadata:
21       # 给Pod贴上了一个标签,标签是app: web,标签是有一定的作用的。
22       labels:
23         app: myweb
24     # spec是详细,详细里面定义了一个容器。
25     spec:
26       # 定义一个容器,可以声明多个容器的。
27       containers:
28         # 容器的名称叫做myweb
29         - name: myweb
30         # 使用了什么镜像,可以使用官方公有的,也可以使用私有的。
31           image: 192.168.110.133:5000/nginx:1.13
32         # ports定义容器的端口。
33           ports:
34         # 容器的端口是80,如果容器有多个端口,可以在后面接着写一行即可。
35             - containerPort: 80

配置,如下所示:

如果如何控制yaml的格式,可以使用notepad++的yaml语言格式,或者在线yaml解析,或者idea的yaml配置文件,idea的yaml配置文件也推荐使用哦。

创建好RC(Replication Controller)之后,可以进行检查。可以看到RC创建了两个Pod,可以查看一下Pod的数量和状态。

 1 [root@k8s-master rc]# kubectl get rc 2 NAME      DESIRED   CURRENT   READY     AGE3 myweb     2         2         1         7m4 [root@k8s-master rc]# kubectl get pods5 NAME          READY     STATUS             RESTARTS   AGE6 myweb-0hqc5   0/1       ImagePullBackOff   0          8m7 myweb-2np4k   1/1       Running            0          8m8 nginx         1/1       Running            1          3d9 test1         0/1       ImagePullBackOff   0          2d
10 test2         2/2       Running            1          2d
11 test4         1/1       Running            0          2d
12 [root@k8s-master rc]# 

很明显,我这里创建的两个Pod,有一个启动失败了。此时,我想将失败的Pod删除掉,但是我删除了一个,RC又帮助你启动了一个,嗯,真的是高可用啊,然后我将RC删除掉,这两个Pod就随着被删除掉了。

 1 [root@k8s-master ~]# kubectl get rc2 NAME      DESIRED   CURRENT   READY     AGE3 myweb     2         2         1         17m4 [root@k8s-master ~]# kubectl get pod -o wide5 NAME          READY     STATUS             RESTARTS   AGE       IP            NODE6 myweb-8cp7h   0/1       ImagePullBackOff   0          5m        172.16.85.3   k8s-master7 myweb-qcgjl   1/1       Running            1          14m       172.16.5.2    k8s-node28 nginx         1/1       Running            2          3d        172.16.38.3   k8s-node39 test1         0/1       ImagePullBackOff   0          2d        172.16.85.2   k8s-master
10 test2         2/2       Running            3          2d        172.16.38.2   k8s-node3
11 test4         1/1       Running            1          2d        172.16.5.3    k8s-node2
12 [root@k8s-master ~]# kubectl delete rc myweb
13 replicationcontroller "myweb" deleted
14 [root@k8s-master ~]# kubectl get rc
15 No resources found.
16 [root@k8s-master ~]# 

这里我将没有用的测试Pod都删除掉,因为我笔记本只有8g内存,可能内存不够用了。搞了一个小时,不是内存的问题,是之前部署k8s的时候,测试nginx的时候将nginx拼错了,尴尬。

1 [root@k8s-node2 ~]# docker images
2 REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
3 docker.io/busybox                         latest              1c35c4412082        8 days ago          1.22 MB
4 192.168.110.133:5000/nginx                1.13                ae513a47849c        2 years ago         109 MB
5 docker.io/nginx                           1.13                ae513a47849c        2 years ago         109 MB
6 192.168.110.133:5000/pod-infrastructure   latest              34d3450d733b        3 years ago         205 MB
7 docker.io/tianyebj/pod-infrastructure     latest              34d3450d733b        3 years ago         205 MB
8 [root@k8s-node2 ~]# 

而我的RC的yaml的配置文件,如下所示:

 1 Version: v12 kind: ReplicationController3 metadata:4   name: myweb5 spec:6   replicas: 27   selector:8     app: myweb9   template:
10     metadata:
11       labels:
12         app: myweb
13     spec:
14       containers:
15         - name: myweb
16           image: 192.168.110.133:5000/nginx:1.13
17           # imagePullPolicy: IfNotPresent
18           ports:
19             - containerPort: 80

但是主节点的docker镜像是192.168.110.133:5000/ngnix,造成了每次创建RC,在主节点的Pod都无法启动,尴尬,还排查了这么久。真打脸。

1 [root@k8s-master ~]# docker images
2 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
3 docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
4 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
5 192.168.110.133:5000/ngnix                            1.13                ae513a47849c        2 years ago         109 MB
6 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
7 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
8 [root@k8s-master ~]# 

报错信息,也贴一下吧,方便自己以后使用,如下所示:

 1 [root@k8s-master ~]# kubectl describe pod myweb-qwgsf2 Name:        myweb-qwgsf3 Namespace:    default4 Node:        k8s-master/192.168.110.1335 Start Time:    Thu, 11 Jun 2020 17:21:45 +08006 Labels:        app=myweb7 Status:        Pending8 IP:        172.16.85.29 Controllers:    ReplicationController/myweb
10 Containers:
11   myweb:
12     Container ID:
13     Image:            192.168.110.133:5000/nginx:1.13
14     Image ID:
15     Port:            80/TCP
16     State:            Waiting
17       Reason:            ErrImagePull
18     Ready:            False
19     Restart Count:        0
20     Volume Mounts:        <none>
21     Environment Variables:    <none>
22 Conditions:
23   Type        Status
24   Initialized     True
25   Ready     False
26   PodScheduled     True
27 No volumes.
28 QoS Class:    BestEffort
29 Tolerations:    <none>
30 Events:
31   FirstSeen    LastSeen    Count    From            SubObjectPath        Type        Reason            Message
32   ---------    --------    -----    ----            -------------        --------    ------            -------
33   12m        12m        1    {kubelet k8s-master}                Warning        MissingClusterDNS    kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
34   12m        12m        1    {default-scheduler }                Normal        Scheduled        Successfully assigned myweb-qwgsf to k8s-master
35   12m        6m        6    {kubelet k8s-master}    spec.containers{myweb}    Normal        Pulling            pulling image "192.168.110.133:5000/nginx:1.13"
36   12m        6m        6    {kubelet k8s-master}    spec.containers{myweb}    Warning        Failed            Failed to pull image "192.168.110.133:5000/nginx:1.13": Error while pulling image: Get http://192.168.110.133:5000/v1/repositories/nginx/images: dial tcp 192.168.110.133:5000: connect: connection refused
37   12m        6m        6    {kubelet k8s-master}                Warning        FailedSync        Error syncing pod, skipping: failed to "StartContainer" for "myweb" with ErrImagePull: "Error while pulling image: Get http://192.168.110.133:5000/v1/repositories/nginx/images: dial tcp 192.168.110.133:5000: connect: connection refused"
38
39   12m    4m    31    {kubelet k8s-master}    spec.containers{myweb}    Normal    BackOff        Back-off pulling image "192.168.110.133:5000/nginx:1.13"
40   12m    4m    31    {kubelet k8s-master}                Warning    FailedSync    Error syncing pod, skipping: failed to "StartContainer" for "myweb" with ImagePullBackOff: "Back-off pulling image \"192.168.110.133:5000/nginx:1.13\""
41
42   32s    32s    1    {kubelet k8s-master}                Warning    MissingClusterDNS    kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy.
43   31s    20s    2    {kubelet k8s-master}    spec.containers{myweb}    Normal    Pulling            pulling image "192.168.110.133:5000/nginx:1.13"
44   31s    20s    2    {kubelet k8s-master}    spec.containers{myweb}    Warning    Failed            Failed to pull image "192.168.110.133:5000/nginx:1.13": Error while pulling image: Get http://192.168.110.133:5000/v1/repositories/nginx/images: dial tcp 192.168.110.133:5000: connect: connection refused
45   31s    20s    2    {kubelet k8s-master}                Warning    FailedSync        Error syncing pod, skipping: failed to "StartContainer" for "myweb" with ErrImagePull: "Error while pulling image: Get http://192.168.110.133:5000/v1/repositories/nginx/images: dial tcp 192.168.110.133:5000: connect: connection refused"
46
47   30s    8s    2    {kubelet k8s-master}    spec.containers{myweb}    Normal    BackOff        Back-off pulling image "192.168.110.133:5000/nginx:1.13"
48   30s    8s    2    {kubelet k8s-master}                Warning    FailedSync    Error syncing pod, skipping: failed to "StartContainer" for "myweb" with ImagePullBackOff: "Back-off pulling image \"192.168.110.133:5000/nginx:1.13\""

此处,将主节点的Docker镜像删除掉。

 1 [root@k8s-master ~]# docker images2 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE3 docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB4 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB5 192.168.110.133:5000/ngnix                            1.13                ae513a47849c        2 years ago         109 MB6 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB7 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB8 [root@k8s-master ~]# docker ps9 CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
10 [root@k8s-master ~]# docker ps -a
11 CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
12 a27987d97039        registry            "/entrypoint.sh /e..."   5 days ago          Exited (2) 3 days ago                         registry
13 ee95778bd5d9        busybox             "sh"                     6 days ago          Exited (127) 6 days ago                       friendly_payne
14 6d459781a3e5        busybox             "sh"                     6 days ago          Exited (137) 5 days ago                       gracious_nightingale
15 [root@k8s-master ~]# docker rmi -f ae513a47849c
16 Untagged: 192.168.110.133:5000/ngnix:1.13
17 Untagged: 192.168.110.133:5000/ngnix@sha256:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90
18 Deleted: sha256:ae513a47849c895a155ddfb868d6ba247f60240ec8495482eca74c4a2c13a881
19 Deleted: sha256:160a8bd939a9421818f499ba4fbfaca3dd5c86ad7a6b97b6889149fd39bd91dd
20 Deleted: sha256:f246685cc80c2faa655ba1ec9f0a35d44e52b6f83863dc16f46c5bca149bfefc
21 Deleted: sha256:d626a8ad97a1f9c1f2c4db3814751ada64f60aed927764a3f994fcd88363b659
22 [root@k8s-master ~]# docker images
23 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
24 docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
25 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
26 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
27 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
28 [root@k8s-master ~]# docker images
29 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
30 docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
31 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
32 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
33 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
34 [root@k8s-master ~]# 

此时,将三台节点重启了,或者重启服务,我这里直接重启了三台机器,其所有服务全部重启。

 1 [root@k8s-master ~]# free -h2               total        used        free      shared  buff/cache   available3 Mem:           2.2G        880M        453M         12M        953M        1.1G4 Swap:          2.0G          0B        2.0G5 [root@k8s-master ~]# kubectl get node 6 NAME         STATUS    AGE7 k8s-master   Ready     6d8 k8s-node2    Ready     6d9 k8s-node3    Ready     6d
10 [root@k8s-master ~]# kubectl get componentstatus
11 NAME                 STATUS    MESSAGE             ERROR
12 scheduler            Healthy   ok
13 controller-manager   Healthy   ok
14 etcd-0               Healthy   {"health":"true"}
15 [root@k8s-master ~]# kubectl get rc
16 NAME      DESIRED   CURRENT   READY     AGE
17 myweb     2         2         2         16m
18 [root@k8s-master ~]# docker images
19 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
20 docker.io/nginx                                       latest              2622e6cca7eb        41 hours ago        132 MB
21 docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
22 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
23 192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB
24 docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB
25 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
26 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
27 [root@k8s-master ~]# kubectl get node -o wide
28 NAME         STATUS    AGE       EXTERNAL-IP
29 k8s-master   Ready     6d        <none>
30 k8s-node2    Ready     6d        <none>
31 k8s-node3    Ready     6d        <none>
32 [root@k8s-master ~]# kubectl get pod -o wide
33 NAME          READY     STATUS    RESTARTS   AGE       IP            NODE
34 myweb-2h8b1   1/1       Running   1          17m       172.16.85.2   k8s-master
35 myweb-lfkmp   1/1       Running   1          17m       172.16.5.2    k8s-node2
36 test4         1/1       Running   1          13m       172.16.38.2   k8s-node3
37 [root@k8s-master ~]# 

RC(Replication Controller)会始终保持Pod的数量为2,可以自己删除一个Pod,k8s的RC(Replication Controller)会里面帮助你启动一个新的Pod,RC(Replication Controller)会时刻监控Pod的状态,少了就启动,多了就进行删除,数量和配置文件yaml的数量一致。

3、RC(Replication Controller)如何与Pod进行关联呢?

答:使用到的标签Label(标签选择器)。在nginx_rc.yaml配置文件中,定义了RC的选择器是通过标签app:myweb来选择的,每一个Pod在运行的时候会自动加上一个标签叫做app:myweb,这样的话,RC会自动根据标签来选择我们的Pod。

可以通过命令kubectl describe pod myweb-2h8b1来查看标签。

 1 [root@k8s-master rc]# kubectl describe pod myweb-2h8b12 Name:        myweb-2h8b13 Namespace:    default4 Node:        k8s-master/192.168.110.1335 Start Time:    Thu, 11 Jun 2020 17:51:06 +08006 Labels:        app=myweb7 Status:        Running8 IP:        172.16.85.29 Controllers:    ReplicationController/myweb
10 Containers:
11   myweb:
12     Container ID:        docker://27a9e6dfb65be540bb50c98d820a5b773c0ed01d09d2350baf6027cdf9e22257
13     Image:            192.168.110.133:5000/nginx:1.13
14     Image ID:            docker-pullable://docker.io/nginx@sha256:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
15     Port:            80/TCP
16     State:            Running
17       Started:            Thu, 11 Jun 2020 18:06:34 +0800
18     Last State:            Terminated
19       Reason:            Completed
20       Exit Code:        0
21       Started:            Thu, 11 Jun 2020 18:01:41 +0800
22       Finished:            Thu, 11 Jun 2020 18:05:42 +0800
23     Ready:            True
24     Restart Count:        1
25     Volume Mounts:        <none>
26     Environment Variables:    <none>
27 Conditions:
28   Type        Status
29   Initialized     True
30   Ready     True
31   PodScheduled     True
32 No volumes.
33 QoS Class:    BestEffort
34 Tolerations:    <none>
35 No events.
36 [root@k8s-master rc]# 

通过查看RC的标签。

1 [root@k8s-master rc]# kubectl get rc -o wide
2 NAME      DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                          SELECTOR
3 myweb     2         2         2         3h        myweb          192.168.110.133:5000/nginx:1.13   app=myweb
4 [root@k8s-master rc]# 

RC(Replication Controller)是通过标签(标签选择器)来选择Pod,通过标签来决定这个Pod是归我来管理的。

通过修改创建好的Pod可以测试,如果多于指定数量的Pod数量,就会被删除掉,注意,删除掉的Pod是最年轻的那个Pod。kubectl edit pod test4命令可以修改创建好的Pod。

 1 [root@k8s-master rc]# kubectl get all2 NAME       DESIRED   CURRENT   READY     AGE3 rc/myweb   2         2         2         3h4 5 NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE6 svc/kubernetes   10.254.0.1   <none>        443/TCP   6d7 8 NAME             READY     STATUS    RESTARTS   AGE9 po/myweb-2h8b1   1/1       Running   1          3h
10 po/myweb-lfkmp   1/1       Running   1          3h
11 po/test4         1/1       Running   1          3h
12 [root@k8s-master rc]# kubectl edit pod test4
13 pod "test4" edited
14 [root@k8s-master rc]# kubectl get all
15 NAME       DESIRED   CURRENT   READY     AGE
16 rc/myweb   2         2         2         3h
17
18 NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
19 svc/kubernetes   10.254.0.1   <none>        443/TCP   6d
20
21 NAME             READY     STATUS    RESTARTS   AGE
22 po/myweb-lfkmp   1/1       Running   1          3h
23 po/test4         1/1       Running   1          3h
24 [root@k8s-master rc]# 

4、RC(Replication Controller)的滚动升级。

答:滚动升级是一种平滑过渡的升级方式,通过逐步替换的策略,保证整体系统的稳定,在初始升级的时候就可以及时发现,调整问题,以保证问题影响度不好扩大。Kubernetes中滚动升级的命令如下所示:

首先,这里将配置文件nginx_rc.yaml进行拷贝,然后修改配置文件nginx_rc2.yaml,将myweb替换为myweb2。

1 [root@k8s-master rc]# cp nginx_rc.yaml nginx_rc2.yaml
2 [root@k8s-master rc]# ls
3 nginx_rc2.yaml  nginx_rc.yaml
4 [root@k8s-master rc]# vim nginx_rc2.yaml
5 [root@k8s-master rc]# 

具体配置,如下所示:

将myweb替换为myweb2,替换过后,然后将镜像版本修改为latest版本,如下所示:

将Nginx的latest版本镜像拉取下来docker pull docker.io/nginx:latest。然后将镜像上传到私有仓库里面,方便下载。

这里需要注意的是,我之前在配置Docker镜像加速的时候,在三台机器的vim /etc/sysconfig/docker。

1 [root@k8s-node3 ~]# vim /etc/sysconfig/docker

我在这个配置文件里面加的镜像加速和配置私有仓库地址。貌似并不是很好使的。

下面,在三台机器的上面,进行如下配置,将Docker镜像加速和私有仓库配置到下面这里。

1 [root@k8s-node3 ~]# docker pull docker.io/nginx:1.15
2 Trying to pull repository docker.io/library/nginx ...
3 Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)

修改配置文件,在三台机器上面都配置如下所示配置,[root@k8s-node3 ~]# vim /etc/docker/daemon.json

1 { "insecure-registries":["192.168.110.133:5000"] ,"registry-mirrors":["https://docker.mirrors.ustc.edu.cn"]}

然后重启三台机器的Docker服务systemctl restart docker。如果实在下载不下来,需要自己从网上找个https://hub.docker.com/

1 [root@k8s-master ~]# docker pull docker.io/nginx:1.15
2 Trying to pull repository docker.io/library/nginx ...
3 sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68: Pulling from docker.io/library/nginx
4 743f2d6c1f65: Pull complete
5 6bfc4ec4420a: Pull complete
6 688a776db95f: Pull complete
7 Digest: sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
8 Status: Downloaded newer image for docker.io/nginx:1.15
9 [root@k8s-master ~]# 

然后将ngnix1.15上传到私有仓库里面。可以使用docker images命令查看是否已经上传到私有仓库。

 1 [root@k8s-master rc]# docker images2 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE3 docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB4 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB5 docker.io/nginx                                       1.15                53f3fd8007f7        13 months ago       109 MB6 192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB7 docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB8 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB9 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
10 [root@k8s-master rc]# docker tag docker.io/nginx:1.15 192.168.110.133:5000/nginx:1.15
11 [root@k8s-master rc]# docker push 192.168.110.133:5000/nginx:1.15
12 The push refers to a repository [192.168.110.133:5000/nginx]
13 Put http://192.168.110.133:5000/v1/repositories/nginx/: dial tcp 192.168.110.133:5000: connect: connection refused
14 [root@k8s-master rc]# docker images
15 REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
16 docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
17 docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
18 docker.io/nginx                                       1.15                53f3fd8007f7        13 months ago       109 MB
19 192.168.110.133:5000/nginx                            1.15                53f3fd8007f7        13 months ago       109 MB
20 docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB
21 192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB
22 registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
23 192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
24 [root@k8s-master rc]# 

滚动升级是一种平滑过渡的升级方式,通过逐步替换的策略,保证整体系统的稳定,在初始升级的时候就可以及时发现,调整问题,以保证问题影响度不好扩大。Kubernetes中滚动升级的命令如下所示:

这里需要注意的是,想要看看Docker私有仓库是否有你想要的镜像,可以使用如下所示查看:

1 [root@k8s-node3 docker]# docker images
2 REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
3 docker.io/busybox                         latest              1c35c4412082        9 days ago          1.22 MB
4 192.168.110.133:5000/nginx                1.15                53f3fd8007f7        13 months ago       109 MB
5 docker.io/nginx                           1.15                53f3fd8007f7        13 months ago       109 MB
6 192.168.110.133:5000/nginx                1.13                ae513a47849c        2 years ago         109 MB
7 docker.io/tianyebj/pod-infrastructure     latest              34d3450d733b        3 years ago         205 MB
8 192.168.110.133:5000/pod-infrastructure   latest              34d3450d733b        3 years ago         205 MB
9 [root@k8s-node3 docker]# 

还有关于使用镜像加速和私有仓库的配置,这里需要说明的是,我的Docker的版本是1.13.1。

 1 [root@k8s-node3 ~]# docker version2 Client:3  Version:         1.13.14  API version:     1.265  Package version: docker-1.13.1-161.git64e9980.el7_8.x86_646  Go version:      go1.10.37  Git commit:      64e9980/1.13.18  Built:           Tue Apr 28 14:43:01 20209  OS/Arch:         linux/amd64
10
11 Server:
12  Version:         1.13.1
13  API version:     1.26 (minimum version 1.12)
14  Package version: docker-1.13.1-161.git64e9980.el7_8.x86_64
15  Go version:      go1.10.3
16  Git commit:      64e9980/1.13.1
17  Built:           Tue Apr 28 14:43:01 2020
18  OS/Arch:         linux/amd64
19  Experimental:    false
20 [root@k8s-node3 ~]# 

关于镜像加速和私有仓库的配置,我此时由于不需要从网上下载所需的软件,这里将三台机器的/etc/docker/daemon.json配置文件全部后面加上了_bak,这里不需要使用它们了。

1 [root@k8s-node3 ~]# cat /etc/docker/daemon.json_bak
2 { "insecure-registries":["192.168.110.133:5000"]}
3 [root@k8s-node3 ~]# cd /etc/docker/
4 [root@k8s-node3 docker]# ls
5 certs.d  daemon.json_20200612  daemon.json_bak  key.json  seccomp.json
6 [root@k8s-node3 docker]# 

关于镜像加速和私有仓库的配置,如果配置不好,真的难为si你的。所以这里说了很多次。此时,三台机器的镜像加速和私有中心在这个里面还进行了配置。特此说明。

 1 [root@k8s-master docker]# cat /etc/sysconfig/docker2 # /etc/sysconfig/docker3 4 # Modify these options if you want to change the way the docker daemon runs5 # OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'6 # 信任私有仓库,镜像加速7 OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false8 --registry-mirror=https://registry.docker-cn.com --insecure-registry=192.168.110.133:5000'9
10
11
12 if [ -z "${DOCKER_CERT_PATH}" ]; then
13     DOCKER_CERT_PATH=/etc/docker
14 fi
15
16 # Do not add registries in this file anymore. Use /etc/containers/registries.conf
17 # instead. For more information reference the registries.conf(5) man page.
18
19 # Location used for temporary files, such as those created by
20 # docker load and build operations. Default is /var/lib/docker/tmp
21 # Can be overriden by setting the following environment variable.
22 # DOCKER_TMPDIR=/var/tmp
23
24 # Controls the /etc/cron.daily/docker-logrotate cron job status.
25 # To disable, uncomment the line below.
26 # LOGROTATE=false
27
28 # docker-latest daemon can be used by starting the docker-latest unitfile.
29 # To use docker-latest client, uncomment below lines
30 #DOCKERBINARY=/usr/bin/docker-latest
31 #DOCKERDBINARY=/usr/bin/dockerd-latest
32 #DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
33 #DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest
34 [root@k8s-master docker]# 

饶了一大圈,此时进行滚动升级,滚动升级是一种平滑过渡的升级方式,通过逐步替换的策略,保证整体系统的稳定,在初始升级的时候就可以及时发现,调整问题,以保证问题影响度不好扩大。Kubernetes中滚动升级的命令如下所示:

 1 [root@k8s-master rc]# kubectl rolling-update myweb -f nginx_rc2.yaml --update-period=10s2 Created myweb23 Scaling up myweb2 from 0 to 2, scaling down myweb from 2 to 0 (keep 2 pods available, don't exceed 3 pods)4 Scaling myweb2 up to 15 Scaling myweb down to 16 Scaling myweb2 up to 27 Scaling myweb down to 08 Update succeeded. Deleting myweb9 replicationcontroller "myweb" rolling updated to "myweb2"
10 [root@k8s-master rc]# 

创建一个myweb2的RC,将myweb2的RC的Pod数量由0调整为2,把myweb的RC的Pod数量由2调整为0。当myweb2存活了30秒以上就会删掉一个myweb,nginx也是myweb的容器,删除也是比较慢的。

升级开始后,首先依据提供的定义文件创建V2版本的RC,然后每隔10s(--update-period=10s)逐步的增加V2版本的Pod副本数,逐步减少V1版本Pod的副本数。升级完成之后,删除V1版本的RC,保留V2版本的RC,以及实现滚动升级。

1 [root@k8s-master ~]# kubectl get pod -o wide
2 NAME           READY     STATUS    RESTARTS   AGE       IP            NODE
3 myweb2-f5400   1/1       Running   0          15s       172.16.38.3   k8s-node3
4 myweb2-mg9sk   1/1       Running   0          26s       172.16.85.2   k8s-master
5 [root@k8s-master ~]# 

升级之后还可以进行回滚,如下所示:

 1 [root@k8s-master rc]# kubectl rolling-update myweb2 -f nginx_rc.yaml --update-period=10s2 Created myweb3 Scaling up myweb from 0 to 2, scaling down myweb2 from 2 to 0 (keep 2 pods available, don't exceed 3 pods)4 Scaling myweb up to 15 Scaling myweb2 down to 16 Scaling myweb up to 27 Scaling myweb2 down to 08 Update succeeded. Deleting myweb29 replicationcontroller "myweb2" rolling updated to "myweb"
10 [root@k8s-master rc]# 

升级过程中,发生了错误中途退出时候,可以选择继续升级。Kubernetes能够智能的判断升级中断之前的状态,然后紧接着继续执行升级。当然,也可以进行退出,命令如下所示:

1 [root@k8s-master rc]# kubectl rolling-update myweb myweb2 --update-period=10s --rollback
2 Setting "myweb" replicas to 2
3 Continuing update with existing controller myweb.
4 Scaling up myweb from 2 to 2, scaling down myweb2 from 1 to 0 (keep 2 pods available, don't exceed 3 pods)
5 Scaling myweb2 down to 0
6 Update succeeded. Deleting myweb2
7 replicationcontroller "myweb" rolling updated to "myweb2"
8 [root@k8s-master rc]# 

Kubernetes(k8s)的RC(Replication Controller)副本控制器相关推荐

  1. kubernetes(k8s)容器集群管理系统

    kubernetes 文章目录 kubernetes 什么是k8s? 为什么需要K8S? 多机编排管理容器 k8s的特性 Kubernetes集群架构与组件 核心组件 Master组件 Kube-ap ...

  2. Kubernetes(k8s)基础之二:容器编排介绍及概念

    目录 1.k8s对象 2. 容器编排的概念 2.1.K8S是如何对容器编排? 2.2.Pod是怎么创建出来的? 2.3.Pod资源组成的应用如何提供外部访问的? 2.4.Service又是怎么关联到P ...

  3. Kubernetes对象之Replication Controller(RC)和Replica Sets(RS)

    关注公众号"风色年代"订阅更多精彩文章,本博大部分文章为转载并已标明原文出处,如有再转敬请保留,请自觉尊重原创作者的劳动成果! 作者:ChenVast: 原文链接: https:/ ...

  4. Kubernetes K8S之资源控制器Daemonset详解

    Kubernetes的资源控制器Daemonset详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7.7 2C/ ...

  5. 容器编排技术 -- Kubernetes Replication Controller

    容器编排技术 -- Kubernetes Replication Controller 1 ReplicationController 工作原理 1.1 示例: 1.2 删除ReplicationCo ...

  6. Kubernetes K8S之资源控制器Job和CronJob详解

    Kubernetes的资源控制器Job和CronJob详解与示例 主机配置规划 服务器名称(hostname) 系统版本 配置 内网IP 外网IP(模拟) k8s-master CentOS7.7 2 ...

  7. Kubernetes基础:资源扩缩容方法(Replication Controller)

    Kubernetes提供了多种方式对于资源的扩缩容进行操作,这篇文章以RC(Replication Controller)为例进行说明. 常用方法 方法1: 使用kubectl scale进行扩缩容 ...

  8. kubernetes(k8s)架构和组件,工作流程 ,资源

    文章目录 一: kubernetes 概述 1.1 K8S 是什么 1.2 为什么使用k8s 1.2.1 传统后端部署的方法和缺陷 1.2.2 裸跑docker的缺陷 1.3 k8s的特性 二: ku ...

  9. Kubernetes k8s理论篇

    目录 一.k8s概述 1.1 什么是k8s? 1.1.1 作用 1.1.2 由来 1.1.3 含义 1.2 为什么需要K8S K8S的目标是让部署容器化应用简单高效. 1.3 k8s的特性 二.Kub ...

最新文章

  1. python写计算器
  2. magento 加速(.htaccess)
  3. anaconda moviepy_Win10配置anaconda和jupyter
  4. 第一节 MongoDB介绍及下载与安装
  5. [转载]JSONP跨域的原理解析
  6. C# 自定义Json解析工具
  7. linux上卓懿应用商城王者荣耀键盘映射如何设置?
  8. udacity-谷歌自动驾驶-课程笔记-localization
  9. java同步数据,int类型清空为0
  10. 数学复习全程规划及方法指导(武忠祥笔记)
  11. linux之bc命令使用详解_【原创】linux命令bc使用详解
  12. js通过Ajax传递数组
  13. 李航《统计学习方法》第二版 实战(mnist为例)
  14. myCPUZ (未完待续)
  15. linux把一个大文件压缩,linux大文件压缩及解压需要注意问题
  16. 一位新竹科学园区朋友的来信
  17. Android 控件 RecyclerView 看这篇就够了
  18. 我国各省市县的名称列表
  19. ByVal 与 ByRef(默认值) 的区别
  20. 河南理工大学2018暑假集训第二次积分赛 2018/07/29

热门文章

  1. 区块链水龙头以及常用浏览器地址
  2. java B2B2C 源码 多级分销springmvc mybatis多租户电子商城系统
  3. 12自由度六足机器人实现原地舞蹈功能
  4. 获取摄像头和麦克风权限_getUserMedia API及HTML5 调用摄像头和麦克风
  5. 360网盘群签到助手V1.4
  6. chkrootkit
  7. OPPO X903 刷机包
  8. 零基础无实物一步一步学PLCS7-1200仿真(五)-四路简易抢答器控制
  9. matlab 计算闭合区域的面积
  10. 人件读书笔记(37)混乱与秩序