外部网络访问K8S的POD服务端口简单实现
1、通过访问POD所在主机的端口连接POD提供的服务使用了hostPort的方式[root@v75 yml]# cat hostpath.yml apiVersion: v1kind: Podmetadata: name: test-pdspec: containers: - image: nginx name: test-container ports...
1、通过访问POD所在主机的端口连接POD提供的服务
使用了hostPort的方式
[root@v75 yml]# cat hostpath.yml
apiVersion: v1
kind: Pod
metadata:
name: test-pd
spec:
containers:
- image: nginx
name: test-container
ports:
- containerPort: 80
hostPort: 8081
volumeMounts:
- mountPath: /test-pd
name: test-volume
volumes:
- name: test-volume
hostPath:
path: /75shell/yml
[root@v75 yml]# kubectl create -f hostpath.yml
pod/test-pd created
[root@v75 yml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-8lrhs 1/1 Running 0 32m
redis-slave-w5qzn 0/1 Terminating 0 7d23h
test-pd 0/1 ContainerCreating 0 5s
[root@v75 yml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-8lrhs 1/1 Running 0 38m
redis-slave-w5qzn 0/1 Terminating 0 7d23h
test-pd 1/1 Running 0 5m33s
[root@v75 yml]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-8lrhs 1/1 Running 0 38m 172.30.94.2 v75 <none> <none>
redis-slave-w5qzn 0/1 Terminating 0 7d23h <none> v77 <none> <none>
test-pd 1/1 Running 0 6m17s 172.30.91.2 v76 <none> <none>
此时通过访问POD所在NODE的IP和映射的端口就能访问POD的服务了
2、通过访问K8S任意节点的8400-9000端口连接POD提供的服务
使用NodePort的方式,并且需要定义service
[root@v75 yml]# cat nginxpod.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
[root@v75 yml]# cat rc1service.yml
kind: Service
apiVersion: v1
metadata:
name: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 8899
selector:
name: nginx
注意:NODE提供访问的端口必须在8400-9000之间,否则报错
[root@v76 yml]# kubectl create -f nginxpod.yml
pod/nginx created
[root@v76 yml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 6s
redis-slave-w5qzn 0/1 Terminating 0 8d
[root@v76 yml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2m53s
redis-slave-w5qzn 0/1 Terminating 0 8d
[root@v76 yml]# kubectl create -f rc1service.yml
service/nginx created
[root@v76 yml]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx 1/1 Running 0 3m13s
pod/redis-slave-w5qzn 0/1 Terminating 0 8d
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 4d5h
service/nginx NodePort 10.254.230.172 <none> 80:8899/TCP 7s
[root@v76 yml]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 19m 172.30.94.2 v75 <none> <none>
redis-slave-w5qzn 0/1 Terminating 0 8d <none> v77 <none> <none>
此时POD虽然只是在v75上面跑,但是访问任意NODE的IP和端口,都能打开nginx的服务
更多推荐
所有评论(0)