1、Node Network(节点网络):物理节点或者虚拟节点的网络,如ens33接口上的网路地址 [root@k8s-master1 ~]# ip addr 2: ens33: mtu 1500 qdisc pfifo_fast state UP link/ether 00:0c:29:87:60:d5 brd ff:ff:ff:ff:ff:ff inet 192.168.7.20/24 brd 192.168.7.255 scope global noprefixroute ens33
2、Pod network(pod 网络),创建的Pod具有的IP地址 [root@k8s-master1 ~]# kubectl get pods -o wide NAME READY STATUS IP NODE frontend-h78gw 1/1 Running 10.244.187.76 k8s-node2 Node Network和Pod network这两种网络地址是我们实实在在配置的,其中节点网络地址是配置在节点接口之上,而pod网络地址是配置在pod资源之上的,因此这些地址都是配置在某些设备之上的,这些设备可能是硬件,也可能是软件模拟的
3、Cluster Network(集群地址,也称为service network),这个地址是虚拟的地址(virtual ip),没有配置在某个接口上,只是出现在service的规则当中。 [root@k8s-master1 ~]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) kubernetes ClusterIP 10.96.0.1 443/TCP
1.2 创建Service资源
① 查看定义Service资源需要的字段有哪些?
[root@k8s-master1 ~]# kubectl explain service
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 #service资源使用的api组
kind #创建的资源类型
metadata
② 查看service的spec字段如何定义?
[root@k8s-master1 ~]# kubectl explain service.spec
KIND: Service
VERSION: v1
RESOURCE: spec
DESCRIPTION:
Spec defines the behavior of a service.
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
ServiceSpec describes the attributes that a user creates on a service.
FIELDS:
allocateLoadBalancerNodePorts
clusterIP
#动态分配的地址,也可以自己在创建的时候指定,创建之后就改不了了
clusterIPs <[]string>
externalIPs <[]string>
externalName
externalTrafficPolicy
healthCheckNodePort
ipFamilies <[]string>
ipFamilyPolicy
loadBalancerIP
loadBalancerSourceRanges <[]string>
ports <[]Object> #定义service端口,用来和后端pod建立联系
publishNotReadyAddresses
selector
1.2.1 Service的四种类型
① 查看定义Service.spec.type需要的字段有哪些?
[root@k8s-master1 ~]# kubectl explain service.spec.type
KIND: Service
VERSION: v1
FIELD: type
DESCRIPTION:
type determines how the Service is exposed. Defaults to ClusterIP. Valid
options are ExternalName, ClusterIP, NodePort, and LoadBalancer.
"ClusterIP" allocates a cluster-internal IP address for load-balancing to
endpoints. Endpoints are determined by the selector or if that is not
specified, by manual construction of an Endpoints object or EndpointSlice
objects. If clusterIP is "None", no virtual IP is allocated and the
endpoints are published as a set of endpoints rather than a virtual IP.
"NodePort" builds on ClusterIP and allocates a port on every node which
routes to the same endpoints as the clusterIP. "LoadBalancer" builds on
NodePort and creates an external load-balancer (if supported in the current
cloud) which routes to the same endpoints as the clusterIP. "ExternalName"
aliases this service to the specified externalName. Several other fields do
not apply to ExternalName services. More info:
https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
[root@k8s-master1 ~]# kubectl explain service.spec.ports
KIND: Service
VERSION: v1
RESOURCE: ports <[]Object>
DESCRIPTION:
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
ServicePort contains information on service's port.
FIELDS:
appProtocol
name #定义端口的名字
nodePort
\#宿主机上映射的端口,比如一个Web应用需要被k8s集群之外的其他用户访问,那么需要配置type=NodePort,若配置nodePort=30001,那么其他机器就可以通过浏览器访问scheme://k8s集群中的任何一个节点ip:30001即可访问到该服务,例如http://192.168.7.20:30001。如果在k8s中部署MySQL数据库,MySQL可能不需要被外界访问,只需被内部服务访问,那么就不需要设置NodePort
port -required- #service的端口,这个是k8s集群内部服务可访问的端口
protocol
targetPort
# targetPort是pod上的端口,从port和nodePort上来的流量,经过kube-proxy流入到后端pod的targetPort上,最后进入容器。与制作容器时暴露的端口一致(使用DockerFile中的EXPOSE),例如官方的nginx暴露80端口。