Showing posts with label kubernetes. Show all posts
Showing posts with label kubernetes. Show all posts

Saturday, October 9, 2021

Expose Pod in Kubernetes

Introduction 

Today we will learn to expose the Pod Resource of Kubernetes.

Expose Pod

We will create and expose the pod using below yaml:

apiVersion: v1
kind: Pod
metadata:
  name: ashok-pod
  labels:
    app: web
spec:
  containers:
    - name: hello-app
      image: gcr.io/google-samples/hello-app:1.0
      ports:
      - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: ashok-svc
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    targetPort: 8080
  selector:
    app: web

We will create the pod and service using:

$ kubectl apply -f pod2.yaml
pod/ashok-pod created
service/ashok-svc created
We can status using below:

$ kubectl get all
NAME            READY   STATUS    RESTARTS   AGE
pod/ashok-pod   1/1     Running   0          92s

NAME                 TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service/ashok-svc    LoadBalancer   10.108.156.134   <pending>     8080:30901/TCP   92s
service/kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP          3d2h

Create a tunnel to access service in browser:
$ minikube tunnel
🏃  Starting tunnel for service ashok-svc.
Open the browser  http://localhost:8080/ :



Happy Coding !!

Running StorageClass Resource on Kubernetes

Introduction

We will learn creating dynamic volume using StorageClass on Minikube today. 

StorageClass

We will create StorageClass, PersistentVolumeClaim, Pod and Service using yaml:

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: slow
provisioner: k8s.io/minikube-hostpath
parameters:
  type: pd-ssd
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ashok-pv-claim
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: slow
  resources:
    requests:
      storage: 10Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: ashok-pv-pod
  labels:
    app: hello  
spec:
  volumes:
    - name: ashok-pv-storage
      persistentVolumeClaim:
        claimName: ashok-pv-claim
  containers:
    - name: ashok-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: ashok-pv-storage
---
apiVersion: v1
kind: Service
metadata:
  name: helloweb-svc
  labels:
    app: hello
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    targetPort: 80
  selector:
    app: hello

We need to create an  index.html file for our nignx pod and then use this file in our PersistentVolumeClaim for storage in our Pod.

You can login to Pod using:

$ kubectl exec -it ashok-pv-pod -- /bin/bash
root@ashok-pv-pod:/#
Create index.html file at /usr/share/nginx/html/index.html :

$ echo 'Hello from Kubernetes storage using storage class' > /usr/share/nginx/html/index.html
Create a tunnel to access service in browser:
$ minikube tunnel
🏃  Starting tunnel for service helloweb-svc.
Open the browser  http://localhost:8080/ :



Happy Coding !!


Pass configs to Kubernetes using ConfigMap

Introduction

Today we will learn about ConfigMap Resource of Kubernetes. This is used to pass the configs to Kubernetes resources like Pod, Deployment etc.

ConfigMap

We will create a ConfigMap using yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  log_level: INFO
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3    

We will create the config from above yaml:

$ kubectl apply -f config.yaml
configmap/special-config created
We will use this config in below pod:
apiVersion: v1
kind: Pod
metadata:
  name: ashok-config-pod
spec:
  containers:
    - name: ashok-config-pod
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: log_level
  restartPolicy: Never
Let's apply above yaml:
$ kubectl apply -f pod.yaml
pod/ashok-config-pod created
We can check the status of the pod:
$ kubectl get po
NAME               READY   STATUS      RESTARTS   AGE
ashok-config-pod   0/1     Completed   0          48s
We can see the status is  completed for our pod. To check our configs passed as environment variables will be visible in the logs.
$ kubectl logs ashok-config-pod
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
LOG_LEVEL=INFO                         <--- Our config from the ConfigMap Resource
HOSTNAME=ashok-config-pod
SHLVL=1
HOME=/root
ASHOK_SVC_SERVICE_HOST=10.106.81.159 
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
ASHOK_SVC_PORT=tcp://10.106.81.159:80
ASHOK_SVC_SERVICE_PORT=80
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
ASHOK_SVC_PORT_80_TCP_ADDR=10.106.81.159
SPECIAL_LEVEL_KEY=very                 <--- Our config from the ConfigMap Resource
ASHOK_SVC_PORT_80_TCP_PORT=80
ASHOK_SVC_PORT_80_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
ASHOK_SVC_PORT_80_TCP=tcp://10.106.81.159:80
We will use another pod yaml to see the example.property.file :
apiVersion: v1
kind: Pod
metadata:
  name: ashok-config-pod
