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

Service – 与外界连通

介绍Servicenode

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

Pod与Service的关系nginx

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

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

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

定义servicegithub

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

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

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

Service 类型

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

NodePortbash

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

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

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

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

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

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

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

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

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

Service 代理模式

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

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

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

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
说明:必须经过ingress控制器与service绑定

《四》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

以上Ingress Controller 建立完成,如今编辑Ingress 规则

查看文档:https://kubernetes.io/docs/concepts/services-networking/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访问
一、须要ca自签证书:

二、执行:bash certs.sh
生成如下两个文件:

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

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

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

须要指定secrets
《四》Service – 与外界连通、Ingress

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

五、绑定域名访问(hosts):

https://sslexample.foo.com/

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

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

将keepalived与ingress关联

现状
由于pod能够分配在不少node上,若域名与一个node节点绑定,这一个node服务器出现问题,则这个域名就挂了,不能实现高可用

解决将每一个node上装上keepalived服务,设置vip,主master,备用的backup,而后域名 绑定到 vip上就实现高可用

相关文章
相关标签/搜索