Thursday, 30 July 2020

Kubernetes: Create a service using yaml file

This is a continuation to my previous post. In my previous post, I explained how to expose a service using

 

Step 1: Create a deployment using yml file.

 

empService.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 1
  selector:
    matchLabels:
      app: employee-service

Step 2: Create a deployment by executing the below command.

kubectl create -f empService.yml

$kubectl create -f empService.yml 
deployment.apps/employee-service-deployment created

Step 3: Create yml definition file to create a service.

 

createServiceToEmpService.yml

apiVersion: v1
kind: Service
metadata:
  name: emp-serv
spec:
  selector:
    app: employee-service
  ports:
  - port: 8080
    name: whatever
  type: NodePort

You can get the selector details from the below command.

kubectl describe deployment employee-service-deployment

 

Create service by executing the below command.

$kubectl create -f createServiceToEmpService.yml 
service/emp-serv created

Query all the services.

$kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
emp-serv     NodePort    10.102.82.169   <none>        8080:32765/TCP   18s
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          31d
Step 4: Get the Kubernetes URL for the specified service in your local cluster.
$minikube service emp-serv
|-----------|----------|---------------|-----------------------------|
| NAMESPACE |   NAME   |  TARGET PORT  |             URL             |
|-----------|----------|---------------|-----------------------------|
| default   | emp-serv | whatever/8080 | http://192.168.99.100:32765 |
|-----------|----------|---------------|-----------------------------|
🎉  Opening service default/emp-serv in default browser...

You will see WildFly server home page in the browser.


You can delete the service by executing the below command.

kubectl delete service emp-serv

$kubectl delete service emp-serv
service "emp-serv" deleted
$
$kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   31d

You can delete the deployment by executing the below command.

$kubectl delete deployment employee-service-deployment
deployment.apps "employee-service-deployment" deleted
$
$kubectl get deployments
No resources found in default namespace.




Previous                                                    Next                                                    Home

Tuesday, 28 July 2020

Kubernetes: Expose service using NodePort type

In this post, I am going to explain how to expose a Pod using

 

NodePort service type

It is a very basic way to expose a service. NodePort service opens a specific port on all the nodes and any request comes to this port will be forwarded to actual service.

 

Step 1:  Define a deployment definition file.

 

nodePortDemo.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notification-service-deployment
  labels:
    app: notification-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: notification-service
      labels:
        app: notification-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: notification-service-container
        image: jboss/wildfly

  replicas: 1
  selector:
    matchLabels:
      app: notification-service

Step 2: Create a deployment by executing the below command.

$kubectl create -f nodePortDemo.yml 
deployment.apps/notification-service-deployment created

Step 3: Expose the

kubectl expose deployment notification-service-deployment --port=8080 --type=NodePort

$kubectl expose deployment notification-service-deployment --port=8080 --type=NodePort
service/notification-service-deployment exposed

Step 4: Query all the services by executing the below commands.

$kubectl get svc
NAME                              TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes                        ClusterIP   10.96.0.1      <none>        443/TCP          31d
notification-service-deployment   NodePort    10.102.16.86   <none>        8080:30054/TCP   11s

Step 5: Execute below command to open wildfly application in the browser.

minikube service notification-service-deployment

$minikube service notification-service-deployment
|-----------|---------------------------------|-------------|-----------------------------|
| NAMESPACE |              NAME               | TARGET PORT |             URL             |
|-----------|---------------------------------|-------------|-----------------------------|
| default   | notification-service-deployment |        8080 | http://192.168.99.100:30054 |
|-----------|---------------------------------|-------------|-----------------------------|
🎉  Opening service default/notification-service-deployment in default browser...



Previous                                                    Next                                                    Home

Kubernetes: Services

Services make deployments accessible from the outside world by providing a single IP and Port combination.

 

A service defines a logical set of Pods and a policy by which we access the Pods.

 

How the service target Pods to expose?

Service use selector to target the Pods to expose

 

Will one service provide access to multiple deployments?

Yes, Services are independent of deployments. One service can provide access to multiple deployments. Services watch deployments, if the deployment matches to the service selector, then service exposes this deployment.

 

Service Types

ClusterIP: It is the default service type. Used to provide internal access.

 

NodePort: It allocates Specific Node port for the communication with Pod.

 

LoadBalancer: This service type is implemented in Public Cloud.

 

ExternalName: It will work on DNS names.

 

ServiceWithoutSelector: Used for direct connection based on IP/Port.

 



