Sunday, July 24, 2022

Create Kubernetes ClusterIP Service Definition using YAML

A ClusterIP service is the default Kubernetes service. It gives you a service inside your cluster that other apps/pods inside your cluster can access. There is no external access. 

The YAML for a ClusterIP service looks like this:
# cat clusterIP.yaml
apiVersion: v1
kind: Service
metadata:  
  name: my-internal-service
spec:
  selector:    
    app: my-app
  type: ClusterIP
  ports:  
  - name: http
    port: 80
    targetPort: 8080
    protocol: TCP
    
You can create the Service by using kubectl apply -f clusterIP.yaml. After you create the Service, you can use kubectl get service to see the stable IP address

Create kubernetes NodePort service using yaml

A NodePort is a kubernetes service type where an open port on every node of your cluster. Kubernetes transparently routes incoming traffic on the NodePort to your service, even if your application is running on a different node. The default range for the NodePort service ports are 30000-32767

Below is a sample nodeport service yaml file

       
# cat NodePort.yaml
apiVersion: v1
kind: Service
metadata:  
  name: my-nodeport-service
spec:
  selector:    
    app: my-app
  type: NodePort
  ports:  
  - name: http
    port: 80
    targetPort: 80
    nodePort: 30036
    protocol: TCP
Run following command to create NodePort service in your Kubernetes cluster
       
# kubectl apply -f NodePort.yaml

Create Kubernetes Pod Definition using YAML

Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. 

A Pod is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers. 

Below is a sample yaml file for creating pod.
       
# cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: web
  labels:
    role: myrole
spec:
  containers:
    - name: web
      image: nginx
      ports:
        - name: web
          containerPort: 80
          protocol: TCP
Run following command to create pod in your Kubernetes cluster
       
# kubectl apply -f pod.yaml

Tuesday, June 21, 2022

How to Restart the pods in Kubernetes

A pod is a collection of containers sharing a network, acting as the basic unit of deployment in Kubernetes. All containers in a pod are scheduled on the same node.

1: kubectl scale

Scale the number of replicas using the kubectl command scale and set the replicas flag to zero:

kubectl scale deployment <deployment_name> --replicas=0 -n <namespace>

Scale the number of replicas using the kubectl command scale and set the replicas flag to its original value:

kubectl scale deployment <deployment_name> --replicas=2 -n <namespace>


2: kubectl rollout restart

kubectl rollout restart deployment <deployment_name> -n <namespace>


3: kubectl delete pod

It will automatically recreate the pod to keep it consistent with the expected one.

kubectl delete pod <pod_name> -n <namespace>


Create Kubernetes ClusterIP Service Definition using YAML

A ClusterIP service is the default Kubernetes service. It gives you a service inside your cluster that other apps/pods inside your cluster c...