上云无忧 > 文档中心 > 百度智能云容器引擎服务 CCE 通过YAML创建LoadBalancer_Service
容器引擎服务CCE
百度智能云容器引擎服务 CCE 通过YAML创建LoadBalancer_Service

文档简介:
本文档会详细介绍如何在CCE下创建类型是 LoadBalancer 的 Service。 注:以下 annotation 对 1.16.3 以下版本可能不生效,辛苦工单联系管理员处理。 当用户创建类型是 LoadBalancer 的 Service,默认情况下,CCE 会联动的创建 BLB,并为此 BLB 绑定 EIP。
*此产品及展示信息均由百度智能云官方提供。免费试用 咨询热线:400-826-7010,为您提供专业的售前咨询,让您快速了解云产品,助您轻松上云! 微信咨询
  免费试用、价格特惠



本文档会详细介绍如何在CCE下创建类型是 LoadBalancer 的 Service。

注:以下 annotation 对 1.16.3 以下版本可能不生效,辛苦工单联系管理员处理

Kubernetes官方教程:Services

快速开始

当用户创建类型是 LoadBalancer 的 Service,默认情况下,CCE 会联动的创建 BLB,并为此 BLB 绑定 EIP。

以创建一个简单的 Nginx 为例:


--- kind: Service apiVersion: v1 metadata: name: nginx-service spec: selector: app: nginx 
type: LoadBalancer ports: - name: nginx-port port: 80 targetPort: 80 protocol: TCP 
--- apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selector:
 matchLabels: app: nginx replicas: 1 template: metadata: labels: app: nginx spec: containers:
 - name: nginx image: nginx ports: - containerPort: 80


(1)创建

$ kubectl create -f nginx.yaml

(2)查询EIP

IP 8.8.8.8 即为此Nginx的EIP。

$ kubectl get svc
NAME            CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
nginx-service   1.1.1.1        8.8.8.8          80:30274/TCP   5m

(3)查询BLB

$ kubectl get svc nginx-service -o jsonpath={.metadata.annotations}
map[service.beta.kubernetes.io/cce-load-balancer-id:lb-xxxxxx]

lb-xxxxxx即为此Service的BLB的id。

(4)访问测试

$ curl -i http://8.8.8.8

三种外部流量策略的 LoadBalancer Service

CCE 提供三种外部流量策略的 LoadBalancer Service,本文称其为 Cluster 模式、Local 模式与 LB 直连 Pod 模式。

对于 Cluster 模式的 LB Service,当其收到数据包后,负载均衡器将数据包发往集群的某个节点上,进而节点二次转发数据包到集群的某个 Pod 上。

目标 Pod 的宿主节点和转发数据包的节点可能不是同一个。

对于 Local 模式的 LB Service,当其收到数据包后,负载均衡器将数据包发往部署有目标 Pod 的节点的上,再由节点转发到自身的 Pod 上。

对于 LB 直连 Pod 模式的 LB Service,当其收到数据包后,负载均衡器直接将数据包发往各个 Pod。相比前两种模式,这种模式减少了一次节点转发操作。

在 VPC 路由网络模式下 三种模式 LoadBalancer Service 的数据包转发路径如图所示:

在 VPC-CNI 网络模式下 三种模式 LoadBalancer Service 的数据包转发路径如图所示:

Cluster 模式

如果希望使用 Cluster 模式的 Service,在创建 Service 时,应指定 externalTrafficPolicy: Cluster 。如以下例子所示:


apiVersion: v1 kind: Service metadata: name: service-example-cluster annotations: prometheus.io/scrape:
 "true" spec: selector: app: nginx type: LoadBalancer externalTrafficPolicy: Cluster sessionAffinity:
 None ports: - name: nginx protocol: TCP port: 80 targetPort: 80


Local 模式

如果希望使用 Cluster 模式的 Service,在创建 Service 时,应指定 externalTrafficPolicy: Local 。如以下例子所示:


apiVersion: v1 kind: Service metadata: name: service-example-local annotations: prometheus.io/scrape: 
"true" spec: selector: app: nginx type: LoadBalancer externalTrafficPolicy: Local sessionAffinity:
 None ports: - name: nginx protocol: TCP port: 80 targetPort: 80


