概述
由于Pod和Service都是k8s集群范围内的虚拟概念,所有集群外的客户端系统无法通过Pod的IP或者Service的虚拟IP和端口访问它们。为了让外部客户端访问这些服务,可以将Pod和Service的端口号映射到宿主机,以使客户端应用能通过物理机访问容器应用。
将容器应用的端口号映射到物理机
-
通过设置容器级别的
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通过物理地址的
IP和8081端口访问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映射到物理机示例
PodapiVersion: v1 kind: Pod metadata: name: webapp labels: app: webapp spec: containers: - name: webapp image: tomcat imagePullPolicy: Never ports: - containerPort: 8080创建
ServiceapiVersion: 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>