
1、service的定义
- "Service"简写"svc”。Pod不能直接提供给外网访问,而是应该使用service。Service就是把Pod暴露出来提供服务,Service才是真正的“服务”,它的中文名就叫“服务”。
- 可以说Service是一个应用服务的抽象,定义了Pod逻辑集合和访问这个Pod集合的策路。Service代理Pod集合,对外表现为一个访问入口,访问该入口的请求将经过负载均衡,转发到后端Pod中的容器。
2、service和pod之间的网络是如何打通的
- service和pod之间的网络布局如下图所示

2.1 service与endpoints的关系

2.2 endpoints和pod的关系

2.3 查看创建service的配置文件和pod的关联信息

2.4 service和pod通信过程
- 1、创建service的时候会同时创建一个endpoints,创建service的时候会带有一个选择器,通过这个标签可以找到对应的pod,同时service也会生成ip地址,提供集群内访问。
- 2、endpoints中包含了pod的ip和端口信息,通过iptables转发数据给node上的kube-proxy,node上的kube-proxy把数据转发给pod中的根容器。
3、service的配置文件解析
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
labels:
app: nginx
spec:
selector:
app: nginx-deploy
ports:
- port: 80
targetPort: 80
name: web
type: NodePort
4、service的代理集群内的资源
4.1 创建service
kubectl create -f nginx-svc.yaml
4.2 查看service 信息
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 5d4h
nginx-svc NodePort 10.1.224.211 <none> 80:31231/TCP 4h16m```
## 4.3 查看service的描述信息
```c
[root@k8s-master ~]# kubectl describe svc nginx-svc
Name: nginx-svc
Namespace: default
Labels: app=nginx
Annotations: <none>
Selector: app=nginx-deploy
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.1.224.211
IPs: 10.1.224.211
Port: web 80/TCP
TargetPort: 80/TCP
NodePort: web 31231/TCP
Endpoints: 10.2.1.55:80,10.2.2.31:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
4.4 进入其他Pod后通过 service name 进行访问
[root@k8s-master ~]# kubectl exec -it dns-test -- sh
/ # wget http:
Connecting to nginx-svc (10.1.224.211:80)
index.html 100% |*************************************************************************************************************************************************************| 612 0:00:00 ETA
/ # cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
4.5 service默认是命名空间级,跨namespace如何访问?
- 默认在当前namespace中访问,如果需要跨namespace访问pod,则在service name的后面加上 . 即可
- eg: curl http://nginx-svc.default
5、Pod通过service访问外部资源方式
- 实现方式如下:
- 编写service配置文件时,不指定selector属性的时候,就不会创建endpoints
- 自己创建endpoints
5.1 service代理k8s的外部服务(通过IP地址访问)
5.1.1 创建一个service的配置文件
apiVersion: v1
kind: Service
metadata:
name: nginx-svc-external
labels:
app: nginx
spec:
ports:
- port: 80
targetPort: 80
name: web
type: ClusterIP
5.1.2 创建一个endpoints的配置文件
apiVersion: v1
kind: Endpoints
metadata:
labels:
name: nginx
name: nginx-svc-external
namespace: default
subsets:
- addresses:
- ip: 47.110.152.250
ports:
- name: web
port: 80
5.1.3 创建service资源
[root@k8s-master ~]# kubectl create -f nginx-svc-external.yaml
service/nginx-svc-external created
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 5d4h
nginx-svc NodePort 10.1.224.211 <none> 80:31231/TCP 4h21m
nginx-svc-external ClusterIP 10.1.63.181 <none> 80/TCP 35s
5.1.4 创建endpoints资源
[root@k8s-master ~]# kubectl create -f nginx-ed-external.yaml
endpoints/nginx-svc-external created
[root@k8s-master ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 10.10.10.100:6443 5d4h
nginx-svc 10.2.1.55:80,10.2.2.31:80 4h22m
nginx-svc-external 47.110.152.250:80 20s
5.1.5 查看endpoints和service描述信息
[root@k8s-master ~]# kubectl describe ep nginx-svc-external
Name: nginx-svc-external
Namespace: default
Labels: app=nginx
Annotations: <none>
Subsets:
Addresses: 47.110.152.250
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
web 80 TCP
Events: <none>
[root@k8s-master ~]# kubectl describe svc nginx-svc-external
Name: nginx-svc-external
Namespace: default
Labels: app=nginx
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.1.63.181
IPs: 10.1.63.181
Port: web 80/TCP
TargetPort: 80/TCP
Endpoints: 47.110.152.250:80
Session Affinity: None
Events: <none>
5.1.6 通过busybox容器测试 ????
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 1 (23h ago) 24h
fluentd-59k8k 1/1 Running 0 6h29m
fluentd-hhtls 1/1 Running 0 6h25m
nginx-deploy-fdd948cf4-69b85 1/1 Running 0 135m
nginx-deploy-fdd948cf4-r8ktj 1/1 Running 0 5h43m
[root@k8s-master ~]# kubectl exec -it dns-test -- sh
/ # wget http:
Connecting to nginx-svc (47.110.152.250:80)
index.html 100% |*************************************************************************************************************************************************************| 612 0:00:00 ETA
/ # cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
5.1.7 k8s集群中的pod访问外部服务的流程

5.2 service反向代理外部域名(通过域名访问 )
5.2.1 创建service的配置文件
apiVersion: v1
kind: Service
metadata:
name: test-svc-external-domian
labels:
app: test-svc-external-domian
spec:
type: ExternalName
externalName: www.lan-he.com.cn
5.2.2 创建service
[root@k8s-master ~]# kubectl create -f test-svc-external-domian.yaml
service/test-svc-external-domian created
5.2.3 查看service信息
service/csdn-svc-external-domian edited
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
test-svc-external-domian ExternalName <none> www.lan-he.com.cn <none> 100s
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 5d15h
nginx-svc NodePort 10.1.224.211 <none> 80:31231/TCP 14h
nginx-svc-external ClusterIP 10.1.63.181 <none> 80/TCP 10h
5.2.4 通过busybox容器测试
[root@k8s-master ~]# kubectl exec -it dns-test -- sh
/ # wget http:
Connecting to www.lan-he.com.cn (47.110.152.250:80)
index.html 100% |*************************************************************************************************************************************************************| 612 0:00:00 ETA
/ # cat index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
6、service中spec中的type常用类型
- ClusterIP: 只能在集群内部使用,不配置类型的话默认就是ClusterlP
- ExternalName:返回定义的CNAME别名,可以配置为域名
- NodePort:
- 会在所有安装了kube-proxy的节点都绑定一个端口,此端口可以代理至对应的Pod,集群外部可以使用任意节点ip+NodePort的端口号访问到集群中对应Pod中的服务。
- 当类型设置为NodePort后,可以在ports配置中增加nodePort配置指定端口,需要在下方的端口范围内,如果不指定会随机指定端口。
- 端口范围:30000~32767
- 端口范围配置在/usr/lib/systemd/system/kube-apiserver.service文件中
- LoadBalance: 使用云服务商(阿里云、腾讯云等)提供的负载均衡器服务