LB 直连 Pod 模式

如果希望使用 LB 直连 Pod 模式的 Service 时,在创建 Service 时,应添加 Annotation service.beta.kubernetes.io/

cce-load-balancer-backend-type: "eni" 。如以下例子所示:
更详细的使用说明请参考使用直连 Pod 模式 LoadBalancer Service.md


apiVersion: v1 kind: Service metadata: name: service-example-direct annotations: prometheus.io/scrape:
 "true" service.beta.kubernetes.io/cce-load-balancer-backend-type: "eni" spec: selector: app: nginx
 type: LoadBalancer sessionAffinity: None ports: - name: nginx protocol: TCP port: 80 targetPort: 80


高级配置

固定EIP

当用户删除Service并重新创建的时候,EIP会变,这样就需要去更改依赖于此IP的其他所有服务,所以CCE提供一种方式来固定此EIP。

固定EIP的方案:

(1)用户预先百度智能云上购买一个EIP实例
(2)在创建Service时,设置loadBalancerIP为此EIP
(3)创建Service,此时EXTERNAL-IP即为此EIP
(4)删除Service,CCE只会解绑此EIP而不会释放此EIP,用户下次还可以继续使用

示例如下:


--- kind: Service apiVersion: v1 metadata: name: nginx-service-eip-with-load-balancer-ip
 spec: selector: app: nginx-eip-with-load-balancer-ip type: LoadBalancer loadBalancerIP: 8.8.8.8 ports:
 - name: nginx-port port: 80 targetPort: 80 protocol: TCP --- apiVersion: apps/v1 kind: Deployment 
metadata: name: nginx-deployment-eip-with-load-balancer-ip spec: selector: matchLabels:
 app: nginx-eip-with-load-balancer-ip replicas: 1 template: metadata: labels: app:
 nginx-eip-with-load-balancer-ip spec: containers: - name: nginx image: nginx ports: - containerPort: 80


这样查到的EXTERNAL-IP即为此EIP:

kubectl get svc nginx-service
NAME                                    TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)        AGE
nginx-service-eip-with-loadBalancerIP   LoadBalancer   1.1.1.1          8.8.8.8          80:30601/TCP   1m

不分配EIP(即VPC内BLB)

用户使用时:
(1)设置Service.Spec.Type=LoadBalancer
(2)为Service添加annotations,即service.beta.kubernetes.io/cce-load-balancer-internal-vpc: "true"

示例如下:


--- kind: Service apiVersion: v1 metadata: name: nginx-service-blb-internal-vpc annotations: 
service.beta.kubernetes.io/cce-load-balancer-internal-vpc: "true" spec: selector: app:
 nginx-blb-internal-vpc type: LoadBalancer ports: - name: nginx-port port: 80 targetPort:
 80 protocol: TCP --- apiVersion: apps/v1 kind: Deployment metadata: name:
 nginx-deployment-blb-internal-vpc spec: selector: matchLabels: app: nginx-blb-internal-vpc replicas: 
1 template: metadata: labels: app: nginx-blb-internal-vpc spec: containers: - name: nginx image:
 nginx ports: - containerPort: 80


这样查到的EXTERNAL-IP只能在VPC内访问:

kubectl get svc nginx-service
NAME                             TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)        AGE
nginx-service-blb-internal-vpc   LoadBalancer   1.1.1.1          2.2.2.2          80:30601/TCP   1m

注:此内网BLB只能在一个VPC内的集群间正常使用;在使用同一个集群内的内网BLB时,会存在问题,建议在同一个集群内直接使用Service的ClusterIP

自定义EIP配置

EIP支持配置类型:

预付费(Prepaid)

项目 限制
公网带宽 1-200Mbps,Int
购买时长 [1,2,3,4,5,6,7,8,9,12,24,36],时间单位,month

后付费(Postpaid)

计费方式 公网带宽 费用举例
按使用流量计费(ByTraffic) 1~200Mbps,Int 配置费用:¥0.00032/分钟;流量费用:¥0.76/GB
按使用带宽计费(ByBandwidth) 1-200Mbps,Int 配置费用(1Mbps为例):¥0.00094/分钟

使用方式: 在创建Service时设置相应Annotation如下:

