Service介绍
Service不是一个实体的服务,而是一条iptables或ipvs的转发规则
每个pod都有自己的ip地址,当Controller用新的pod替代发生故障的pod时,新的pod会分配搭配新的ip地址,这样就产生一个问题,如果一组pod对外提供服务(例如http),他们的ip很有可能发生变化,那么客户端就无法访问这个服务,为了解决此问题,k8s提出了Service
Service作用
通过Service为pod客户端提供访问pod方法,即客户端访问pod的入口
Service通过pod标签与pod进行关联
Service类型
ClusterIP
:默认,分配一个集群内部可以访问的虚拟
IP
NodePort:在每个Node上分配一个端口作为外部访问入口
LoadBalancer
:工作在特定的
Cloud Provider
上,例如
Google Cloud
,
AWS
,
OpenStack
ExternalName:表示把集群外部的服务引入到集群内部中来,即实现了集群内部
pod
和集群外部 的服务进行通信
Service参数
port:访问service使用的端口
targetPort:pod中容器的端口
NodePort:通过Node实现外网用户访问k8s集群内service(30000-32767)
Service创建
Service的创建时通过资源清单文件yaml文件创建的
编写一个用于创建Service资源清单文件
[root@k8s-master ~]# vim create-deployment-server-clusterip.yaml
[root@k8s-master ~]# cat create-deployment-server-clusterip.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-apps
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginxapps
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-apps
spec:
type: ClusterIP
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
[root@k8s-master ~]# kubectl apply -f create-deployment-server-clusterip.yaml
deployment.apps/nginx-apps created
service/nginx-apps created
验证Deployment类型应用创建情况
[root@k8s-master ~]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-apps 1/1 1 1 101s
访问Service以实现访问pod的目的
查看Service
[root@k8s-master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 3d9h
nginx-apps ClusterIP 10.1.181.93 <none> 80/TCP 8m36s
访问Service
[root@k8s-master ~]# curl http://10.1.181.93
<!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>
基于资源清单YAML文件创建NodePort类型Service
[root@k8s-master ~]# vim create-deployment-service-nodeport.yaml
[root@k8s-master ~]# cat create-deployment-service-nodeport.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app-nodeport
labels:
app: nginx-app
spec:
replicas: 1
selector:
matchLabels:
app: nginx-app
template:
metadata:
labels:
app: nginx-app
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-app
spec:
selector:
app: nginx-app
type: NodePort
ports:
- protocol: TCP
nodePort: 30001
port: 80
targetPort: 80
应用资源清单文件
[root@k8s-master ~]# kubectl apply -f create-deployment-service-nodeport.yaml
deployment.apps/nginx-app-nodeport created
service/nginx-app created
验证资源对象创建情况
[root@k8s-master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 3d9h
nginx-app NodePort 10.1.148.51 <none> 80:30001/TCP 11s
nginx-apps ClusterIP 10.1.181.93 <none> 80/TCP 36m
nginx-service ClusterIP 10.1.162.240 <none> 80/TCP 7h16m
查看k8s集群所在主机的30001端口侦听状态
[root@k8s-master ~]# ss -anput | grep 30001
tcp LISTEN 0 128 *:30001 *:* users:(("kube-proxy",pid=26793,fd=8))
在k8s集群外部访问Service
删除Service
方法一:通过命令行删除
[root@k8s-master ~]# kubectl delete service nginx-app
service "nginx-app" deleted
方法二:通过资源清单yaml文件删除
[root@k8s-master ~]# kubectl delete -f create-deployment-service-nodeport.yaml
deployment.apps "nginx-app-nodeport" deleted