①. Service服务发现ClusterIP方式
- ①. Service:Pod的服务发现与负载均衡
- ②. 有三个pods实例的nginx,进入到容器内部,对默认页面进行如下操作
cd /usr/share/nginx echo "hello 111"->index.html echo "hello 222"->index.html echo "hello 333"->index.html
③. 隐藏pod的端口和ip,暴露serviceip和端口,在集群内部进行访问
# 和下面命令等价kubectl expose deploy my-dep-01 --port=8000 --target-port=80 --type=ClusterIP [root@k8smaster ~]# kubectl expose deployment my-dep-01 --port=8000 --target-port=80 [root@k8smaster ~]# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d15h my-dep-01 ClusterIP 10.96.170.26 <none> 8000/TCP 2m12s [root@k8smaster ~]# curl 10.96.170.26:8000 hello 111- [root@k8smaster ~]# curl 10.96.170.26:8000 hello 222- [root@k8smaster ~]# curl 10.96.170.26:8000 hello 111- [root@k8smaster ~]# curl 10.96.170.26:8000 hello 333- [root@k8smaster ~]# curl 10.96.170.26:8000
④. 如果说这个时候我们创部署一个前端的项目,前端项目可以通过域名的方式进行访问
curl my-dep-01.default.svc:8000 (注意不能在浏览器中访问,也不能在集群不进入容器的方式进行访问)
⑤. 通过yaml文件配置Service
app: my-dep-01通过如下命令查询到的
[root@k8smaster ~]# kubectl get pod --show-labels NAME READY STATUS RESTARTS AGE LABELS my-dep-01-686cfb7bf-b2vbl 1/1 Running 0 55m k8s-app=my-dep-01,pod-template-hash=686cfb7bf my-dep-01-686cfb7bf-f9wdc 1/1 Running 0 55m k8s-app=my-dep-01,pod-template-hash=686cfb7bf my-dep-01-686cfb7bf-jdhqh 1/1 Running 0 55m k8s-app=my-dep-01,pod-template-hash=686cfb7bf
apiVersion: v1 kind: Service metadata: labels: app: my-dep name: my-dep spec: selector: app: my-dep-01 ports: - port: 8000 protocol: TCP targetPort: 80