service与服务发现
运行于pod中的容器化应用绝大多数是服务类的守护进程,他们受控于控制器资源对象。是不可再生类组件。
service资源为这种非再生类组件提供一个固定统一的访问接口及负载均衡能力,并支持新一代的DNS系统的服务发现功能。
然而,service对象的IP地址都仅仅在kubernetes集群内部可达,无法接入集群外部的访问流量。解决办法是:除了可以在单一节点上做端口暴露(hostport)以及让pod资源共享使用节点的网络名称空间外,还有更好的办法:使用nodeport或者loadbalancer类型的service资源或者是具有七层负载均衡能力的Ingree资源
service资源中以pod形式运行的应用进程的生命周期通常受控于dployment或者statefulset一类的控制器,,pod故障或者中断后,会自己重新创建新对象取代,而扩缩容或者更新操作带来pod对象群体变动。(删除service资源的pod对象,要先删除资源控制器deployment类,再删除service资源及pod对象)
[root@k8s-master01 yaml]# cat nginx-nodeport-service.yaml kind: Service apiVersion: v1 metadata: name: nginx-nodeport-service spec: type: NodePort selector: app: nginx ports: - name: http protocol: TCP port: 80 targetPort: 80 nodePort: 30001
[root@k8s-master01 yaml]# kubectl apply -f nginx-nodeport-service.yaml service/nginx-nodeport-service created [root@k8s-master01 yaml]# kubectl create deployment nginx --image=nginx:latest deployment.apps/nginx created [root@k8s-master01 yaml]# kubectl scale deployment/nginx --replicas=3 deployment.apps/nginx scaled [root@k8s-master01 yaml]# kubectl describe services nginx | grep "^Endpoints" #集群内部分配了3个80端口的ip,,并且工作在不通节点上 Endpoints: 10.244.1.30:80,10.244.2.31:80,10.244.3.38:80 [root@k8s-master01 yaml]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-6799fc88d8-fzr5b 1/1 Running 0 2m31s 10.244.2.31 k8s-node01 <none> <none> nginx-6799fc88d8-kbn25 1/1 Running 0 2m35s 10.244.1.30 k8s-node02 <none> <none> nginx-6799fc88d8-s6mqx 1/1 Running 0 2m31s 10.244.3.38 k8s-node03 <none> <none>
我们在集群外部通过各个工作节点IP:30001 可以访问集群内部的nginx容器服务
NodePort类型的service资源虽然能够在集群外部访问,但是呢,外部客户端必须事先知道nodeport的端口号,和至少知道一个工作节点的IP地址,一旦被选定的节点发生故障,客户端还得请求访问其他节点,这时候如果有一个固定ip地址及固定接入端点就更好啦。
浙公网安备 33010602011771号