Kubernetes的外部服務(wù)管理

場景

在Kubernetes中引入外部服務(wù),大致分為兩種場景:

  • 容器訪問外部獨(dú)立服務(wù)

部署在Kubernetes集群中的容器需要訪問外部服務(wù)時(shí),例如MySQL時(shí),需要在代碼中填寫MySQl的外部IP地址。引入Endpoint后,只需要?jiǎng)?chuàng)建外部服務(wù)的Endpoint和Service后,容器就能通過ServiceName訪問外部服務(wù)了。

  • LoadBalance引入外部服務(wù)

Kubernetes開發(fā)的負(fù)載均衡器,都有一個(gè)優(yōu)點(diǎn),能夠?qū)崟r(shí)更新后端容器服務(wù)的IP地址。基于這點(diǎn),現(xiàn)公司架構(gòu),可以利用K8s負(fù)載均衡器的特性,將自己傳統(tǒng)的負(fù)載均衡器納入到K8s的管理中來。

方案

1. 容器訪問外部獨(dú)立服務(wù)

  • 外部服務(wù)的Service,以Mysql為例
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
    - port: 3306
  • 外部服務(wù)的Eedpoint
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql-production
  namespace: default
subsets:
- addresses:
  - ip: 10.17.72.2
    nodeName: 10.17.72.2
  ports:
  - port: 3306

2. LoadBalance引入外部服務(wù)

  • 外部服務(wù)的Service,以Nginx為例
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: nginx
  name: nginx
spec:
  clusterIP: 10.33.80.219
  ports:
  - name: port443
    port: 443
    protocol: TCP
    targetPort: 443
  - name: port80
    port: 80
    protocol: TCP
    targetPort: 80
  sessionAffinity: ClientIP
  type: ClusterIP
  • 外部服務(wù)的Endpoint
apiVersion: v1
kind: Endpoints
metadata:
  name: nginx
subsets:
- addresses:
  - ip: 10.20.6.129
    nodeName: 10.17.64.38
  - ip: 10.20.6.194
    nodeName: 10.17.64.39
  - ip: 10.20.7.1
    nodeName: 10.17.64.40
  ports:
  - port: 443
    name: port443
    protocol: TCP
  - port: 80
    name: port80
    protocol: TCP

在LoadBalance上可以看到Service的更新

# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.33.80.219:80 rr persistent 10800
  -> 10.20.6.129:80               Masq    1      0          0         
  -> 10.20.6.194:80               Masq    1      0          0         
  -> 10.20.7.1:80                 Masq    1      0          0         
TCP  10.33.80.219:443 rr persistent 10800
  -> 10.20.6.129:443              Masq    1      0          0         
  -> 10.20.6.194:443              Masq    1      0          0         
  -> 10.20.7.1:443                Masq    1      0          0   
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • kubernetes 簡介 一個(gè)迅速過一遍kubernetes 非常不錯(cuò)的資源:基于Kubernetes構(gòu)建Doc...
    bradyjoestar閱讀 15,299評論 2 7
  • ?Kubernetes介紹1.背景介紹云計(jì)算飛速發(fā)展- IaaS- PaaS- SaaSDocker技術(shù)突飛猛進(jìn)-...
    Zero___閱讀 14,760評論 0 21
  • 這篇博客中我們將比較Kubernetes與Mesos + Marathon容器編排解決方案。我們將深入的討論Kub...
    shangyaqi閱讀 17,185評論 0 18
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,948評論 18 139
  • Service Mesh新秀,初出茅廬便聲勢浩蕩,前有Google,IBM和Lyft傾情奉獻(xiàn),后有業(yè)界大佬俯首膜拜...
    datazhen閱讀 5,774評論 1 31