K8s Hands-On: Multi-container pod and Service
Tags: beginners, kubernetes, handson, tutorial
Published: 2023-10-03 09:10:54

**Introduction**
Hi, I am Akshay Rao. This series will help you in hands on experience.
**Pre-requisites**
have done with create pod 
https://dev.to/aksrao1998/k8s-hands-on-create-a-pod-8ef
**Agenda**
In the last blog we saw how to create a pod of single container but we were not able to access the nginx application. In this we will access the nginx and deploy multi-container pod.
**Let's Start**

- we our pod named pod1 running but to access it we need to connect the container port 80 -> node port 80. So when the container port is connect to node port then the container is exposed to the outside world.
- So to connect this ports we will use another k8s component that is service, in which using the labels we will tell the controller to connect.
- we will do some slight changes in the pod1.yaml, before that if the pod is running pls delete it.
```
apiVersion: v1
kind: Pod
metadata:
  name:  pod1
  labels:         <- add this line to pod1.yaml
    type: webserver <- add this line to pod1.yaml
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
```
- now create another file ``` vi service.yaml``` and fill it with the below content
```
apiVersion: v1
kind: Service
metadata:
  name: basicservice #name of the service
spec:
  selector:
    type: webserver  <- same as in the pod1.yaml
  type: NodePort
  ports:
  - protocol: TCP
    port: 80

```
- now deploy both of them
```
kubectl create -f pod1.yaml
kubectl create -f service.yaml

# verify
kubectl get po
[ts-akshay.rao@JP-FVFZ91DHL414 k8s-ckad (⎈|minikube:default)]$ k get svc
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
basicservice   NodePort    10.105.134.51   <none>        80:30933/TCP   106m
kubernetes     ClusterIP   10.96.0.1       <none>        443/TCP        19h
```
- pick the ip address then ssh into the minikube as the cluster is running inside and curl the ip address.
```
[k8s-ckad (⎈|minikube:default)]$ minikube ssh
docker@minikube:~$ curl http://10.105.134.51:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
``` 
**Multi-container Pod**
- edit the pod1.yaml (first delete the existing pod)and deploy it.
```
apiVersion: v1
kind: Pod
metadata:
  name:  pod1
  labels:
    type: webserver
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
  - name: collector
    image: fluentd

```
- here we are using fluentd and nginx container in one pod.
- now describe this pod
```
[k8s-ckad (⎈|minikube:default)]$ k describe pods pod1
Name:             pod1
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Tue, 03 Oct 2023 18:08:00 +0900
Labels:           type=webserver
Annotations:      <none>
Status:           Pending
IP:               
IPs:              <none>
Containers:
  nginx:
    Container ID:   
    Image:          nginx:latest
    Image ID:       
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-hml2n (ro)
  collector:
    Container ID:   
    Image:          fluentd
    Image ID:       
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       ContainerCreating
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-hml2n (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  kube-api-access-hml2n:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  11s   default-scheduler  Successfully assigned default/pod1 to minikube
  Normal  Pulling    10s   kubelet            Pulling image "nginx:latest"
  Normal  Pulled     8s    kubelet            Successfully pulled image "nginx:latest" in 2.04728618s
  Normal  Created    8s    kubelet            Created container nginx
  Normal  Started    8s    kubelet            Started container nginx
  Normal  Pulling    8s    kubelet            Pulling image "fluentd

```
- you can see the the events and of nginx and fluentd.

**Links for further undertanding**
https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport