运维八一 运维八一
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)

运维八一

运维,运维!
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)
  • Devops笔记

  • Kubernetes

    • k8s 使用YAML配置文件管理资源
    • k8s 调度约束
    • k8s 资源约束
    • k8s 健康检测
    • k8s 重启策略
    • k8s pod管理
    • k8s DaemonSet典型应用
    • k8s Job任务
    • k8s service服务
      • service 服务
  • 运维监控系统

  • go分布式爬虫

  • Linux性能优化

  • 夜莺(nightingale)开源观测平台

  • kubernetes-network-policy-recipes

  • 专栏
  • Kubernetes
lyndon
2022-10-05
目录

k8s service服务

# service 服务

Kubernetes Service 从逻辑上代表了一组 Pod,具体是哪些 Pod 则是由 label 来挑选。Service 有自己 IP,而且这个 IP 是不变的。

客户端只需要访问 Service 的 IP,Kubernetes 则负责建立和维护 Service 与 Pod 的映射关系。无论后端 Pod 如何变化,对客户端不会有任何影响,因为 Service 没有变。

示例:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:
   - name: httpd
     image: httpd
     ports:
        - containerPort: 80
---

apiVersion: v1
kind: Service
metadata:
  name: httpd-svc
  namespace: kube-system
spec:
  selector:
    run: httpd
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

说明:

启动3个pod,并定义label标签为run: httpd,Service 将会用这个 label 来挑选 Pod。

selector 指明挑选那些 label 为 run: httpd 的 Pod 作为 Service 的后端。

ports 将 Service 的 8080 端口映射到 Pod 的 80 端口,使用 TCP 协议。

namespace: kube-system 指定资源所属的 namespace。多个资源可以在一个 YAML 文件中定义,用 --- 分割。

service暴露服务的三种方式

  • Cluster IP:Service 通过 Cluster 内部的 IP 对外提供服务,只有 Cluster 内的节点和 Pod 可访问,这是默认的 Service 类型。
  • NodePort:Service 通过 node 的静态端口对外提供服务。Cluster 外部可以通过 <NodeIP>:<NodePort> 访问 Service。
  • LoadBalancer:Service 利用 cloud provider 特有的 load balancer 对外提供服务,cloud provider 负责将 load balancer 的流量导向 Service。目前支持的 cloud provider 有 GCP、AWS、Azur 等。

集群内部访问service

  • 通过Cluster IP:Service Cluster IP 是一个虚拟 IP,是由 Kubernetes 节点上的 iptables 规则管理的,iptables 将访问 Service 的流量转发到后端 Pod,而且使用类似轮询的负载均衡策略。
  • 通过DNS访问:kube-dns 是一个DNS服务器,每当有新的Service被创建,kube-dns会添加该Service的DNS记录,Cluster中的Pod可以通过<SERVICE_NAME>.<NAMESPACE_NAME> 访问 Service,如果pod与service属于同一个namespace,直接使用<SERVICE_NAME>即可访问。

集群外部访问service

  • nodeport:借助iptables规则,将node的端口nodeport映射到ClusterIP的端口port上,再转发到pod的targetport。

示例:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 3
  template:
    metadata:
      labels:
        run: httpd
    spec:
      containers:

   - name: httpd
     image: httpd
     ports:
        - containerPort: 80
---

apiVersion: v1
kind: Service
metadata:
  name: httpd-svc
spec:
  type: NodePort
  selector:
    run: httpd
  ports:
  - protocol: TCP
    nodePort: 30000
    port: 8080
    targetPort: 80
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

说明:

  • type: NodePort 指定service类型为nodeport。
  • nodePort 是节点上监听的端口。
  • port 是 ClusterIP 上监听的端口。
  • targetPort 是 Pod 监听的端口。
上次更新: 2022/10/05, 15:51:58
k8s Job任务
监控系统

← k8s Job任务 监控系统→

最近更新
01
ctr和crictl显示镜像不一致
03-13
02
alpine镜像集成常用数据库客户端
03-13
03
create-cluster
02-26
更多文章>
Theme by Vdoing | Copyright © 2015-2024 op81.com
苏ICP备18041258号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式