Kubernetes的外部服务管理
2017-11-16 本文已影响80人
魔哈Moha
场景
在Kubernetes中引入外部服务,大致分为两种场景:
- 容器访问外部独立服务
部署在Kubernetes集群中的容器需要访问外部服务时,例如MySQL时,需要在代码中填写MySQl的外部IP地址。引入Endpoint后,只需要创建外部服务的Endpoint和Service后,容器就能通过ServiceName访问外部服务了。
- LoadBalance引入外部服务
Kubernetes开发的负载均衡器,都有一个优点,能够实时更新后端容器服务的IP地址。基于这点,现公司架构,可以利用K8s负载均衡器的特性,将自己传统的负载均衡器纳入到K8s的管理中来。
方案
1. 容器访问外部独立服务
- 外部服务的Service,以Mysql为例
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
- 外部服务的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引入外部服务
- 外部服务的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
- 外部服务的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