CKA认证

【K8s 精选】CKA - ClusterIP、NodePort

2022-03-02  本文已影响0人  熊本极客

1.ClusterIP 的使用

说明ClusterIP 通过集群的内部 IP 暴露服务,即只能在集群内部访问,也是默认的 ServiceType

my-service.yaml 配置文件的内容如下,通过 kubectl 命令创建,kubectl apply -f my-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
# 创建 clusterIP
$kubectl apply -f my-service.yaml
service/my-service-clusterip created

# 查看 clusterIP
$kubectl get service |grep my
my-service ClusterIP   10.96.0.108   <none>        80/TCP         9s
$kubectl describe service my-service
Name:              my-service-clusterip
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=MyApp
Type:              ClusterIP
IP Families:       <none>
IP:                10.96.0.108
IPs:               10.96.0.108
Port:              <unset>  80/TCP
TargetPort:        9376/TCP
Endpoints:         10.244.2.240:80,10.244.3.14:80,10.244.3.47:80
Session Affinity:  None
Events:            <none>

2.NodePort 的使用

说明:通过每个节点上的 IP 和静态端口(NodePort)暴露服务。 NodePort 服务会路由到自动创建的 ClusterIP 服务。 通过请求 <节点 IP>:<nodeport>,可以从集群的外部访问一个 NodePort 服务

nodeport-my-service.yaml 配置文件的内容如下,通过 kubectl 命令创建,kubectl apply -f nodeport-my-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nodeport-my-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
      # 默认情况下,为了方便起见,`targetPort` 被设置为与 `port` 字段相同的值。
    - port: 80
      targetPort: 80
      # 可选字段
      # 默认情况下,为了方便起见,Kubernetes 控制平面会从某个范围内分配一个端口号(默认:30000-32767)
      nodePort: 30007
# 创建 nodeport
$kubectl apply -f nodeport-my-service.yaml
service/my-service created

# 查看 nodeport
$kubectl get service |grep my
nodeport-my-service   NodePort    10.96.0.19   <none>        80:30007/TCP   17s
$kubectl describe service nodeport-my-service
Name:                     nodeport-my-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=MyApp
Type:                     NodePort
IP Families:              <none>
IP:                       10.96.0.19
IPs:                      10.96.0.19
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30007/TCP
# Endpoints 表示后端微服务的 Pod-IP:port
Endpoints:                10.244.2.240:80,10.244.3.14:80,10.244.3.47:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

3.LoadBalancer 的使用

说明:使用云提供商的负载均衡器向外部暴露服务。 外部负载均衡器可以将流量路由到自动创建的 NodePort 服务和 ClusterIP 服务上。

loadbalancer-my-service.yaml 配置文件的内容如下,设置 type 的值为 LoadBalancer,将为 Service 提供负载均衡器。负载均衡器是异步创建的,关于被提供的负载均衡器的信息将会通过 Servicestatus.loadBalancer 字段发布出去。通过 kubectl 命令创建,kubectl apply -f loadbalancer-my-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: loadbalancer-my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
      - ip: 192.0.2.127
# 创建 loadbalancer
$kubectl apply -f loadbalancer-my-service.yaml
service/loadbalancer-my-service created

# 查看 loadbalancer
$kubectl describe service loadbalancer-my-service
Name:                     loadbalancer-my-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=MyApp
Type:                     LoadBalancer
IP Families:              <none>
IP:                       10.96.0.49
IPs:                      10.96.0.49
Port:                     <unset>  80/TCP
TargetPort:               9376/TCP
NodePort:                 <unset>  31549/TCP
Endpoints:                10.244.2.240:80,10.244.3.14:80,10.244.3.47:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

上一篇下一篇

猜你喜欢

热点阅读