概述

由于PodService都是k8s集群范围内的虚拟概念,一切集群外的客户端体系无法经过PodIP或许Service的虚拟IP和端口访问它们。为了让外部客户端访问这些服务,可以将PodService的端口号映射到宿主机,以使客户端使用能经过物理机访问容器使用。

将容器使用的端口号映射到物理机

  • 经过设置容器等级的hostPort,将容器使用的端口号映射到物理机上

    apiVersion: v1
    kind: Pod
    metadata:
      name: webapp
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: tomcat
        ports:
        - containerPort: 8080
          hostPort: 8081
    

    创立并检查成果

    [root@master1 service]# kubectl apply -f 09.yaml
    pod/webapp created
    [root@master1 service]# kubectl get pod
    NAME     READY   STATUS    RESTARTS   AGE
    webapp   1/1     Running   0          4m2s
    

    经过物理地址的IP8081端口访问Pod的容器服务

    <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>
    
  • 经过设置Pod等级的hostNetwork=true

    Pod中一切容器的端口都将被直接映射到物理机上。在设置hostNetwork=true时需要注意,在容器的Ports界说部分假如不指定hostPort,则默认hostPort等于containerPort,假如指定了hostPort,则hostPort必须等于containerPort的值

    apiVersion: v1
    kind: Pod
    metadata:
      name: webapp
      labels:
        app: webapp
    spec:
      hostNetwork: true
      containers:
      - name: webapp
        image: tomcat
        imagePullPolicy: Never
        ports:
        - containerPort: 8080
    

    运转并检查成果

    [root@master1 service]# kubectl apply -f 10.yaml
    pod/webapp created
    [root@master1 service]# kubectl get pod -o wide
    NAME     READY   STATUS    RESTARTS   AGE    IP               NODE    NOMINATED NODE   READINESS GATES
    webapp   1/1     Running   0          5m8s   192.168.40.182   node1   <none>           <none>
    

    访问

<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>

Service映射到物理机

  • 经过设置nodePort映射到物理机

    示例Pod

    apiVersion: v1
    kind: Pod
    metadata:
      name: webapp
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: tomcat
        imagePullPolicy: Never
        ports:
        - containerPort: 8080
    

    创立Service

    apiVersion: v1
    kind: Service
    metadata:
      name: webapp
    spec:
      type: NodePort
      ports:
      - port: 8080
        targetPort: 8080
        nodePort: 30660
      selector:
        app: webapp
    

    创立并查询成果

    [root@master1 service]# kubectl get svc -o wide
    NAME     TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE   SELECTOR
    webapp   NodePort   10.10.157.91   <none>        8080:30660/TCP   54s   app=webapp
    

    经过宿主机 端口访问

    curl 192.168.40.180:30660

    <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>