Service服务类型
Service代理模式
Service存在的意义
service引入主要是解决Pod的动态变化,提供统一访问入口:
- 防止Pod失联,准备找到提供同一个服务的Pod(服务发现)
- 定义一组Pod的访问策略(负载均衡)
Pod与Service的关系:
- Service通过标签关联一组Pod
- Service使用iptables或者ipvs为一组Pod提供负载均衡能力
Kubernetes 中Service有如下4中类型:
ClusterIP:默认类型,自动分配一个仅 Cluster 内部可以访问的虚拟IP
NodePort:在 ClusterIP 基础上为Service在每台机器上绑定一个端口,这样可以通过 NodeIP:NodePort来访问服务
LoadBalancer:在 NodePort 的基础上,借助 cloud provider 创建一个外部负载均衡器,并将请求转发到 NodeIP:NodePort
ExternalName:把集群外部的服务引入到集群内部来,在集群内部直接使用,没有任何类型代理被创建,这只有Kubernetes 1.7或更高版本的kube-dns才支持
ClusterIP
默认类型,自动分配一个仅Cluster内部能够访问的虚拟IP
编写xx.yaml文件
[root@master manifest]# cat network.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: notwork
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: web
release: v1
template:
metadata:
labels:
app: web
release: v1
spec:
containers:
- name: web
image: nginx
imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: ClusterIP # 指定ClusterIP类型
selector:
app: web
ports:
- name: web
port: 80
targetPort: 80
[root@master manifest]# kubectl create -f network.yaml deployment.apps/notwork created
service/web created
#查看ip地址
[root@master ~]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/notwork-6cb9497c86-hz8jm 1/1 Running 0 6s
pod/notwork-6cb9497c86-sfjbk 1/1 Running 0 6s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d23h
service/web ClusterIP 10.107.190.89 <none> 80/TCP 6s
#访问
[root@master ~]# curl 10.107.190.89
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>