创建Service资源
Nodeport类型:
apiVersion: v1 #api版本
kind: Service #资源类型
metadata:
name: nginx-service #Service 的名称
labels: #Service 自己的标签
app: nginx #为该 Service 设置 key 为 app,value 为 nginx 的标签
spec: #这是关于该 Service 的定义,描述了 Service 如何选择 Pod,如何被访问
selector: #标签选择器
app: nginx #选择包含标签 app:nginx 的 Pod
ports:
- name: nginx-port #端口的名字
protocol: TCP #协议类型 TCP/UDP
port: 80 #集群内的其他容器组可通过 80 端口访问 Service
nodePort: 32600 #通过任意节点的 32600 端口访问 Service
targetPort: 80 #将请求转发到匹配 Pod 的 80 端口
type: NodePort #Serive的类型,ClusterIP/NodePort/LoaderBalancer
ClusterIP类型:
apiVersion: v1
kind: Service
metadata:
name: cluster-svc
spec:
selector:
app: nginx
version: v1
type: ClusterIP
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
ExternalName类型:
apiVersion: v1
kind: Service
metadata:
name: client-svc #Service名称
spec:
type: ExternalName #创建ExternalName类型的Service
externalName: deploy-test.default.svc.cluster.local #软链接,可跨名称空间,容器内请求client-svc等于请求deploy-test.default.svc.cluster.local
ports:
- name: http
port: 80 #Service本身的端口
targetPort: 80 #容器内程序自身监听的端口
LoadBalancer类型:
apiVersion: v1
kind: Service
metadata:
name: balancer-svc
labels:
app: nginx
version: v1
spec:
selector:
app: nginx # 这里应该与你的Pod的标签匹配
ports:
- protocol: TCP
port: 80 # Service的端口号
targetPort: 80 # Pod中应用程序实际监听的端口号
type: LoadBalancer #Kubernetes为Service自动配置一个负载均衡器