spec:
  containers:
    - name: ashok-config-pod
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # Provide the name of the ConfigMap containing the files you want
        # to add to the container
        name: special-config
  restartPolicy: Never
Similarly you can check the configs in logs.

Happy Coding !!

Create PersistentVolume, PersistentVolume on Kubernetes

Introduction

I will show how to create PersistentVolume and PersistentVolumeClaim on Kubernetes. I am using Minikube for our learning.

We will first create an  index.html file for our Nginx pod and then use this file in our PersistentVolume and PersistentVolumeClaim for storage in our Pod.

Storage

You can login to Minikube using:

$ minikube ssh
Last login: Sun Oct 10 01:14:15 2021 from 192.168.49.1
docker@minikube:~$
We will now create index.html file at /mnt/data/index.html :
$ sudo mkdir /mnt/data
$ sudo sh -c "echo 'Hello from PVC example' > /mnt/data/index.html"

PersistentVolume

We can create a PersistentVolume using yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: ashok-pv-vol
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

Apply the PV:

$ kubectl apply -f pv.yaml
persistentvolume/ashok-pv-vol created

We can check the status of the PersistentVolume:

$ kubectl get pv
NAME           CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
ashok-pv-vol   20Gi       RWO            Retain           Available           manual                  4s

PersistentVolumeClaim

We will create an PersistentVolumeClaim using yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ashok-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

PersistentVolumeClaim can be created as below:

$ kubectl apply -f pvc.yaml
persistentvolumeclaim/ashok-pv-claim created

We can check the status of the PersistentVolumeClaim:

$ kubectl get pvc
NAME             STATUS   VOLUME         CAPACITY   ACCESS MODES   STORAGECLASS   AGE
ashok-pv-claim   Bound    ashok-pv-vol   20Gi       RWO            manual         8s

The persistent volume status is now changed to bound :

$ kubectl get pv
NAME           CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                    STORAGECLASS   REASON   AGE
ashok-pv-vol   20Gi       RWO            Retain           Bound    default/ashok-pv-claim   manual                  2m31s

Let's create a pod for using the static volume.

Using PVC in Pod


We will create a pod using yaml:
apiVersion: v1
kind: Pod
metadata:
  name: ashok-pv-pod
spec:
  volumes:
    - name: ashok-pv-storage
      persistentVolumeClaim:
        claimName: ashok-pv-claim
  containers:
    - name: ashok-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: ashok-pv-storage

Let's create pod:
$ kubectl apply -f pv-pod.yaml
pod/ashok-pv-pod created
We will check the status of the pod:
$ kubectl get po
NAME           READY   STATUS    RESTARTS   AGE
ashok-pv-pod   1/1     Running   0          79s
To make this pod accessible from browser, you can use  kubectl port-forward :
$ kubectl port-forward nginx 8888:80
Forwarding from 127.0.0.1:8888 -> 80
Forwarding from [::1]:8888 -> 80

Open the browser  http://localhost:8888/ :

Clean Up

We can delete the pod, pvc and pv using below commands:

$ kubectl delete po ashok-pv-pod
pod "ashok-pv-pod" deleted
$ kubectl delete pvc ashok-pv-claim
persistentvolumeclaim "ashok-pv-claim" deleted
$ kubectl delete pv ashok-pv-vol
persistentvolume "ashok-pv-vol" deleted

Let me know if you need any help.

Happy Coding !!!