Search⌘ K

Creating Services through Declarative Syntax

Explore how to define and create Kubernetes Services using declarative syntax. Understand Service types like NodePort, selectors for routing requests to pods, and how Kubernetes load balances traffic. Gain practical skills to expose applications and manage service resources in your cluster.

Looking into the syntax

We can accomplish a similar result as the one using kubectl expose through the go-demo-2-svc.yml specification shown below:

YAML
apiVersion: v1
kind: Service
metadata:
name: go-demo-2
spec:
type: NodePort
ports:
- port: 28017
nodePort: 30001
protocol: TCP
selector:
type: backend
service: go-demo-2
  • Lines 1–4: Since we have already discussed the meaning of apiVersion, kind, and metadata, we’ll jump straight into the spec section.

  • Line 5: Since we have already explored some of the options through the kubectl expose command, the spec should be relatively easy to grasp.

  • Line 6: The type of the Service is set to NodePort, meaning that the ports will be available both within the cluster as well as from outside by sending requests to any of the nodes. ...