Service资源清单
目录
一 Service资源清单
1 Service类型
- ClusterIP:
默认模式,只能在集群内部访问
- Nodeport:
每个节点上都监听一个同样的端口号(30000-32767),集群外部可以访问<NodeIP>:<NodePort>联系到集群内部服务,可以配合外部负载均衡使用(配合阿里云的SLB)
- LoadBalancer:
要配合支持公有云负载均衡使用比如GCE、AWS。
其实也是NodePort,只不过会把<NodeIP>:<NodePort>自动添加到公有云的负载均衡当中
- ExternalName:
把集群外部的服务引入集群内部,直接使用
2 创建Service
- 基于命令行创建Service
- 基于资源清单创建Service,资源清单为yaml格式
3 命令行创建
# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port==80 --protocol=TCP
4 资源清单
4.1 资源清单定义获方法
# kubectl explain svc
KIND: Service
VERSION: v1
DESCRIPTION:
Service is a named abstraction of software service (for example, mysql)
consisting of local port (for example 3306) that the proxy listens on, and
the selector that determines which pods will answer requests sent through
the proxy.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
spec <Object>
Spec defines the behavior of a service.
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
status <Object>
Most recently observed status of the service. Populated by the system.
Read-only. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
# kubectl explain svc.spec
KIND: Service
VERSION: v1
RESOURCE: spec <Object>
DESCRIPTION:
Spec defines the behavior of a service.
https://git.k8s.io/community/contributors/devel/api-conventions.md#spec-and-status
ServiceSpec describes the attributes that a user creates on a service.
FIELDS:
clusterIP <string>
clusterIP is the IP address of the service and is usually assigned randomly
by the master. If an address is specified manually and is not in use by
others, it will be allocated to the service; otherwise, creation of the
service will fail. This field can not be changed through updates. Valid
values are "None", empty string (""), or a valid IP address. "None" can be
specified for headless services when proxying is not required. Only applies
to types ClusterIP, NodePort, and LoadBalancer. Ignored if type is
ExternalName. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies
......
ports <[]Object>
The list of ports that are exposed by this service. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies
......
# kubectl explain svc.spec.ports
4.2 ClusterIP资源清单
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
selector:
app: myapp
release: canary
clusterIP: 10.97.97.97
type: ClusterIP
ports:
- port: 80
targetPort: 80
4.3 Nodeport资源清单
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
selector:
app: myapp
release: canary
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080
4.4 内网测试环境服务资源清单
apiVersion: v1
kind: Service
metadata:
name: aws
namespace: default
spec:
selector:
service: aws
module: admin
clusterIP: 10.100.80.4
type: ClusterIP
ports:
- port: 80
targetPort: 8004
apiVersion: v1
kind: Service
metadata:
name: ylop-test
namespace: default
spec:
selector:
service: youleweb
module: admin
clusterIP: 10.100.80.82
type: ClusterIP
type: NodePort
ports:
- port: 80
targetPort: 8082
nodePort: 30001
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程