k8s(8)Service(服务)

简介: Service(服务)

Service(服务)

传送门

[root@k8s ~]# kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/nginx-deploy-855866bb46-8fgzg   1/1     Running   0          10m
pod/nginx-deploy-855866bb46-87scw   1/1     Running   0          10m
pod/nginx-deploy-855866bb46-5rqh4   1/1     Running   0          10m
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.43.0.1    <none>        443/TCP   39h
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deploy   3/3     3            3           81m
NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deploy-855866bb46   3         3         3       22m

创建service

ServiceType 取值

  • ClusterIP(默认):将服务公开在集群内部。kubernetes会给服务分配一个集群内部的 IP,集群内的所有主机都可以通过这个Cluster-IP访问服务。集群内部的Pod可以通过service名称访问服务。
  • NodePort:通过每个节点的主机IP 和静态端口(NodePort)暴露服务。 集群的外部主机可以使用节点IP和NodePort访问服务。
  • ExternalName:将集群外部的网络引入集群内部。
  • LoadBalancer:使用云提供商的负载均衡器向外部暴露服务。

ClusterIP

如果不设置ServiceType,默认是ClusterIP

可以在内部主机访问

内部主机可以访问ip+端口

内部主机的pod可以访问service名+端口

[root@k8s ~]# kubectl expose deploy nginx-deploy --name=nginx-service --port=8080 --target-port=80
service/nginx-service exposed
[root@k8s ~]# kubectl get service
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes      ClusterIP   10.43.0.1      <none>        443/TCP    39h
nginx-service   ClusterIP   10.43.65.176   <none>        8080/TCP   7s

NodePort

可以在外部主机访问

主机ip+NodePort

[root@k8s ~]# kubectl expose deploy nginx-deploy --name=nginx-outside --type=NodePort --port=8081 --target-port=80
service/nginx-outside exposed
[root@k8s ~]# kubectl get service
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.43.0.1      <none>        443/TCP          40h
nginx-service   ClusterIP   10.43.65.176   <none>        8080/TCP         40m
nginx-outside   NodePort    10.43.127.65   <none>        8081:32109/TCP   10s

服务器是192.168.80.15,192.168.80.16,192.168.80.17

都可以访问

访问服务

集群的每个服务器都能访问10.43.65.176:8080,这个是进群内部分配的ip

[root@k8s ~]# curl 10.43.65.176:8080
<!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>

在主机内部可以通过服务名+端口访问,服务器是不能直接访问服务的

[root@k8s ~]# curl nginx-service
curl: (6) Could not resolve host: nginx-service; 未知的错误
[root@k8s ~]# kubectl run test -it --image=nginx:1.22 --rm -- bash
If you dont see a command prompt, try pressing enter.
root@test:/# curl nginx-service:8080
<!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>

查看服务

[root@k8s ~]# kubectl describe service nginx-service
Name:              nginx-service
Namespace:         default
Labels:            app=nginx-deploy
Annotations:       <none>
Selector:          app=nginx-deploy
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.43.65.176
IPs:               10.43.65.176
Port:              <unset>  8080/TCP
TargetPort:        80/TCP
Endpoints:         10.42.0.36:80,10.42.1.26:80,10.42.2.25:80
Session Affinity:  None
Events:            <none>

负载均衡

修改某一个pod的页面为hello,然后多次访问,发现某一次访问到hello

[root@k8s ~]# kubectl get pod
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-855866bb46-8fgzg   1/1     Running   0          59m
nginx-deploy-855866bb46-87scw   1/1     Running   0          59m
nginx-deploy-855866bb46-5rqh4   1/1     Running   0          59m
[root@k8s ~]# kubectl exec -it nginx-deploy-855866bb46-5rqh4 -- bash
root@nginx-deploy-855866bb46-5rqh4:/# cd /usr/share/nginx/html/
root@nginx-deploy-855866bb46-5rqh4:/usr/share/nginx/html# ls
50x.html  index.html
root@nginx-deploy-855866bb46-5rqh4:/usr/share/nginx/html# echo hello > index.html 
root@nginx-deploy-855866bb46-5rqh4:/usr/share/nginx/html# exit
exit
[root@k8s ~]# curl 10.43.65.176:8080
<!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>
[root@k8s ~]# curl 10.43.65.176:8080
hello
相关实践学习
容器服务Serverless版ACK Serverless 快速入门:在线魔方应用部署和监控
通过本实验,您将了解到容器服务Serverless版ACK Serverless 的基本产品能力,即可以实现快速部署一个在线魔方应用,并借助阿里云容器服务成熟的产品生态,实现在线应用的企业级监控,提升应用稳定性。
云原生实践公开课
课程大纲 开篇:如何学习并实践云原生技术 基础篇: 5 步上手 Kubernetes 进阶篇:生产环境下的 K8s 实践 相关的阿里云产品:容器服务&nbsp;ACK 容器服务&nbsp;Kubernetes&nbsp;版(简称&nbsp;ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情:&nbsp;https://www.aliyun.com/product/kubernetes
目录
相关文章
|
1月前
|
存储 运维 Kubernetes
容器服务ACK常见问题之修改service的名字失败如何解决
容器服务ACK(阿里云容器服务 Kubernetes 版)是阿里云提供的一种托管式Kubernetes服务,帮助用户轻松使用Kubernetes进行应用部署、管理和扩展。本汇总收集了容器服务ACK使用中的常见问题及答案,包括集群管理、应用部署、服务访问、网络配置、存储使用、安全保障等方面,旨在帮助用户快速解决使用过程中遇到的难题,提升容器管理和运维效率。
|
4月前
|
Kubernetes 负载均衡 网络协议
k8s学习-Service(概念、模板、创建、外部代理、删除等)
k8s学习-Service(概念、模板、创建、外部代理、删除等)
144 0
|
2月前
|
Prometheus 监控 Kubernetes
如何用 Prometheus Operator 监控 K8s 集群外服务?
如何用 Prometheus Operator 监控 K8s 集群外服务?
|
4月前
|
Kubernetes 负载均衡 网络协议
k8s教程(service篇)-总结(上)
k8s教程(service篇)-总结(上)
70 0
|
20天前
|
Kubernetes Linux Windows
kubectl 本地远程链接k8s多个集群,远程管控多集群,查看日志 部署服务(windows版)
kubectl 本地远程链接k8s多个集群,远程管控多集群,查看日志 部署服务(windows版)
234 0
|
4月前
|
Kubernetes Shell Linux
linux|shell脚本|有趣的知识---格式化输出日志和脚本调试方法以及kubernetes集群核心服务重启和集群证书备份脚本
linux|shell脚本|有趣的知识---格式化输出日志和脚本调试方法以及kubernetes集群核心服务重启和集群证书备份脚本
63 0
|
3月前
|
Kubernetes 负载均衡 网络协议
|
28天前
|
人工智能 监控 Serverless
如何基于ACK Serverless快速部署AI推理服务
通过上述步骤,可以在ACK Serverless上快速部署AI推理服务,实现高可用、弹性扩展的服务架构。
21 1
|
28天前
|
Kubernetes 网络协议 Docker
K8S核心插件-coredns服务
K8S核心插件-coredns服务
15 0
|
1月前
|
Kubernetes 应用服务中间件 nginx
Kubernetes服务网络Ingress网络模型分析、安装和高级用法
Kubernetes服务网络Ingress网络模型分析、安装和高级用法
36 5