Service
测试 Service
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
type: ClusterIP
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
CLB 直连 Pod
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
annotations:
# 显式声明使用直连
service.cloud.tencent.com/direct-access: "true"
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
使用已有 CLB
- 使用已有 CLB
- 多个 Service 复用同一个 CLB
让 Service 使用指定已经创建好的 CLB:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
annotations:
# 指定要已有的 CLB ID
service.kubernetes.io/tke-existed-lbid: "lb-********"
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
多个 Service 可以使用同一个 CLB (前提是端口不冲突):
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
annotations:
# 指定要已有的 CLB ID
service.kubernetes.io/tke-existed-lbid: "lb-********"
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
apiVersion: v1
kind: Service
metadata:
name: nginx2
labels:
app: nginx2
annotations:
# 指定要已有的 CLB ID
service.kubernetes.io/tke-existed-lbid: "lb-********"
spec:
type: LoadBalancer
ports:
- port: 8080
protocol: TCP
targetPort: 80
selector:
app: nginx2
指定 CLB 属性
- 内网 CLB
- 多可用区
- 指定 VIP
默认创建的 CLB 是公网类型,如果要内网类型,需传入子网 ID:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
annotations:
# 指定子网 ID
service.kubernetes.io/qcloud-loadbalancer-internal-subnetid: subnet-********
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
annotations:
# 指定主可用区和备可用区
service.kubernetes.io/service.extensiveParameters: '{ "MasterZoneId": "ap-guangzhou-4", "SlaveZoneId": "ap-guangzhou-8" }'
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
指定 CLB 要使用的 VIP(误删恢复时很有用),前提是 VIP 还没有被占用:
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
annotations:
# 指定主可用区和备可用区
service.kubernetes.io/service.extensiveParameters: '{ Vip: "1.1.1.1" }'
spec:
type: LoadBalancer
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx