kubernetes学习 Service (一)

wo我们不应该期望Pod是健壮的,而是要假设Pod中的容器很可能因为各种原因发生故障而死掉。

Controller会通过动态创建和销毁Pod来保证应用整体的健壮性。

每个Pod都有自己的IP地址。当Controller用新的Pod替代发生故障的Pod时,新Pod会被分配新的IP。

那么:Pod对外提供服务,他们的IP很可能发生变化,用户该怎么找到并访问这个服务?

Kubernetes的解决方案:Service

一、创建Service

  Kubernetes Service逻辑上代表一组Pod,集体哪些Pod是由label来挑选的。

  Service有自己的IP,用户只需要访问Service的IP,Kubernetes则负责建立和维护ServicePod的映射关系。

  无论Pod怎么变化,客户端不会有任何变化,因为Service不会变。

  Deployment用例:

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

  

  Service的例子:

apiVersion: v1
kind: Service
metadata:
  name: httpd-svc
spec:
  selector:
    run: httpd
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 80

  apiVersion:  Service的apiVersion

  kind:指明当前资源的类型为Service

  name:Service的名字为httpd-svc 

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

  port:Service的端口; targetPort:Pod的端口 

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

  使用命令查看Service:

kubectl get sevice

  

  Service被分配到了一个CLUSTER-IP,可以通过这个IP来访问8080所映射的Pod

      

  另外除了我们创建的httpd-svc,还有一个Service 叫做 Kubernetes, CLUSTER通过这个 Service 访问Kubernetes API Server

  kubeclt describe service 命令查看httpd-svc与Pod的映射关系

      

  Endpoints罗列了三个PodIP端口。

原文地址:https://www.cnblogs.com/Lyh1997/p/10263979.html