集群内部,相同namespace内和不同namespace如何服务发现

1)相同namespace 服务访问

[root@master01 ~]# kubectl get svc 
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.1.0.1      <none>        443/TCP    25d
nginx-svc    ClusterIP   10.1.114.3    <none>        80/TCP     2d23h
tomcat-svc   ClusterIP   10.1.87.138   <none>        8080/TCP   16m

1,通过env 环境变量:

root@web-test-f47f968f6-wbkv4:/# env
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=web-test-f47f968f6-wbkv4
PWD=/
NGINX_SVC_PORT_80_TCP=tcp://10.1.114.3:80
NGINX_SVC_PORT=tcp://10.1.114.3:80
PKG_RELEASE=1~bullseye
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.1.0.1:443
NGINX_SVC_SERVICE_PORT=80
TOMCAT_SVC_SERVICE_PORT=8080
TOMCAT_SVC_PORT_8080_TCP_PROTO=tcp
NGINX_SVC_PORT_80_TCP_ADDR=10.1.114.3
NJS_VERSION=0.7.1
TOMCAT_SVC_SERVICE_HOST=10.1.87.138
TERM=xterm
NGINX_SVC_PORT_80_TCP_PORT=80
TOMCAT_SVC_PORT_8080_TCP=tcp://10.1.87.138:8080
TOMCAT_SVC_PORT_8080_TCP_PORT=8080
SHLVL=1
TOMCAT_SVC_PORT_8080_TCP_ADDR=10.1.87.138
NGINX_SVC_PORT_80_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PROTO=tcp
TOMCAT_SVC_PORT=tcp://10.1.87.138:8080
KUBERNETES_PORT_443_TCP_ADDR=10.1.0.1
KUBERNETES_SERVICE_HOST=10.1.0.1
KUBERNETES_PORT=tcp://10.1.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NGINX_VERSION=1.21.5
NGINX_SVC_SERVICE_HOST=10.1.114.3
_=/usr/bin/env




root@web-test-f47f968f6-wbkv4:/# curl $TOMCAT_SVC_SERVICE_HOST:8080

2,直接通过服务名字

root@web-test-f47f968f6-wbkv4:/# curl tomcat-svc:8080

2)不同namespace服务访问, svcname.namespacename.svc.cluster.local

**cluster.local ** 集群默认域名

[root@master01 ~]# kubectl get svc 
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   10.1.0.1      <none>        443/TCP    25d
nginx-svc    ClusterIP   10.1.114.3    <none>        80/TCP     2d23h
tomcat-svc   ClusterIP   10.1.87.138   <none>        8080/TCP   16m
[root@master01 ~]# 
[root@master01 ~]# kubectl get svc -n project 
NAME        TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
nginx-svc   ClusterIP   10.1.54.41   <none>        80/TCP    15m

[root@master01 ~]# kubectl exec -ti  web-test-f47f968f6-wbkv4  -- /bin/bash
root@web-test-f47f968f6-wbkv4:/# curl nginx-svc.project.svc.cluster.local
<!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>
文章作者: emporer
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Emporer-Linux
kubernetes 服务发现 kubernetes
喜欢就支持一下吧