Kubernetes Services¶
Overview¶
This guide covers Kubernetes services, including different service types, networking configurations, and load balancing strategies.
Prerequisites¶
- Basic understanding of Kubernetes concepts
- Knowledge of networking basics
- Familiarity with load balancing concepts
- Understanding of DNS and service discovery
Learning Objectives¶
- Understand service types
- Learn service configuration
- Master service networking
- Implement load balancing
- Configure service discovery
Table of Contents¶
Service Types¶
ClusterIP Service¶
apiVersion: v1
kind: Service
metadata:
name: backend-service
spec:
type: ClusterIP
selector:
app: backend
ports:
- protocol: TCP
port: 80
targetPort: 8080
NodePort Service¶
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: NodePort
selector:
app: web
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30080
LoadBalancer Service¶
apiVersion: v1
kind: Service
metadata:
name: frontend-service
spec:
type: LoadBalancer
selector:
app: frontend
ports:
- protocol: TCP
port: 80
targetPort: 8080
ExternalName Service¶
apiVersion: v1
kind: Service
metadata:
name: external-service
spec:
type: ExternalName
externalName: api.external-service.com
Service Configuration¶
Basic Service Configuration¶
apiVersion: v1
kind: Service
metadata:
name: app-service
labels:
app: myapp
spec:
selector:
app: myapp
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
Multi-Port Service¶
apiVersion: v1
kind: Service
metadata:
name: multi-port-service
spec:
selector:
app: myapp
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080
- name: https
protocol: TCP
port: 443
targetPort: 8443
Load Balancing¶
Internal Load Balancing¶
apiVersion: v1
kind: Service
metadata:
name: internal-lb
annotations:
cloud.google.com/load-balancer-type: "Internal"
spec:
type: LoadBalancer
selector:
app: internal-app
ports:
- port: 80
targetPort: 8080
External Load Balancing¶
apiVersion: v1
kind: Service
metadata:
name: external-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: nlb
spec:
type: LoadBalancer
selector:
app: external-app
ports:
- port: 80
targetPort: 8080
Service Discovery¶
DNS Configuration¶
apiVersion: v1
kind: Service
metadata:
name: backend-service
namespace: default
spec:
selector:
app: backend
ports:
- protocol: TCP
port: 80
targetPort: 8080
Headless Service¶
apiVersion: v1
kind: Service
metadata:
name: headless-service
spec:
clusterIP: None
selector:
app: stateful-app
ports:
- port: 80
targetPort: 8080
Network Policies¶
Basic Network Policy¶
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-allow
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
ports:
- protocol: TCP
port: 8080
Namespace Network Policy¶
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: namespace-policy
spec:
podSelector: {}
policyTypes:
- Ingress
ingress:
- from:
- namespaceSelector:
matchLabels:
environment: production
Best Practices¶
- Use appropriate service types
- Implement proper load balancing
- Configure health checks
- Use meaningful DNS names
- Implement network policies
- Monitor service performance
- Configure proper timeouts
Common Pitfalls¶
- Incorrect service type selection
- Poor load balancing configuration
- Missing network policies
- Inadequate monitoring
- DNS misconfiguration
- Security misconfigurations
Implementation Examples¶
Complete Service Configuration¶
apiVersion: v1
kind: Service
metadata:
name: web-service
labels:
app: web
environment: production
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '9090'
spec:
type: LoadBalancer
selector:
app: web
ports:
- name: http
protocol: TCP
port: 80
targetPort: 8080
- name: https
protocol: TCP
port: 443
targetPort: 8443
- name: metrics
protocol: TCP
port: 9090
targetPort: 9090
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
loadBalancerSourceRanges:
- 10.0.0.0/8
externalTrafficPolicy: Local
Service with Network Policy¶
apiVersion: v1
kind: Service
metadata:
name: api-service
spec:
selector:
app: api
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: api-network-policy
spec:
podSelector:
matchLabels:
app: api
policyTypes:
- Ingress
- Egress
ingress:
- from:
- podSelector:
matchLabels:
app: frontend
- namespaceSelector:
matchLabels:
environment: production
ports:
- protocol: TCP
port: 8080
egress:
- to:
- podSelector:
matchLabels:
app: database
ports:
- protocol: TCP
port: 5432
Resources for Further Learning¶
- Kubernetes Services Documentation
- Network Policies Documentation
- DNS for Services and Pods
- Service Load Balancing
Practice Exercises¶
- Create different types of services
- Implement load balancing
- Configure network policies
- Set up service discovery
- Test service connectivity