k8s-svc实验
示例:1.创建yaml文件(1个deployment、1个service)vim svc-deployment.yamlapiVersion: apps/v1kind: Deploymentmetadata:name: myapp-deploynamespace: defaultspec:replicas: 3selector:matchL...
示例:
1.创建yaml文件(1个deployment、1个service)
vim 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:v1
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中。
创建deployment、svc
kubectl apply -f svc-deployment.yaml/ svc.yaml
随后查看pod、svc
[root@apiserver ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-5556b6864d-4cb2r 1/1 Running 0 8m39s 192.168.102.185 localhost.localdomain <none> <none>
myapp-deploy-5556b6864d-hz595 1/1 Running 0 8m39s 192.168.102.189 localhost.localdomain <none> <none>
myapp-deploy-5556b6864d-qp7n4 1/1 Running 0 8m39s 192.168.102.186 localhost.localdomain <none> <none>
[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:10.102.30.82就相当于访问pod的IP,也就是当pod重新获取IP也不会影响。
Headless Services
有时不需要或不想要负载均衡,以及单独的 Service IP。 遇到这种情况,可以通过指定 Cluster IP(spec.clusterIP)的值为 “None” 来创建 Headless Service。
您可以使用 headless Service 与其他服务发现机制进行接口,而不必与 Kubernetes 的实现捆绑在一起。
对这 headless Service 并不会分配 Cluster IP,kube-proxy 不会处理它们,而且平台也不会为它们进行负载均衡和路由。 DNS 如何实现自动配置,依赖于 Service 是否定义了 selector。
示例:
创建yaml
vim 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
查看svc
[root@apiserver ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp-headless ClusterIP None <none> 80/TCP 8s
查看Headless Services代理的IP
使用dig需要先安装:yum -y install bind-utils
dig -t A myapp-headless.default.svc.cluster.local. @192.168.137.141
解释myapp-headless.default.svc.cluster.local. @192.168.137.14 :
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-peoxy进一步到给对应的pod.
创建yaml:
vim 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:
示例:
创建yaml
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
或者:
apiVersion: v1
kind: Namespace
metadata:
name: prod
labels:
name: prod
检查是否有prod这个命名空间:
kubectl get namespaces
kubectl apply -f external.yaml
查看SVC(会发现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
更多推荐
所有评论(0)