Previous                                                    Next                                                    Home

Kubernetes: Scheduling Pods to specific nodes

Sometimes we may need to run the Pods on nodes of specific type.

 

For example, you may want to run your Pod on a Node with high GPU. You can specify node requirements in a manifest file using node labels and Kubernetes will figure out the node that matches your requirements.

 

How to attach labels to a node?

Using ‘kubectl label’ command, you can attach labels to a node.

 

Let’s get the available nodes by executing the below command.

$kubectl get nodes
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   35d   v1.18.0

Attach labels to the node ‘minikube’ by executing the below command.

$kubectl label node minikube gpu=true availability=high
node/minikube labelled

You can get labels using the option --show-labels.

$kubectl get nodes --show-labels
NAME       STATUS   ROLES    AGE   VERSION   LABELS
minikube   Ready    master   35d   v1.18.0   availability=high,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,gpu=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=minikube,kubernetes.io/os=linux,minikube.k8s.io/commit=93af9c1e43cab9618e301bc9fa720c63d5efa393,minikube.k8s.io/name=minikube,minikube.k8s.io/updated_at=2020_05_06T15_25_50_0700,minikube.k8s.io/version=v1.9.2,node-role.kubernetes.io/master=

How can we specify node requirements?

You can specify the node requirements using ‘spec.nodeSelector’.

 

For example, below snippet instruct Kubernetes to deploy the application to a node that labeled with gpu to true.

 

spec:

  nodeSelector:

    gpu: "true"

 

podWithNodeSelector.yml

apiVersion: v1
kind: Pod
metadata:
  name: employee-web-service
  labels:
    app: employee-service
    author: krishna
    serviceType: webservice
spec:
  nodeSelector:
    gpu: "true"
  containers:
    - name: employee-service-container
      image: jboss/wildfly


Previous                                                    Next                                                    Home

Kubernetes: List specific labels

In the previous post, I explained how to get labels associated with a resource using –show-labels option. If you want to print a specific label, then you can use the option -L.

 

For example, below statement print labels author and serviceType of all the pods.

 

kubectl get pods -L author,serviceType

$kubectl get pods -L author,serviceType
NAME               READY   STATUS    RESTARTS   AGE     AUTHOR    SERVICETYPE
employee-service   1/1     Running   0          3h31m   krishna   webservice




Previous                                                    Next                                                    Home

Kubernetes: --show-labels: Get labels associated with a resource

‘—show-labels’ option is used to get the labels associated with a resource.

 

For example, below statement prints the labels associated with all the pods.

 

kubectl get pods --show-labels

$kubectl get pods --show-labels
NAME               READY   STATUS    RESTARTS   AGE     LABELS
employee-service   1/1     Running   0          3h29m   app=employee-service,author=krishna,serviceType=webservice




Previous                                                    Next                                                    Home

Monday, 27 July 2020

Kubernetes: Labels, Selectors, Annotations and Namespaces

These are used to organize our application.

 

Lables

Labels are key:value pairs. These can be attached to objects like pods, services, deployments. Labels are mainly used to organize Kubernetes objects.

 

Why labels?

a.   Users can easily identify the objects.

b.   Used to organize clusters

 

Can I change the labels at runtime?

Labels can be added at deployment time and can be changed at any time.

 

Example

environment:prod

release:beta

service:front-end

 

For example, you have hundreds of microservices running in Kubernetes Environment. By providing labels like appType, environment you can easily organize them. Below image depicts the same.

From the above image, you can easily query for all the pods that of type ‘weservice’ and running in prod environment.


employeeServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice

........
........

In the above example, I added labels app, author, serviceType to the Deployment.

 

Selectors

Label selectors are used to identify a group of objects.

 

Types of selectors

a.   Equality Based Selector: =, !=

b.   Set Based Selector: IN, NONIN, EXISTS

 

Label Selectors are used with kubectl command.

 

Namespaces

Allow multiple virtual clusters backed by the same physical cluster. When you launch Kubernetes, a ‘default’ namespace is created. Objects are placed in ‘default’ namespace by default.

 

Annotations

Annotations provide detailed metadata in an object (For example, who created this pod etc.,). Annotations are used by tools.

 

Syntax to create annotation

kubectl annotate pod {podName} annotation1=value1

 

Can I use annotations in queries?

No, annotation is used to provide additional information.

 

Let’s create a deployment and add labels and annotations to it.

 

Step 1: Create a deployment.

 

employeeServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

