美文网首页
六、service使用

六、service使用

作者: jan29 | 来源:发表于2021-11-25 19:47 被阅读0次

    1、ClusterIP

    apiVersion: v1
    kind: Service
    metadata:
      name: base-svc-nginx
      namespace: default
    spec:
      selector:
        app: web-http
        release: stable
      type: ClusterIP
      sessionAffinity: ClientIP
      ports:
       - name: nginx
         port: 80
         targetPort: 80
    

    此方式只能再集群内部访问
    sessionAffinity: ClientIP 表示客户端会话保持,每次调度到同一pod

    2、NodePort

    apiVersion: v1
    kind: Service
    metadata:
      name: base-svc-nginx
      namespace: default
    spec:
      selector:
        app: web-http
        release: stable
      type: NodePort
      sessionAffinity: ClientIP
      ports:
       - name: nginx
         port: 80
         targetPort: 80
         nodePort: 32222
    

    用于外部访问,外部本机地址端口为32222

    3、无头服务

    apiVersion: v1
    kind: Service
    metadata:
      name: base-svc-nginx
      namespace: default
    spec:
      selector:
        app: web-http
        release: stable
      type: ClusterIP
      clusterIP: None
      sessionAffinity: ClientIP
      ports:
       - name: nginx
         port: 80
         targetPort: 80
    

    此方式不能通过集群IP进行访问

    4、LoadBalancer

    apiVersion: v1
    kind: Service
    metadata:
      name: base-svc-nginx
      namespace: default
    spec:
      selector:
        app: web-http
        release: stable
      type: LoadBalancer
      sessionAffinity: ClientIP
      ports:
       - name: nginx
         port: 80
         targetPort: 80
    
    [root@master inventory]# kubectl get svc
    NAME             TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
    base-svc-nginx   LoadBalancer   10.98.35.142   <pending>     80:30234/TCP   11s
    kubernetes       ClusterIP      10.96.0.1      <none>        443/TCP        10d
    

    此方式适用于公有云上的 Kubernetes 服务
    Kubernetes会调用 CloudProvider 在公有云上创建一个负载均衡服务,并且把被代理的 Pod 的 IP地址配置给负载均衡服务做后端。
    此方式在本地环境不能使用

    5、ExternalName

    apiVersion: v1
    kind: Service
    metadata:
      name: base-svc-nginx
      namespace: default
    spec:
      selector:
        app: web-http
        release: stable
      type: ExternalName
      externalName: www.baidu.com
      sessionAffinity: ClientIP
      ports:
       - name: nginx
         port: 80
         targetPort: 80
    

    此方式适用于集群内部容器访问外部资源

    6、直接分配公网

    apiVersion: v1
    kind: Service
    metadata:
      name: base-svc-nginx
      namespace: default
    spec:
      selector:
        app: web-http
        release: stable
      externalIPs:
      - 168.84.68.1
      sessionAffinity: ClientIP
      ports:
       - name: nginx
         port: 80
         targetPort: 80
    
    [root@master inventory]# kubectl get svc
    NAME             TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
    base-svc-nginx   ClusterIP   10.97.84.192   168.84.68.1   80/TCP    3s
    kubernetes       ClusterIP   10.96.0.1      <none>        443/TCP   10d
    

    此方式直接将服务发布在公网之上

    相关文章

      网友评论

          本文标题:六、service使用

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