Kubernetes(k8s)的RC(Replication Controller)副本控制器

薛正华 2020-06-21

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配置文件。

[ ~]# kubectl get nods
the server doesn‘t have a resource type "nods"
[ ~]# kubectl get node 
NAME         STATUS    AGE
k8s-master   Ready     6d
k8s-node2    Ready     6d
k8s-node3    Ready     6d
[ ~]# kubectl get nodes
NAME         STATUS    AGE
k8s-master   Ready     6d
k8s-node2    Ready     6d
k8s-node3    Ready     6d
[ ~]# clear
[ ~]# kubectl get componentstatus 
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
[ ~]# clear
[ ~]# cd k8s/
[master k8s]# ls
pod
[master k8s]# mkdir rc
[ k8s]# cd rc/
[master rc]# ls
[master rc]# vim nginx_rc_yaml
[ rc]# kubectl create -f nginx_rc_yaml 
replicationcontroller "myweb" created
[ rc]# kubectl get rc 
NAME      DESIRED   CURRENT   READY     AGE
myweb     2         2         1         13s
[ rc]#

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

# 声明api的版本。
apiVersion: v1
# kind代表资源的类型,资源是ReplicationController。
kind: ReplicationController
# 资源叫什么名字,是在其属性metadata里面的。
metadata:
  # 第一个属性name的值是myweb,即ReplicationController的名字就叫做myweb。
  name: myweb
# spec是详细,详细里面定义了一个容器。
spec:
  # 声明副本数量是2,代表了RC会启动两个相同的Pod。
  replicas: 2
  # 选择器。
  selector:
    app: myweb
  # Pod的启动模板,和Pod的yaml配置信息基本差不多的,几乎一样,但是这里没有名称,是因为两个Pod名称不能完全一样的。
  # 没有指定名称,RC会随机生成一个名称。
  template:
    # 资源叫什么名字,是在其属性metadata里面的。但是这里让RC随机生成指定数量的名称。
    metadata:
      # 给Pod贴上了一个标签,标签是app: web,标签是有一定的作用的。
      labels:
        app: myweb
    # spec是详细,详细里面定义了一个容器。
    spec:
      # 定义一个容器,可以声明多个容器的。
      containers:
        # 容器的名称叫做myweb
        - name: myweb
        # 使用了什么镜像,可以使用官方公有的,也可以使用私有的。
          image: 192.168.110.133:5000/nginx:1.13
        # ports定义容器的端口。
          ports:
        # 容器的端口是80,如果容器有多个端口,可以在后面接着写一行即可。
            - containerPort: 80

配置,如下所示:

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

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

[ rc]# kubectl get rc 
NAME      DESIRED   CURRENT   READY     AGE
myweb     2         2         1         7m
[ rc]# kubectl get pods
NAME          READY     STATUS             RESTARTS   AGE
myweb-0hqc5   0/1       ImagePullBackOff   0          8m
myweb-2np4k   1/1       Running            0          8m
nginx         1/1       Running            1          3d
test1         0/1       ImagePullBackOff   0          2d
test2         2/2       Running            1          2d
test4         1/1       Running            0          2d
[ rc]#

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

[ ~]# kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
myweb     2         2         1         17m
[ ~]# kubectl get pod -o wide
NAME          READY     STATUS             RESTARTS   AGE       IP            NODE
myweb-8cp7h   0/1       ImagePullBackOff   0          5m        172.16.85.3   k8s-master
myweb-qcgjl   1/1       Running            1          14m       172.16.5.2    k8s-node2
nginx         1/1       Running            2          3d        172.16.38.3   k8s-node3
test1         0/1       ImagePullBackOff   0          2d        172.16.85.2   k8s-master
test2         2/2       Running            3          2d        172.16.38.2   k8s-node3
test4         1/1       Running            1          2d        172.16.5.3    k8s-node2
[ ~]# kubectl delete rc myweb
replicationcontroller "myweb" deleted
[ ~]# kubectl get rc
No resources found.
[ ~]#

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

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

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

Version: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 2
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: myweb
          image: 192.168.110.133:5000/nginx:1.13
          # imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80

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

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

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

