《四》Service – 与外界连通、Ingress

Service – 与外界连通

介绍Service

  • 防止Pod失联
  • 定义一组Pod的访问策略
  • 支持ClusterIP,NodePort以及LoadBalancer三种类型
  • Service的底层实现主要有iptables和ipvs二种网络模式

Pod与Service的关系

  • • 通过label-selector相关联
  • • 通过Service实现Pod的负载均衡( TCP/UDP 4层)

《四》Service – 与外界连通、Ingress

说明:service 通过selector 关联到pod的labels 标签

定义service

《四》Service – 与外界连通、Ingress

查看pod的app信息
《四》Service – 与外界连通、Ingress

详细信息
《四》Service – 与外界连通、Ingress

Service 类型

  • • ClusterIP:默认,分配一个集群内部可以访问的虚拟IP(VIP),不需要外部访问
  • • NodePort:在每个Node上分配一个端口作为外部访问入口
  • • LoadBalancer:工作在特定的Cloud Provider上,例如Google Cloud,AWS,OpenStack

NodePort

《四》Service – 与外界连通、Ingress

node上采用的是ipvs的工具(定义如下):
《四》Service – 与外界连通、Ingress

《四》Service – 与外界连通、Ingress

LoadBalancer
比如阿里云(slb)、aws的负载均衡器

《四》Service – 与外界连通、Ingress

总结:
NodePort:
用户 ===》域名===》负载均衡器(nginx、lvs)===>NodeIP:Port===> PodIP:Port

LoadBalancer(提供特定的云提供商LB的底层接口(slb,aws,openstack)):
用户 ===》域名===》负载均衡器(阿里云、aws,这一步是自动完成的,不需要手动的添加)===>NodeIP:Port===> PodIP:Port

固定node的端口:
《四》Service – 与外界连通、Ingress

创建一个pod分配到了一个node上,只要ENDPOINTS的方式,所有node都可以访问:
《四》Service – 与外界连通、Ingress

Service 代理模式

底层流量转发与负载均衡实现:
• Iptables 通过iptables 的规则匹配,有问题时就比较麻烦的处理,得清空 iptables -F
1、创建很多iptables规则(更新,非增量更新,因为iptables都是从上往下执行的,一条一条匹配)
2、以上带来的问题就是 若是规则越多,很难管理

• IPVS
1、只执行4层(阿里云SLB就是根据这个来做的)
2、ipvsadm -ln
3、设置策略(rr,wrr,lc,wlc,ip hash)

     ![](http://i2.51cto.com/images/blog/201812/17/bd4780b77b8c05bc565159751564f997.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

        systemctl restart kube-proxy

DNS
内部通讯不可能根据ip来通信,因为ip不是固定的,为每一个Service创建DNS记录用于域名解析。

参考地址:https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dns/coredns

[root@docker demo]# kubectl apply -f coredns.yaml
《四》Service – 与外界连通、Ingress

验证是否可以解析
《四》Service – 与外界连通、Ingress

如果解析不同的命名空间下的(在kube-system 解析default下的service):
《四》Service – 与外界连通、Ingress

Ingress
Pod与Ingress的关系如下:

通过label-selector相关联
通过Ingress Controller实现Pod的负载均衡,支持TCP/UDP 4层和HTTP 7层

《四》Service – 与外界连通、Ingress

Ingress Controller
《四》Service – 与外界连通、Ingress

部署文档:https://github.com/kubernetes/ingress-nginx/blob/master/docs/deploy/index.md

注意事项:
• 镜像地址修改成国内的:lizhenliang/nginx-ingress-controller:0.20.0
• 使用宿主机网络:hostNetwork: true

wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/mandatory.yaml

修改(这个源下载不了):
《四》Service – 与外界连通、Ingress

执行:
kubectl apply -f mandatory.yaml

《四》Service – 与外界连通、Ingress

验证:
《四》Service – 与外界连通、Ingress

若是失败,则查询:
《四》Service – 与外界连通、Ingress

以上Ingress Controller 创建完成,现在编辑Ingress 规则

查看文档:https://kubernetes.io/docs/concepts/services-networking/ingress/

《四》Service – 与外界连通、Ingress

[root@docker ingress]# cat ingress1.yaml 
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /
        backend:
          serviceName: my-service
          servicePort: 802

多个域名访问:
《四》Service – 与外界连通、Ingress

查看控制器分配在了哪个node上,然后做域名绑定到这个node
《四》Service – 与外界连通、Ingress

编写hosts:
《四》Service – 与外界连通、Ingress

访问验证:http://foo.bar.com/

基于https访问
1、需要ca自签证书:
《四》Service – 与外界连通、Ingress

2、执行:bash certs.sh
生成以下两个文件:
《四》Service – 与外界连通、Ingress

3、创建密钥
[root@docker ingress]# kubectl create secret tls sslexample-foo-com --cert=sslexample.foo.com.pem --key=sslexample

《四》Service – 与外界连通、Ingress

《四》Service – 与外界连通、Ingress

4、执行:
[root@docker ingress]# kubectl apply -f httpsingress1.yaml

5、绑定域名访问:
《四》Service – 与外界连通、Ingress

https://sslexample.foo.com/

说明:如果secretName 写错了,k8s会自动颁发一个证书,颁发者就不是kubernetes

总结
Ingress
1、四层、七层负载均衡转发
2、支持自定义service访问策略
3、只支持基于域名的网站访问
4、执行TLS
5、需要部署多个Ingress Controller,避免一个挂了

猜你喜欢

转载自blog.51cto.com/jacksoner/2332022