Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Deploy and manage Kubernetes workloads: manifests, RBAC, Helm charts, service mesh, GitOps, and troubleshooting.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/networking.md
1# Kubernetes Networking23## Service Types45### ClusterIP (Default)67```yaml8apiVersion: v19kind: Service10metadata:11name: web-app-service12namespace: production13labels:14app: web-app15spec:16type: ClusterIP17selector:18app: web-app19tier: frontend20ports:21- name: http22port: 8023targetPort: 808024protocol: TCP25- name: metrics26port: 909027targetPort: metrics28protocol: TCP29sessionAffinity: ClientIP30sessionAffinityConfig:31clientIP:32timeoutSeconds: 360033```3435### Headless Service (StatefulSet)3637```yaml38apiVersion: v139kind: Service40metadata:41name: postgres-headless42namespace: database43spec:44clusterIP: None # Headless45selector:46app: postgres47ports:48- name: postgres49port: 543250targetPort: 543251```5253### NodePort5455```yaml56apiVersion: v157kind: Service58metadata:59name: external-app60namespace: production61spec:62type: NodePort63selector:64app: external-app65ports:66- name: http67port: 8068targetPort: 808069nodePort: 30080 # Range: 30000-3276770protocol: TCP71```7273### LoadBalancer7475```yaml76apiVersion: v177kind: Service78metadata:79name: public-web80namespace: production81annotations:82service.beta.kubernetes.io/aws-load-balancer-type: "nlb"83service.beta.kubernetes.io/aws-load-balancer-internal: "false"84spec:85type: LoadBalancer86selector:87app: web-app88ports:89- name: http90port: 8091targetPort: 808092- name: https93port: 44394targetPort: 844395loadBalancerSourceRanges:96- 203.0.113.0/24 # Restrict source IPs97```9899## Ingress Resources100101### NGINX Ingress102103```yaml104apiVersion: networking.k8s.io/v1105kind: Ingress106metadata:107name: web-ingress108namespace: production109annotations:110nginx.ingress.kubernetes.io/rewrite-target: /111nginx.ingress.kubernetes.io/ssl-redirect: "true"112nginx.ingress.kubernetes.io/force-ssl-redirect: "true"113nginx.ingress.kubernetes.io/proxy-body-size: "10m"114nginx.ingress.kubernetes.io/rate-limit: "100"115cert-manager.io/cluster-issuer: "letsencrypt-prod"116spec:117ingressClassName: nginx118tls:119- hosts:120- www.example.com121- api.example.com122secretName: example-tls123rules:124- host: www.example.com125http:126paths:127- path: /128pathType: Prefix129backend:130service:131name: frontend-service132port:133number: 80134- host: api.example.com135http:136paths:137- path: /v1138pathType: Prefix139backend:140service:141name: api-service142port:143number: 8080144- path: /v2145pathType: Prefix146backend:147service:148name: api-v2-service149port:150number: 8080151```152153### Path-Based Routing154155```yaml156apiVersion: networking.k8s.io/v1157kind: Ingress158metadata:159name: app-ingress160namespace: production161spec:162ingressClassName: nginx163rules:164- host: app.example.com165http:166paths:167- path: /api168pathType: Prefix169backend:170service:171name: backend-api172port:173number: 8080174- path: /175pathType: Prefix176backend:177service:178name: frontend179port:180number: 80181```182183## NetworkPolicy (Zero Trust)184185### Default Deny All186187```yaml188apiVersion: networking.k8s.io/v1189kind: NetworkPolicy190metadata:191name: default-deny-all192namespace: production193spec:194podSelector: {}195policyTypes:196- Ingress197- Egress198```199200### Allow Frontend to Backend201202```yaml203apiVersion: networking.k8s.io/v1204kind: NetworkPolicy205metadata:206name: frontend-to-backend207namespace: production208spec:209podSelector:210matchLabels:211tier: backend212policyTypes:213- Ingress214ingress:215- from:216- podSelector:217matchLabels:218tier: frontend219ports:220- protocol: TCP221port: 8080222```223224### Backend to Database225226```yaml227apiVersion: networking.k8s.io/v1228kind: NetworkPolicy229metadata:230name: backend-to-database231namespace: production232spec:233podSelector:234matchLabels:235app: postgres236policyTypes:237- Ingress238ingress:239- from:240- podSelector:241matchLabels:242tier: backend243- namespaceSelector:244matchLabels:245name: production246ports:247- protocol: TCP248port: 5432249```250251### Allow DNS and External HTTPS252253```yaml254apiVersion: networking.k8s.io/v1255kind: NetworkPolicy256metadata:257name: allow-dns-and-https258namespace: production259spec:260podSelector:261matchLabels:262tier: backend263policyTypes:264- Egress265egress:266- to:267- namespaceSelector:268matchLabels:269name: kube-system270ports:271- protocol: UDP272port: 53273- to:274- namespaceSelector: {}275ports:276- protocol: TCP277port: 443278```279280### Cross-Namespace Communication281282```yaml283apiVersion: networking.k8s.io/v1284kind: NetworkPolicy285metadata:286name: allow-monitoring287namespace: production288spec:289podSelector:290matchLabels:291app: web-app292policyTypes:293- Ingress294ingress:295- from:296- namespaceSelector:297matchLabels:298name: monitoring299podSelector:300matchLabels:301app: prometheus302ports:303- protocol: TCP304port: 8080305```306307## DNS Configuration308309### Service DNS Names310311```yaml312# Within same namespace313http://web-app-service314315# Cross-namespace316http://web-app-service.production.svc.cluster.local317318# Headless service (StatefulSet)319postgres-0.postgres-headless.database.svc.cluster.local320postgres-1.postgres-headless.database.svc.cluster.local321postgres-2.postgres-headless.database.svc.cluster.local322```323324### Custom DNS Policy325326```yaml327apiVersion: v1328kind: Pod329metadata:330name: custom-dns331spec:332dnsPolicy: None333dnsConfig:334nameservers:335- 8.8.8.8336- 8.8.4.4337searches:338- production.svc.cluster.local339- svc.cluster.local340- cluster.local341options:342- name: ndots343value: "2"344containers:345- name: app346image: myapp:latest347```348349## Service Mesh (Istio Example)350351### VirtualService352353```yaml354apiVersion: networking.istio.io/v1beta1355kind: VirtualService356metadata:357name: web-app-routes358namespace: production359spec:360hosts:361- web-app-service362http:363- match:364- headers:365canary:366exact: "true"367route:368- destination:369host: web-app-service370subset: v2371- route:372- destination:373host: web-app-service374subset: v1375weight: 90376- destination:377host: web-app-service378subset: v2379weight: 10380```381382### DestinationRule383384```yaml385apiVersion: networking.istio.io/v1beta1386kind: DestinationRule387metadata:388name: web-app-destination389namespace: production390spec:391host: web-app-service392trafficPolicy:393connectionPool:394tcp:395maxConnections: 100396http:397http1MaxPendingRequests: 50398http2MaxRequests: 100399loadBalancer:400simple: LEAST_REQUEST401subsets:402- name: v1403labels:404version: v1.0.0405- name: v2406labels:407version: v2.0.0408```409410## EndpointSlice (Modern Alternative to Endpoints)411412```yaml413apiVersion: discovery.k8s.io/v1414kind: EndpointSlice415metadata:416name: web-app-abc123417namespace: production418labels:419kubernetes.io/service-name: web-app-service420addressType: IPv4421ports:422- name: http423protocol: TCP424port: 8080425endpoints:426- addresses:427- "10.244.1.5"428conditions:429ready: true430nodeName: node-1431- addresses:432- "10.244.2.7"433conditions:434ready: true435nodeName: node-2436```437438## Best Practices4394401. **Default Deny**: Start with deny-all NetworkPolicy, then allow specific traffic4412. **Least Privilege**: Only open required ports and protocols4423. **Service Selection**: Use ClusterIP by default, LoadBalancer sparingly4434. **DNS Names**: Use service DNS names, avoid hardcoded IPs4445. **TLS Termination**: Terminate TLS at Ingress when possible4456. **Health Checks**: Configure proper health check paths4467. **Rate Limiting**: Apply rate limits at Ingress level4478. **Monitoring**: Expose metrics endpoints for Prometheus448