概述
由于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>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
