5分钟带你体验kubernetes 对外提供访问

作者声明:本篇文章系本人依照真实部署过程原创,未经许可,谢绝转载。

上一篇文章介绍了kubernetes kubernetes RollingUpdate滚动升级镜像回滚,这一篇文章带你来 5分钟带你体验kubernetes 对外提供访问

环境准备

本文是之前环境的延续。

kubernetes 对外提供访问

在master我们执行命令查看pod

1
2
3
4
5
#  kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-799bbcd6b4-8nz5g 1/1 Running 0 91s 10.244.1.30 node01.k8s.com <none> <none>
myapp-799bbcd6b4-jms4x 1/1 Running 0 92s 10.244.1.29 node01.k8s.com <none> <none>
myapp-799bbcd6b4-l7q6v 1/1 Running 0 89s 10.244.2.34 node02.k8s.com <none> <none>

在master我们执行命令查看service

1
2
3
4
5
查看 service
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 72m
myapp ClusterIP 10.100.210.72 <none> 80/TCP 17m

我们想要访问pod,目前只能在虚拟机的节点上访问,10.244.XXX.XXX的网址或者通过service服务的ip访问,
如果我们想要在虚拟机之外访问集群内部的地址,这个时候我们需要执行命令修改service的文件

1
2
master上执行命令
# kubectl edit svc myapp

得到如下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2019-03-07T05:53:57Z"
labels:
run: myapp
name: myapp
namespace: default
resourceVersion: "5195"
selfLink: /api/v1/namespaces/default/services/myapp
uid: 38910862-4942-11e9-bbc4-000c296cc22a
spec:
clusterIP: 10.100.210.72
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: myapp
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}

找到 type: ClusterIP 修改为 type: NodePort
修改后结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2019-03-18T05:53:57Z"
labels:
run: myapp
name: myapp
namespace: default
resourceVersion: "5195"
selfLink: /api/v1/namespaces/default/services/myapp
uid: 38910862-4942-11e9-bbc4-000c296cc22a
spec:
clusterIP: 10.100.210.72
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
run: myapp
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}

保存退出
再次查看service

1
2
3
4
# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4h14m
myapp NodePort 10.100.210.72 <none> 80:32313/TCP 3h19m

发现myapp service的prot变为 80:32313/TCP,32313为master主机对外访问的端口号。
master的ip为 192.168.110.140,此时我们想要在虚拟机外部访问集群内部pod,需要使用 http://192.168.110.140:32313
在master节点上执行命令查看效果

1
2
3
4
5
6
7
8
9
10
# curl http://192.168.110.140:32313
V1
myapp-799bbcd6b4-l7q6v
# curl http://192.168.110.140:32313
V1
myapp-799bbcd6b4-jms4x
# curl http://192.168.110.140:32313
V1
myapp-799bbcd6b4-8nz5g
# curl http://192.168.110.140:32313

同时我在虚拟机的外部机器上访问 http://192.168.110.140:32313

以上是kubernetes 对外提供访问 整个过程