Basic Exercises ( YAML )

References:

https://www.mirantis.com/blog/introduction-to-yaml-creating-a-kubernetes-deployment/

http://containertutorials.com/get_started_kubernetes/k8s_example.html

https://kubernetes.io/docs/concepts/workloads/controllers/deployment/

replica

https://www.mirantis.com/blog/kubernetes-replication-controller-replica-set-and-deployments-understanding-replication-options/

Example 1: Launch the student app(REST API, 5000 Port) in Pod(Single Instance) and expose the PORT to outside.

image: sureshkvl/test:1-2

Create a Pod:

  1. Create a yaml file for pod, as below
cloud@mkube:~/test$ cat test.pod.yml 
apiVersion: v1
kind: Pod
metadata:
  name: hello
  labels:
    app: hello
spec:
  containers:
    -
      image: sureshkvl/test:1-2
      name: hello1
      ports:
        - 
          containerPort: 5000   
cloud@mkube:~/test$

Note: I have the image test:1-2 available in dockerhub sureshkvl repository.

labels - app:hello will be used as "service selection" in service resource to expose the ports.

2.create a pod using this yaml file,

kubectl apply -f <filename>

cloud@mkube:~/test$ kubectl apply -f test.pod.yml 
W1024 08:28:42.655192   25145 factory_object_mapping.go:423] Failed to download OpenAPI (the server could not find the requested resource), falling back to swagger
pod "hello" created
cloud@mkube:~/test$ kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
hello     1/1       Running   0          10s
cloud@mkube:~/test$

Now pod is up and running, the port(containerPort: 5000) need to exposed to access the app from outside. For that we need to create a service.

Creat Service:

  1. Create a service yaml file as below
cloud@mkube:~/test$ cat test.service.yml 
apiVersion: v1
kind: Service
metadata:
  name: hello1
spec:
  type: NodePort
  selector:
    app: hello
  ports:
    -
      name: test
      port: 5000
      nodePort: 32002
cloud@mkube:~/test$

Here, selector: "app:hello" is referring the POD labels. "nodePort" is the port used for communicating from outside.

  1. Create a service using this yaml file
cloud@mkube:~/test$ kubectl apply -f test.service.yml 
W1024 08:36:36.425718   25190 factory_object_mapping.go:423] Failed to download OpenAPI (the server could not find the requested resource), falling back to swagger
service "hello1" created
cloud@mkube:~/test$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
hello1       NodePort    10.0.0.2     <none>        5000:32002/TCP   6s
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP          7d
cloud@mkube:~/test$

Now, we can access our app using port 32002 with node ip.

In my setup minikube ip.

cloud@mkube:~/test$ minikube ip
There is a newer version of minikube available (v0.22.3).  Download it here:
https://github.com/kubernetes/minikube/releases/tag/v0.22.3

To disable this notification, run the following:
minikube config set WantUpdateNotification false
192.168.42.19
cloud@mkube:~/test$ curl 192.168.42.19:32002/students                                                                     
{}
cloud@mkube:~/test$

Example 2: Launch the student app(REST API, 5000 Port) with loadbalancing and high availability, and expose the PORT to outside.

This can be achieved with replication controller or deployment.

Replication Controller

cloud@mkube:~/test1$ cat test.rc.yml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: hello
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      -
        image: sureshkvl/test:1-2
        name: hello1
        ports:
          - 
            containerPort: 5000   
cloud@mkube:~/test1$

Apply this rc yaml file.

cloud@mkube:~/test1$ kubectl apply -f test.rc.yml                              
W1024 13:01:49.097064   25512 factory_object_mapping.go:423] Failed to download OpenAPI (the server could not find the requeste
d resource), falling back to swagger
replicationcontroller "hello" created
cloud@mkube:~/test1$ clear
cloud@mkube:~/test1$ kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
hello     3         3         3         13s
cloud@mkube:~/test1$ kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
hello-cvxkf   1/1       Running   0          16s
hello-lcnjl   1/1       Running   0          16s
hello-whx6k   1/1       Running   0          16s
cloud@mkube:~/test1$

Create a service for this rc

cloud@mkube:~/test1$ cat test.service.yml 
apiVersion: v1
kind: Service
metadata:
  name: hello1
spec:
  type: NodePort
  selector:
    app: hello
  ports:
    -
      name: test
      port: 5000
      nodePort: 32002
cloud@mkube:~/test1$
cloud@mkube:~/test1$ kubectl apply -f test.service.yml 
W1024 13:16:18.744864   25613 factory_object_mapping.go:423] Failed to download OpenAPI (the server could not find the requeste
d resource), falling back to swagger
service "hello1" created
cloud@mkube:~/test1$ kubectl get services 
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
hello1       NodePort    10.0.0.237   <none>        5000:32002/TCP   6s
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP          7d
cloud@mkube:~/test1$ minikube ip
192.168.42.19

cloud@mkube:~/test1$ curl 192.168.42.19:32002/students
{}

Removing the RC:

cloud@mkube:~/test1$ kubectl get rc
NAME      DESIRED   CURRENT   READY     AGE
hello     3         3         3         22m

cloud@mkube:~/test1$ kubectl delete rc hello
replicationcontroller "hello" deleted
cloud@mkube:~/test1$ kubectl get pods
No resources found.
cloud@mkube:~/test1$ kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
hello1       NodePort    10.0.0.237   <none>        5000:32002/TCP   8m
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP          7d
cloud@mkube:~/test1$ kubectl delete service hello1                                                                             
service "hello1" deleted
cloud@mkube:~/test1$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP   7d
cloud@mkube:~/test1$

Using Deployment models:

yaml file:

cloud@mkube:~/kubernetes-examples/kubernets-deployments$ cat sms/deployment/sms-deployment.yaml                                apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: app
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
        - name: app
          image: sureshkvl/test:1-2
          ports:
            - containerPort: 5000
cloud@mkube:~/kubernetes-examples/kubernets-deployments$
cloud@mkube:~/kubernetes-examples/kubernets-deployments/sms/deployment$ kubectl apply -f ./sms-deployment.yaml 
W1024 14:30:16.064895   26957 factory_object_mapping.go:423] Failed to download OpenAPI (the server could not find the requested resource), falling back to swagger
deployment "app" created
cloud@mkube:~/kubernetes-examples/kubernets-deployments/sms/deployment$ kubectl apply -f ./sms-service.yaml 
W1024 14:30:20.394325   26968 factory_object_mapping.go:423] Failed to download OpenAPI (the server could not find the requested resource), falling back to swagger
service "hello1" created
cloud@mkube:~/kubernetes-examples/kubernets-deployments/sms/deployment$ kubectl get deployment 
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
app       3         3         3            3           10s
cloud@mkube:~/kubernetes-examples/kubernets-deployments/sms/deployment$ kubectl get pod
NAME                   READY     STATUS    RESTARTS   AGE
app-2474755846-69jrh   1/1       Running   0          13s
app-2474755846-k206x   1/1       Running   0          13s
app-2474755846-xc5wk   1/1       Running   0          13s
cloud@mkube:~/kubernetes-examples/kubernets-deployments/sms/deployment$ kubectl get service
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
hello1       NodePort    10.0.0.243   <none>        5000:32002/TCP   16s
kubernetes   ClusterIP   10.0.0.1     <none>        443/TCP          8d
cloud@mkube:~/kubernetes-examples/kubernets-deployments/sms/deployment$

results matching ""

    No results matching ""