外部访问 k8s pod 应用

/**

个人学习笔记,如有问题欢迎交流,文章编排和格式问题请见谅!

*/

如果单纯启动 pod,没有指定外部的端口和 pod 内部端口映射,除部署的当前 pod 外,集群中的其它节点没有办法访问 pod 中的应用,例如下面的 pod.yaml。

apiVersion: v1 # 指定api版本
kind: Pod # kind: string类型,指定资源类型,例如Pod,Deployment,Service等
metadata: #资源的元数据/属性
  name: nginx #资源的名字,在同一个namespace中必须唯一
  labels: #设定资源的标签,详情请见http://blog.csdn.net/liyingke112/article/details/77482384
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:alpine # 指定镜像
    ports: # 指定暴露端口
    - containerPort: 80

下面通过 HostPort 方式访问 pod 应用。

HostPort 方式访问 pod

pod-nodePort.yaml 文件内容如下所示。

apiVersion: v1 # 指定api版本
kind: Pod # kind: string类型,指定资源类型,例如Pod,Deployment,Service等
metadata: #资源的元数据/属性
  name: nginx #资源的名字,在同一个namespace中必须唯一
  labels: #设定资源的标签,详情请见http://blog.csdn.net/liyingke112/article/details/77482384
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:alpine # 指定镜像
    ports: # 指定暴露端口
       - containerPort: 80
         hostPort: 8088

在 master 节点执行如下命令。

[root@k8s-master pod]# kubectl apply -f pod-nodePort.yaml

在 master 节点或集群中其它节点访问 pod,如下所示。

[root@k8s-master pod]# curl http://192.168.231.150:8088
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@k8s-master pod]#

猜你喜欢

转载自blog.csdn.net/u011074149/article/details/131270218