As you see the definition file, I added three labels to the deployment.

  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice

Create deployment by executing the below command.

$kubectl create -f employeeServiceDeployment.yml
deployment.apps/employee-service-deployment created

You can get all the deployments by executing the below command.

$kubectl get deployments
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           29s

Step 2: Let's add a label to the deployment ‘employee-service-deployment’.

 

kubectl label deployment employee-service-deployment  env=prod version=1.24

 

Above statement adds two labels env, version to the deployment ‘employee-service-deployment’.

$kubectl label deployment employee-service-deployment  env=prod version=1.24
deployment.apps/employee-service-deployment labelled

Step 3: Let’s see the labels by executing the below command.

kubectl get deployments --show-labels

$kubectl get deployments --show-labels
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE     LABELS
employee-service-deployment   5/5     5            5           3m15s   app=employee-service-deployment,author=krishna,env=prod,serviceType=webservice,version=1.24

Step 4: Let's create other deployments to understand the usage of selectors.

 

notificationServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notification-service-deployment
  labels:
    app: notification-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: notification-service
      labels:
        app: notification-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: notification-service-container
        image: jboss/wildfly

  replicas: 10
  selector:
    matchLabels:
      app: notification-service

Create the deployment by executing the below command.

$kubectl create -f notificationServiceDeployment.yml 
deployment.apps/notification-service-deployment created

Let’s see the labels associated with all the deployments.

$kubectl get deployments --show-labels
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE     LABELS
employee-service-deployment       5/5     5            5           6m42s   app=employee-service-deployment,author=krishna,env=prod,serviceType=webservice,version=1.24
notification-service-deployment   5/10    10           5           27s     app=notification-service-deployment,author=krishna,serviceType=webservice

Step 5: Query the deployments using selectors.

 

kubectl get deployments --selector app=employee-service-deployment

kubectl get deployments --selector version=1.24

$kubectl get deployments --selector app=employee-service-deployment
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           8m51s
$
$
$kubectl get deployments --selector version=1.24 
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           9m13s

Step 6: You can even get the selector used by Kubernetes for the deployments by executing ‘kubectl describe’ command.

 

kubectl describe deployment employee-service-deployment

$kubectl describe deployment employee-service-deployment
Name:                   employee-service-deployment
Namespace:              default
CreationTimestamp:      Sun, 07 Jun 2020 15:12:19 +0530
Labels:                 app=employee-service-deployment
                        author=krishna
                        env=prod
                        serviceType=webservice
                        version=1.24
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=employee-service
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=employee-service
           author=krishna
           serviceType=webservice
  Containers:
   employee-service-container:
    Image:        jboss/wildfly
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   employee-service-deployment-dff6c9cdf (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  14m   deployment-controller  Scaled up replica set employee-service-deployment-dff6c9cdf to 5

As you see the output, you will find the below statement.

Selector:               app=employee-service

 

How to delete a label?

$kubectl get deployments --show-labels
NAME                              READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
notification-service-deployment   10/10    10           10           26s   app=notification-service-deployment,author=krishna,serviceType=webservice

Using – operator, we can delete the label.

 

For example, below statement delete the label app from the deployment.

kubectl label deployments notification-service-deployment app-

 

Let's delete the label for pods.

$kubectl get pods --show-labels
NAME                                              READY   STATUS    RESTARTS   AGE     LABELS
notification-service-deployment-7dc84f6d4-5wtsg   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-7cc8p   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-92ftm   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-9prw6   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-crknr   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-ct7bq   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-g8hc6   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-mnlb2   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-sgrm4   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice
notification-service-deployment-7dc84f6d4-tlsb4   1/1     Running   0          2m33s   app=notification-service,author=krishna,pod-template-hash=7dc84f6d4,serviceType=webservice

Below statement delete the label author from the pods.

 

kubectl label pods notification-service-deployment-7dc84f6d4-5wtsg  author-

$kubectl label pods notification-service-deployment-7dc84f6d4-5wtsg  author-
pod/notification-service-deployment-7dc84f6d4-5wtsg labeled
$
$kubectl get pods notification-service-deployment-7dc84f6d4-5wtsg --show-labels
NAME                                              READY   STATUS    RESTARTS   AGE     LABELS
notification-service-deployment-7dc84f6d4-5wtsg   1/1     Running   0          4m36s   app=notification-service,pod-template-hash=7dc84f6d4,serviceType=webservice


Previous                                                    Next                                                    Home