• 11、Service访问Pod、Service IP原理、DNS访问Service、外部访问service


    Pod可能因为各种原因发生故障而死掉,Deployment等Controller会通过动态创建和销毁Pod来保障应用整体的健壮性。 Pod是脆弱的,但应用是健壮的
    每个Pod都有自己的IP地址,当controller用新的Pod替代发生故障的Pod时,新Pod会分配到新的IP地址,那这时客户端如何找到并访问这个服务呢??-----Service
      

    一、创建Service

    Kubernetes Service从逻辑上代表一组pod,具体哪些Pod由label挑选。
    Service有自己的IP,这个IP是不变的。客户端只需要访问Service的IP, Kubernetes则负责建立和维护Service与Pod的映射关系。无论后端Pod如何变化,对客户端不会有任何影响,因为Service没变。
    # 编辑httpd_deployment.yaml  
    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:                  
    4.   name: httpd-deployment
    5. spec:
    6.   selector:
    7.     matchLabels:
    8.       app:  httpd  # 通过标签选择被控制的pod
    9.   replicas:  3
    10.   template:
    11.     metadata:
    12.       labels:
    13.         app: httpd  # 给pod打上标签,Service、Deployment 将会用这个 label 来挑选 Pod
    14.     spec:
    15.       containers:
    16.       - name: httpd
    17.         image: httpd
    18.         ports:
    19.         - containerPort: 80  # 转发到后端pod的端口号

    Pod分配了各自的IP地址,但这些IP只能被Kubernetes Cluster中的容器与节点访问。

    # 编辑httpd_service.yaml  
    1. apiVersion: v1
    2. kind: Service
    3. metadata:
    4.   name: httpd-service # 必填,service名称
    5. spec:
    6.   selector:
    7.     app: httpd # 必填,在selector字段中指定了为哪一个标签的app进行负载均衡
    8.   ports: # 将 Service 的 8080 端口映射到 Pod 的 80 端口,使用 TCP 协议
    9.   - protocol: TCP
    10.     port: 8080      # service监听端口
    11.     targetPort: 80  # 转发到后端pod的端口号

    httpd-service分配到一个CLUSTER-IP,可以通过该IP访问后端的httpd Pod

    除了我们创建的 httpd-svc,还有一个 Service kubernetes,Cluster 内部通过这个 Service 访问 kubernetes API Server。
    kubectl describe service httpd-service  查看service与Pod的对应关系

    二、Service IP原理

    Service的Cluster IP在哪里配置的?Cluster IP又是怎样与Pod IP映射的??---iptables
    Service Cluster-ip是个虚拟的IP,是由kubernetes节点上iptables规则管理的。
    通过  sudo iptables-save 命令打印当前节点的 iptables 规则,因输出较多,这里只截取与 httpd-service Cluster IP 10.98.165.152 相关的信息:
    1. $ sudo iptables-save | grep '10.98.165.152'
    2. -A KUBE-SERVICES ! -s 10.244.0.0/16 -d 10.98.165.152/32 -p tcp -m comment --comment "default/htt
  • 相关阅读:
    算法竞赛入门【码蹄集进阶塔335题】(MT2291-2295)
    docker更新镜像
    flink-sql所有表连接器
    2023最新SSM计算机毕业设计选题大全(附源码+LW)之java影院售票系统6fg71
    SpringMVC_拦截器
    一个简单的iOS天气应用程序源码
    基于NVIDIA的deepstream进行串行多任务模型开发,DeepStream 多模型组合检测(精)
    Restriction (mathematics)
    Webrtc源码编译之个人仓库
    浅论前后端分离模式:低代码强势推动开发效率提升
  • 原文地址:https://blog.csdn.net/leiwuhen92/article/details/127900868