Link Search Menu Expand Document

Service

multi-port-service.yaml

---
# https://kubernetes.io/docs/concepts/services-networking/service/#multi-port-services
apiVersion: v1
kind: Service
metadata:
  name: multi-port-service-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 8080
    - name: https
      protocol: TCP
      port: 443
      targetPort: 8443

simple.yaml

---
# https://kubernetes.io/docs/concepts/services-networking/service/
apiVersion: v1
kind: Service
metadata:
  name: service-simple-service
spec:
  selector:
    app: service-simple-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

spec.externalIPs/

external-ips.yaml

            ---
# https://kubernetes.io/docs/concepts/services-networking/service/#external-ips
apiVersion: v1
kind: Service
metadata:
  name: service-external-ips-service
spec:
  selector:
    app: MyApp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 9376
  externalIPs:
    - 80.11.12.10
 

spec.externalName/

external-name.yaml

            ---
# https://kubernetes.io/docs/concepts/services-networking/service/#externalname
apiVersion: v1
kind: Service
metadata:
  name: service-external-name-service
spec:
  type: ExternalName
  externalName: my.database.example.com
 

spec.topologyKeys/

fallback.yaml

            ---
# https://kubernetes.io/docs/concepts/services-networking/service-topology/#prefer-node-local-zonal-then-regional-endpoints
# A Service that prefers node local, zonal, then regional endpoints but falls back to cluster wide endpoints.
apiVersion: v1
kind: Service
metadata:
  name: service-topolgies-fallback-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
  topologyKeys:
    - "kubernetes.io/hostname"
    - "topology.kubernetes.io/zone"
    - "topology.kubernetes.io/region"
    - "*"
 

spec.type/

load-balancer.yaml

            ---
# https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer
apiVersion: v1
kind: Service
metadata:
  name: service-load-balancer-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
  type: LoadBalancer
 

spec.ports.nodePort/

node-port.yaml

            ---
# https://kubernetes.io/docs/concepts/services-networking/service/#nodeport
apiVersion: v1
kind: Service
metadata:
  name: service-node-port-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
    - port: 80         # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
      targetPort: 80   # Optional field.
      nodePort: 30007  # Optional field. By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
 

Pod.spec.subdomain/

subdomain.yaml

            ---
# Taken from: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-hostname-and-subdomain-fields
# Currently when a pod is created, its hostname is the Pod's metadata.name value.
# The Pod spec has an optional hostname field, which can be used to specify the Pod's hostname.
# When specified, it takes precedence over the Pod's name to be the hostname of the pod.
# For example, given a Pod with hostname set to "my-host", the Pod will have its hostname set to "my-host".
# The Pod spec also has an optional subdomain field which can be used to specify its subdomain.
# For example, a Pod with hostname set to "foo", and subdomain set to "bar", in namespace
# "default", will have the fully qualified domain name (FQDN) "foo.bar.default.svc.cluster-domain.example".
#
# If there exists a headless service in the same namespace as the pod and with the same name as the subdomain,
# the cluster's DNS Server also returns an A or AAAA record for the Pod's fully qualified hostname.
# For example, given a Pod with the hostname set to "subdomain-simple-hostname-1" and the subdomain
# set to "subdomain-simple-subdomain-service", and a headless Service named "subdomain-simple-subdomain-service"
# in the same namespace, the pod will see its own FQDN as
# "subdomain-simple-hostname-1.subdomain-simple-subdomain-service.default.svc.cluster-domain.example".
# DNS serves an A or AAAA record at that name, pointing to the Pod's IP.
# Both pods "subdomain-simple-pod-1" and "subdomain-simple-pod-2" can have their distinct A or AAAA records.

Example:
apiVersion: v1
kind: Service
metadata:
  name: subdomain-simple-subdomain-service
spec:
  clusterIP: None  # A headless service
  ports:
    - name: subdomain-simple-port-name  # Actually, no port is needed.
      port: 1234
      targetPort: 1234
  selector:
    name: subdomain-simple-selector
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    name: subdomain-simple-selector
  name: subdomain-simple-pod-1
spec:
  containers:
    - command:
        - sleep
        - "3600"
      image: busybox
      name: subdomain-simple-container-1
  hostname: subdomain-simple-hostname-1
  subdomain: subdomain-simple-subdomain-service
---
apiVersion: v1
kind: Pod
metadata:
  name: subdomain-simple-pod-2
  labels:
    name: subdomain-simple-selector
spec:
  containers:
    - command:
        - sleep
        - "3600"
      image: busybox
      name: subdomain-simple-container-2
  hostname: subdomain-simple-hostname-2
  subdomain: subdomain-simple-subdomain-service
 

headless-service/

headless-service.yaml

            # Example of a headless service.
# To see the difference, exec onto the headless service app, and do:
#
# nslookup headless-service-normal-service
# nslookup headless-service-headless-service
#
# from the dns-debug service (does not work from the deployed app itself - not sure why)
---
apiVersion: v1
kind: Service
metadata:
  name: headless-service-normal-service
spec:
  selector:
    app: headless-service-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: headless-service-headless-service
spec:
  clusterIP: None  # This marks this service out as a headless service
  selector:
    app: headless-service-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: headless-service-deployment
  labels:
    app: headless-service-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: headless-service-app
  template:
    metadata:
      labels:
        app: headless-service-app
    spec:
      containers:
        - command:
            - sleep
            - "3600"
          image: busybox
          name: headless-service-app
          ports:
            - containerPort: 3000
---
# This pod allows us to do the DNS lookup
# https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/
apiVersion: v1
kind: Pod
metadata:
  name: headless-service-dnsutils-pod
spec:
  containers:
    - command:
        - sleep
        - "3600"
      image: gcr.io/kubernetes-e2e-test-images/dnsutils:1.3
      name: dnsutils
 

spec.clusterIP/

headless-service.yaml

            # Example of a headless service.
# To see the difference, exec onto the headless service app, and do:
#
# nslookup headless-service-normal-service
# nslookup headless-service-headless-service
#
# from the dns-debug service (does not work from the deployed app itself - not sure why)
---
apiVersion: v1
kind: Service
metadata:
  name: headless-service-normal-service
spec:
  selector:
    app: headless-service-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: headless-service-headless-service
spec:
  clusterIP: None  # This marks this service out as a headless service
  selector:
    app: headless-service-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: headless-service-deployment
  labels:
    app: headless-service-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: headless-service-app
  template:
    metadata:
      labels:
        app: headless-service-app
    spec:
      containers:
        - command:
            - sleep
            - "3600"
          image: busybox
          name: headless-service-app
          ports:
            - containerPort: 3000
---
# This pod allows us to do the DNS lookup
# https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/
apiVersion: v1
kind: Pod
metadata:
  name: headless-service-dnsutils-pod
spec:
  containers:
    - command:
        - sleep
        - "3600"
      image: gcr.io/kubernetes-e2e-test-images/dnsutils:1.3
      name: dnsutils