#Service
Service为Pod提供4层负载均衡, 访问 -> Service -> Pod组
#4种类型
- ClusterIP 默认,分配一个VIP,只能内部访问
- NodePort ClusterIP基础上,在每个节点绑定一个对外访问端口
- LoadBalancer 在NodePort基础上,外部负载均衡器转发到NodePort
- ExternalName 集群外部的服务引入到集群内部来,集群内部使用
#代理模式
-
iptables 代理模式
-
ipvs 代理模式
#ips轮询模式 rr 轮询调度 lc 最小连接数 dh 目标哈希 sh 源哈希 sed 最短期望延迟 nq 不排队调度
#svc-demo.yaml
#Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: s1
template:
metadata:
labels:
app: myapp
release: s1
env: test
spec:
containers:
- name: myapp
image: alivv/nginx:node
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
---
#ClusterIP
apiVersion: v1
kind: Service
metadata:
name: svc-ip
namespace: default
spec:
type: ClusterIP
selector:
app: myapp
release: s1
ports:
- name: http
port: 80
targetPort: 80
---
#Headless Service
apiVersion: v1
kind: Service
metadata:
name: svc-headless
namespace: default
spec:
selector:
app: myapp
clusterIP: "None"
ports:
- port: 80
targetPort: 80
---
#NodePort
apiVersion: v1
kind: Service
metadata:
name: svc-port
namespace: default
spec:
type: NodePort
selector:
app: myapp
release: s1
ports:
- name: http
port: 80
targetPort: 80
---
#ExternalName
kind: Service
apiVersion: v1
metadata:
name: svc-cname
namespace: default
spec:
type: ExternalName
externalName: hub.elven.vip
#创建
kubectl apply -f svc-demo.yaml
#查看
kubectl get pod
kubectl get deployment
kubectl get svc
##ClusterIP
#curl访问svc-ip查看轮询效果
svc_ip=$(kubectl get svc |awk '/svc-ip/{print $3}')
for((i=1;i<10;i++));do curl $svc_ip ;done
##Headless Service #无头service
#不分配Cluster IP ,service name直接解析为Pod ip
#查看kube-dns ip 默认是10.96.0.10
kubectl get svc -A |grep kube-dns
#指定dns解析svc
dig -t A @10.96.0.10 svc-headless.default.svc.cluster.local
##NodePort
#访问node-ip:port
##ExternalName
#DNS层CNAME别名方式绑定外部地址,访问Service实际访问外部地址
#指定dns解析svc-cname,查看到cname记录
dig -t A @10.96.0.10 svc-cname.default.svc.cluster.local
#删除
kubectl delete -f svc-demo.yaml
Blog地址 https://www.cnblogs.com/elvi/p/11755749.html
本文git地址 https://gitee.com/alivv/k8s/tree/master/notes