kubernetes中跨namespace的服务调用 & 外部服务调用 & host配置
1 2 3 4 | /etc # cat resolv.conf nameserver 10.96.0.10 search *****.svc.cluster. local svc.cluster. local cluster. local chn.gbl options ndots:5 |
方式一(集群内的服务调用)
集群内不同namespace中的调用
1 | http: // [serviceName][*****.svc.cluster. local ] |
1 | [*****.svc.cluster. local ]:通过进入pods的etc目录查看 |
查看方式:
1 2 3 4 5 6 7 8 9 | PS D:\*****\jsx>kubectl exec -ti msap-****-deployment-764565cb79-9bt26 /bin/bash -n msap-alpha root@msap-****-deployment-764565cb79-9bt26: /app # nslookup msap-*** Server: 10.96.0.10 Address: 10.96.0.10 #53 Name: msap-***.msap-alpha.svc.cluster. local Address: 10.106.234.33 root@msap-****-deployment-764565cb79-9bt26: /app # |
官网解释:https://kubernetes.io/docs/tasks/administer-cluster/namespaces/
Understanding namespaces and DNS
When you create a Service, it creates a corresponding DNS entry. This entry is of the form <service-name>.<namespace-name>.svc.cluster.local
, which means that if a container just uses <service-name>
it will resolve to the service which is local to a namespace. This is useful for using the same configuration across multiple namespaces such as Development, Staging and Production. If you want to reach across namespaces, you need to use the fully qualified domain name (FQDN).
方式二(添加服务,充当host重定向 & 添加ingress外部可访问)
官网介绍:https://kubernetes.io/docs/concepts/services-networking/service/#externalname
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | # 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: name: mail-api namespace: test spec: externalName: 172.16.10.39 ports: - port: 80 protocol: TCP targetPort: 80 sessionAffinity: None type : ExternalName status: loadBalancer: {} |
Type ExternalName(服务-ip||域名的映射关系,相当于window的host文件的ip-域名映射关系)
Services of type ExternalName map a Service to a DNS(域名系统(服务)协议(DNS)) name, not to a typical selector such as my-service
or cassandra
. You specify these Services with the spec.externalName
parameter.
This Service definition, for example, maps the my-service
Service in the prod
namespace to my.database.example.com
:
1 2 3 4 5 6 7 8 | apiVersion: v1 kind: Service metadata: name: my-service namespace: prod spec: type : ExternalName externalName: my.database.example.com |
Note: ExternalName accepts an IPv4 address string, but as a DNS names comprised of digits, not as an IP address. ExternalNames that resemble IPv4 addresses are not resolved by CoreDNS or ingress-nginx because ExternalName is intended to specify a canonical DNS name. To hardcode an IP address, consider using headless Services.
When looking up the host my-service.prod.svc.cluster.local
, the cluster DNS Service returns a CNAME
record with the value my.database.example.com
.
Accessing my-service
works in the same way as other Services but with the crucial difference that redirection happens at the DNS level rather than via proxying or forwarding. Should you later decide to move your database into your cluster, you can start its Pods, add appropriate selectors or endpoints, and change the Service’s type
.
Warning:You may have trouble using ExternalName for some common protocols, including HTTP and HTTPS. If you use ExternalName then the hostname used by clients inside your cluster is different from the name that the ExternalName references.
For protocols that use hostnames this difference may lead to errors or unexpected responses. HTTP requests will have a
Host:
header that the origin server does not recognize; TLS(安全传输层协议(TLS)) servers will not be able to provide a certificate matching the hostname that the client connected to.
Note: This section is indebted to the Kubernetes Tips - Part 1 blog post from Alen Komljen.
External IPs(跨cluster,服务暴露)
If there are external IPs that route to one or more cluster nodes, Kubernetes Services can be exposed on those externalIPs
.
Traffic that ingresses into the cluster with the external IP (as destination IP), on the Service port, will be routed to one of the Service endpoints. externalIPs
are not managed by Kubernetes and are the responsibility of the cluster administrator.
In the Service spec, externalIPs
can be specified along with any of the ServiceTypes
. In the example below, “my-service
” can be accessed by clients on “80.11.12.10:80
” (externalIP:port
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: MyApp ports: - name: http protocol: TCP port: 80 targetPort: 9376 externalIPs: - 80.11.12.10 |
方式三:修改pods上的host配置(通过修改pod 的yaml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | apiVersion: v1 kind: Pod metadata: name: hostaliases-pod spec: restartPolicy: Never hostAliases: - ip: "127.0.0.1" hostnames: - "foo.local" - "bar.local" - ip: "10.1.2.3" hostnames: - "foo.remote" - "bar.remote" containers: - name: cat -hosts image: busybox command : - cat args: - "/etc/hosts" |
进入pods内部查看host配置:
1 2 3 4 5 6 7 8 9 10 11 12 | root@*****-deployment-77fc8b4645-xmqkk: # cat /etc/hosts # Kubernetes-managed hosts file. 127.0.0.1 localhost ::1 localhost ip6-localhost ip6-loopback fe00::0 ip6-localnet fe00::0 ip6-mcastprefix fe00::1 ip6-allnodes fe00::2 ip6-allrouters # Entries added by HostAliases. 127.0.0.1 foo. local bar. local 10.1.2.3 foo.remote bar.remote |
通过pods创建日志,查看host添加情况
kubectl logs [podName]
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· 面试官:你是如何进行SQL调优的?
2019-04-24 DTS(待了解)
2019-04-24 BPMN(待了解)
2018-04-24 微服务架构与实践9_微服务与持续交付
2018-04-24 微服务架构与实践8_功能迭代