// 付费方式,默认:Postpaid;可选:Postpaid、Prepaid
service.beta.kubernetes.io/cce-elastic-ip-payment-timing:"Postpaid"
// 计费方式,默认:ByTraffic;可选:ByTraffic、ByBandwidth
service.beta.kubernetes.io/cce-elastic-ip-billing-method:"ByTraffic"
// 公网带宽,单位为Mbps,默认:100;对于prepay以及bandwidth类型的EIP,限制为为1~200之间的整数,对于traffic类型的EIP
,限制为1~200之间的整数。
service.beta.kubernetes.io/cce-elastic-ip-bandwidth-in-mbps:"100"
// 对于预付费,必须设置时长,[1,2,3,4,5,6,7,8,9,12,24,36],单位月;对于后付费,此设置无效
service.beta.kubernetes.io/cce-elastic-ip-reservation-length:"36"

后付费举例:


kind: Service apiVersion: v1 metadata: name: nginx-service annotations: service.beta.kubernetes
.io/cce-elastic-ip-payment-timing: "Postpaid" service.beta.kubernetes.io/cce-elastic
-ip-billing-method: "ByTraffic" service.beta.kubernetes.io/cce-elastic-ip-bandwidth-in-mbps: 
"200" spec: selector: app: nginx type: LoadBalancer ports: - name: http port: 80 targetPort: 80


预付费举例(请确定余额充足,否则会失败):


kind: Service apiVersion: v1 metadata: name: nginx-service annotations: service.beta.kubernetes.
io/cce-elastic-ip-payment-timing: "Prepaid" service.beta.kubernetes.io/cce-elastic-ip-bandwidth-
in-mbps: "10" service.beta.kubernetes.io/cce-elastic-ip-reservation-length:"1" spec: selector: app:
 nginx type: LoadBalancer ports: - name: http port: 80 targetPort: 80


说明事项 默认配置: 默认为:后付费+按流量+100M带宽。

固定EIP: 不支持对固定EIP的配置进行更新,请用户自行到控制台修改。

用户更新Service EIP的配置(即手动编辑annotation): 支持更新的配置有:公网带宽

预付费:

(1)对于预付费,由于EIP API的限制,目前不支持自动续费,需要用户自行到console上续费。

(2)对于预付费,不需要设置计费方式

(3)删除Service时,预付费EIP不会释放,到期后才会释放

UDP-Service

修改spec.ports.protocol为UDP即可使用UDP Service的功能,举例如下:

---
apiVersion: v1
kind: Service
metadata:
  name: udp-server-demo-svc
  labels:
    app: udp-server-demo
spec:
  type: LoadBalancer
  ports:
  - name: udp-server-demo-port
    port: 3005
    targetPort: 3005
    protocol: UDP
  selector:
    app: udp-server-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: udp-server-demo
  labels:
    app: udp-server-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: udp-server-demo
  template:
    metadata:
      labels:
        app: udp-server-demo
    spec:
      containers:
      - name: udp-server-demo
        image: hub.baidubce.com/jpaas-public/udp-server-demo:latest
        ports:
        - containerPort: 3005
          protocol: UDP

(1)部署udp测试服务

$ kubectl apply -f udp.yaml

(2)UDP Service创建成功

$ kubectl get svc
NAME                  TYPE           CLUSTER-IP       EXTERNAL-IP      PORT(S)          AGE
kubernetes            ClusterIP      172.16.0.1443/TCP          6h
udp-server-demo-svc   LoadBalancer   172.16.122.139   10.10.10.10      3005:31441/UDP   1m

(3)查看服务日志

$ kubectl logs -f udp-server-demo-6fdf5d796f-h6595
Received: HealthCheck
Get Health Check, response OK
Received: HealthCheck
Get Health Check, response OK
Received: HealthCheck
Get Health Check, response OK

注:根据百度云负载均衡BLB的要求,对于监听UDP的服务,一定要通过UDP健康检查,BLB才会把流量转发到后端,所以需要用户的后端UDP服务响应健康检查字符串,详见:UDP健康检查介绍

为Service的BLB指定子网

为方便用户管理网络相关资源,支持创建Service时为BLB指定子网 用户使用时:

(1)设置Service.Spec.Type=LoadBalancer

