fx
fx copied to clipboard
Service Type
The type
property in the Service's spec determines how the service is exposed to the network. It changes where a Service is able to be accessed from. The possible types are ClusterIP, NodePort, and LoadBalancer
- ClusterIP, The default value. The service is only accessible from within the Kubernetes cluster – you can’t make requests to your Pods from outside the cluster!
- NodePort, This makes the service accessible on a static port on each Node in the cluster. This means that the service can handle requests that originate from outside the cluster.
- LoadBalancer, The service becomes accessible externally through a cloud provider's load balancer functionality. GCP, AWS, Azure, and OpenStack offer this functionality. The cloud provider will create a load balancer, which then automatically routes requests to your Kubernetes Service
- ExternalName, it maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up.
NodePort usage
For example, you have a running pod test-fx-pod, and expose to service test-fx-pod-svc with NodePort type,
$
kubectl expose deployment test-fx-pod --name=local_access --port=3000 --target-port=3000 node-port=30001 --type=NodePort
Then you can access the pod with following different ways,
- Access pod with pod's IP
$ kubectl describe pod test-fx-pod
Name: test-fx-pod
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: fx-test-control-plane/172.17.0.3
Start Time: Sat, 28 Sep 2019 18:27:28 +0800
Labels: fx-app=fx-app
Annotations: <none>
Status: Running
IP: 10.244.0.12
Containers:
fx-placeholder-container-name:
Container ID: containerd://04d235365c1fdf2fce22a2ab8a327a207bbda2782d1fab08b3035d9aed32ccdd
Image: metrue/kube-hello
Image ID: docker.io/metrue/kube-hello@sha256:514e0802941c6608b1d2efe4d676e5edff43871544211b95b234e18a9f921ef7
Port: 3000/TCP
Host Port: 3000/TCP
State: Running
Started: Sat, 28 Sep 2019 18:27:31 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xwg5b (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xwg5b:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xwg5b
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
Then you can access it with,
$ kubectl exec -it test-fx-pod curl 10.244.0.12:3000
or
kubectl exec -it test-fx-pod curl 127.0.0.1:3000
- Access it with serivce IP
$ kubectl describe svc test-fx-pod-svc
Name: test-fx-pod-svc
Namespace: default
Labels: <none>
Annotations: <none>
Selector: fx-app=fx-app
Type: NodePort
IP: 10.104.28.208
Port: fx-function-as-an-api 3000/TCP
TargetPort: 3000/TCP
NodePort: fx-function-as-an-api 30001/TCP
Endpoints: 10.244.0.12:3000
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
then,
$ kubectl exec -it test-fx-pod curl 10.244.0.12:3000
- Access it with cluster IP
$ kubectl get svc test-fx-pod-svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
test-fx-pod-svc NodePort 10.104.28.208 <none> 3000:30001/TCP 15h
then you can,
kubectl exec -it test-fx-pod curl 10.104.28.208:3000
But how to access the pod's functionality with kubectl exec
, like cURL
for instance?
The reason discussed here,
https://github.com/kubernetes-sigs/kind/issues/99