skip to content
Astro Cactus

Kubernetes Services and Ingress: Managing Network Access

/ 3 min read

Understanding Services and Ingress

Services provide a stable endpoint for accessing pods, while Ingress manages external access to services in a cluster.

Services

Service Types

  1. ClusterIP (default)
  2. NodePort
  3. LoadBalancer
  4. ExternalName

Basic Service Configuration

apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: MyApp
ports:
- protocol: TCP
port: 80
targetPort: 9376

Service Types Examples

1. ClusterIP Service

apiVersion: v1
kind: Service
metadata:
name: backend
spec:
type: ClusterIP
selector:
app: backend
ports:
- protocol: TCP
port: 80
targetPort: 8080

2. NodePort Service

apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 8080
nodePort: 30080

3. LoadBalancer Service

apiVersion: v1
kind: Service
metadata:
name: my-loadbalancer
spec:
type: LoadBalancer
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080

4. ExternalName Service

apiVersion: v1
kind: Service
metadata:
name: my-external-service
spec:
type: ExternalName
externalName: api.example.com

Ingress

Basic Ingress Configuration

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: minimal-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- http:
paths:
- path: /testpath
pathType: Prefix
backend:
service:
name: test
port:
number: 80

TLS Configuration

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: tls-example-ingress
spec:
tls:
- hosts:
- https-example.foo.com
secretName: testsecret-tls
rules:
- host: https-example.foo.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: service1
port:
number: 80

Advanced Configurations

1. Multi-Service Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: multi-service-ingress
spec:
rules:
- host: foo.bar.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80
- path: /web
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80

2. Session Affinity

apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
selector:
app: webapp
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
ports:
- protocol: TCP
port: 80
targetPort: 8080

3. External Traffic Policy

apiVersion: v1
kind: Service
metadata:
name: external-service
spec:
type: LoadBalancer
externalTrafficPolicy: Local
ports:
- port: 80
targetPort: 8080

Real-World Examples

1. Microservices Architecture

apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
selector:
app: frontend
ports:
- port: 80
targetPort: 3000
---
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
selector:
app: backend
ports:
- port: 80
targetPort: 8080
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: microservices-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: example.com
http:
paths:
- path: /api/(.*)
pathType: Prefix
backend:
service:
name: backend-service
port:
number: 80
- path: /(.*)
pathType: Prefix
backend:
service:
name: frontend-service
port:
number: 80

2. Load Balancing Configuration

apiVersion: v1
kind: Service
metadata:
name: web-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: nlb
service.beta.kubernetes.io/aws-load-balancer-cross-zone-load-balancing-enabled: "true"
spec:
type: LoadBalancer
selector:
app: web
ports:
- port: 80
targetPort: 8080

Ingress Controllers

1. NGINX Ingress Controller

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
nginx.ingress.kubernetes.io/use-regex: "true"
nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
ingressClassName: nginx
rules:
- host: example.com
http:
paths:
- path: /api(/|$)(.*)
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80

2. Traefik Ingress Controller

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: traefik-ingress
annotations:
traefik.ingress.kubernetes.io/router.middlewares: default-strip-prefix@kubernetescrd
spec:
rules:
- host: example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service:
name: api-service
port:
number: 80

Best Practices

1. Service Design

  • Use meaningful names
  • Implement proper selectors
  • Configure appropriate ports
  • Consider session affinity

2. Ingress Configuration

  • Use proper annotations
  • Implement TLS
  • Configure path types
  • Use meaningful hosts

3. Security

  • Implement TLS
  • Use network policies
  • Configure authentication
  • Restrict access

Series Navigation

Troubleshooting

Common issues and solutions:

  1. Service Discovery Issues

    • Check selectors
    • Verify port configurations
    • Review endpoints
    • Check DNS resolution
  2. Ingress Problems

    • Verify controller setup
    • Check annotations
    • Review TLS configuration
    • Check backend services

Monitoring and Maintenance

1. Service Monitoring

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: example-service
spec:
selector:
matchLabels:
app: example
endpoints:
- port: web

2. Health Checks

apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
ports:
- port: 80
targetPort: 8080
selector:
app: web
---
apiVersion: v1
kind: Pod
metadata:
name: web-pod
labels:
app: web
spec:
containers:
- name: web
image: nginx
ports:
- containerPort: 8080
livenessProbe:
httpGet:
path: /healthz
port: 8080
initialDelaySeconds: 3
periodSeconds: 3

Conclusion

Services and Ingress are fundamental to networking in Kubernetes. Understanding their configuration and best practices is essential for building robust and accessible applications.