- Hands-On Kubernetes on Windows
- Piotr Tylenda
- 513字
- 2021-06-24 16:54:10
Services
Pods that are created by ReplicaSets or Deployments have a finite life cycle. At some point, you can expect them to be terminated and new Pod replicas with new IP addresses will be created in their place. So, what if you have a Deployment running web server Pods that need to communicate with Pods that have been created as a part of another Deployment, for example, backend Pods? Web server Pods cannot assume anything about IP addresses or the DNS names of backend Pods, as they may change over time. This issue is resolved with Service API objects, which provide reliable networking for a set of Pods.
In general, Services target a set of Pods, and this is determined by label selectors. The most common scenario is exposing a Service for an existing Deployment by using exactly the same label selector. The Service is responsible for providing a reliable DNS name and IP address, as well as for monitoring selector results and updating the associated Endpoint Object with the current IP addresses of matching Pods.
For internal clients (Pods in the cluster), the communication to Pods behind a service is transparent – they use the Cluster IP or DNS name of the Service and the traffic is routed to one of the destination Pods. Routing capabilities are provided by kube-proxy, but it is important to know that the traffic is not routed through any master components – kube-proxy implements routing at the operating system kernel level and directly routes this to an appropriate Pod's IP address. In its simplest form, the destination Pod will be chosen randomly, but with IP Virtual Server (IPVS) proxy mode, you can have more complex strategies, such as least connection or shortest expected delay.
The principle of how Service works can be seen in the following diagram:
Let's expose an example service for our nginx Deployment:
- If you don't have a running Deployment on the Katacoda playground, you can create one using the following command:
kubectl apply -f https://raw.githubusercontent.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/master/Chapter04/03_deployment-example/nginx-deployment.yaml --record
- Expose the Service for a deployment using the following kubectl expose command:
kubectl expose deployment nginx-deployment-example
- This command is imperative and should be avoided in favor of the declarative manifest. This command is equivalent to applying the following Service manifest:
apiVersion: v1
kind: Service
metadata:
name: nginx-deployment-example
spec:
selector:
environment: test
type: ClusterIP
ports:
- port: 80
protocol: TCP
targetPort: 80
- Now, after the Service has been exposed, create an interactive busybox Pod and start the Bourne shell process:
kubectl run --generator=run-pod/v1 -i --tty busybox --image=busybox --rm --restart=Never -- sh
- When the container shell prompt appears, download the default web page served by nginx Pods while using the nginx-deployment-example Service name as the DNS name:
wget http://nginx-deployment-example && cat index.html
Next, let's take a quick look at objects that provide storage in Kubernetes.
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- Visual C++串口通信開(kāi)發(fā)入門(mén)與編程實(shí)踐
- Hands-On Enterprise Automation with Python.
- Mastering Apache Spark 2.x(Second Edition)
- 分布式數(shù)據(jù)庫(kù)原理、架構(gòu)與實(shí)踐
- ASP.NET Web API Security Essentials
- 官方 Scratch 3.0 編程趣味卡:讓孩子們愛(ài)上編程(全彩)
- SSH框架企業(yè)級(jí)應(yīng)用實(shí)戰(zhàn)
- 軟件測(cè)試技術(shù)
- Android技術(shù)內(nèi)幕(系統(tǒng)卷)
- Learning Shiny
- R語(yǔ)言編程基礎(chǔ)
- Ajax與jQuery程序設(shè)計(jì)
- 微軟辦公軟件認(rèn)證考試MOS Access 2013實(shí)訓(xùn)教程
- Java程序設(shè)計(jì)