Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Several access modes of K8s Cluster pod

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

K8s can access pod and service in the following ways

HostNetwork

HostPort

NodePort

LoadBalancer

Ingress

HostNetwork host network mode, which is equivalent to docker run-net=host

Sample demonstration

ApiVersion: v1kind: Podmetadata: name: nginx-hostspec: hostNetwork: true containers:-name: nginx-host image: nginx

Kubectl create-f hostNetwork.yaml

Check that the ip of the pod is the ip of the node node, and select this network mode for pod scheduling. The pod will change according to the node's choice of node ip, and the port port needs to be kept free from conflicts with the port port on the host.

Hostport mode

HostPort directly routes the port of the container to the port on the scheduled node, so that the user can access the Pod by adding the IP of the host.

Sample demonstration

ApiVersion: v1kind: Podmetadata: name: nginx-portspec: hostNetwork: true containers:-name: nginx-host image: nginx ports:-containerPort: 80 hostPort: 80

Kubectl create-f hostPort.yaml

Curl 192.168.222.250:80/index.html

When the pod is scheduled for different nodes, the ip will change, and the corresponding relationship between the pod and the host needs to be maintained.

NodePort mode

NodePort mode k8s service uses cluster IP mode by default, so service produces a Cluster IP mode. This cluster ip can only be accessed within the cluster by default. If you want to access service directly outside the cluster, you need to change the type of service type to nodePort mode, nodePort value, range is 30000-32767.

Examples are as follows

ApiVersion: v1kind: Podmetadata: name: nginx labels: name: nginxspec:-name: nginx image: nginx ports:-containerPort: 80---kind: ServiceapiVersion: v1metadata: name: nginxspec: type: NodePort ports:-port: 80 nodePort: 30008 # nodePort value, range is 30000-32767 selector: name: nginx

Kubectl apply-f Node_pod.yaml

Three ways to access pod

Intra-cluster access

Pod ip: curl 10.244.1.245

ClusterIP: curl 10.1.250.193

External access to the cluster

NodePort: master:

Http://192.168.222.240:30008 # using NodePort mode will open a new port on master and node nodes. Using multiple NodePort modes needs to maintain the correspondence of ports to prevent port conflicts.

LoadBalancer needs the support of load balancer, which is generally provided by cloud service providers.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report