NodePort

NodePort示意图
NodePort示意图

创建 myapp-deploy.yaml 文件

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
[root@master manifests]# vim myapp-nodeport-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-nodeport-deploy
namespace: default
spec:
replicas: 3
selector:
  matchLabels:
    app: myapp
    release: stabel
    svc: nodeport
template:
  metadata:
    labels:
      app: myapp
      release: stabel
      env: test
      svc: nodeport
  spec:
    containers:
    - name: myapp-container
      image: wangyanglinux/myapp:v1.0
      imagePullPolicy: IfNotPresent
      ports:
      - name: http
        containerPort: 80

创建 Service 信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@master manifests]# vim myapp-nodeport-service.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-nodeport
namespace: default
spec:
type: NodePort
selector:
  app: myapp
  release: stabel
  svc: nodeport
ports:
- name: http
  port: 80
  targetPort: 80
  nodePort: 30010

服务外部流量策略

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
[root@k8s-master01 ~]# kubectl explain svc.spec.externalTrafficPolicy
KIND: Service
VERSION: v1

FIELD: externalTrafficPolicy <string>
ENUM:
Cluster
Local

DESCRIPTION:
externalTrafficPolicy describes how nodes distribute service traffic they
receive on one of the Service's "externally-facing" addresses (NodePorts,
ExternalIPs, and LoadBalancer IPs). If set to "Local", the proxy will
configure the service in a way that assumes that external load balancers
will take care of balancing the service traffic between nodes, and so each
node will deliver traffic only to the node-local endpoints of the service,
without masquerading the client source IP. (Traffic mistakenly sent to a
node with no endpoints will be dropped.) The default value, "Cluster", uses
the standard behavior of routing to all endpoints evenly (possibly modified
by topology and other features). Note that traffic sent to an External IP or
LoadBalancer IP from within the cluster will always get "Cluster" semantics,
but clients sending to a NodePort from within the cluster may need to take
traffic policy into account when picking a node.

Possible enum values:
- `"Cluster"` routes traffic to all endpoints.
- `"Local"` preserves the source IP of the traffic by routing only to
endpoints on the same node as the traffic was received on (dropping the
traffic if there are no local endpoints).