kubernetes中跨namespace的服務調用 & 外部服務調用 & host配置


/etc# cat resolv.conf
nameserver 10.96.0.10
search *****.svc.cluster.local svc.cluster.local cluster.local chn.gbl
options ndots:5

 

方式一(集群內的服務調用)

集群內不同namespace中的調用

http://[serviceName][*****.svc.cluster.local]
[*****.svc.cluster.local]:通過進入pods的etc目錄查看

 查看方式:

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

# 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:

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)

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)

官方介紹:https://kubernetes.io/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/

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配置:

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]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM