K8s pod cluster of several access mode


k8s access pod and service are the following ways


host network

hostPort

NodePort

LoadBalancer

Ingress


hostNetwork host network mode corresponds docker run --net = host 


Example shows 

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host
spec:
  hostNetwork: true
  containers:
    - name: nginx-host
      image: nginx

 kubectl create -f hostNetwork.yaml

image.png


See the pod is ip ip Node node, select such a network scheduling mode pod, pod vary depending on the selected nodes Node ip, port port, with the port needs to remain on the host port conflict.


hostport mode 


hostPort port is directly routed to a port on the container scheduled node, so that users can add Pod access by the host IP


Example shows

apiVersion: v1
kind: Pod
metadata:
  name: nginx-port
spec:
  hostNetwork: true
  containers:
    - name: nginx-host
      image: nginx
      ports: 
        - containerPort: 80
          hostPort: 80


kubectl create -f hostPort.yaml


image.png

image.png


curl 192.168.222.250:80/index.html


pod different nodes are scheduled, ip will change, you need to maintain a correspondence between the pod and the host


NodePort mode


NodePort default mode k8s service using cluster IP way, this service will produce a Cluster IP model, the cluster ip default can only be accessed within the cluster, you want direct access to the service outside, the type of service type needs to be modified to nodePort mode, nodePort value, the range 30000-32767


Examples are as follows

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    name: nginx
spec:
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: nginx
spec:
  type: NodePort
  ports: 
    - port: 80
      nodePort: 30008  #  nodePort值,范围是30000-32767
  selector:
    name: nginx


kubectl apply  -f Node_pod.yaml


image.png



image.png


Three ways to access the pod


Cluster internal access


pod ip  :curl 10.244.1.245


ClusterIP :curl 10.1.250.193


External Access cluster


NodePort: master:

http://192.168.222.240:30008  # NodePort mode will use to open a port on the master node and nodes, using multiple modes NodePort need to maintain a good correspondence between the port, the port to prevent conflict


image.png


LoadBalancer required to support the load balancer, the general cloud service providers have provided










Guess you like

Origin blog.51cto.com/sdsca/2422193