外部访问 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]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值