(2)为Service添加annotations,指定子网ID,即service.beta.kubernetes.io/cce-load-balancer-subnet-id: "sbn-*"

示例如下:


--- kind: Service apiVersion: v1 metadata: name: nginx-service-blb-subnet-id annotations: service.beta.
kubernetes.io/cce-load-balancer-subnet-id: "sbn-123456" spec: selector: app: nginx type: LoadBalancer ports: 
- name: nginx-port port: 80 targetPort: 80 protocol: TCP --- apiVersion: apps/v1 kind: Deployment metadata: 
name: nginx-deployment-blb-subnet-id spec: selector: matchLabels: app: nginx replicas: 1 template: metadata:
 labels: app: nginx spec: containers: - name: nginx image: nginx ports: - containerPort: 80


部署服务后,可以去百度智能云BLB页面查询创建的BLB所在的子网进行验证 注:仅支持在创建Service时指定子网,不支持创建Service之后编辑Service添加或修改该annotation

指定 BLB 创建 LB Service

通过为 Service 添加 annotations,即 service.beta.kubernetes.io/cce-load-balancer-id: "lb-xxxxxxxx",指定 LB 的BLB。 示例如下:


kind: Service apiVersion: v1 metadata: name: nginx-service-blb-assigned-id annotations:
 service.beta.kubernetes.io/cce-load-balancer-id: "xxxxxx" spec: selector: app: nginx
 type: LoadBalancer ports: - name: nginx-port port: 80 targetPort: 80 protocol: TCP


删除时不会保留 BLB,如需保留,请使用 删除Service保留BLB 的 annotation

删除 Service 保留 BLB

通过为 Service 添加 annotations,即 service.beta.kubernetes.io/cce-load-balancer-reserve-lb: "true", 指定删除 Service 时,保留 BLB。 示例如下:


kind: Service apiVersion: v1 metadata: name: nginx-service-blb-reserve-lb annotations: service.beta.kubernetes.
io/cce-load-balancer-reserve-lb: "true" spec: selector: app: nginx type: LoadBalancer ports: - name: 
nginx-port port: 80 targetPort: 80 protocol: TCP




相似文档
  • 直连 Pod 模式 LoadBalancer Service 以 Pod 的 IP 地址作为其后端服务器。 当访问直连 Pod 模式 LoadBalancer Service 时,Service ,请求只需经过一次负载均衡操作即可被转发到具体的 Pod 上。 这种模式的 Service 具有以下优势: 保留请求的源 IP; 少一次请求转发,系统具有更高的性能。 Pod 层面更均匀的负载均衡。
  • Ingress 是 Kubernetes 提供的一种 7 层流量接入方式,它通过连接外部负载均衡与容器内部服务的方式对流量进行管理,相比 LoadBalancer Service,Ingress 可以简化主机对外开放端口的管理,同时利用外部负载的能力提供更完善的路由和安全规则,详情参考官网说明:Kubernetes Ingress。
  • 本文档介绍如何通过YAML创建和管理CCE Ingress。 创建Ingress Controller资源: 使用以下yaml内容创建Ingress Controller: kubectl create -f ingress-controller.yaml ingress-controller.yaml 如下所示: apiVersion: rbac.authorization.k8s.io/v1
  • 本文介绍如何利用百度云容器服务的Ingress功能,实现蓝绿发布。 背景信息: 灰度及蓝绿发布是为新版本创建一个与老版本完全一致的生产环境,在不影响老版本的前提下,按照一定的规则把部分流量切换到新版本,当新版本试运行一段时间没有问题后,将用户的全量流量从老版本迁移至新版本。
  • 本文介绍使用 Nginx Ingress 作为 Ingress 的实现方式。 Kubernetes Ingress 除了 CCE Ingress 实现之外,也可以使用 Kubernetes 社区的 Nginx Ingress实现。 Nginx 支持反向代理与负载均衡器等功能。 Nginx Ingress Controller 是 Ingress 控制器,使用 Nginx 实现反向代理功能,并通过解析集群中的 Ingress 来配置转发规则。
官方微信
联系客服
400-826-7010
7x24小时客服热线
分享
  • QQ好友
  • QQ空间
  • 微信
  • 微博
返回顶部