Service
演示
SVC
- 创建
svc-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: stabel
template:
metadata:
labels:
app: myapp
release: stabel
env: test
spec:
containers:
- name: myapp
image: nginx
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
vim svc.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
type: ClusterIP
selector:
app: myapp
release: stabel
ports:
- name: http
port: 80
targetPort: 80
注意
:svc的selector的标签一定要全都包含在deployment的labels中
创建deploymen、svc
kubectl apply -f svc-deployment.yaml
kuebctl apply -f svc.yaml
查看
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-bfd7cc44f-qcnhw 1/1 Running 0 102m 10.244.166.177 node1 <none> <none>
myapp-deploy-bfd7cc44f-txcxf 1/1 Running 0 102m 10.244.166.176 node1 <none> <none>
myapp-deploy-bfd7cc44f-wbrhh 1/1 Running 0 102m 10.244.104.28 node2 <none> <none>
8 node2 <none> <none>
查看svc
[root@apiserver ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp ClusterIP 10.102.30.82 <none> 80/TCP 39s
访问验证
通过svc IP访问pod
curl 10.102.30.82
Headless Service
有时不需要或不想要负载均衡,以及单独的 Service IP
。遇到这种情况,可以通过指定 ClusterIP(spec.clusterIP)
的值为“None”
来创建 Headless Service 。这类 Service 并不会分配 Cluster IP, kube-proxy 不会处理它们,而且平台也不会为它们进行负载均衡和路由
演示
示例
创建svc-none.yaml
文件
apiVersion: v1
kind: Service
metadata:
name: myapp-headless
namespace: default
spec:
selector:
app: myapp
clusterIP: "None"
ports:
- port: 80
targetPort: 80
创建Svc
kubectl apply -f svc-none.yaml
查看
[root@apiserver ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp-headless ClusterIP None <none> 80/TCP 8s
查看Headles Service代理IP
#使用dig需要
yum -y install bind-utils
dig -t A myapp-headless.default.svc.cluster.local. @192.168.137.141
#解释:
myapp-headless.:svc的名字
default:命名空间
svc.cluster.local. :默认域名
@192.168.137.14 :coredns的IP
查看coredns的方法:
kubectl get pod -n kube-system
[root@apiserver ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-6967fb4995-5fg4q 1/1 Running 4 3d10h
coredns-6967fb4995-8v5kb 1/1 Running 4 3d10h
连接验证
[root@apiserver ~]# dig -t A myapp-headless.default.svc.cluster.local. @192.168.137.141
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-9.P2.el7 <<>> -t A myapp-headless.default.svc.cluster.local. @192.168.137.141
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32737
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;myapp-headless.default.svc.cluster.local. IN A
;; ANSWER SECTION:
myapp-headless.default.svc.cluster.local. 30 IN A 192.168.102.186
myapp-headless.default.svc.cluster.local. 30 IN A 192.168.102.185
myapp-headless.default.svc.cluster.local. 30 IN A 192.168.102.189
;; Query time: 1 msec
;; SERVER: 192.168.137.141#53(192.168.137.141)
;; WHEN: 三 1月 01 02:09:09 CST 2020
;; MSG SIZE rcvd: 237
curl 192.168.102.189
就能访问页面
NodePort
nodePort 的原理在于在 node 上开了一个端口,将向该端口的流量导入到 kube-proxy,然后由 kube-proxy 进
一步到给对应的 pod
演示
创建nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
type: NodePort
selector:
app: myapp
release: stabel
ports:
- name: http
port: 80
targetPort: 80
创建svc
kubectl apply -f nodeport.yaml
查看svc
[root@apiserver ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp NodePort 10.102.30.82 <none> 80:30250/TCP 83m
访问验证
在网页上输入master
或者node节点
的IP+30250
就能访问
LoadBalancer
LoadBalancer和nodePort其实是同一种方式,区别在于loadBalancer比nodePort多了一步,就是可以调用cloud provider去创建LB来向节点导流。(收费服务)
类型ExternalName
类型为 ExternalName 的服务将服务映射到 DNS 名称,而不是典型的选择器,例如 my-service 或者 cassandra。 您可以使用 spec.externalName 参数指定这些服务。
演示
例如,以下 Service 定义将 prod 名称空间中的 my-service 服务映射到 hub.kxq.com:
示例:
创建
vim external.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
namespace: prod
spec:
type: ExternalName
externalName: hub.kxq.com
注意:
前提必须有prod的namespace没有的话,需要先创建:
创建svc
kubectl create namespace prod
检查是否有prod这个命名空间
kubectl get namespaces
kubectl apply -f external.yaml
查看
会发现EXTERNAL-IP 以变为我修改的名字
[root@apiserver ~]# kubectl get svc -n prod
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-service-1 ExternalName <none> hub.kxq.com <none> 10s
dig解析查看
[root@apiserver ~]# dig -t A my-service-1.prod.hub.kxq.com. @192.168.137.141
;; ANSWER SECTION:
my-service-1.default.svc.cluster.local. 30 IN CNAME hub.kxq.com.
hub.kxq.com. 30 IN A 199.59.241.250