首页 技术 正文
技术 2022年11月14日
0 收藏 607 点赞 3,430 浏览 2262 个字

一集群外部访问

由于Pod和Service都是Kubernetes集群范围内的虚拟概念,所以集群外的客户端默认情况,无法通过Pod的IP地址或者Service的虚拟IP地址:虚拟端口号进行访问。通常可以通过以下方式进行访问Kubernetes集群内的服务。

1.1外部访问——映射Pod到物理机

为了让外部客户端可以访问这些服务,可以将Pod或Service的端口号映射到宿主机,以使客户端应用能够通过物理机访问容器应用。示例1:[root@k8smaster01 study]# vi pod-hostport.yaml

  1 apiVersion: v1
2 kind: Pod
3 metadata:
4 name: webapp
5 labels:
6 app: webapp
7 spec:
8 containers:
9 - name: webapp
10 image: tomcat
11 ports:
12 - containerPort: 8080
13 hostPort: 8081

[root@k8smaster01 study]# kubectl create -f pod-hostport.yaml [root@k8smaster01 study]# kubectl get pods -l=”app=webapp” NAME READY STATUS RESTARTS AGEwebapp 1/1 Running 0 2m1[root@k8smaster01 study]# curl 172.24.8.71:8081示例2:[root@k8smaster01 study]# cat pod-hostnetwork.yaml

  1 apiVersion: v1
2 kind: Pod
3 metadata:
4 name: webapp2
5 labels:
6 app: webapp2
7 spec:
8 hostNetwork: true
9 containers:
10 - name: webapp2
11 image: tomcat
12 ports:
13 - containerPort: 8080
14 hostPort: 8080

提示:通过设置Pod级别的hostNetwork=true,该Pod中所有容器的端口号都将被直接映射到物理机上。在设置hostNetwork=true时需要注意,在容器的ports定义部分如果不指定hostPort,则默认hostPort等于containerPort,如果指定了hostPort,则hostPort必须等于containerPort的值。[root@k8smaster01 study]# curl 172.24.8.73:8080

1.2外部访问——映射Service到物理机

示例1:[root@k8smaster01 study]# vi webappsvc.yaml

  1 apiVersion: v1
2 kind: Service
3 metadata:
4 name: webapp
5 spec:
6 type: NodePort
7 ports:
8 - port: 8080
9 targetPort: 8080
10 nodePort: 8081
11 selector:
12 app: webapp

提示:为了k8s和其他程序不起冲突,默认端口的范围是:30000-32767,因此默认情况以上yaml可能出现如下告警,可通过如下方式修改:provided port is not in the valid range. The range of valid ports is 30000-32767#告警


[root@k8smaster01 ~]# vi /etc/systemd/system/kube-apiserver.service#在所有master上修改

  1 ExecStart=/opt/k8s/bin/kube-apiserver \
2 ……
3 --service-node-port-range=1-65535 \#添加此参数
4 ……

[root@k8smaster01 ~]# systemctl daemon-reload


[root@k8smaster01 ~]# systemctl restart kube-apiserver.service
[root@k8smaster01 study]# kubectl create -f webappsvc.yaml[root@k8smaster01 study]# curl 172.24.8.71:8081通过设置LoadBalancer映射到云服务商提供的LoadBalancer地址。这种用法仅用于在公有云服务提供商的云平台上设置Service的场景。示例2:如下所示status.loadBalancer.ingress.ip设置的146.148.47.155为第三方提供的负载均衡器的IP地址。对该Service的访问请求将会通过LoadBalancer转发到后端Pod上,负载分发的实现方式则依赖于第三方提供的LoadBalancer的实现机制。[root@k8smaster01 study]# vi myoutsvc.yaml

  1 apiVersion: v1
2 kind: Service
3 metadata:
4 name: my-service
5 spec:
6 type: LoadBalancer
7 selector:
8 app: MyApp
9 ports:
10 - protocol: TCP
11 port: 80
12 targetPort: 9376
13 nodePort: 30061
14 clusterIP: 10.0.171.239
15 status:
16 loadBalancer:
17 ingress:
18 - ip: 47.96.145.131

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,111
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,584
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,431
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922