[ ~]# kubectl describe pod myweb-qwgsf
Name:        myweb-qwgsf
Namespace:    default
Node:        k8s-master/192.168.110.133
Start Time:    Thu, 11 Jun 2020 17:21:45 +0800
Labels:        app=myweb
Status:        Pending
IP:        172.16.85.2
Controllers:    ReplicationController/myweb
Containers:
  myweb:
    Container ID:        
    Image:            192.168.110.133:5000/nginx:1.13
    Image ID:            
    Port:            80/TCP
    State:            Waiting
      Reason:            ErrImagePull
    Ready:            False
    Restart Count:        0
    Volume Mounts:        <none>
    Environment Variables:    <none>
Conditions:
  Type        Status
  Initialized     True 
  Ready     False 
  PodScheduled     True 
No volumes.
QoS Class:    BestEffort
Tolerations:    <none>
Events:
  FirstSeen    LastSeen    Count    From            SubObjectPath        Type        Reason            Message
  ---------    --------    -----    ----            -------------        --------    ------            -------
  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.
  12m        12m        1    {default-scheduler }                Normal        Scheduled        Successfully assigned myweb-qwgsf to k8s-master
  12m        6m        6    {kubelet k8s-master}    spec.containers{myweb}    Normal        Pulling            pulling image "192.168.110.133:5000/nginx:1.13"
  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
  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"

  12m    4m    31    {kubelet k8s-master}    spec.containers{myweb}    Normal    BackOff        Back-off pulling image "192.168.110.133:5000/nginx:1.13"
  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\""

  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.
  31s    20s    2    {kubelet k8s-master}    spec.containers{myweb}    Normal    Pulling            pulling image "192.168.110.133:5000/nginx:1.13"
  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
  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"

  30s    8s    2    {kubelet k8s-master}    spec.containers{myweb}    Normal    BackOff        Back-off pulling image "192.168.110.133:5000/nginx:1.13"
  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镜像删除掉。

