apiVersion: v1
kind: Service # 资源类型为 Servicemetadata:name: nginx-svc # service 名字labels:app: nginx # service 的标签spec:selector:# 选择器,匹配那些pod会被该service 代理app: nginx-deploy # 所有匹配到这个标签的pod 都可以通过该 service 进行访问ports:# 端口樱色-port:80# service自己的端口哦,在使用内网ip访问时使用targetPort:80# 目标 pod的端口name: web # 为service端口起一个名字# 随机启动一个端口(30000-32767),映射到ports中的端口,该端口是直接绑定在node上的,且集群中的每一个node都会绑定这个端口# 也可以用于将服务器暴露给外部访问,但是这种方式实际生产环境不推荐,效率低,而且service是四层负载 type: NodePort
4、service的代理集群内的资源
4.1 创建service
kubectl create -f nginx-svc.yaml
4.2 查看service 信息
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1<none>443/TCP 5d4h
nginx-svc NodePort 10.1.224.211<none>80:31231/TCP 4h16m```
## 4.3 查看service的描述信息
```c
[root@k8s-master ~]# kubectl describe svc nginx-svc
Name: nginx-svc
Namespace:default
Labels: app=nginx
Annotations:<none>
Selector: app=nginx-deploy
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP:10.1.224.211
IPs:10.1.224.211
Port: web 80/TCP
TargetPort:80/TCP
NodePort: web 31231/TCP
Endpoints:10.2.1.55:80,10.2.2.31:80
Session Affinity: None
External Traffic Policy: Cluster
Events:<none>
4.4 进入其他Pod后通过 service name 进行访问
[root@k8s-master ~]# kubectl exec -it dns-test -- sh
/ # wget http://nginx-svc
Connecting to nginx-svc(10.1.224.211:80)
index.html 100%|*************************************************************************************************************************************************************|6120:00:00 ETA
/ # cat index.html
<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>
body {
width:35em;
margin:0auto;
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>
[root@k8s-master ~]# kubectl create -f nginx-svc-external.yaml
service/nginx-svc-external created
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1<none>443/TCP 5d4h
nginx-svc NodePort 10.1.224.211<none>80:31231/TCP 4h21m
nginx-svc-external ClusterIP 10.1.63.181<none>80/TCP 35s
5.1.4 创建endpoints资源
[root@k8s-master ~]# kubectl create -f nginx-ed-external.yaml
endpoints/nginx-svc-external created
[root@k8s-master ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 10.10.10.100:64435d4h
nginx-svc 10.2.1.55:80,10.2.2.31:804h22m
nginx-svc-external 47.110.152.250:8020s
5.1.5 查看endpoints和service描述信息
[root@k8s-master ~]# kubectl describe ep nginx-svc-external
Name: nginx-svc-external
Namespace:default
Labels: app=nginx
Annotations:<none>
Subsets:
Addresses:47.110.152.250
NotReadyAddresses:<none>
Ports:
Name Port Protocol
----------------
web 80 TCP
Events:<none>[root@k8s-master ~]# kubectl describe svc nginx-svc-external
Name: nginx-svc-external
Namespace:default
Labels: app=nginx
Annotations:<none>
Selector:<none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP:10.1.63.181
IPs:10.1.63.181
Port: web 80/TCP
TargetPort:80/TCP
Endpoints:47.110.152.250:80
Session Affinity: None
Events:<none>
5.1.6 通过busybox容器测试 ????
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dns-test 1/1 Running 1(23h ago)24h
fluentd-59k8k 1/1 Running 06h29m
fluentd-hhtls 1/1 Running 06h25m
nginx-deploy-fdd948cf4-69b85 1/1 Running 0135m
nginx-deploy-fdd948cf4-r8ktj 1/1 Running 05h43m
[root@k8s-master ~]# kubectl exec -it dns-test -- sh
/ # wget http://nginx-svc-external
Connecting to nginx-svc(47.110.152.250:80)
index.html 100%|*************************************************************************************************************************************************************|6120:00:00 ETA
/ # cat index.html
<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>
body {
width:35em;
margin:0auto;
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>
5.1.7 k8s集群中的pod访问外部服务的流程
5.2 service反向代理外部域名(通过域名访问 )
5.2.1 创建service的配置文件
apiVersion: v1
kind: Service # 资源类型为 Servicemetadata:name: test-svc-external-domian # service 名字labels:app: test-svc-external-domian # service 的标签spec:type: ExternalName
externalName: www.lan-he.com.cn
5.2.2 创建service
[root@k8s-master ~]# kubectl create -f test-svc-external-domian.yaml
service/test-svc-external-domian created
5.2.3 查看service信息
service/csdn-svc-external-domian edited
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
test-svc-external-domian ExternalName <none> www.lan-he.com.cn <none>100s
kubernetes ClusterIP 10.1.0.1<none>443/TCP 5d15h
nginx-svc NodePort 10.1.224.211<none>80:31231/TCP 14h
nginx-svc-external ClusterIP 10.1.63.181<none>80/TCP 10h
5.2.4 通过busybox容器测试
[root@k8s-master ~]# kubectl exec -it dns-test -- sh
/ # wget http://www.lan-he.com.cn
Connecting to www.lan-he.com.cn(47.110.152.250:80)
index.html 100%|*************************************************************************************************************************************************************|6120:00:00 ETA
/ # cat index.html
<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>
body {
width:35em;
margin:0auto;
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>