当前位置: 首页 > news >正文

Kubernetes实战(五)-pod之间网络请求实战

1 同namespace内pod网络请求

1.1 创建namespace ygq

$ kubectl create namespace ygq
namespace/ygq created

1.2 创建svc和deployment 

在naemspace ygq下创建两个应用:nginx和nginx-test。

1.2.1 部署应用nginx

$ cat nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:name: nginxnamespace: ygq
spec:selector:app: nginxports:- port: 80type: ClusterIP
$ cat deployment-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: nginxname: nginxnamespace: ygq
spec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:creationTimestamp: nulllabels:app: nginxspec:containers:- image: docker.io/library/nginx:latestname: nginximagePullPolicy: IfNotPresentimagePullSecrets:- name: harbor-login
$ kubectl apply -f nginx-svc.yaml
$ kubectl apply -f deployment-nginx.yaml
$ kubectl get svc -n ygq 
NAME         TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
nginx        ClusterIP   192.168.245.168   <none>        80/TCP    3d
$ kubectl get pod -n ygq
NAME                          READY   STATUS    RESTARTS        AGE
nginx-547cc75cb7-j46zl        1/1     Running   0               2d22h

1.2.2 部署应用nginx-test

$ cat nginx-test-svc.yaml
apiVersion: v1
kind: Service
metadata:name: nginx-testnamespace: ygq
spec:selector:app: nginx-testports:- port: 80type: ClusterIP
$ cat deployment-nginx-test.yaml
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: nginx-testname: nginx-testnamespace: ygq
spec:replicas: 1selector:matchLabels:app: nginx-testtemplate:metadata:creationTimestamp: nulllabels:app: nginx-testspec:containers:- image: docker.io/library/nginx:latestname: nginximagePullPolicy: IfNotPresentimagePullSecrets:- name: harbor-login
$ kubectl apply -f nginx-test-svc.yaml
$ kubectl apply -f deployment-nginx-test.yaml
$ kubectl get svc -n ygq 
NAME         TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
nginx-test   ClusterIP   192.168.97.154    <none>        80/TCP    3d
$ kubectl get pod -n ygq
NAME                          READY   STATUS    RESTARTS        AGE
nginx-test-6c5f4dfc79-2ldhg   1/1     Running   1 (2d23h ago)   3d

1.3 测试nginx与nginx-test互相访问

1.3.1 nginx访问nginx-test

1.3.1.1 登录nginx pod
$ kubectl exec -it nginx-547cc75cb7-j46zl /bin/bash -n ygq 
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
1.3.1.2 svc name方式访问nginx-test
root@nginx-547cc75cb7-j46zl:/# curl nginx 
<!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.3.1.3 pod ip方式访问nginx-test
# kubectl get pod -n ygq -o wide 
NAME                          READY   STATUS    RESTARTS        AGE     IP              NODE                      NOMINATED NODE   READINESS GATES
nginx-test-6c5f4dfc79-2ldhg   1/1     Running   1 (2d23h ago)   3d      172.20.176.17   cn-shanghai.10.12.46.85   <none>           <none>

pod ip是172.20.176.17。

root@nginx-547cc75cb7-j46zl:/# curl http://172.20.176.17: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.3.1.4 dns方式访问

k8s 中dns的组成结构为:service_name.namespace_name.svc.cluster.local:port,可简写为service_name.namespace_name.svc:port。

deployment nginx-test的端口为80,其dns为:nginx-test.ygq.svc.cluster.local:80,简写为:nginx-test.ygq.svc:80。

1)完整dns

root@nginx-547cc75cb7-j46zl:/# curl http://nginx-test.ygq.svc.cluster.local: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>

2)简写dns

root@nginx-547cc75cb7-j46zl:/# curl http://nginx-test.ygq.svc: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.3.2  nginx-test访问nginx

1.3.2.1 登录nginx-test pod
$ kubectl exec -it nginx-test-6c5f4dfc79-2ldhg /bin/bash -n ygq 
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
1.3.2.2 svc name方式访问nginx
root@nginx-test-6c5f4dfc79-2ldhg:/# curl nginx 
<!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.3.2.3 pod ip方式访问nginx
$ kubectl get pod -n ygq -o wide 
NAME                          READY   STATUS    RESTARTS        AGE     IP              NODE                      NOMINATED NODE   READINESS GATES
nginx-547cc75cb7-j46zl        1/1     Running   0               2d23h   172.20.176.24   cn-shanghai.10.12.46.85   <none>           <none>
root@nginx-test-6c5f4dfc79-2ldhg:/# curl http://172.20.176.24: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.3.2.4 dns方式访问

deployment nginx的端口为80,其dns为:nginx.ygq.svc.cluster.local:80,简写为:nginx.ygq.svc:80。

1)完整dns

