Kubernetes-9:Service介绍及演示
2021-01-24 17:15
                         标签:其他   lin   targe   调度   这一   not   访问   time   yml    Kubernetes 的Service定义了这样一种抽象:一个 Pod 的逻辑分组,一种可以访问他们的策略 —— 微服务,这一组Pod能够被Service访问到,通常是通过tabel Selector匹配。 Service 能够提供负载均衡的能力,但是在使用上有以下的限制: 只提供四层负载均衡,不存在七层负载 负载的方式有通过 userspace、iptables转发 和 ipvs 转发三种,如果系统不支持 ipvs 模块,则会自动降级为iptables转发模式 userspace:client端访问Server Pod时需要将请求转发给kube-proxy,再有kube-proxy进行调度,同时apiServer也通过Porxy获取Node节点信息,这种模式下kube-proxy的压力非常大。 iptables:client端访问Server Pod直接通过iptables进行转发,无需经过kube-Proxy ipvs:client端访问Server Pod直接通过ipvs进行转发,前提ipvs模块必须加载 vim svc-deployment.yml vim svc-cluNone.yml vim svc-nodeport.yml 此Service类型主要功能为在Node的外部做一个负载均衡器,但是需要收费,了解即可。   当有需求Pod需要访问svc时,可以通过直接访问svc ip和svc name两种方式访问。 但由于svc ip是不固定的,当svc删除或者重建的时候,ip会发生改变,所以如果想固定的访问此svc,要使用coredns的方式,也就是访问svc name。 注意:使用coredns解析svc name时,要在Pod的资源清单中加入dnsPolicy: ClusterFirstWithHostNet 这个配置,该设置是使POD使用的k8s的dns,pod默认使用所在宿主主机使用的DNS(踩过的坑,不知道其他版本如何) Kubernetes-9:Service介绍及演示 标签:其他   lin   targe   调度   这一   not   访问   time   yml    原文地址:https://www.cnblogs.com/v-fan/p/13252274.htmlService
Service的类型
示例
clusterIP
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      release: stabel
  template:
    metadata:
      labels:
        app: myapp
        release: stabel
        version: v1
    spec:
      containers:
      - name: myapp
        image: hub.vfancloud.com/test/myapp:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: svc-cluster
  namespace: default
spec:
  type: ClusterIP
  selector:
    app: myapp
    release: stabel
  ports:
  - name: http
    port: 80
    targetPort: 80
创建、测试
[root@Centos8 k8sYaml]# kubectl create -f svc-deployment.yml 
deployment.apps/myapp-deploy created
service/svc-cluster created
[root@Centos8 k8sYaml]# kubectl get deployment 
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
myapp-deploy   3/3     3            3           2m38s
[root@Centos8 k8sYaml]# kubectl get svc
NAME          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes    ClusterIP   10.96.0.1      
 
不需要负载以及单独的ClusterIP时
apiVersion: v1
kind: Service
metadata:
  name: svc-clunone
spec:
  clusterIP: "None"
  selector:
    app: myapp
    release: stabel
  ports:
  - port: 80
    targetPort: 80
 
NodePort
apiVersion: v1
kind: Service
metadata:
  name: svc-nodePort
  namespace: default
spec:
  type: NodePort
  selector:
    app: myapp
    release: stabel
  ports:
  - name: http
    port: 80  # 用于k8s集群内部服务之间相互访问端口
    targetPort: 80  # 实际web容器expose的端口
    nodePort: 30001  # 用于外部client访问,其会在k8s每个node节点开启30001
创建、测试
[root@Centos8 k8sYaml]# kubectl create -f svc-nodeport.yml 
service/svc-nodeport created
[root@Centos8 k8sYaml]# kubectl get svc 
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes     ClusterIP   10.96.0.1       
 
LoadBalancer
 
ExternalName
apiVersion: v1
kind: Service
metadata:
  name: svc-external
  namespace: default
spec:
  type: ExternalName
  externalName: hub.vfancloud.com
Pod访问svc的方式
## 当前svc
[root@Centos8 http]# kubectl get svc 
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
ingress-svc1   ClusterIP   10.102.201.28   
上一篇:.Net 代码环境配置
下一篇:MVC
文章标题:Kubernetes-9:Service介绍及演示
文章链接:http://soscw.com/index.php/essay/46421.html