简体   繁体   中英

access azure kubernetes cluster using a NodePort

I'm running a pod (website) and a simple service

apiVersion: v1
kind: Service
metadata:
  name: ui
spec:
  type: NodePort
  selector:
    app: ui
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000

$> kubectl get services
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE   SELECTOR   LABELS
kubernetes   ClusterIP   10.0.0.1      <none>        443/TCP        83m   <none>     component=apiserver,provider=kubernetes
ui           NodePort    10.0.25.205   <none>        80:30180/TCP   53m   app=ui     <none>

Because this service is of type NodePort it opens a port on each cluster node. In my case I'm running kubernetes in Azure, single node setup. But how do I access my service/website?

$> kubectl describe service ui
Name:                     ui
Namespace:                default
Labels:                   <none>
Annotations:              kubectl.kubernetes.io/last-applied-configuration:
                            {"apiVersion":"v1","kind":"Service","metadata": {"annotations":{},"name":"ui","namespace":"default"},"spec":{"ports":[{"port":80,"protocol"...
Selector:                 app=ui
Type:                     NodePort
IP:                       10.0.25.205
Port:                     <unset>  80/TCP
TargetPort:               3000/TCP
NodePort:                 <unset>  30180/TCP
Endpoints:                10.244.0.14:3000,10.244.0.15:3000
Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  Type                  29m   service-controller  NodePort -> LoadBalancer
  Normal  EnsuringLoadBalancer  29m   service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   27m   service-controller  Ensured load balancer
  Normal  Type                  10m   service-controller  LoadBalancer -> NodePort
  Normal  DeletingLoadBalancer  10m   service-controller  Deleting load balancer
  Normal  DeletedLoadBalancer   9m5s  service-controller  Deleted load balancer

I don't see an external IP.

For example, if I change NodePort to LoadBalancer I get an external IP and I can access my website, but how can I do this with NodePort?

As far as I know, the AKS is a managed service and it just exposes the master which is also managed by Azure to control all the actions. The slave nodes do not expose and do not have the external IP in default.

In the AKS cluster, you only can access the applications through the service with a load balancer or the ingress(which also uses the load balancer for its service).

If you really want to use the node type for your service, there is also a way to solve it. You can create public IPs manually and associate them to the nodes that you want to create the services with node type. Then the nodes have the external IPs. But all operations for AKS Iaas are not recommended. So the load balancer type is the most appropriate way for the service if you want to access them from the Internet.

You can get the IP of you nodes by querying the nodes from the kubernetes api:

kubectl get nodes -o wide

It will print the IP of each node. Since a NodePort is exposed on all nodes, you can use any node to access the service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM