从零开始学习k8s-08访问pod
点点寒彬 2021-11-03 23:24:19
kubernetes
背景
我们之前跟着官方文档学习了k8s
中pod
的基本使用,那么现在就遇到一个问题了,我们创建的pod
是没办法访问的。本文就继续跟着官方文档来学习如何访问pod
的。
创建一个Nginx Pod
我们还是使用之前的方案,创建一个nginx
的pod
apiVersion: v1
kind: Pod
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
这里的yaml
文件跟之前的有一些差异,增加了labels
,用来后面我们创建service
的时候选择使用的。我们尝试创建并访问它
kubectl get pods -l run=my-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx 1/1 Running 0 24s 172.17.0.9 minikube <none> <none>
$ curl localhost:80
curl: (7) Failed to connect to ::1: Network is unreachable
curl http://localhost:80
curl: (7) Failed to connect to ::1: Network is unreachable
curl http://172.17.0.9:80
curl: (7) Failed to connect to ::1: Network is unreachable
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
可以看到,这个nginx
是完全无法访问的。似乎这个k8s给我们分配的IP
是一个假IP
。或者,这个nginx
压根就没起来?
kubectl exec -it my-nginx -- /bin/bash
root@my-nginx:/# curl localhost:80
<!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>
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
可以看到,我们进入容器时候,是能够正常的访问nginx
,也就是说,没有办法从外部来访问。
通过Service暴露端口
执行命令
kubectl expose pod/my-nginx
service/my-nginx exposed
1
2
2
这个时候我们端口就暴露出来了,这个命令相当于我们为这个pod
创建了一个service
。
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
直接那这个yaml
来创建,也是可以的。这里的service
通过之前我们创建容器的label
的run: my-nginx
来定位这个service
为哪个pod
转发请求。
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20h
my-nginx ClusterIP 10.110.98.179 <none> 80/TCP 13m
1
2
3
4
2
3
4
执行这个命令就可以看到我们创建的service
列表了。
这个时候,我们在集群的任意一个节点访问这个地址,就能看到nginx
返回的信息了。
kubectl get ep
NAME ENDPOINTS AGE
my-nginx 100.121.138.194:80 13m
curl 100.121.138.194:80
<!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>
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
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
参考资料
https://kubernetes.io/zh/docs/concepts/services-networking/connect-applications-service/