Kubernetes - Expose Your App Publicly

Using a Service to Expose Your App

了解k8s中的Service了解什么是labels以及LabelSelector objects关联到Service使用Service在k8s cluster外公开app

Service

Pod是有lifecycle的,当worker node死亡时,包含在内的pod也会一起消失
而一个ReplicaSet是动态的驱动cluster回到期望的状态,以维持app的运作

Service在k8s内是一种抽象层,他定义pod逻辑以及policy来决定如何访问
服务再依赖的pod之间实现鬆散的耦合,并使用YAML(首选) or JSON来定义

虽然每个pod都有独立的ip address,且能够暴露ip在cluster之外不需要依赖service
Service允许你的app接收流量,以及透过不同的方式将服务裸露

ClusterIP (default) - Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.

NodePort - Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using :. Superset of ClusterIP.

LoadBalancer - Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort.

ExternalName - Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up. This type requires v1.7 or higher of kube-dns, or CoreDNS version 0.0.8 or higher.

此外需要注意,再有时候service并没有定义selector,,当service没有与selector一起创建,将不会产生endpoint,这可以使你手动service对应到特定endpoint上
此外没有selector可能是採用严格的type: ExternalName

Labels

service使用lables以及selectors匹配pod,而这将允许你在k8s中的objects进行分组
Labels是採用key/value的形式附加在objects上
举例来说,可以使用labels将以下的objects进行分组

Designate objects for development, test, and productionEmbed version tagsClassify an object using tags

Interactive Tutorial - Exposing Your App

Step 1 Create a new service

首先取得pods kubectl get pods

取得cluster中的service kubectl get services

此时的service是turtorial创建的,接着要使用NodePort做为参数创建service并expose
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
成功后会在console看到 service/kubernetes-bootcamp exposed

再次运行 kubectl get services 会看到有两个service以及接收一个独立的ip以及内部和外部对应的port

接着查看有有哪些藉由NodePort选项而暴露的port kubectl describe services/kubernetes-bootcamp

将NodePort导出为环境变数

$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')$ echo NODE_PORT=$NODE_PORT
使用curl呼叫API curl $(minikube ip):$NODE_PORT

Step2 Using Labels

Deployment会为pod创建label,所以先查看deployment的状况 kubectl describe deployment

接着取得pod列表,使用-l的参数带入在deployment内查看到的label kubectl get pods -l app=kubernetes-bootcamp

service也能用同样的方法查询 kubectl get services -l app=kubernetes-bootcamp

将pod name导出为环境变数

$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')$ echo Name of the Pod: $POD_NAME

加入新的label kubectl label pods $POD_NAME version=v1

再次查看pod会发现有新的label kubectl describe pods $POD_NAME

也能用新的label取得pod kubectl get pods -l version=v1

Step 3 Deleting a service

使用标籤删除service kubectl delete service -l app=kubernetes-bootcamp 可以再删除前先下 kubectl get services 在执行删除

再次取得services list kubectl get services 可以发现有service被删除

确认由NodePort暴露的Service被删除 curl $(minikube ip):$NODE_PORT

此时证明了从外部无法透过service访问app,此时可以在k8s内部下达指令查看app是否正常 kubectl exec -ti $POD_NAME -- curl localhost:8080

参考

https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/

关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章