Kubernetes Service发布
一、定义Service
1-1、首先创建一个Deployment 类型nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #定义Deployment类型nginx yaml文件 apiVersion: apps /v1 kind: Deployment metadata: labels: app: nginx name: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx:1.16.1-alpine name: nginx ports: - containerPort: 80 #创建nginx kubectl create -f nginx_deploy.yaml |
1-2、定义Service 的yaml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #service yaml 文件 apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: nginx #匹配pod标签 ports: - protocol: TCP port: 80 #service端口 targetPort: 80 #容器端口 #创建service kubectl create -f service.yaml |
1-3、查看创建Service状态
1 2 3 4 5 6 7 8 9 10 | #查看所有service状态 [root@k8s-master1 opt] # kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.0.0.1 <none> 443 /TCP 20h my-service ClusterIP 10.2.99.129 <none> 80 /TCP 10m #只查看上面定义的my-service状态 [root@k8s-master1 opt] # kubectl get svc my-service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-service ClusterIP 10.2.99.129 <none> 80 /TCP 10m |
注:
该示例为my-server:80即可访问到具有app=myapp标签的Pod的80端口。
需要注意的是,Service 能够将一个接收端口映射到任意的 targetPort,如果 targetPort 为空,
targetPort 将被设置为与 Port 字段相同的值。targetPort 可以设置为一个字符串,引用 backend
Pod 的一个端口的名称,这样的话即使更改了 Pod 的端口,也不会对 Service 的访问造成影响。
Kubernetes Service 能够支持 TCP、UDP、SCTP 等协议,默认为 TCP 协议。
二、Service类型
2-1、Kubernetes Service Type(服务类型)主要包括以下几种:
➢ ClusterIP:在集群内部使用,默认值,只能从集群中访问。
➢ NodePort:在所有安装了 Kube-Proxy 的节点上打开一个端口,此端口可以代理至后端
Pod,可以通过 NodePort 从集群外部访问集群内的服务,格式为 NodeIP:NodePort。
➢ LoadBalancer:使用云提供商的负载均衡器公开服务,成本较高。
2-2、NodePort类型
说明:
如果将 Service 的 type 字段设置为 NodePort,则 Kubernetes 将从--service-node-port-range 参
数指定的范围(默认为 30000-32767)中自动分配端口,也可以手动指定 NodePort,创建该 Service
1)定义NodePort类型的Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #定义Service NodePort类型yaml apiVersion: v1 kind: Service metadata: name: my-service spec: type : NodePort #定义NodePort类型 selector: app: nginx #匹配pod标签 ports: - protocol: TCP port: 80 #service端口 targetPort: 80 #容器端口 nodePort: 30000 #映射nodePort类型自定义端口。如果该项不设置会自动分配NodePort端口 #新定义的NodePort类型刷新 kubectl replace -f service.yaml |
2)可以通过以下命令进行设置NodePort 类型端口映射命令如下
1 | kubectl edit svc my-service |
3)查看NodePort端口映射状态
1 2 3 | [root@k8s-master1 opt] # kubectl get svc my-service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE my-service NodePort 10.2.99.129 <none> 80:30000 /TCP 36m |
注:
80:30000/TCP 这里的30000端口是通过上面设置NodePort类型,端口自定义设置的端口。
外网访问:可以通过宿主机:映射的端口访问。例如:192.168.3.123:30000端口即可访问nginx
4)如果不自定义设置NodePort port端口会自动分配端口,分配端口范围是30000-32767。端口自动分配范围设置查看
1 2 | [root@k8s-master1 opt] # cat /usr/lib/systemd/system/kube-apiserver.service | grep "service-node-port-range" --service-node-port-range=30000-32767 \ |
2-3、使用Service代理k8s 外部服务
1)使用场景
➢ 希望在生产环境中使用某个固定的名称而非 IP 地址访问外部的中间件服务;
➢ 希望 Service 指向另一个 Namespace 中或其他集群中的服务;
➢ 正在将工作负载转移到 Kubernetes 集群,但是一部分服务仍运行在 Kubernetes 集群之外的 backend。
2)编辑Service,Endpoints yaml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #编辑Endpoints yaml文件 apiVersion: v1 kind: Service metadata: labels: app: nginx-svc-external name: nginx-svc-external spec: ports: - name: http port: 80 protocol: TCP targetPort: 80 sessionAffinity: None type : ClusterIP --- apiVersion: v1 kind: Endpoints metadata: labels: app: nginx-svc-external name: nginx-svc-external subsets: - addresses: - ip: 140.205.94.189 ports: - name: http port: 80 protocol: TCP #创建 kubectl create -f nginx-svc-external.yaml |
注:
3)查询创建的Endpoints service状态
1 2 3 | [root@k8s-master1 opt] # kubectl get svc nginx-svc-external NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-svc-external ClusterIP 10.4.96.53 <none> 80 /TCP 23s |
4)根据上面查看的状态获取的 CLUSTER-IP,测试是否可以通过IP地址访问外网140.205.94.189
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #通过内部service IP地址访问 [root@k8s-master1 opt] # curl 10.4.96.53 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN" > <html> < head ><title>501 Not Implemented< /title >< /head > <body bgcolor= "white" > <h1>501 Not Implemented< /h1 > <p>The requested method to the URL not supported. Sorry for the inconvenience.<br/> Please report this message and include the following information to us.<br/> Thank you very much!< /p > <table> < tr > <td>URL:< /td > <td>http: //10 .4.96.53/< /td > < /tr > < tr > <td>Server:< /td > <td>l3src10.eu6< /td > < /tr > < tr > <td>Date:< /td > <td>2022 /09/21 20:36:57< /td > < /tr > < /table > <hr/>Powered by Tengine< /body > < /html > #访问代理IP地址 [root@k8s-master1 opt] # curl 140.205.94.189 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN" > <html> < head ><title>501 Not Implemented< /title >< /head > <body bgcolor= "white" > <h1>501 Not Implemented< /h1 > <p>The requested method to the URL not supported. Sorry for the inconvenience.<br/> Please report this message and include the following information to us.<br/> Thank you very much!< /p > <table> < tr > <td>URL:< /td > <td>http: //140 .205.94.189/< /td > < /tr > < tr > <td>Server:< /td > <td>l3src9.eu6< /td > < /tr > < tr > <td>Date:< /td > <td>2022 /09/21 20:37:13< /td > < /tr > < /table > <hr/>Powered by Tengine< /body > < /html > |
注:
通过测试访问Service Endpoints 代理的外网IP地址和内部service地址访问正常
示例2、访问外部MySQL数据库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | apiVersion: v1 kind: Service metadata: name: endpoints-mysql-jdbc spec: type : ClusterIP ports: - protocol: TCP port: 3306 name: mysql-endpoints-port --- apiVersion: v1 kind: Endpoints metadata: name: endpoints-mysql-jdbc subsets: - addresses: - ip: 192.168.3.10 ports: - name: mysql-endpoints-port protocol: TCP port: 3306 |
注:Service yaml文件里面的medata.name 和 Endpoints yaml metadata.name名称需要保持一致
访问测试:
1 2 3 4 5 6 7 8 9 10 | [root@k8s-master1 www] # kubectl get svc endpoints-mysql-jdbc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE endpoints-mysql-jdbc ClusterIP 10.2.168.42 <none> 3306 /TCP 7m5s [root@k8s-master1 www] # telnet 10.2.168.42 3306 Trying 10.2.168.42... Connected to 10.2.168.42. Escape character is '^]' . J 5.7.27 j #Exl{I!xW,7Vd4vmysql_native_password |
三、Igress Controller安装使用
3-1、官网安装文档:https://kubernetes.github.io/ingress-nginx/deploy/#bare-metal-clusters
3-2、可以通过访问把yaml文件创建到本地进行安装,安装的时候上面的yaml文件最好改成国内镜像连接地址
1)镜像连接地址修改如下(注:如果国外的镜像连接地址可以访问下载修改可以忽略):
1 | image: registry.cn-beijing.aliyuncs.com /dotbalo/controller :v1.2.0 |
2)安装创建的ymal文件,创建的yaml文件名称为deploy-ingress.yaml
deploy-ingress.yaml下载地址:
链接:https://pan.baidu.com/s/1voXbd2vhbQNU5UmJSSHqJg?pwd=gvrn
提取码:gvrn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | [root@k8s-master1 opt] # kubectl create -f deploy-ingress.yaml namespace /ingress-nginx created serviceaccount /ingress-nginx created serviceaccount /ingress-nginx-admission created role.rbac.authorization.k8s.io /ingress-nginx created role.rbac.authorization.k8s.io /ingress-nginx-admission created clusterrole.rbac.authorization.k8s.io /ingress-nginx created clusterrole.rbac.authorization.k8s.io /ingress-nginx-admission created rolebinding.rbac.authorization.k8s.io /ingress-nginx created rolebinding.rbac.authorization.k8s.io /ingress-nginx-admission created clusterrolebinding.rbac.authorization.k8s.io /ingress-nginx created clusterrolebinding.rbac.authorization.k8s.io /ingress-nginx-admission created configmap /ingress-nginx-controller created service /ingress-nginx-controller created service /ingress-nginx-controller-admission created deployment.apps /ingress-nginx-controller created job.batch /ingress-nginx-admission-create created job.batch /ingress-nginx-admission-patch created ingressclass.networking.k8s.io /nginx created validatingwebhookconfiguration.admissionregistration.k8s.io /ingress-nginx-admission created |
3-3、使用域名发布服务
1)创建一个web服务
1 | kubectl create deploy nginx --image=registry.cn-beijing.aliyuncs.com /dotbalo/nginx :1.15.12 |
2)暴露服务
1 | kubectl expose deploy nginx --port 80 |
注: 如果根据开始(一)的示例进行安装可以忽略创建一个web服和暴露服的操作
3)创建Ingress
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #编辑ingress yaml文件 [root@k8s-master1 opt] # vim web-ingress.yaml apiVersion: networking.k8s.io /v1 # k8s >= 1.22 必须 v1 kind: Ingress metadata: name: nginx-ingress spec: ingressClassName: nginx rules: - host: nginx. test .com http: paths: - backend: service: name: my-service #指的是service名称 port: number: 80 path: / pathType: ImplementationSpecific #创建 [root@k8s-master1 opt] # kubectl create -f web-ingress.yaml ingress.networking.k8s.io /nginx-ingress created |
4)检查创建的Ingress状态
1 2 3 | [root@k8s-master1 opt] # kubectl get ingress NAME CLASS HOSTS ADDRESS PORTS AGE nginx-ingress nginx nginx. test .com 80 10s |
注:在设置Ingress 名称(name)的时候最好是简单明了。例如设置的上面的nginx-ingress名称根据自己业务进行设置
5)pathType:路径的匹配方式,目前有 ImplementationSpecific、Exact 和 Prefix 方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | apiVersion: networking.k8s.io /v1 # k8s >= 1.22 必须 v1 kind: Ingress metadata: name: nginx-ingress spec: ingressClassName: nginx rules: - host: nginx. test .com http: paths: - backend: service: name: my-service #指的是service名称 port: number: 80 path: / pathType: ImplementationSpecific - host: web. test .com http: paths: - backend: service: name: my-service #指的是service名称 port: number: 80 path: / pathType: ImplementationSpecific |
注:创建多域名访问的时候域名不要重复,创建多域名直接把 - host 行到pathType 行进行复制拷贝重新定义新的域名
8)创建单个域名多路径
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | kind: Ingress metadata: name: nginx-ingress spec: ingressClassName: nginx rules: - host: nginx. test .com http: paths: - backend: service: name: my-service #指的是service名称 port: number: 80 path: / pathType: ImplementationSpecific - backend: service: name: my-service #指的是service名称 port: number: 80 path: /data pathType: ImplementationSpecific |
注:创建单域名多路径的时候可以直接从- backend 行到pathType行进行复制拷贝,修改路径即可
9)创建多域名和多路径可以直接通过命令编辑
1 | kubectl edit ingress |
注:打开后根据自己的需求编辑即可
10)Ingress不添加域名进行访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | apiVersion: networking.k8s.io /v1 # k8s >= 1.22 必须 v1 kind: Ingress metadata: name: nginx-ingress-IP spec: ingressClassName: nginx rules: - http: paths: - backend: service: name: my-service #指的是service名称 port: number: 80 path: / pathType: ImplementationSpecific |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)