...

/

Creating Services through Declarative Syntax

Creating Services through Declarative Syntax

Learn to create Services through declarative syntax.

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:

Press + to interact
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. ...