zoukankan      html  css  js  c++  java
  • Kubernetes-Host网络模式应用

    目录贴:Kubernetes学习系列

      在实际生产环境中,有些容器内应用(比如编码器)需要用到物理层面的网络资源(比如组播流)。这就要求Kubernetes中的该Pod以HOST模式来启动。以下实验了Kubernetes-HOST网络模式,并给出了一些运维建议。

    1、Pod的网络

      每个Pod都会默认启动一个pod-infrastructure(或pause)的容器,作为共享网络的基准容器。其他业务容器在启动之后,会将自己的网络模式指定为“"NetworkMode": "container:pause_containerID”。这样就能做到Pod中的所有容器网络都是共享的,一个Pod中的所有容器中的网络是一致的,它们能够通过本地地址(localhost)访问其他用户容器的端口。在Kubernetes的网络模型中,每一个Pod都拥有一个扁平化共享网络命名空间的IP,称为PodIP。通过PodIP,Pod就能够跨网络与其他物理机和容器进行通信。

      也可以设置Pod为Host网络模式,即直接使用宿主机的网络,不进行网络虚拟化隔离。这样一来,Pod中的所有容器就直接暴露在宿主机的网络环境中,这时候,Pod的PodIP就是其所在Node的IP。从原理上来说,当设定Pod的网络为Host时,是设定了Pod中pod-infrastructure(或pause)容器的网络为Host,Pod内部其他容器的网络指向该容器。如下所示(65070affecfc61为业务容器,f60a2ee415e3为pod-infrastructure容器):

    [root@k8s-node-3 ~]# docker inspect 65070affecfc6131b2385e5c40d4f21f73c343cc15e7983cdce8594e38ed020f | grep NetworkMode
                "NetworkMode": "container:f60a2ee415e301491f30ffc12855880273da6eded2526a5319eed72a92caef7f",
    [root@k8s-node-3 ~]# docker inspect f60a2ee415e301491f30ffc12855880273da6eded2526a5319eed72a92caef7f  | grep NetworkMode
                "NetworkMode": "host",
    [root@k8s-node-3 ~]#

    2、启动示例

    [root@k8s-master yaml]# cat test-host.yaml 
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: test-host
    spec:
      replicas: 4
      template:
        metadata:
          labels: 
            name: test-host
        spec:
          containers:
          - name: test-host
            image: registry:5000/back_demon:1.0
            command:
            - /jboss/jboss-eap-6.1/bin/standalone.sh 
            ports:
            - containerPort: 8080
          hostNetwork: true

    3、运维经验

    3.1 副本数量

      对于同Deployment下的Host模式启动的Pod,每个node上只能启动一个。也就是说,Host模式的Pod启动副本数不可以多于“目标node”的数量,“目标node”指的是在启动Pod时选定的node,若未选定(没有指定nodeSelector),“目标node”的数量就是集群中全部的可用的node的数量。当副本数大于“目标node”的数量时,多出来的Pod会一直处于Pending状态,因为schedule已经找不到可以调度的node了。

      以下示例中,集群只有4个node,当设置副本数量为5时,最后一个Pod状态会一直处于Pending状态。

    [root@k8s-master yaml]# kubectl get pod -o wide
    NAME                         READY     STATUS    RESTARTS   AGE       IP             NODE
    test-host-1108333573-11wbl   1/1       Running   0          17s       10.0.251.153   k8s-node-1
    test-host-1108333573-2k35s   1/1       Running   0          17s       10.0.251.146   k8s-node-3
    test-host-1108333573-lnlpy   1/1       Running   0          17s       10.0.251.222   k8s-node-4
    test-host-1108333573-t6izr   1/1       Running   0          17s       10.0.251.155   k8s-node-2
    test-host-1108333573-tf4mc   0/1      Pending   0          17s       <none>         

    3.2 PodIP

      Pod的PodIP就是其所在Node的IP,具体如下:

    [root@k8s-master yaml]# kubectl get pod -o wide
    NAME                         READY     STATUS    RESTARTS   AGE       IP             NODE
    test-host-1108333573-11wbl   1/1       Running   0          2h        10.0.251.153   k8s-node-1
    test-host-1108333573-2k35s   1/1       Running   0          2h        10.0.251.146   k8s-node-3
    test-host-1108333573-lnlpy   1/1       Running   0          2h        10.0.251.222   k8s-node-4
    test-host-1108333573-t6izr   1/1       Running   0          2h        10.0.251.155   k8s-node-2

    虽然PodIP是NodeIP,但也可以通过service的方式加到Kubernetes的虚拟化网络中被使用。具体如下:

    [root@k8s-master yaml]# cat demon1/frontend-service.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: frontend-service
      labels:
        name: frontend-service
    spec:
      type: NodePort
      ports:
      - port: 8080
        nodePort: 30002
      selector:
        name: frontend-service
    [root@k8s-master yaml]# kubectl create -f test-host-svc.yaml 
    service "test-host" created
    [root@k8s-master yaml]# kubectl describe svc test-host 
    Name:            test-host
    Namespace:        default
    Labels:            name=test-host
    Selector:        name=test-host
    Type:            NodePort
    IP:            10.254.127.198
    Port:            <unset>    8080/TCP
    NodePort:        <unset>    30003/TCP
    Endpoints:        10.0.251.146:8080,10.0.251.153:8080,10.0.251.155:8080 + 1 more...
    Session Affinity:    None
    No events.

      创建成功之后,集群内部的应用可通过虚拟网络中的clusterIP:10.254.127.198:8080来访问后端服务,集群外部的应用(如浏览器)可以通过nodeIP+NodePort来访问后端服务。

    3.3 端口占用

    3.3.1 其他Pod端口占用

      若同集群中,用host模式启动的deployment(或RC)有多个,若这些 deployment中定义的containerPort有相同的值,那么,Kubernetes会校验出端口资源的冲突。示例如下:

    [root@k8s-master yaml]# cat test-host.yaml 
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: test-host
    spec:
      replicas: 3
      template:
        metadata:
          labels: 
            name: test-host
        spec:
          containers:
          - name: test-host
            image: registry:5000/back_demon:1.0
            command:
            - /jboss/jboss-eap-6.1/bin/standalone.sh 
            ports:
            - containerPort: 8080
          hostNetwork: true
    [root@k8s-master yaml]# cat test-host1.yaml 
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
      name: test-host1
    spec:
      replicas: 2
      template:
        metadata:
          labels: 
            name: test-host1
        spec:
          containers:
          - name: test-host1
            image: registry:5000/back_demon:1.0
            command:
            - /jboss/jboss-eap-6.1/bin/standalone.sh 
            ports:
            - containerPort: 8080
          hostNetwork: true
    [root@k8s-master yaml]#  kubectl get pod -o wide
    NAME                         READY     STATUS    RESTARTS   AGE       IP             NODE
    test-host-1108333573-9oo55   1/1       Running   0          19m       10.0.251.155   k8s-node-2
    test-host-1108333573-uc2v2   1/1       Running   0          19m       10.0.251.146   k8s-node-3
    test-host-1108333573-vxezq   1/1       Running   0          19m       10.0.251.153   k8s-node-1
    test-host1-6476903-q8e6o     0/1       Pending   0          19m       <none>         
    test-host1-6476903-zmk5l     1/1       Running   0          19m       10.0.251.222   k8s-node-4

      实验集群下,可用的node数量只有4个,而两个deployment——test-host、test-host1对外暴露额端口是相同的——8080。两个deployment一共要启动的副本数量为5,这时,最后启动的那个Pod就会一直处于Pending状态。

    3.3.2 宿主机端口占用

      当Host模式的Deployment(或RC)声明一个端口时,比如8080,若宿主机上有非Kubernetes控制的程序占用了8080这个端口,这时Kubernetes是无法校验到的。也就是说,schedule仅仅会记录Kubernetes集群中的端口占用信息,并在调度时做相关的校验工作。但schedule不会校验宿主机上真正的端口占用信息。这其实是非常合理的,集群中的node通常成千上万,被当做一台台单纯的提供计算能力的资源,计算什么由中心节点来决定。没有必要,也没有场景需要在node上额外的跑其他程序。

    在使用Host模式网络时,需注意的是,每个应用(部署成一个deployment)都要有自己固定的、和其他应用不同的端口,比如编码器永远固定成9525、源服务器永远固定成9537等。且宿主机在做了Kubernetes集群中的node之后,尽量不对非本集群应用提供服务。

    3.4 镜像制作要求

      必须用Host模式启动的Pod,在镜像制作时要求端口唯一、且单一。

      一般Pod中只会存在一个业务主镜像,该业务镜像在制作时,应该只放一种应用,这个应用只对外开放一个接口。例如,编码器主控节点这个应用,主要有两方面的功能:1)接收组播流,并控制处理节点,占用端口9525;2)可视化操控界面,占用端口8080。其中接收组播流这块,需要使用Host模式的网络。拆分建议为两个业务镜像,部署时部署两个deployment。即接收组播流拆成一个镜像,固定端口9525,使用Host模式启动;可视化界面拆成一个镜像,用Kubernetes默认网络模式启动。

  • 相关阅读:
    【javascript基础】【转】各浏览器对页面外部资源加载的策略
    【javascript基础】【转】javascript模块化、模块加载器初探
    【javascript基础】js线程机制【转】
    【css】【转】那些年我们一起清除过的浮动
    IL入门之旅(三)——Dump对象
    学习TPL(一)
    弱引用应用的注意点
    IL入门之旅(二)——动态包装
    学习TPL(二)
    IL之旅(前言)
  • 原文地址:https://www.cnblogs.com/zhenyuyaodidiao/p/6739099.html
Copyright © 2011-2022 走看看