美文网首页
K8s-Service

K8s-Service

作者: 会倒立的香飘飘 | 来源:发表于2021-03-05 16:44 被阅读0次

k8s-Services

简介:

Kubernetes 在设计之初就充分考虑了针对容器的服务发现与负载均衡机制,提供了 Service 资源,并通过 kube-proxy 配合 cloud provider 来适应不同的应用场景。随着 kubernetes 用户的激增,用户场景的不断丰富,又产生了一些新的负载均衡机制。目前,kubernetes 中的负载均衡大致可以分为以下几种机制,每种机制都有其特定的应用场景:

Service:直接用 Service 提供 cluster 内部的负载均衡,并借助 cloud provider 提供的 LB 提供外部访问
Ingress Controller:还是用 Service 提供 cluster 内部的负载均衡,但是通过自定义 Ingress Controller 提供外部访问
Service Load Balancer:把 load balancer 直接跑在容器中,实现 Bare Metal 的 Service Load Balancer
Custom Load Balancer:自定义负载均衡,并替代 kube-proxy,一般在物理部署 Kubernetes 时使用,方便接入公司已有的外部服务


image.png

Service 是对一组提供相同功能的 Pods 的抽象,并为它们提供一个统一的入口。借助 Service,应用可以方便的实现服务发现与负载均衡,并实现应用的零宕机升级。Service 通过标签来选取服务后端,一般配合 Replication Controller 或者 Deployment 来保证后端容器的正常运行。这些匹配标签的 Pod IP 和端口列表组成 endpoints,由 kube-proxy 负责将服务 IP 负载均衡到这些 endpoints 上。
Service有四种类型:
ClusterIP:默认类型,自动分配一个仅 cluster 内部可以访问的虚拟 IP
NodePort:在 ClusterIP 基础上为 Service 在每台机器上绑定一个端口,这样就可以通过 <NodeIP>:NodePort 来访问该服务。如果 kube-proxy 设置了 --nodeport-addresses=10.240.0.0/16(v1.10 支持),那么仅该 NodePort 仅对设置在范围内的 IP 有效。
LoadBalancer:在 NodePort 的基础上,借助 cloud provider 创建一个外部的负载均衡器,并将请求转发到 <NodeIP>:NodePort
ExternalName:将服务通过 DNS CNAME 记录方式转发到指定的域名(通过 spec.externlName 设定)。需要 kube-dns 版本在 1.7 以上。
另外,也可以将已有的服务以 Service 的形式加入到 Kubernetes 集群中来,只需要在创建 Service 的时候不指定 Label selector,而是在 Service 创建好后手动为其添加 endpoint。

使用Cluster类型创建一个services

vim nginx.svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: nginx-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
          protocol: TCP 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
spec:
  selector:
    app: nginx
  clusterIP: 10.96.88.8
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80

查看service

[root@k8s-master daem]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   71d
nginx        ClusterIP   10.96.88.8   <none>        80/TCP    5m38s

[root@k8s-master daem]# kubectl describe svc nginx 
Name:              nginx
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=nginx
Type:              ClusterIP
IP:                10.96.88.8
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.104:80,10.244.1.108:80,10.244.2.66:80 + 1 more...
Session Affinity:  None
Events:            <none>

使用NodePort创建一个Service

vim myapp.svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: myapp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:        
        app: myapp
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
  clusterIP: 10.96.99.99 
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
查看创建的svc和pod
root@master:~/k8s_yaml# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP        2d5h
myapp        NodePort    10.96.99.99   <none>        80:30107/TCP   19m
root@master:~/k8s_yaml# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-5cbd66595b-2dghs   1/1     Running   0          15m
myapp-5cbd66595b-m5d6l   1/1     Running   0          21m

NodePort暴露端口在集群外部可以访问


image.png

工作原理图:

image.png

相关文章

  • k8s-service

    Service 内部访问方式 外部访问方式

  • K8s-Service

    k8s-Services 简介: Kubernetes 在设计之初就充分考虑了针对容器的服务发现与负载均衡机制,提...

  • k8s-Service

    Service 存在的意义 Pod与Service的关系 Service定义与创建 Service三种常用类型 C...

网友评论

      本文标题:K8s-Service

      本文链接:https://www.haomeiwen.com/subject/ozihqltx.html