If you need to read more about Kubernetes basics and local dev env setup, read this first Getting started with Kubernetes.

Clean up manual configuration of services and deployments using kubectl delete all --all. Now check for pods and services presents using kubectl get pods and kubectl get svc. If the list is empty, we can continue with the setup.

You can create YAML configuration files to set up deployment.yaml and service.yaml. If you have VS Code extension installed for Kubernetes. You can create a template configuration file for by typing “Deployment”.

See the Kubernetes configuration API documentation at https://kubernetes.io/docs/reference/kubernetes-api/ for information about generated YAML parameters. Deployments and Services YAML API documentation. From here you can read more about available options and its values. Also see PODs API documentation.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-node-express
spec:
  selector:
    matchLabels:
      app: k8s-node-express
  template:
    metadata:
      labels:
        app: k8s-node-express
    spec:
      containers:
      - name: k8s-node-express
        image: devwl/k8s-node-express
        resources:
          limits:
            memory: "128Mi"
            cpu: "250m"
        ports:
        - containerPort: 3000

replicaSet.yaml

apiVersion: v1
kind: ReplicationController
metadata:
  name: k8s-node-express
spec:
  replicas: 5
  selector:
    app: k8s-node-express
  template:
    metadata:
      name: k8s-node-express
      labels:
        app: k8s-node-express
    spec:
      containers:
        - name: k8s-node-express
          image: devwl/k8s-node-express
          ports:
            - containerPort: 3000

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: k8s-node-express
spec:
  type: LoadBalancer
  selector:
    app: k8s-node-express
  ports:
  - port: 3030
    targetPort: 3000

To run all above files, you can simply enter the fallowing commands:

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
kubectl apply -f replicaSet.yaml

Clean up

To clean all services and deployments predefined in YAML files you can simple use one simple command

kubectl delete -f deployment.yaml -f service.yaml -f replicaSet.yaml

If you have any leftovers after type errors in YAML configuration, you can clean up all pods with kubectl delete all --all

YAML one file

The above example presents how we could modularize YAML configuration file. However, we could also merge all configuration files in to one configuration file. Naming of the YAML configuration files are not predefine. That means that we can use any naming convention we like. For example we could use our project name: k8s-node-express.yaml.

While combining multiple YAML files, we need to remember that content of each file should be separated by three dashes: --- . This is a handy setup for simple projects.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-node-express
spec:
  selector:
    matchLabels:
      app: k8s-node-express
  template:
    metadata:
      labels:
        app: k8s-node-express
    spec:
      containers:
      - name: k8s-node-express
        image: devwl/k8s-node-express
        resources:
          limits:
            memory: "128Mi"
            cpu: "250m"
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: k8s-node-express
spec:
  replicas: 3
  selector:
    app: k8s-node-express
  template:
    metadata:
      name: k8s-node-express
      labels:
        app: k8s-node-express
    spec:
      containers:
        - name: k8s-node-express
          image: devwl/k8s-node-express
          ports:
            - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: k8s-node-express
spec:
  type: LoadBalancer
  selector:
    app: k8s-node-express
  ports:
  - port: 3030
    targetPort: 3000
0
Would love your thoughts, please comment.x
()
x