原文連接html
有時候須要在集羣中使用外部集羣的服務。node
經過類型爲ExternalName的Service 或 手動設置Endpoints即可實現。nginx
在Service和Pod之間實際上還存在一種資源Endpoints。能夠經過 kubectl describe
來查看到git
$ kubectl describe svc/nginx
Name: nginx
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP: 10.96.27.104
Port: web-8080 8080/TCP
TargetPort: 80/TCP
Endpoints: 172.32.0.6:80,172.40.0.6:80
Session Affinity: None
Events: <none>
$ kubectl get endpoints nginx
NAME ENDPOINTS AGE
nginx 172.32.0.6:80,172.40.0.6:80
複製代碼
在客戶端鏈接到服務時,服務代理選擇Endpoints中存儲的IP:PORT中的一個,而後將連接重定向到該位置。github
做爲Service與Pod中間的關聯層,當Service未定義Pod選擇器時,就不會自動建立Endpoints資源,須要手動建立。web
type Endpoints struct { metav1.TypeMeta `json:",inline"` metav1.ObjectMeta `json:"metadata,omitempty"` // 組成服務的Address的集合。 // 每一個地址對應多個端口,其中有部分已經就緒,有部分未就緒。就緒與未就緒的放入不一樣的Addresses集合中。 // 不存在某個地址同時存在於Addresses與NotReadyAddreses中。 Subsets []EndpointSubset `json:"subsets,omitempty"` } type EndpointSubset struct { // 已經就緒的地址集。這些終端能夠被負載均衡器和客戶端正常發現和使用 Addresses []EndpointAddress `json:"addresses,omitempty"` // 未就緒的地址集,可能因爲未啓動完成、就緒檢查失敗或者最近的存活檢查失敗。 NotReadyAddresses []EndpointAddress `json:"notReadyAddresses,omitempty"` // 在地址上可用的端口集 Ports []EndpointPort `json:"ports,omitempty"` } type EndpointAddress struct { // Endpoint的IP地址 IP string `json:"ip"` // Endpoint的主機名 Hostname string `json:"hostname,omitempty"` // 節點主機名,可使得改Endpoints被部署到指定的節點上 NodeName *string `json:"nodeName,omitempty"` // 引用某個對象來提供Endpoint能力,暫不瞭解 TargetRef *ObjectReference `json:"targetRef,omitempty"` } type EndpointPort struct { // 端口名,必須與Service中定義的ServicePort一致 // 在只有一個端口時爲可選項,多個端口則必填 Name string `json:"name,omitempty"` // Endpoint的端口 Port int32 `json:"port"` // IP協議,支持TCP(默認)、UDP和SCTP Protocol Protocol `json:"protocol,omitempty"` } type Protocol string const ( ProtocolTCP Protocol = "TCP" ProtocolUDP Protocol = "UDP" ProtocolSCTP Protocol = "SCTP" ) 複製代碼
兩種方案實現將外部服務引入集羣json
某些狀況下,應用系統須要將一個非本集羣的服務做爲後端服務進行鏈接,這時候能夠經過建立一個無LabelSelector的Service來實現後端
apiVersion: v1 kind: Service metadata: name: external-svc spec: ports: - port: 80 --- apiVersion: v1 kind: Endpoints metadata: name: external-svc subsets: - addresses: - ip: 47.95.148.6 ports: - port: 80 複製代碼
$ kubectl run -it --rm --generator=run-pod/v1 busybox --image=busybox /bin/sh If you don't see a command prompt, try pressing enter. / # wget external-svc Connecting to external-svc (10.96.138.102:80) saving to 'index.html' index.html 100% |*******************************| 613 0:00:00 ETA 'index.html' saved 複製代碼
建立一個類型爲ExternalName的服務,並配置externalName字段,便可實現。api
此類服務僅在DNS級別爲服務建立了簡單的CNAME記錄。所以鏈接此服務將會直接鏈接到外部的服務,徹底繞過代理。 因此此類服務也不會有集羣IP。bash
apiVersion: v1 kind: Service metadata: name: external-name-svc spec: type: ExternalName externalName: ali.cdnnnn.com ports: - port: 80 複製代碼
# work @ ali in ~/k8s [20:05:42] $ kubectl run -it --rm --generator=run-pod/v1 busybox --image=busybox /bin/sh If you don't see a command prompt, try pressing enter. / # wget external-name-svc Connecting to external-name-svc (47.95.148.6:80) saving to 'index.html' index.html 100% |***********************************************| 613 0:00:00 ETA 'index.html' saved 複製代碼