[ ~]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
192.168.110.133:5000/ngnix                            1.13                ae513a47849c        2 years ago         109 MB
registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
[ ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[ ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                    PORTS               NAMES
a27987d97039        registry            "/entrypoint.sh /e..."   5 days ago          Exited (2) 3 days ago                         registry
ee95778bd5d9        busybox             "sh"                     6 days ago          Exited (127) 6 days ago                       friendly_payne
6d459781a3e5        busybox             "sh"                     6 days ago          Exited (137) 5 days ago                       gracious_nightingale
[ ~]# docker rmi -f ae513a47849c
Untagged: 192.168.110.133:5000/ngnix:1.13
Untagged: 192.168.110.133:5000/:e4f0474a75c510f40b37b6b7dc2516241ffa8bde5a442bde3d372c9519c84d90
Deleted: sha256:ae513a47849c895a155ddfb868d6ba247f60240ec8495482eca74c4a2c13a881
Deleted: sha256:160a8bd939a9421818f499ba4fbfaca3dd5c86ad7a6b97b6889149fd39bd91dd
Deleted: sha256:f246685cc80c2faa655ba1ec9f0a35d44e52b6f83863dc16f46c5bca149bfefc
Deleted: sha256:d626a8ad97a1f9c1f2c4db3814751ada64f60aed927764a3f994fcd88363b659
[ ~]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
[ ~]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
[ ~]#

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

[ ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           2.2G        880M        453M         12M        953M        1.1G
Swap:          2.0G          0B        2.0G
[ ~]# kubectl get node 
NAME         STATUS    AGE
k8s-master   Ready     6d
k8s-node2    Ready     6d
k8s-node3    Ready     6d
[ ~]# kubectl get componentstatus 
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"}   
[ ~]# kubectl get rc 
NAME      DESIRED   CURRENT   READY     AGE
myweb     2         2         2         16m
[ ~]# docker images 
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx                                       latest              2622e6cca7eb        41 hours ago        132 MB
docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB
docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB
registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
[ ~]# kubectl get node -o wide
NAME         STATUS    AGE       EXTERNAL-IP
k8s-master   Ready     6d        <none>
k8s-node2    Ready     6d        <none>
k8s-node3    Ready     6d        <none>
[ ~]# kubectl get pod -o wide
NAME          READY     STATUS    RESTARTS   AGE       IP            NODE
myweb-2h8b1   1/1       Running   1          17m       172.16.85.2   k8s-master
myweb-lfkmp   1/1       Running   1          17m       172.16.5.2    k8s-node2
test4         1/1       Running   1          13m       172.16.38.2   k8s-node3
[ ~]#

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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。

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

[ rc]# kubectl describe pod myweb-2h8b1
Name:        myweb-2h8b1
Namespace:    default
Node:        k8s-master/192.168.110.133
Start Time:    Thu, 11 Jun 2020 17:51:06 +0800
Labels:        app=myweb
Status:        Running
IP:        172.16.85.2
Controllers:    ReplicationController/myweb
Containers:
  myweb:
    Container ID:        docker://27a9e6dfb65be540bb50c98d820a5b773c0ed01d09d2350baf6027cdf9e22257
    Image:            192.168.110.133:5000/nginx:1.13
    Image ID:            docker-pullable://docker.io/:b1d09e9718890e6ebbbd2bc319ef1611559e30ce1b6f56b2e3b479d9da51dc35
    Port:            80/TCP
    State:            Running
      Started:            Thu, 11 Jun 2020 18:06:34 +0800
    Last State:            Terminated
      Reason:            Completed
      Exit Code:        0
      Started:            Thu, 11 Jun 2020 18:01:41 +0800
      Finished:            Thu, 11 Jun 2020 18:05:42 +0800
    Ready:            True
    Restart Count:        1
    Volume Mounts:        <none>
    Environment Variables:    <none>
Conditions:
  Type        Status
  Initialized     True 
  Ready     True 
  PodScheduled     True 
No volumes.
QoS Class:    BestEffort
Tolerations:    <none>
No events.
[ rc]#

Kubernetes(k8s)的RC(Replication Controller)副本控制器

通过查看RC的标签。

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

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

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

[ rc]# kubectl get all
NAME       DESIRED   CURRENT   READY     AGE
rc/myweb   2         2         2         3h

NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
svc/kubernetes   10.254.0.1   <none>        443/TCP   6d

NAME             READY     STATUS    RESTARTS   AGE
po/myweb-2h8b1   1/1       Running   1          3h
po/myweb-lfkmp   1/1       Running   1          3h
po/test4         1/1       Running   1          3h
[master rc]# kubectl edit pod test4 
pod "test4" edited
[ rc]# kubectl get all
NAME       DESIRED   CURRENT   READY     AGE
rc/myweb   2         2         2         3h

NAME             CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
svc/kubernetes   10.254.0.1   <none>        443/TCP   6d

NAME             READY     STATUS    RESTARTS   AGE
po/myweb-lfkmp   1/1       Running   1          3h
po/test4         1/1       Running   1          3h
[ rc]#

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

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

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

[master rc]# cp nginx_rc.yaml nginx_rc2.yaml 
[master rc]# ls
nginx_rc2.yaml  nginx_rc.yaml
[master rc]# vim nginx_rc2.yaml 
[ rc]#

具体配置,如下所示:

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

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

1 [ ~]# vim /etc/sysconfig/docker

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

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

1 [ ~]# 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)

修改配置文件,在三台机器上面都配置如下所示配置,[ ~]# 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/

[ ~]# docker pull docker.io/nginx:1.15
Trying to pull repository docker.io/library/nginx ... 
sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68: Pulling from docker.io/library/nginx
743f2d6c1f65: Pull complete 
6bfc4ec4420a: Pull complete 
688a776db95f: Pull complete 
Digest: sha256:23b4dcdf0d34d4a129755fc6f52e1c6e23bb34ea011b315d87e193033bcd1b68
Status: Downloaded newer image for docker.io/nginx:1.15
[ ~]#

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

[master rc]# docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
docker.io/nginx                                       1.15                53f3fd8007f7        13 months ago       109 MB
192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB
docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB
registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
[ rc]# docker tag docker.io/nginx:1.15 192.168.110.133:5000/nginx:1.15
[ rc]# docker push 192.168.110.133:5000/nginx:1.15
The push refers to a repository [192.168.110.133:5000/nginx]
Put http://192.168.110.133:5000/v1/repositories/nginx/: dial tcp 192.168.110.133:5000: connect: connection refused
[master rc]# docker images 
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
docker.io/busybox                                     latest              1c35c4412082        8 days ago          1.22 MB
docker.io/registry                                    latest              708bc6af7e5e        4 months ago        25.8 MB
docker.io/nginx                                       1.15                53f3fd8007f7        13 months ago       109 MB
192.168.110.133:5000/nginx                            1.15                53f3fd8007f7        13 months ago       109 MB
docker.io/nginx                                       1.13                ae513a47849c        2 years ago         109 MB
192.168.110.133:5000/nginx                            1.13                ae513a47849c        2 years ago         109 MB
registry.access.redhat.com/rhel7/pod-infrastructure   latest              99965fb98423        2 years ago         209 MB
192.168.110.133:5000/pod-infrastructure               latest              34d3450d733b        3 years ago         205 MB
[ rc]#

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

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

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

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

[ ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-161.git64e9980.el7_8.x86_64
 Go version:      go1.10.3
 Git commit:      64e9980/1.13.1
 Built:           Tue Apr 28 14:43:01 2020
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-161.git64e9980.el7_8.x86_64
 Go version:      go1.10.3
 Git commit:      64e9980/1.13.1
 Built:           Tue Apr 28 14:43:01 2020
 OS/Arch:         linux/amd64
 Experimental:    false
[ ~]#

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

[ ~]# cat /etc/docker/daemon.json_bak 
{ "insecure-registries":["192.168.110.133:5000"]}
[ ~]# cd /etc/docker/
[node3 docker]# ls
certs.d  daemon.json_20200612  daemon.json_bak  key.json  seccomp.json
[ docker]#

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

[ docker]# cat /etc/sysconfig/docker
# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
# OPTIONS=‘--selinux-enabled --log-driver=journald --signature-verification=false‘
# 信任私有仓库,镜像加速
OPTIONS=‘--selinux-enabled --log-driver=journald --signature-verification=false
--registry-mirror=https://registry.docker-cn.com --insecure-registry=192.168.110.133:5000‘



if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

# Do not add registries in this file anymore. Use /etc/containers/registries.conf
# instead. For more information reference the registries.conf(5) man page.

# Location used for temporary files, such as those created by
# docker load and build operations. Default is /var/lib/docker/tmp
# Can be overriden by setting the following environment variable.
# DOCKER_TMPDIR=/var/tmp

# Controls the /etc/cron.daily/docker-logrotate cron job status.
# To disable, uncomment the line below.
# LOGROTATE=false

# docker-latest daemon can be used by starting the docker-latest unitfile.
# To use docker-latest client, uncomment below lines
#DOCKERBINARY=/usr/bin/docker-latest
#DOCKERDBINARY=/usr/bin/dockerd-latest
#DOCKER_CONTAINERD_BINARY=/usr/bin/docker-containerd-latest
#DOCKER_CONTAINERD_SHIM_BINARY=/usr/bin/docker-containerd-shim-latest
[ docker]#

Kubernetes(k8s)的RC(Replication Controller)副本控制器

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

[ rc]# kubectl rolling-update myweb -f nginx_rc2.yaml --update-period=10s
Created myweb2
Scaling up myweb2 from 0 to 2, scaling down myweb from 2 to 0 (keep 2 pods available, don‘t exceed 3 pods)
Scaling myweb2 up to 1
Scaling myweb down to 1
Scaling myweb2 up to 2
Scaling myweb down to 0
Update succeeded. Deleting myweb
replicationcontroller "myweb" rolling updated to "myweb2"
[ rc]#

Kubernetes(k8s)的RC(Replication Controller)副本控制器

创建一个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,以及实现滚动升级。

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

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

[ rc]# kubectl rolling-update myweb2 -f nginx_rc.yaml --update-period=10s
Created myweb
Scaling up myweb from 0 to 2, scaling down myweb2 from 2 to 0 (keep 2 pods available, don‘t exceed 3 pods)
Scaling myweb up to 1
Scaling myweb2 down to 1
Scaling myweb up to 2
Scaling myweb2 down to 0
Update succeeded. Deleting myweb2
replicationcontroller "myweb2" rolling updated to "myweb"
[ rc]#

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

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

相关推荐