Subscribe Us


Breaking

Recent In Voip

Popular

Comments

Recent

Kubernetes Examples (Config Maps & Cluster IP)

 


 Config Maps 

1. Creating Configmap from "multiple files" & Consuming it inside Pod from "volumes" 


   1a. Create Configmap "nginx-configmap-vol" from "multiple files"

   1b. Consume "nginx-configmap-vol" configmap inside Pod from "volumes" 

   1c. Create | Display | Validate


2. Creating Configmap from "literal values" & Consuming it inside Pod from "environment variables" 


   2a. Create configmap ìredis-configmap-envî from "literal values"

   2b. Consume ìredis-configmap-envî configmap inside pod from ìEnvironment Variablesî inside pod

   2c. Create | Display | Validate


3. Cleanup


   3a. Delete configmaps

   3b. Delete pods

   3c. Validate


*************************************************************************************************************************************************


1. Creating Configmap from "multiple files" & Consuming it inside Pod from "volumes" 



1a.  Create Configmap "nginx-configmap-vol" from "multiple files":

------------------------------------------------------------------

echo -n 'Non-sensitive data inside file-1' > file-1.txt

echo -n 'Non-sensitive data inside file-2' > file-2.txt


kubectl create configmap nginx-configmap-vol --from-file=file-1.txt --from-file=file-2.txt

# rm -f file-1 file-2


kubectl get configmaps

kubectl describe configmaps nginx-configmap-vol


==========================================================


1b.  Consume above "nginx-configmap-vol" configmap inside Pod from "volumes" 

---------------------------------------------------------------------------


#nginx-pod-configmap-vol.yaml

apiVersion: v1

kind: Pod

metadata:

  name: nginx-pod-configmap-vol

spec:

  containers:

  - name: nginx-container

    image: nginx

    volumeMounts:

    - name: test-vol

      mountPath: "/etc/non-sensitive-data"

      readOnly: true

  volumes:

    - name: test-vol

      configMap:

        name: nginx-configmap-vol

        items:

        - key: file-1.txt

          path: file-a.txt

        - key: file-2.txt

          path: file-b.txt




==========================================================


1c. Create | Display | Validate:

--------------------------------


# Create

kubectl create -f nginx-pod-configmap-vol.yaml


# Display

kubectl get po

kubectl get configmaps

kubectl describe pod nginx-pod-configmap-vol


# Validate from "inside" the pod

kubectl exec nginx-pod-configmap-vol -it /bin/sh

cd /etc/non-sensitive-data

ls 

cat Non-sensitive data inside file-1.txt

cat password.txt

exit


(OR)


# Validate from "outside" the pod

kubectl exec nginx-pod-configmap-vol ls /etc/non-sensitive-data

kubectl exec nginx-pod-configmap-vol cat /etc/non-sensitive-data/file-a.txt

kubectl exec nginx-pod-configmap-vol cat /etc/non-sensitive-data/file-b.txt



*************************************************************************************************************************************************


2. Creating Configmap from "literal values" & Consuming it inside Pod from "environment variables"  



2a.  Create configmap ìredis-configmap-envî from "literal values"

-----------------------------------------------------------------


kubectl create configmap redis-configmap-env --from-literal=file.1=file.a --from-literal=file.2=file.b


kubectl get configmap

kubectl describe configmap redis-configmap-env


===============================================================================


2b. Consume ìredis-configmap-envî configmap inside pod from ìEnvironment Variablesî inside pod

-----------------------------------------------------------------------------------------------


# redis-pod-configmap-env.yaml

apiVersion: v1

kind: Pod

metadata:

  name: redis-pod-configmap-env

spec:

  containers:

  - name: redis-container

    image: redis

    env:

      - name: FILE_1

        valueFrom:

          configMapKeyRef:

            name: redis-configmap-env

            key: file.1

      - name: FILE_2

        valueFrom:

          configMapKeyRef:

            name: redis-configmap-env

            key: file.2

  restartPolicy: Never


===============================================================================


2c. Create | Display | Validate:


# Create

kubectl create -f  redis-pod-configmap-env.yaml


# Display

kubectl get pods

kubectl get configmaps

kubectl describe pod redis-pod-configmap-env



# Validate from "inside" the pod

kubectl exec redis-pod-configmap-env -it /bin/sh

env | grep  FILE

exit


(OR)


# Validate from "outside" the pod

kubectl exec redis-pod-configmap-env env | grep FILE



*********************************************************************************** 


3. Cleanup


# Delete configmaps

kubectl delete configmaps nginx-configmap-vol redis-configmap-env


# Delete pods

kubectl delete pods nginx-pod-configmap-vol redis-pod-configmap-env


# Validate

kubectl get pods

kubectl get configmaps



*********************************************************************************** 


Step-1: Set up a "Redis master"


1a. Create redis-master "deployment"

