🚀
4. K8s Config File

Kubernetes Configuration File

Each configuration file has 3 parts:

  • API version (shows the version of a component)
  • Kind (kind of configuration file)
  1. Metadata
  2. Specification (attributes are specific to the kind)
  3. Status (auto generated and managed by k8s)
    • Desired State (assign a key value pair to the deployment)
    • Actual State (which is basically specification) (stored in etcd)
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
   labels:                # key value pairs attached to objects (for selection and grouping)
    app: nginx
spec:                     # describes the desired state of the deployment
  selector:               # defines how deployment find which pod to manage
    matchLabels:          # select labels of pods, which are to be managed by deployment
      app: nginx
  replicas: 2             # tells deployment to run 2 pods matching the template
  template:               # the configuration of the pods are defined here
    metadata:
      labels:
        app: nginx
    spec:                 # blueprint of a pod
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
service.yaml
apiVersion: v1
kind: Service
metadata: 
	name: nginx-service
spec:
	selector:
		app: nginx          # connects with the deployment and pods lines 6, 18
	ports:
		- protocol: TCP
	      port: 27017       # accesable ports
	      targetPort: 80    # target port is the container port

Apply k8s Configuration File

  • kubectl apply -f <yaml file>
$ kubectl apply -f deployment.yaml
 
deployment.apps/nginx-deployment created
 
$ kubectl get all
 
NAME                                    READY   STATUS    RESTARTS      AGE
pod/nginx-deployment-77d8468669-5s86g   1/1     Running   1 (36s ago)   17h
pod/nginx-deployment-77d8468669-p94hk   1/1     Running   1 (36s ago)   17h
 
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
service/kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP     5d12h
service/nginx-service   ClusterIP   10.105.137.204   <none>        27017/TCP   17h
 
NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   2/2     2            2           17h
 
NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-77d8468669   2         2         2       17h
$ kubectl apply -f service.yaml
 
service/nginx-service created
 
$ kubectl get services
 
NAME            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)     AGE
kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP     4d19h
nginx-service   ClusterIP   10.105.137.204   <none>        27017/TCP   3m45s
$ kubectl describe service nginx-service 
 
Name:              nginx-service
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=nginx
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.105.137.204
IPs:               10.105.137.204
Port:              <unset>  27017/TCP
TargetPort:        80/TCP
Endpoints:         10.244.0.12:80,10.244.0.13:80 
Session Affinity:  None
Events:            <none>

See IP of all Pods

  • kubectl get pod -o wide
$ kubectl get pod -o wide
 
NAME                                READY   STATUS    RESTARTS   AGE   IP            NODE       NOMINATED NODE   READINESS GATES
nginx-deployment-77d8468669-5s86g   1/1     Running   0          17m   10.244.0.12   minikube   <none>           <none>
nginx-deployment-77d8468669-p94hk   1/1     Running   0          17m   10.244.0.13   minikube   <none>           <none>

Export Deployment to YAML

  • kubectl get deployment <deployment name> -o yaml exports configuration of deployment alongside the actual state of the deployment from etcd.

  • kubectl get deployment <deployment name> -o yaml > <filename>.yaml exports configuration and saves it in YAML file

$ kubectl get deployment nginx-deployment -o yaml
 
apiVersion: apps/v1
kind: Deployment
metadata:
  ...
spec:
  ...
status:
  availableReplicas: 2
  conditions:
  - lastTransitionTime: "2024-08-04T12:36:29Z"
    lastUpdateTime: "2024-08-04T12:36:29Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2024-08-04T12:36:10Z"
    lastUpdateTime: "2024-08-04T12:36:29Z"
    message: ReplicaSet "nginx-deployment-77d8468669" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 2
  replicas: 2
  updatedReplicas: 2

Delete k8s Configuration File

  • kubectl delete -f <configuration file>.yaml
$ kubectl delete -f nginx-deployment.yaml
 
deployment.apps "nginx-deployment" deleted

© 2024 Driptanil Datta.All rights reserved

Made with Love ❤️

Last updated on Mon Oct 20 2025