root@nginx-test-6c5f4dfc79-2ldhg:/# curl nginx.ygq.svc.cluster.local: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>

2)简写dns

root@nginx-test-6c5f4dfc79-2ldhg:/# curl nginx.ygq.svc: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.4 结论

同namespace下不同pod直接可通过svc name、pod ip及dns互相访问。

pod ip是不固定的,会伴随pod的状态变化发生改变,生产环境不建议使用pod ip作为请求地址。 

2 不同namespace间pod网络请求

2.1 创建namespace dev

$ kubectl create namespace dev
namespace/dev created

2.2 创建svc和deployment 

在naemspace dev下创建应用:nginx-dev。

2.2.1 部署应用nginx-dev

$ cat deployment-nginx-dev.yaml
apiVersion: apps/v1
kind: Deployment
metadata:creationTimestamp: nulllabels:app: nginx-devname: nginx-devnamespace: dev
spec:replicas: 4selector:matchLabels:app: nginx-devtemplate:metadata:creationTimestamp: nulllabels:app: nginx-devspec:containers:- image: docker.io/library/nginx:latestname: nginximagePullPolicy: IfNotPresentimagePullSecrets:- name: harbor-login
$ cat nginx-dev-svc.yaml
apiVersion: v1
kind: Service
metadata:name: nginx-devnamespace: dev
spec:selector:app: nginx-devports:- port: 80type: ClusterIP
$ kubectl apply -f nginx-dev-svc.yaml
$ kubectl apply -f deployment-nginx-dev.yaml
# kubectl get svc -n dev
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
nginx-dev   ClusterIP   192.168.28.113   <none>        80/TCP    3d
$ kubectl get pod -n dev
NAME                         READY   STATUS    RESTARTS     AGE
nginx-dev-5966c9747d-gbdq4   1/1     Running   1 (3d ago)   3d

2.3 测试nginx与nginx-dev互相访问

2.3.1 nginx访问nginx-dev

2.3.1.1 登录nginx pod
$ kubectl exec -it nginx-547cc75cb7-j46zl /bin/bash -n ygq 
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
2.3.1.2 svc name方式访问
root@nginx-547cc75cb7-j46zl:/# curl nginx-dev
curl: (6) Could not resolve host: nginx-dev
2.3.1.3 pod ip方式访问 
$ kubectl get pod -n dev -o wide
NAME                         READY   STATUS    RESTARTS     AGE   IP             NODE                      NOMINATED NODE   READINESS GATES
nginx-dev-5966c9747d-gbdq4   1/1     Running   1 (3d ago)   3d    172.20.176.9   cn-shanghai.10.12.46.85   <none>           <none>
root@nginx-547cc75cb7-j46zl:/# curl 172.20.176.9: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>
2.3.1.4 dns方式访问

deployment nginx-dev的端口为80,其dns为:nginx-dev.dev.svc.cluster.local:80,简写为:nginx-dev.dev.svc:80。

1)完整dns

root@nginx-547cc75cb7-j46zl:/# curl nginx-dev.dev.svc.cluster.local: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>

2)简写dns

root@nginx-547cc75cb7-j46zl:/# curl nginx-dev.dev.svc: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>

2.3.2 nginx-dev访问nginx

2.3.2.1 登录nginx-dev pod
$ kubectl exec -it nginx-dev-5966c9747d-gbdq4 /bin/bash -n dev
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
2.3.2.2 svc name方式访问
root@nginx-dev-5966c9747d-gbdq4:/# curl nginx    
curl: (6) Could not resolve host: nginx
2.3.2.3 pod ip方式访问
$ kubectl get pod -n ygq -o wide 
NAME                          READY   STATUS    RESTARTS     AGE     IP              NODE                      NOMINATED NODE   READINESS GATES
nginx-547cc75cb7-j46zl        1/1     Running   0            2d23h   172.20.176.24   cn-shanghai.10.12.46.85   <none>           <none>
root@nginx-dev-5966c9747d-gbdq4:/# curl 172.20.176.24: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>
2.3.2.4 dns方式访问

deployment nginx的端口为80,其dns为:nginx.ygq.svc.cluster.local:80,简写为:nginx.ygq.svc:80。

1)完整dns

root@nginx-dev-5966c9747d-gbdq4:/# curl nginx.ygq.svc.cluster.local: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>

2)简写dns

root@nginx-dev-5966c9747d-gbdq4:/# curl nginx.ygq.svc: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>

2.4 结论 

不同namespace下pod直接可通过pod ip及dns互相访问,但不能通过svc name进行访问

pod ip是不固定的,会伴随pod的状态变化发生改变,生产环境不建议使用pod ip作为请求地址。 

3 pod name实战

3.1 同一namespace下

3.1.1 deployment

