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
原文地址:https://www.cnblogs.com/evescn/p/12260204.html