Kubernetes Service Generator

Build a Service of any of the four types, with the selector kept straight from the pod labels it has to match and the port fields explained. The output is checked by our own validator as you type.

Service
Behaviour

No virtual IP. DNS returns the pod addresses directly, which is what a StatefulSet needs.

Affinity is per client IP, so everything behind one NAT gateway lands on the same pod.

Needed by some clustered databases that must find their peers before any of them is Ready.

service.yaml

updates as you type

    Common mistakes

    These are the ones that fail silently. The config is accepted, nothing raises an error, and the consequence arrives later.

    1. Confusing port and targetPort

      port is what clients connect to on the Service, targetPort is the container's port. Swapping them gives a Service that accepts connections and forwards nowhere.

      Instead:targetPort must match containerPort, or a named port.

    2. Using a hostPort or NodePort where a Service was enough

      hostPort binds the node's port and limits the pod to one replica per node. NodePort exposes it on every node.

      Instead:ClusterIP for internal traffic, and an Ingress or LoadBalancer for external.

    3. Setting clusterIP: None without meaning to

      That makes it headless: DNS returns pod addresses and there is no load balancing or virtual IP.

      Instead:Leave clusterIP unset unless you specifically want per-pod DNS.

    The four Service types, and the ports nobody agrees on

    A Service is a stable name and address in front of a set of pods that keep changing. Almost every problem with one is either the selector matching nothing, or a misunderstanding about which of the three port numbers does what.

    port, targetPort and containerPort

    Three numbers, and only two of them matter. port is what clients connect to. targetPort is the port on the pod that traffic is forwarded to, and it defaults to the same value as port when omitted, not to whatever the container declared. containerPort is documentation only: a container can listen on a port it never declared and traffic still arrives.

    ports:
      - port: 80          clients connect here
        targetPort: 8080  forwarded to this pod port
    
    targetPort omitted -> defaults to port (80), NOT to
                          the containerPort
    
    targetPort: http   -> resolved against the container's
                          declared port NAMES. If nothing
                          declares that name, no endpoint
                          is ever created.

    A NodePort cannot be any number you like

    The range is 30000 to 32767 unless the API server was started with a different one. Anyone translating a docker run -p 8080:80 reaches for nodePort 8080 and the object is rejected. Leaving nodePort unset lets the cluster allocate one, which is what you want unless something external is hard-coded to a port.

    docker run -p 8080:80        any host port
    
    Service type: NodePort
      nodePort: 8080             rejected
      nodePort: 30080            fine
      nodePort omitted           allocated for you
    
    # the true local equivalent of docker -p is:
    kubectl port-forward svc/api 8080:80

    externalTrafficPolicy trades the client IP against balancing

    Cluster spreads traffic evenly across pods and SNATs it, so the application sees a node address rather than the client's. Local preserves the source IP and only delivers to nodes actually running a pod, so with fewer pods than nodes the load is uneven and nodes with no pod drop their share until health checks catch up.

    Cluster   even spread, source IP lost
              10 nodes, 3 pods -> traffic hops between
              nodes, every pod gets a third
    
    Local     source IP kept, uneven spread
              10 nodes, 3 pods -> 7 nodes have nothing
              to deliver to. The load balancer must
              health check them out first.

    Headless is not broken

    clusterIP: None means no virtual IP and no kube-proxy involvement at all. DNS returns every ready pod address directly and the client chooses. That is what a StatefulSet needs for stable per-pod names, and what a gRPC client wants: with a normal ClusterIP, gRPC opens one long-lived connection and stays pinned to a single pod forever, so the Service balances nothing.

    spec:
      clusterIP: None
    
    dig api.default.svc.cluster.local
      10.1.2.3
      10.1.2.9      one A record per ready pod
      10.1.3.1
    
    # and with a StatefulSet's serviceName:
    # api-0.api.default.svc.cluster.local -> one pod

    ExternalName does nothing but answer DNS

    It returns a CNAME and stops there. No proxying, no ports, no selector, no TLS handling. So a Service on port 80 aliasing an HTTPS endpoint does not work, because there is nothing in the path to rewrite anything. It is a name, and the client connects directly to whatever that name resolves to.

    spec:
      type: ExternalName
      externalName: db.rds.amazonaws.com
    
    # clients connecting to `db` inside the cluster get
    # a CNAME to that host and connect straight out.
    # Ports listed here are ignored entirely.

    More kubernetes tools

    Kubernetes YAML Generator Answer a few questions, get a manifest Kubernetes YAML Validator Is this YAML valid, and where is it wrong? Kubernetes Rollout & Probe Timing Restart loops, capacity dips and stuck drains Kubernetes Service Checker Will this Service have any endpoints? Kubernetes Network Policy Viewer What can reach what, and what just broke Kubernetes Manifest Diff What actually changed Kubernetes Resource Calculator What these manifests actually reserve Kubernetes RBAC Viewer Who can actually do what Kubernetes Deployment Generator See the capacity dip before you deploy Kubernetes StatefulSet Generator Stable names, and volumes that outlive you Kubernetes QoS Class Calculator And where you sit in the eviction order Kubernetes Node Capacity Planner Allocatable is not capacity Kubernetes Max Pods per Node Calculator The ENI limit, not the 110 default Kubernetes Label Selector Tester Which key failed, not just that one did Kubernetes Taint & Toleration Tester Tolerations permit, they do not attract Kubernetes Resource Name Validator RFC 1123 subdomain, label, or 1035 Kubernetes Label & Annotation Validator Why your image tag is not a legal label Kubernetes CrashLoopBackOff Guide The status is a timer, not a cause Kubernetes TLS Certificate Decoder When it expires, and what it covers Kubernetes ServiceAccount Token Decoder Bound or legacy, and when it dies Kubernetes imagePullSecret Generator The registry URL nobody gets right Kubernetes cert-manager Certificates Issuer scope, and the rate limit that costs a week Kubernetes Flux HelmRelease Generator With the source it cannot work without Kubernetes Helm Chart Generator Chart.yaml, values, and working templates Kubernetes Argo CD Application Generator The defaults that are off, turned on Kubernetes Ingress Generator pathType, and the TLS secret that fails quietly Kubernetes HPA Generator Why it says unknown and never scales Kubernetes SecurityContext Generator Pod level, container level, and which wins Kubernetes Probe Generator How long it gets to start before liveness kills it Kubernetes PVC Generator Access modes your storage can actually do Kubernetes ResourceQuota Generator And the LimitRange that stops it breaking deploys Kubernetes CronJob Generator The fields that decide whether it runs Kubernetes NetworkPolicy Generator With the DNS rule already in it Kubernetes RBAC Generator And what it actually grants Kubernetes PodDisruptionBudget Generator Can this budget ever be satisfied? Kubernetes API Deprecation Checker What breaks on the next upgrade Kubernetes Kubeconfig Viewer Read it without handing it to anyone Kubernetes Quantity Parser What 512Mi, 100m and 1e3 actually mean Kubernetes Multi-Document YAML Splitter One file per object, in apply order Kubernetes .env to ConfigMap & Secret Split the credentials out on the way Kubernetes Secret Decoder See what is actually in there Kubernetes Ingress to Gateway API Which annotations silently stop working Kubernetes Manifest Visualizer What points at what, and what points at nothing Kubernetes Patch Tester Which --type, and what it deletes Kubernetes kubectl JSONPath Tester kubectl's dialect, not generic JSONPath Kubernetes Node Affinity Tester Required excludes, preferred only scores Kubernetes Helm Values Diff A missing key is a third value Kubernetes Pod and Service CIDR Fixed at cluster creation Kubernetes Secret Encoder base64 is not encryption Kubernetes API Version Checker Will this apply on that cluster Kubernetes Go Template Tester Go truthiness is not JavaScript's Kubernetes Cluster Cost Estimator At your rates, not a price table Kubernetes Kustomize Build Transformers run in kustomize's order Kubernetes Helm Chart Validator version is SemVer, appVersion is not

    Elsewhere on the site