How do I expose Kubernetes services running on my Amazon EKS cluster?

Last updated: 2020-01-20

I want to expose Kubernetes services running on my Amazon Elastic Kubernetes Service (Amazon EKS) cluster.

Short Description

The following steps show you how to create a sample application, and then apply the following Kubernetes ServiceTypes to your sample application: ClusterIP, NodePort, and LoadBalancer.

Keep in mind the following:

  • ClusterIP exposes the service on a cluster-internal IP address.
  • NodePort exposes the service on each node’s IP address at a static port.
  • LoadBalancer exposes the service externally using a load balancer.

Note: Amazon EKS supports the Network Load Balancer and the Classic Load Balancer for pods running on Amazon Elastic Compute Cloud (Amazon EC2) instance worker nodes through LoadBalancer. Amazon EKS doesn't support the Network Load Balancer and Classic Load Balancer for pods running on AWS Fargate. For Fargate ingress, it's a best practice to use the ALB Ingress Controller on Amazon EKS (minimum version 1.1.4).

Resolution

Create a sample application

1.    To create a sample NGINX deployment, run the following command:

kubectl run nginx  --replicas=2 --labels='app=nginx' --image=nginx --port=80

2.    To verify that your pods are running and have their own internal IP addresses, run the following command:

kubectl get pods -l 'app=nginx' -o wide | awk {'print $1" " $3 " " $6'} | column -t

The output should be similar to the following:

NAME                   STATUS   IP
nginx-cf4fddc59-92krx  Running  10.1.3.202
nginx-cf4fddc59-v2s6h  Running  10.1.3.150

Create a ClusterIP service

1.    Create a file called clusterip.yaml, and then set type to ClusterIP. See the following example:

cat <<EOF > clusterip.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: ClusterIP
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
EOF

2.    Create the ClusterIP object in Kubernetes using either a declarative or imperative command.

To create the object and apply the clusterip.yaml file, run the following declarative command:

kubectl create -f clusterip.yaml

You receive the following output:

service/nginx-service created

--or--

To expose a deployment of ClusterIP type, run the following imperative command:

kubectl expose deployment nginx  --type=ClusterIP  --name=nginx-service

You receive the following output:

service "nginx-service" exposed

Note: The exposed command creates a service without creating a YAML file. However, kubectl translates your imperative command into a declarative Kubernetes Deployment object.

3.    To access the application and get the ClusterIP number, run the following command:

kubectl get service nginx-service

You receive the following output:

NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
nginx-service   ClusterIP   172.20.65.130   <none>        80/TCP    15s

Create a NodePort service

1.    To create a NodePort service, create a file called nodeport.yaml, and then set type to NodePort. See the following example:

cat <<EOF > nodeport.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
EOF

2.    To delete the ClusterIP service and create a NodePort service with the same service name (nginx-service), run the following command:

kubectl delete service nginx-service

You receive the following output:

service "nginx-service" deleted

3.    Create the NodePort object in Kubernetes using either a declarative or imperative command.

To create the object and apply the nodeport.yaml file, run the following declarative command:

kubectl create -f nodeport.yaml

--or--

To expose a deployment of NodePort type, run the following imperative command:

kubectl expose deployment nginx  --type=NodePort  --name=nginx-serviceservice "nginx-service" exposed

You receive the following output:

service/nginx-service created

4.    To get information about nginx-service, run the following command:

kubectl get service/nginx-service

You receive the following output:

NAME            TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-service   NodePort   172.20.36.247   <none>        80:30648/TCP   11s

Note: The ServiceType is NodePort. ClusterIP is created automatically and takes the route from the NodePort. The NodePort service is exposed externally on the available worker nodes on port 30648.

5.    To check the node’s public IP address, run the following command:

kubectl get nodes -o wide |  awk {'print $1" " $2 " " $7'} | column -t

You receive the following output:

NAME                                      STATUS  EXTERNAL-IP
ip-10-0-3-226.eu-west-1.compute.internal  Ready   1.1.1.1
ip-10-1-3-107.eu-west-1.compute.internal  Ready   2.2.2.2

Note: Before you access NodeIP:NodePort from an outside cluster, you must enable the security group of the nodes to allow incoming traffic through port 30648.

Create a LoadBalancer service

1.    To create a LoadBalancer service, create a file called loadbalancer.yaml, and then set type to LoadBalancer. See the following example:

cat <<EOF > loadbalancer.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
EOF

2.    To delete the ClusterIP service and create a LoadBalancer service with the same service name (nginx-service), run the following command:

kubectl delete service nginx-service

You receive the following output:

service "nginx-service" deleted

3.    To apply the loadbalancer.yaml file, run the following command:

kubectl create -f loadbalancer.yaml

You receive the following output:

service/nginx-service created

--or--

To expose a deployment of LoadBalancer type, run the following imperative command:

kubectl expose deployment nginx  --type=LoadBalancer  --name=nginx-service

You receive the following output:

service "nginx-service" exposed

4.    To get information about nginx-service, run the following command:

kubectl get service/nginx-service |  awk {'print $1" " $2 " " $4 " " $5'} | column -t

You receive the following output:

NAME           TYPE          EXTERNAL-IP                        PORT(S)
nginx-service  LoadBalancer  *****.eu-west-1.elb.amazonaws.com  80:30039/TCP

5.    To verify that you can access the load balancer externally, run the following command:

curl -silent *****.us-west-2.elb.amazonaws.com:80 | grep title

You should receive the following output: <title>Welcome to nginx!</title>


Did this article help you?

Anything we could improve?


Need more help?