$ kubectl get pod -n ygq  -o wide 
NAME                          READY   STATUS    RESTARTS     AGE    IP              NODE                      NOMINATED NODE   READINESS GATES
nginx-547cc75cb7-j46zl        1/1     Running   0            3d     172.20.176.24   cn-shanghai.10.12.46.85   <none>           <none>
nginx-test-6c5f4dfc79-2ldhg   1/1     Running   1 (3d ago)   3d2h   172.20.176.17   cn-shanghai.10.12.46.85   <none>           <none>
$ kubectl create -f deployment-nginx.yaml 
Error from server (AlreadyExists): error when creating "deployment-nginx.yaml": deployments.apps "nginx" already exists

3.1.2 Service

$ kubectl get svc -n ygq  -o wide 
NAME         TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE    SELECTOR
nginx        ClusterIP   192.168.245.168   <none>        80/TCP    3d1h   app=nginx
nginx-test   ClusterIP   192.168.97.154    <none>        80/TCP    3d1h   app=nginx-test
$ kubectl create -f nginx-svc.yaml
Error from server (AlreadyExists): error when creating "nginx-svc.yaml": services "nginx" already exists

3.2 不同namespace 

3.2.1 deployment

$ kubectl get pod -n dev -o wide 
NAME                         READY   STATUS    RESTARTS       AGE    IP              NODE                      NOMINATED NODE   READINESS GATES
nginx-cfcb57f6d-vr79r        1/1     Running   0              10s    172.20.176.28   cn-shanghai.10.12.46.85   <none>           <none>
nginx-dev-5966c9747d-gbdq4   1/1     Running   1 (3d1h ago)   3d1h   172.20.176.9    cn-shanghai.10.12.46.85   <none>           <none>
$ kubectl get pod -n ygq  -o wide 
NAME                          READY   STATUS    RESTARTS     AGE    IP              NODE                      NOMINATED NODE   READINESS GATES
nginx-547cc75cb7-j46zl        1/1     Running   0            3d     172.20.176.24   cn-shanghai.10.12.46.85   <none>           <none>
nginx-test-6c5f4dfc79-2ldhg   1/1     Running   1 (3d ago)   3d2h   172.20.176.17   cn-shanghai.10.12.46.85   <none>           <none>

3.2.2 Service 

$ kubectl get svc -n dev -o wide 
NAME        TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE    SELECTOR
nginx       ClusterIP   192.168.87.200   <none>        80/TCP    7s     app=nginx
nginx-dev   ClusterIP   192.168.28.113   <none>        80/TCP    3d1h   app=nginx-dev
$ kubectl get svc -n ygq  -o wide 
NAME         TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE    SELECTOR
nginx        ClusterIP   192.168.245.168   <none>        80/TCP    3d1h   app=nginx
nginx-test   ClusterIP   192.168.97.154    <none>        80/TCP    3d1h   app=nginx-test

3.3 结论

不同namescpace下可以存在相同名称的资源,同一namespace下不允许有相同名称的资源。

4 总结

  • 同一namespace下的应用可以通过svc name、pod ip和dns互相访问,不同namespace下可以通过pod ip和dns互相访问。
  • 同一namespace下不允许有相同名称的资源,不同namescpace下可以存在名字一样的资源。

 

 

 

 

http://www.lryc.cn/news/236679.html

相关文章:

  • 7年经验之谈 —— 如何高效的开展app的性能测试?
  • 小程序action-sheet结合自定义tabbar显示
  • 机器学习笔记 - 隐马尔可夫模型的简述
  • iOS学习 --- Xcode 15 下载iOS_17.0.1_Simulator失败解决方法
  • 多视图聚类论文阅读(二)
  • Docker在Centos7下的安装
  • LLM大模型4位量化实战【GPTQ】
  • 安装keras、tensorflow
  • ffmpeg知识点整理
  • Git 笔记之gitignore
  • 【配置】Redis常用配置详解
  • Linux(Ubuntu)安装JDK环境
  • OpenCV C++ 张正友相机标定【相机标定原理、相机标定流程、图像畸变矫正】
  • SDL2 播放音频(MP4)
  • WMS仓库管理系统库位功能
  • vue2组件通信中的一些拓展(props,emit,ref父子双向传参)
  • Flink1.17 DataStream API
  • 数据结构中树、森林 与 二叉树的转换
  • 力扣labuladong——一刷day43
  • MapApp 地图应用
  • Java之反射获取和赋值字段
  • ckplayer自己定义风格播放器的开发记录
  • 全网最全Django面试题整理(一)
  • vue统一登录
  • MVSNet论文笔记
  • 大型 APP 的性能优化思路
  • K8S配置资源管理
  • Redis 的集群模式实现高可用
  • 21、嵌套路由实战操作
  • WPF 控件的缩放和移动