1:service详解
| 1、每次访问pod的时候,ip地址都不是固定的 |
| |
| 2、service有一个虚拟ip和端口,可以使用这个来进行访问 |
| |
| 3、kube-proxy,api server将service的信息存入到etcd中,kube-proxy将其转换为一个访问规则,这个就是本质 |
| |
| 4、表象,就是标签,本质就是规则,通过标签,来进行要管理哪些pod, |
| |
| 5、访问pod里面的容器的时候,是根据轮询的状态,可以设置session亲和性来进行设置,将多个请求转发到一个pod里面的容器上 |
| |


环境准备
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| name: pc-deployment |
| namespace: dev |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: nginx-pod |
| template: |
| metadata: |
| labels: |
| app: nginx-pod |
| spec: |
| containers: |
| - name: nginx |
| image: nginx:1.17.1 |
| ports: |
| - containerPort: 80 |
| |
| root@pc-deployment-5cb65f68db-fxlpt:/usr/share/nginx/html |
| 50x.html index.html |
| root@pc-deployment-5cb65f68db-fxlpt:/usr/share/nginx/html |
1、ClusterIP
| [root@master service] |
| apiVersion: v1 |
| kind: Service |
| metadata: |
| name: svc1 |
| namespace: dev |
| spec: |
| selector: |
| app: nginx-pod |
| clusterIP: 10.96.0.100 |
| type: ClusterIP |
| ports: |
| - port: 8080 |
| targetPort: 80 |
| |
| [root@master service] |
| Name: svc1 |
| Namespace: dev |
| Labels: <none> |
| Annotations: <none> |
| Selector: app=nginx-pod |
| Type: ClusterIP |
| IP Family Policy: SingleStack |
| IP Families: IPv4 |
| IP: 10.96.0.100 |
| IPs: 10.96.0.100 |
| Port: <unset> 8080/TCP |
| TargetPort: 80/TCP |
| Endpoints: 10.244.1.67:80,10.244.1.68:80,10.244.2.48:80 |
| Session Affinity: None |
| Events: <none> |
| |
| |
| CP 10.96.0.100:8080 rr |
| -> 10.244.1.67:80 Masq 1 0 0 |
| -> 10.244.1.68:80 Masq 1 0 0 |
| -> 10.244.2.48:80 Masq 1 0 0 |
| |
2、headliness
| clusterip 默认是随机的负载均衡分发策略,这个类型的不会分发clusterip,只能通过域名来进行控制访问 |
3、NodePort
| 就是将svc的port映射到node节点上面,通过nodeip+端口来实现访问,占用了主机的一个端口 |
| apiVersion: v1 |
| kind: Service |
| metadata: |
| name: svc2 |
| namespace: dev |
| spec: |
| selector: |
| app: nginx-pod |
| type: NodePort |
| ports: |
| - port: 80 |
| targetPort: 80 |
| nodePort: 30002 |
| |
| |
| [root@master service] |
| NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| nginx NodePort 10.96.20.47 <none> 80:30528/TCP 89m |
| svc1 ClusterIP 10.96.0.100 <none> 8080/TCP 34m |
| svc2 NodePort 10.96.139.188 <none> 80:30002/TCP 2m4s |
| |
| |
| [root@master service] |
| 10.244.1.67 |
| [root@master service] |
| 10.244.2.48 |
| |
4、LoadBalancer
5、ExternalName
6、Ingress
| 1、因为nodeport会占用主机上面的一个端口,因此的话很多服务的话,1就浪费了大量的端口 |
| |
| 2、使用ingress服务 |
| 就是通过域名来进行转发到对应的service上面,实现访问 |
| |


