• k8s service NodePort 方式向外发布
  • k8s 无头service 方式向内发布
  • k8s service 服务发现 {ServiceName}.{Namespace}.svc.{ClusterDomain}
    1 )有头的服务发现 DNS解析服务,可以看到ClusterIP地址
    2 )没有的服务发现 DNS解析服务,可以看到单个Pod的cname和IP

Kubernetes 网络解决四方面的问题:

  • 一个 Pod 中的容器之间通过本地回路(loopback)通信。
  • 集群网络在不同 pod 之间提供通信。
  • Service 资源允许你对外暴露 Pods 中运行的应用程序,以支持来自于集群外部的访问。
  • 可以使用 Services 来发布仅供集群内部使用的服务。

案例,假定有一个 deployment,containerPort 端口80,同时还被打上 python=myweb 标签。
deployment内容如下

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mywebdeployment
spec:
  selector:
    matchLabels:
      python: myweb
  replicas: 6
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 0
      maxUnavailable: 1
  template:
    metadata:
     labels:
       python: myweb
    spec:
      containers:
      - name: mywebcontainer
        image: python:3.7
        command: ['sh', '-c']
        args: ['echo "<p>The host is $(hostname) </p>" > index.html;python -m http.server 80']
        ports:
        - name: mywebport
          containerPort: 80

service 的内容如下

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    python: myweb 
  type: NodePort
  ports:
    - protocol: TCP
      port: 81
      targetPort: 80
      nodePort: 30001
   

需要注意的是,Service 能够将一个接收 port 映射到任意的 targetPort。 默认情况下,targetPort 将被设置为与 port 字段相同的值。
事例里把service ClusterIP 的端口81 映射到 node节点上的端口30001
nodePort的取值范围为 30000 - 32767
k8s service NodePort 方式向外发布

相关文章:

  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2021-08-15
  • 2021-06-24
  • 2023-01-19
  • 2022-01-03
  • 2021-04-02
猜你喜欢
  • 2021-11-14
  • 2022-12-23
  • 2021-12-19
  • 2021-09-28
  • 2021-06-02
  • 2021-04-24
  • 2022-02-21
相关资源
相似解决方案