Kubernetes2

Kubernetes2

ยท

6 min read

Hello everyone I am Goutham in this blog I would like to explain what minikube is, beginner kubectl commands, how to write deployment, secret config maps, and service files.

Minikube

Minikube is a single node Kubernetes cluster which means we can create master nodes, worker nodes on a single node which is purely meant for practicing.It is used run a Kubernetes environment on a local computer.

Kubectl

Kubectl is a terminal from which we can interact with our Kubernetes cluster whether it might be a minikube k8s cluster or k8s cluster in a cloud or on-premise. Kubectl will be automatically installed while installing minikube. There are some kubectl commands which are used to interact with k8s cluster . Here I would like to share a few beginner commands which are frequently used like create deployments ,services etc...

In kubernetes cluster pod is the smallest execution unit.

Pod encapsulates one or more containers.

But we don't create pods instead we create deployments which create replica sets which in turn create pods inside pods we have our containers inside containers we have our application running.

Pods are ephemeral which means they die frequently so if we create pods using deployments , replicasets ensures desired pods are always running and we do not have to look after them.

Kubectl commands

Creating a Pod in a K8s cluster

We can directly create a pod or we can create a pod through a deployment but creating it through deployments is preferred.

This is how a basic deployment file looks like.We don't need to remember the whole syntax, in VS code type deployment it auto completes.

# deployment.yml
apiVersion: apps/v1  # api version type kubectl apiresources and 
kind: Deployment     # kind of congiguration
metadata:
  name: mydeployment     # Name of the deployment
spec:
  repliacs: 3     # Number of pods we want to create
  selector:
    matchLabels:  
      app: myapp   
  template:  
    metadata:
      labels:  # All key value pairs here in labels has to match with matchLabels as the kubernetes knows it has to create only this pod while cretaing deployment                                                                                                  
        app: myapp 

    spec:
      containers:   # There can be one or more containers inside a pod
      - name: myapp    # name of the container 
        image: <Image>  # docker image 
        resources:
          limits:  # Resources for container 
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080 # port that was opened inside   container

Now with this, we can create a pod

kubectl apply -f deployment.yml

Displaying pods, Replicasets,Nodes in Cluster

kubectl get pods or simply kubectl get po

kubectl get node or simply kubectl get no

kubectl get repliacsets or simply kubectl get rs

kubectl get deployment

Debugging

kubectl get pods -o wide displays more information than get pods

kubectl describe pod <podName> displays more information than -o wide

kubectl get pods -w screen waits and displays each step in creating pod

Same goes with deployments and replicasets aswell.

Creating Service

Service provides a permanent IP to pods through which pods can communicate with each other.

There are different types of services

  1. Cluster IP service

  2. Node Port Service

  3. Load Balancer services

Cluster IP services are default services and are internal services through which pods communicate internally. A ClusterIP service is the default Kubernetes service. It gives you a service inside your cluster that other apps inside your cluster can access. There is no external access.

Node Port services are used to allow external traffic these services open port on the node, external requests are directly made using node IP address and port address from node the request then flow to service and from service to attached pods.

Load Balancer service also allows external traffic and forwards to service that attached to pods.

kind: Service
apiVersion: v1
metadata:
  name:  myservice # Name of the service
spec:
  selector:
    app:  Selector Label  # This selector label has to match with pod name only then service will be attached to pods.
  type:  LoadBalancer | ClusterIP | NodePort # type of service
  ports:
  - name:  name-of-the-port
    port:  80           # Port on the service that accepts requests 
    targetPort:  8080   # target port this has to match with container's port

We can write service and deployment in a single file by separating with ---.

Creating ConfigMap and Secrets

If we don't want to mention some environment variables that application inside the container use, in deployment files then we can use config maps and secrets.

# frontend.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: recctpod

spec:
 replicas: 2
 selector: 
  matchLabels:
     app: reactapp
 template:
     metadata:

       labels:
          app: reactapp
     spec: 
       containers:
          - name: reactcontainer
            image: Goutham2/react-app # image from  docker hub  
            ports:        
            - containerPort: 3000
            env:
            - name: REACT_APP_AUTH0_DOMAIN    # env name used inside appliaction.
              valueFrom:
                  secretKeyRef:
                       name: react-secrets  # Name of the secret has to match with the name in meta data in  secret file then only corresponding secret will be mapped . 
                       key: react-domain # secret we want to use

            - name: ME_CONFIG_MONGODB_SERVER # env name used inside appliaction.
              valueFrom:
                 configMapKeyRef:
                       name: myconfigmap
                       key: servicename # key servicename will be mapped from configmap 
---

apiVersion: v1
kind: Service
metadata:
  name: externalservice
spec:
  selector:
    app: reactapp
  type: LoadBalancer
  ports:
  - port: 80 # This can be any port
    targetPort: 3000   # This has to match with container port.
# configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: myconfigmap
data:
  servicename: myservice # this is name of the service that we have already created and frontend.yaml deployment is using it  to communicate with mydeployment microservice as myservice service is attached to mydeployment pods.
# secrets.yml
apiVersion: v1
kind: Secret
metadata:
  name: react-secrets
type: Opaque
data:
 react-domain: VXNlcg==  # Just by naming in secret doesn't make it secret instead we have to use base64 encoded string . Just type 
 # echo -n <your secret> | base64  in your terminal.

Before applying deployment file we have to apply configmap and secrets

kubectl apply -f configmap.yml

kubectl apply -f secrets.yml

kuectl apply -f frontend.yml

By minikube service externalservice random port will be opened on a node on which pod was running

We can know what are the services available by

kubectl get service or simply kubetl get svc

We can even get more information for a service by

kubetl describe service <Service Name>

Thought of explaining about ingress aswell but I could not able to implement ingress correctly. But I'll explain what it does .

Ingress are used to avoid external requests using IP address and port instead requests were handled using domain name . These are not services.

We can attach ingress to different services for different paths.

We have to install ingress controller to manage ingress routes. There are many 3rd party controllers like nginx, Google cloud load Balancers ...

I took Node port Image , Load balancer image , Ingress image from Sandeep Dinesh Medium blog.

Thanks For reading my blog If you find any thing wrong please let me know.

Have a great day ๐ŸŽ‰ ๐ŸŽ‰

ย