Thursday 30 June 2022

Configure Pods in Kubernetes Cluster | Label and Service Example with apache


 ls

pod.yaml  pod1.yaml  service.yaml


Configuring and setting up the apache Pods


cat pod.yaml 

apiVersion: v1

kind: Pod

metadata:

  name: apache2

  labels:

    mycka: mylabel

spec:

  containers:

  - name: mycontainer

    image: docker.io/httpd

    ports:

    - containerPort: 80


kubectl create -f pod.yaml 

pod/apache2 created


 cat pod1.yaml 

apiVersion: v1

kind: Pod

metadata:

  name: apache3

  labels:

    mycka: mylabel

spec:

  containers:

  - name: mycontainer

    image: docker.io/httpd

    ports:

    - containerPort: 80


kubectl create -f pod1.yaml 

pod/apache3 created


kubectl get po

NAME                    READY   STATUS    RESTARTS       AGE

apache2                 1/1     Running   0              70s

apache3                 1/1     Running   0              15s


Configuring and setting up the Service 


cat service.yaml 

kind: Service

apiVersion: v1

metadata:

  name: myservice

spec:

  selector: 

      mycka: mylabel

  ports:

    - protocol: TCP

      port: 8081

      targetPort: 80


 kubectl create -f service.yaml 

service/myservice created


 kubectl get svc

NAME              TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE

kubernetes        ClusterIP   10.96.0.1      <none>        443/TCP        5d5h

myservice         ClusterIP   10.98.192.24   <none>        8081/TCP       8s


Executing the Apache services 


kubectl exec -it apache2 bash

kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

root@apache2:/usr/local/apache2# echo \342\200\234Hello from pod1 \342\200\235 > htdocs/index.html

root@apache2:/usr/local/apache2# cat htdocs/index.html

“Hello from pod1 ”

root@apache2:/usr/local/apache2# exit

exit


kubectl exec -it apache3 bash

kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.

root@apache3:/usr/local/apache2# echo \342\200\234Hello from pod2 \342\200\235 > htdocs/index.html

root@apache3:/usr/local/apache2# cat htdocs/index.html

“Hello from pod2 ”

root@apache3:/usr/local/apache2# exit

exit


#myservice is connected to apache2 and apache3 

kubectl get svc -o wide

NAME              TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE     SELECTOR

kubernetes        ClusterIP   10.96.0.1      <none>        443/TCP        5d5h    <none>

myservice         ClusterIP   10.98.192.24   <none>        8081/TCP       3m43s   mycka=mylabel


#service can call from both pods

curl 10.98.192.24:8081

“Hello from pod1 ”


curl 10.98.192.24:8081

“Hello from pod2 ”


No comments:

Post a Comment