1b. Create redis-master "service"


--------------------------------------------


Step-2: Set up a "Redis slave"

2a. Create redis-master "deployment"

2b. Create redis-master "slave"


--------------------------------------------


Step 3: Set up the "guestbook web frontend"

3a. Create guestbook web frontend "deployment"

3c. Expose frontend on an external IP address (LoadBalancer)


*************************************************************************************************************************************************


Step-1: Set up a "Redis master"


a. Create redis-master "deployment"


# redis-master-deployment.yaml

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

  name: redis-master

spec:

  replicas: 1

  template:

    metadata:

      labels:

        app: redis

        role: master

        tier: backend

    spec:

      containers:

      - name: master

        image: k8s.gcr.io/redis:e2e  # or just image: redis

        resources:

          requests:

            cpu: 100m

            memory: 100Mi

        ports:

        - containerPort: 6379


-------------------------------------


b. Create redis-master "service"


# redis-master-service.yaml

apiVersion: v1

kind: Service

metadata:

  name: redis-master

  labels:

    app: redis

    role: master

    tier: backend

spec:

  type: ClusterIP

  ports:

  - port: 6379

    targetPort: 6379

  selector:

    app: redis

    role: master

    tier: backend


-------------------------------------


kubectl create -f redis-master-deployment.yaml

kubectl create -f redis-master-service.yaml

kubectl get deploy

kubectl get svc

kubectl get pods

kubectl get logs -f [pod-name]



*************************************************************************************************************************************************


Step 2: Set up Redis "slaves"


2a. Create redis-slave "deployment"


# redis-slave-deployment.yaml

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

  name: redis-slave

spec:

  replicas: 2

  template:

    metadata:

      labels:

        app: redis

        role: slave

        tier: backend

    spec:

      containers:

      - name: slave

        image: gcr.io/google_samples/gb-redisslave:v1

        resources:

          requests:

            cpu: 100m

            memory: 100Mi

        env:

        - name: GET_HOSTS_FROM

          value: dns

          # If your cluster config does not include a dns service, then to

          # instead access an environment variable to find the master

          # service's host, comment out the 'value: dns' line above, and

          # uncomment the line below:

          # value: env

        ports:

        - containerPort: 6379



-----------------------------------------------


2b. Create redis-slave "service"


# redis-slave-service.yaml

apiVersion: v1

kind: Service

metadata:

  name: redis-slave

  labels:

    app: redis

    role: slave

    tier: backend

spec:

  ports:

  - port: 6379

  selector:

    app: redis

    role: slave

    tier: backend


--------------------------------------------------


kubectl create -f redis-slave-deployment.yaml

kubectl create -f redis-slave-service.yaml

kubectl get deploy

kubectl get svc

kubectl get pods



*************************************************************************************************************************************************


Step 3: Set up the guestbook web frontend


3a. Create fronend "deployment"


# frontend-deployment.yaml

apiVersion: extensions/v1beta1

kind: Deployment

metadata:

  name: frontend

spec:

  replicas: 2

  template:

    metadata:

      labels:

        app: guestbook

        tier: frontend

    spec:

      containers:

      - name: php-redis

        image: gcr.io/google-samples/gb-frontend:v4

        resources:

          requests:

            cpu: 100m

            memory: 100Mi

        env:

        - name: GET_HOSTS_FROM

          value: dns

          # If your cluster config does not include a dns service, then to

          # instead access environment variables to find service host

          # info, comment out the 'value: dns' line above, and uncomment the

          # line below:

          # value: env

        ports:

        - containerPort: 80


--------------------------------------------------


3b. Expose frontend on an external IP address


# frontend-service.yaml

apiVersion: v1

kind: Service

metadata:

  name: frontend

  labels:

    app: guestbook

    tier: frontend

spec:

  # if your cluster supports it, uncomment the following to automatically create

  # an external load-balanced IP for the frontend service.

  type: LoadBalancer

  ports:

  - port: 80

  selector:

    app: guestbook

    tier: frontend


----------------------------------------------


Create & display "frontend" deployment and service


kubectl create -f frontend-deployment.yaml

kubectl create -f frontend-service.yaml

kubectl get deploy

kubectl get svc

kubectl get pods

kubectl get service frontend


[Web-browser] - http://[LB-IP]



*******************************************************************


4. Cleanup:


# Delete redis-master

kubectl delete -f redis-master-deployment.yaml

kubectl delete -f redis-master-service.yaml


# Delete redis-slave

kubectl delete -f redis-slave-deployment.yaml

kubectl delete -f redis-slave-service.yaml


# Delete frontend-app

kubectl delete -f frontend-deployment.yaml

kubectl delete -f frontend-service.yaml


# Display

kubectl get deploy

kubectl get svc

kubectl get pods


*******************************************************************

 

0 on: "Kubernetes Examples (Config Maps & Cluster IP)"