| 环境准备 |
| |
| wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.2/deploy/static/provider/cloud/deploy.yaml |
| |
| |
| registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.1.0 |
| |
| registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1 |
| |
| |
| |
| [root@master service] |
| NAME READY STATUS RESTARTS AGE |
| ingress-nginx-admission-create-q984g 0/1 Completed 0 49m |
| ingress-nginx-admission-patch-twmqm 0/1 Completed 1 49m |
| ingress-nginx-controller-6f4d47c657-bv8qx 1/1 Running 0 49m |
| [root@master service] |
| NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| ingress-nginx-controller NodePort 10.96.70.212 <none> 80:30906/TCP,443:30339/TCP 49m |
| ingress-nginx-controller-admission ClusterIP 10.107.42.140 <none> 443/TCP 49m |
| |
| |
| [root@master service] |
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| name: pc-deployment |
| namespace: dev |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: nginx-pod |
| template: |
| metadata: |
| labels: |
| app: nginx-pod |
| spec: |
| containers: |
| - name: nginx |
| image: nginx:1.17.1 |
| ports: |
| - containerPort: 80 |
| |
| --- |
| apiVersion: apps/v1 |
| kind: Deployment |
| metadata: |
| name: tomcat-deployment |
| namespace: dev |
| spec: |
| replicas: 3 |
| selector: |
| matchLabels: |
| app: t1 |
| template: |
| metadata: |
| labels: |
| app: t1 |
| spec: |
| containers: |
| - name: tomcat |
| image: tomcat:8.5-jre10-slim |
| ports: |
| - containerPort: 8080 |
| |
| --- |
| apiVersion: v1 |
| kind: Service |
| metadata: |
| name: nginx-service |
| namespace: dev |
| spec: |
| selector: |
| app: nginx-pod |
| clusterIP: None |
| type: ClusterIP |
| ports: |
| - port: 80 |
| targetPort: 80 |
| |
| --- |
| apiVersion: v1 |
| kind: Service |
| metadata: |
| name: tomcat-service |
| namespace: dev |
| spec: |
| selector: |
| app: t1 |
| type: ClusterIP |
| clusterIP: None |
| ports: |
| - port: 8080 |
| targetPort: 8080 |
| |
| |
| [root@master service] |
| apiVersion: networking.k8s.io/v1 |
| kind: Ingress |
| metadata: |
| name: ingress-http |
| namespace: dev |
| annotations: |
| nginx.ingress.kubernetes.io/rewrite-target: / |
| spec: |
| ingressClassName: nginx |
| rules: |
| - host: nginx.com |
| http: |
| paths: |
| - pathType: Prefix |
| path: /app |
| backend: |
| service: |
| name: nginx-service |
| port: |
| number: 80 |
| - host: tomcat.com |
| http: |
| paths: |
| - pathType: Prefix |
| path: / |
| backend: |
| service: |
| name: tomcat-service |
| port: |
| number: 8080 |
| |
| |
| [root@master service] |
| 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 |
| ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 |
| 10.104.43.43 master |
| 10.104.43.93 node1 |
| 10.104.43.126 node2 |
| 10.104.43.43 nginx.com |
| 10.104.43.43 tomcat.com |
| |
| |
| [root@master service] |
| NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| ingress-nginx-controller NodePort 10.96.70.212 <none> 80:30906/TCP,443:30339/TCP 51m |
| ingress-nginx-controller-admission ClusterIP 10.107.42.140 <none> 443/TCP 51m |
| [root@master service] |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <title>Welcome to nginx!</title> |
| <style> |
| body { |
| width: 35em; |
| margin: 0 auto; |
| font-family: Tahoma, Verdana, Arial, sans-serif; |
| } |
| </style> |
| </head> |
| <body> |
| <h1>Welcome to nginx!</h1> |
| <p>If you see this page, the nginx web server is successfully installed and |
| working. Further configuration is required.</p> |
| |
| <p>For online documentation and support please refer to |
| <a href="http://nginx.org/">nginx.org</a>.<br/> |
| Commercial support is available at |
| <a href="http://nginx.com/">nginx.com</a>.</p> |
| |
| <p><em>Thank you for using nginx.</em></p> |
| </body> |
| </html> |
| |
| [root@master service] |
| |
| |
| |
| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8" /> |
| <title>Apache Tomcat/8.5.35</title> |
| <link href="favicon.ico" rel="icon" type="image/x-icon" /> |
| <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /> |
| <link href="tomcat.css" rel="stylesheet" type="text/css" /> |
| </head> |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库