小知识:关于k8s 使用 Service 控制器对外暴露服务的问题

Service 引入主要是解决 Pod 的动态变化,提供统一访问入口:

防止 Pod 失联,准备找到提供同一个服务的 Pod (服务发现)  定义一组 Pod 的访问策略 (负载均衡)

部署 deploy

?
1
kubectl apply -f deploy.yaml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: apps/v1
kind: Deployment
metadata:
name: chiyi-nginx
spec:
replicas: 3
selector:
matchLabels:
app: chiyi-nginx
template:
metadata:
labels:
app: chiyi-nginx
spec:
containers:
– name: nginx
image: nginx:1.14.2
ports:
– containerPort: 80

部署 service

?
1
kubectl apply -f service.yaml
?
1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: chiyi-nginx
spec:
selector:
app: chiyi-nginx
ports:
– protocol: TCP
port: 80
targetPort: 80
nodePort: 30002
type: NodePort

查看 service 和 pod 的关系

?
1
2
kubectl  get ep
curl 10.244.1.58:80

说明:

Service 通过标签关联一组 Pod

Service 为一组 Pod 提供负载均衡能力

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@k8s-master service]# kubectl get ep
NAME          ENDPOINTS                                      AGE
chiyi-nginx   10.244.1.58:80,10.244.1.59:80,10.244.2.46:80   5m19s
kubernetes    172.17.28.225:6443                             23h
[root@k8s-master service]# curl 10.244.1.58:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>

查看 service

?
1
2
kubectl  get service
curl 10.101.104.218
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@k8s-master service]# kubectl get service
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
chiyi-nginx   NodePort    10.101.104.218   <none>        80:30002/TCP   6m3s
kubernetes    ClusterIP   10.96.0.1        <none>        443/TCP        23h
[root@k8s-master service]# curl 10.101.104.218
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>

查看端口

?
1
ss -antp |grep 30002
?
1
2
[root@k8s-master service]# ss -antp |grep 30002
LISTEN     0      128          *:30002                    *:*                   users:((“kube-proxy”,pid=3544,fd=13))

导出 yaml

?
1
kubectl  get service chiyi-nginx -o yaml

筛选 service 关联 pod

?
1
kubectl get pods -l app=chiyi-nginx
?
1
2
3
4
5
[root@k8s-master service]# kubectl get pods -l app=chiyi-nginx
NAME                           READY   STATUS    RESTARTS   AGE
chiyi-nginx-5bbf8bff4b-6bwfz   1/1     Running   0          3m58s
chiyi-nginx-5bbf8bff4b-bpvvc   1/1     Running   0          3m58s
chiyi-nginx-5bbf8bff4b-pwwt4   1/1     Running   0          3m58s

扩容测试

?
1
2
kubectl scale deployment chiyi-nginx –replicas=1
kubectl  get service,pods,ep

Service 三种常用类型

ClusterIP 集群内部使用,任一节点服务器和 pod 内部都可以访问 NodePort 对外暴露应用(端口默认范围:30000-32767),任一节点服务器公网IP+端口号,可在浏览器访问。 LoadBalancer 对外暴露应用,适合公有云

到此这篇关于k8s 使用 Service 控制器对外暴露服务的文章就介绍到这了,更多相关k8s对外暴露服务内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/smile_tianya/article/details/123775454

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:详解Docker Compose配置文件参数

2023-3-8 18:07:22

建站知识

小知识:Docker镜像与容器的导入导出操作实践

2023-3-8 18:21:54

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索