Accessing Cluster Applications via NodePort Service
When internal access to a application isn't sufficiant and external access is required, a NodePort Service can be used. This service type exposes the application using a static port on each node’s IP. From outside the cluster, the service can be accessed via <NodeIP>:<NodePort>.
Here's a sample YAML definition:
kind: Service
apiVersion: v1
metadata:
name: nodeport-service
spec:
type: NodePort
ports:
- port: 80
nodePort: 31001
selector:
app: demo
Create and verify the service with the following commands:
kubectl create -f nodePortService.yaml
kubectl get services nodeport-service
Once created, the service will be accessible externally via the node IP and port (e.g., 172.16.2.201:31001).
However, this method has limitations:
- Each port can only be used by one service.
- Port range is limited to
30000-32767. - Service becomes unreachable if the node IP changes.
These constraints make NodePort suitable for development and testing, but not ideal for production environments.
Accessing Cluster Applications via LoadBalancer Service
To overcome the limitations of NodePort, a LoadBalancer Service provides a more robust solution. It exposes the service externally using a cloud provider's load balancer, typically assigning a public IP address.
Below is an example YAML definition for a LoadBalancer Service:
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: default
spec:
clusterIP: 10.3.255.28
loadBalancerIP: 106.52.99.55
ports:
- name: tcp-80-80
nodePort: 31504
port: 80
protocol: TCP
targetPort: 80
selector:
app: demo
k8s-app: demo
qcloud-app: demo
type: LoadBalancer
After deploying this service, external access can be achieved via:
- Node IP and port
- LoadBalancer IP
- Domain name (configured with DNS pointing to the LoadBalancer IP)
Deploying Microsoft SQL Server in Kubernetes
This section demonstrates deploying Microsoft SQL Server on Linux using Kubernetes objects. The deployment goals are:
- Deploy SQL Server 2017 on Linux.
- Persist data to
/var/mssqlon the host. - Set the SA password to
123456abcD. - Expose port 1433 using NodePort
30338.
The YAML definition includes a Deployment and a Service:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mssql
labels:
app: mssql
spec:
replicas: 1
selector:
matchLabels:
app: mssql
template:
metadata:
labels:
app: mssql
spec:
containers:
- name: mssql
image: mcr.microsoft.com/mssql/server:2017-latest-ubuntu
imagePullPolicy: Always
ports:
- containerPort: 1433
env:
- name: ACCEPT_EULA
value: "Y"
- name: SA_PASSWORD
value: "123456abcD"
resources:
limits:
cpu: "2"
memory: 2096Mi
requests:
cpu: 100m
memory: 827Mi
volumeMounts:
- name: data-vol
mountPath: /var/opt/mssql/
volumes:
- name: data-vol
hostPath:
path: /var/mssql
---
apiVersion: v1
kind: Service
metadata:
name: mssql
spec:
ports:
- name: tcp-1433-1433
port: 1433
targetPort: 1433
nodePort: 30338
protocol: TCP
selector:
app: mssql
type: NodePort
Apply the configuration using:
kubectl apply -f mssqlserver.yaml
Check the deployment status with:
kubectl get svc -o wide -l app=mssql
kubectl get po -o wide -l app=mssql
kubectl get deployment -o wide -l app=mssql
Once the deployment is ready, you can connect to the SQL Server instance using a database management tool.