Friday, 7 August 2020

Kubernetes: Working with labels

 Step 1: Define .yml file with some labels.

labels-demo.yml

apiVersion: v1
kind: Pod
metadata:
  name: user-service
  labels:
    env: dev
    author: Krishna
    application_type: service
    release-version: "1.0"
spec:
  containers:
  - name: user-service
    image: jboss/wildfly

Following labels are defined in the above yml file.

env: dev

         author: Krishna

application_type: service

         release-version: "1.0"

 

Step 2: Create a resource from a file ‘labels-demo.yml’.

$kubectl create -f labels-demo.yml 
pod/user-service created

Step 3: View labels associated with pod using the below command.

kubectl get pods --show-labels

$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE    LABELS
user-service   1/1     Running   0          2m1s   application_type=service,author=Krishna,env=dev,release-version=1.0

How to add labels to the already deployed pod?

Below command add new label ‘support=true’

 

kubectl label pod/user-service support=true

$kubectl label pod/user-service support=true
pod/user-service labeled
$
$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE    LABELS
user-service   1/1     Running   0          6m7s   application_type=service,author=Krishna,env=dev,release-version=1.0,support=true


How to override existing labels?

Use --overwrite option to override existing labels.

 

For example, below statement override the author's name to chamu.

kubectl label pod/user-service author=chamu --overwrite

$kubectl label pod/user-service author=chamu --overwrite
pod/user-service labeled
$
$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE     LABELS
user-service   1/1     Running   0          7m43s   application_type=service,author=chamu,env=dev,release-version=1.0,support=true

How to delete a label?

Below statement delete the label ‘support’.

 

kubectl label pod/user-service support-

$kubectl label pod/user-service support-
pod/user-service labeled
$
$kubectl get pods --show-labels
NAME           READY   STATUS    RESTARTS   AGE   LABELS
user-service   1/1     Running   0          10m   application_type=service,author=chamu,env=dev,release-version=1.0




Previous                                                    Next                                                    Home

Kubernetes: Delete Everything from current namespace

 Execute below command to delete everything from the current namespace.

kubectl delete all --all

$kubectl delete all --all
pod "hello-node-59fddbb58-h5mgt" deleted
pod "hello-node-59fddbb58-rv8wt" deleted
pod "hello-node-59fddbb58-s2lrj" deleted
pod "hello-node-from-file-c67467bd7-8xq6d" deleted
pod "user-service-67b679f45f-bllwj" deleted
service "hello-node" deleted
service "hello-node-from-file" deleted
service "helloworld-all-service" deleted
service "kubernetes" deleted
service "user-service" deleted
deployment.apps "hello-node" deleted
deployment.apps "hello-node-from-file" deleted
deployment.apps "user-service" deleted
replicaset.apps "user-service-67b679f45f" deleted

You can confirm the same by executing ‘kubelet get *’ commands.

$kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   59s
$
$kubectl get deployments
No resources found in default namespace.
$
$kubectl get rs
No resources found in default namespace.
$
$kubectl get pods
No resources found in default namespace.





Previous                                                    Next                                                    Home

Kubernetes: list all pods, replicasets, services and deployments

Execute the command ‘kubectl get all’ to list all pods, replicasets, services and deployments.
$kubectl get all
NAME                                              READY   STATUS    RESTARTS   AGE
pod/employee-service-deployment-dff6c9cdf-2wrg9   1/1     Running   0          9m13s
pod/employee-service-deployment-dff6c9cdf-8m7cr   1/1     Running   0          9m13s
pod/employee-service-deployment-dff6c9cdf-kv9ck   1/1     Running   0          9m13s
pod/employee-service-deployment-dff6c9cdf-pbfpr   1/1     Running   0          9m13s
pod/employee-service-deployment-dff6c9cdf-s4nh4   1/1     Running   0          9m13s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   28d

NAME                                          READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/employee-service-deployment   5/5     5            5           9m13s

NAME                                                    DESIRED   CURRENT   READY   AGE
replicaset.apps/employee-service-deployment-dff6c9cdf   5         5         5       9m13s




Previous                                                    Next                                                    Home

Tuesday, 4 August 2020

Talk to Kubernetes API Server using CURL command

You can talk to Kubernetes API server in 3 ways.a

a.   Using kubectl command

b.   Kubernetes Dashboard

c.    Any rest client like Postman, curl

 

API server provides REST Apis to communicate with it. You can create Kubernetes objects like Pods, ReplicaSets, Deployments, etc., by talking to API Server.

 

 

Let’s talk to Kubernetes using curl command-line tool.

 

Step 1: In order to communicate using curl, first you need to start kubectl proxy.

 

kubectl proxy --port=1234

Above command starts a proxy on port 1234.

$kubectl proxy --port=1234
Starting to serve on 127.0.0.1:1234

You can run the proxy in background by adding & at the end of the command.

 

kubectl proxy --port=1234 &

 

Step 2: Communicate to the proxy using curl.

 

Execute the command ‘curl http://localhost:1234’ to see all the APIs provided by kubectl proxy.

$curl http://localhost:1234
{
  "paths": [
    "/api",
    "/api/v1",
    "/apis",
    "/apis/",
    "/apis/admissionregistration.k8s.io",
    "/apis/admissionregistration.k8s.io/v1",
    "/apis/admissionregistration.k8s.io/v1beta1",
    "/apis/apiextensions.k8s.io",
    "/apis/apiextensions.k8s.io/v1",
    "/apis/apiextensions.k8s.io/v1beta1",
    "/apis/apiregistration.k8s.io",
    "/apis/apiregistration.k8s.io/v1",
    "/apis/apiregistration.k8s.io/v1beta1",
    "/apis/apps",
    "/apis/apps/v1",
    "/apis/authentication.k8s.io",
    "/apis/authentication.k8s.io/v1",
    "/apis/authentication.k8s.io/v1beta1",
    "/apis/authorization.k8s.io",
    "/apis/authorization.k8s.io/v1",
    "/apis/authorization.k8s.io/v1beta1",
    "/apis/autoscaling",
    "/apis/autoscaling/v1",
    "/apis/autoscaling/v2beta1",
    "/apis/autoscaling/v2beta2",
    "/apis/batch",
    "/apis/batch/v1",
    "/apis/batch/v1beta1",
    "/apis/certificates.k8s.io",
    "/apis/certificates.k8s.io/v1beta1",
    "/apis/coordination.k8s.io",
    "/apis/coordination.k8s.io/v1",
    "/apis/coordination.k8s.io/v1beta1",
    "/apis/discovery.k8s.io",
    "/apis/discovery.k8s.io/v1beta1",
    "/apis/events.k8s.io",
    "/apis/events.k8s.io/v1beta1",
    "/apis/extensions",
    "/apis/extensions/v1beta1",
    "/apis/networking.k8s.io",
    "/apis/networking.k8s.io/v1",
    "/apis/networking.k8s.io/v1beta1",
    "/apis/node.k8s.io",
    "/apis/node.k8s.io/v1beta1",
    "/apis/policy",
    "/apis/policy/v1beta1",
    "/apis/rbac.authorization.k8s.io",
    "/apis/rbac.authorization.k8s.io/v1",
    "/apis/rbac.authorization.k8s.io/v1beta1",
    "/apis/scheduling.k8s.io",
    "/apis/scheduling.k8s.io/v1",
    "/apis/scheduling.k8s.io/v1beta1",
    "/apis/storage.k8s.io",
    "/apis/storage.k8s.io/v1",
    "/apis/storage.k8s.io/v1beta1",
    "/healthz",
    "/healthz/autoregister-completion",
    "/healthz/etcd",
    "/healthz/log",
    "/healthz/ping",
    "/healthz/poststarthook/apiservice-openapi-controller",
    "/healthz/poststarthook/apiservice-registration-controller",
    "/healthz/poststarthook/apiservice-status-available-controller",
    "/healthz/poststarthook/bootstrap-controller",
    "/healthz/poststarthook/crd-informer-synced",
    "/healthz/poststarthook/generic-apiserver-start-informers",
    "/healthz/poststarthook/kube-apiserver-autoregistration",
    "/healthz/poststarthook/rbac/bootstrap-roles",
    "/healthz/poststarthook/scheduling/bootstrap-system-priority-classes",
    "/healthz/poststarthook/start-apiextensions-controllers",
    "/healthz/poststarthook/start-apiextensions-informers",
    "/healthz/poststarthook/start-cluster-authentication-info-controller",
    "/healthz/poststarthook/start-kube-aggregator-informers",
    "/healthz/poststarthook/start-kube-apiserver-admission-initializer",
    "/livez",
    "/livez/autoregister-completion",
    "/livez/etcd",
    "/livez/log",
    "/livez/ping",
    "/livez/poststarthook/apiservice-openapi-controller",
    "/livez/poststarthook/apiservice-registration-controller",
    "/livez/poststarthook/apiservice-status-available-controller",
    "/livez/poststarthook/bootstrap-controller",
    "/livez/poststarthook/crd-informer-synced",
    "/livez/poststarthook/generic-apiserver-start-informers",
    "/livez/poststarthook/kube-apiserver-autoregistration",
    "/livez/poststarthook/rbac/bootstrap-roles",
    "/livez/poststarthook/scheduling/bootstrap-system-priority-classes",
    "/livez/poststarthook/start-apiextensions-controllers",
    "/livez/poststarthook/start-apiextensions-informers",
    "/livez/poststarthook/start-cluster-authentication-info-controller",
    "/livez/poststarthook/start-kube-aggregator-informers",
    "/livez/poststarthook/start-kube-apiserver-admission-initializer",
    "/logs",
    "/metrics",
    "/openapi/v2",
    "/readyz",
    "/readyz/autoregister-completion",
    "/readyz/etcd",
    "/readyz/log",
    "/readyz/ping",
    "/readyz/poststarthook/apiservice-openapi-controller",
    "/readyz/poststarthook/apiservice-registration-controller",
    "/readyz/poststarthook/apiservice-status-available-controller",
    "/readyz/poststarthook/bootstrap-controller",
    "/readyz/poststarthook/crd-informer-synced",
    "/readyz/poststarthook/generic-apiserver-start-informers",
    "/readyz/poststarthook/kube-apiserver-autoregistration",
    "/readyz/poststarthook/rbac/bootstrap-roles",
    "/readyz/poststarthook/scheduling/bootstrap-system-priority-classes",
    "/readyz/poststarthook/start-apiextensions-controllers",
    "/readyz/poststarthook/start-apiextensions-informers",
    "/readyz/poststarthook/start-cluster-authentication-info-controller",
    "/readyz/poststarthook/start-kube-aggregator-informers",
    "/readyz/poststarthook/start-kube-apiserver-admission-initializer",
    "/readyz/shutdown",
    "/version"
  ]
}

curl http://localhost:1234/metrics

Give you the metrics

 

curl http://localhost:1234/version

Give you the version information.

$curl http://localhost:1234/version
{
  "major": "1",
  "minor": "18",
  "gitVersion": "v1.18.0",
  "gitCommit": "9e991415386e4cf155a24b1da15becaa390438d8",
  "gitTreeState": "clean",
  "buildDate": "2020-03-25T14:50:46Z",
  "goVersion": "go1.13.8",
  "compiler": "gc",
  "platform": "linux/amd64"
}

curl http://localhost:1234/api/v1/namespaces

Give all the available namespaces.

 

curl http://localhost:1234/api/v1/namespaces/default/services

List all the services in the default namespace.

 

curl http://localhost:1234/api/v1/namespaces/default/pods

List all the available pods in the default namespace.

 

Let’s create a deployment and explore the API objects using curl tool.

 

Step 1: Create employeeServiceDeployment.yml with the below content.

 

employeeServiceDeployment.yml

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-servic

As you see above definition, it creates 5 pods with the image ‘jboss/wildfly’.

 

Step 2: Create deployment by executing below command.

 

kubectl create -f employeeServiceDeployment.yml

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

Step 3: You can query the pods information by executing below command.

curl http://localhost:1234/api/v1/namespaces/default/pods

 

I hope you got some basic idea and now you can go through Kubernetes documentation and explore the APIs.




Previous                                                    Next                                                    Home

Kubernetes: kubectl explain: Get documentation about resources

‘kubectl explain {resource}’ command is used to get documentation about the resource.

 

Example

kubectl explain Pod

kubectl explain Deployment

$kubectl explain Deployment
KIND:     Deployment
VERSION:  apps/v1

DESCRIPTION:
     Deployment enables declarative updates for Pods and ReplicaSets.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object metadata.

   spec	<Object>
     Specification of the desired behavior of the Deployment.

   status	<Object>
     Most recently observed status of the Deployment.

If you want to know about the spec property, use '.' operator like below.

 

kubectl explain Deployment.spec

$kubectl explain Deployment.spec
KIND:     Deployment
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     Specification of the desired behavior of the Deployment.

     DeploymentSpec is the specification of the desired behavior of the
     Deployment.

FIELDS:
   minReadySeconds	<integer>
     Minimum number of seconds for which a newly created pod should be ready
     without any of its container crashing, for it to be considered available.
     Defaults to 0 (pod will be considered available as soon as it is ready)

   paused	<boolean>
     Indicates that the deployment is paused.

   progressDeadlineSeconds	<integer>
     The maximum time in seconds for a deployment to make progress before it is
     considered to be failed. The deployment controller will continue to process
     failed deployments and a condition with a ProgressDeadlineExceeded reason
     will be surfaced in the deployment status. Note that progress will not be
     estimated during the time a deployment is paused. Defaults to 600s.

   replicas	<integer>
     Number of desired pods. This is a pointer to distinguish between explicit
     zero and not specified. Defaults to 1.

   revisionHistoryLimit	<integer>
     The number of old ReplicaSets to retain to allow rollback. This is a
     pointer to distinguish between explicit zero and not specified. Defaults to
     10.

   selector	<Object> -required-
     Label selector for pods. Existing ReplicaSets whose pods are selected by
     this will be the ones affected by this deployment. It must match the pod
     template's labels.

   strategy	<Object>
     The deployment strategy to use to replace existing pods with new ones.

   template	<Object> -required-
     Template describes the pods that will be created.

If you want to know about the ‘selector’ property of ‘spec’ use below command.

kubectl explain Deployment.spec.selector




Previous                                                    Next                                                    Home

Kubernetes: Print the supported API versions on the server

‘kubectl api-versions’ command print all the supported API versions on the server.

$kubectl api-versions
admissionregistration.k8s.io/v1
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
coordination.k8s.io/v1
coordination.k8s.io/v1beta1
discovery.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
networking.k8s.io/v1beta1
node.k8s.io/v1beta1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
scheduling.k8s.io/v1
scheduling.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1




Previous                                                    Next                                                    Home

Kubernetes: Print the supported API resources on the server

‘kubectl api-resources’ command print all the supported API resources on the server.

$kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED   KIND
bindings                                                                      true         Binding
componentstatuses                 cs                                          false        ComponentStatus
configmaps                        cm                                          true         ConfigMap
endpoints                         ep                                          true         Endpoints
events                            ev                                          true         Event
limitranges                       limits                                      true         LimitRange
namespaces                        ns                                          false        Namespace
nodes                             no                                          false        Node
persistentvolumeclaims            pvc                                         true         PersistentVolumeClaim
persistentvolumes                 pv                                          false        PersistentVolume
pods                              po                                          true         Pod
podtemplates                                                                  true         PodTemplate
replicationcontrollers            rc                                          true         ReplicationController
resourcequotas                    quota                                       true         ResourceQuota
secrets                                                                       true         Secret
serviceaccounts                   sa                                          true         ServiceAccount
services                          svc                                         true         Service
mutatingwebhookconfigurations                  admissionregistration.k8s.io   false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io   false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io           false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io         false        APIService
controllerrevisions                            apps                           true         ControllerRevision
daemonsets                        ds           apps                           true         DaemonSet
deployments                       deploy       apps                           true         Deployment
replicasets                       rs           apps                           true         ReplicaSet
statefulsets                      sts          apps                           true         StatefulSet
tokenreviews                                   authentication.k8s.io          false        TokenReview
localsubjectaccessreviews                      authorization.k8s.io           true         LocalSubjectAccessReview
selfsubjectaccessreviews                       authorization.k8s.io           false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io           false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io           false        SubjectAccessReview
horizontalpodautoscalers          hpa          autoscaling                    true         HorizontalPodAutoscaler
cronjobs                          cj           batch                          true         CronJob
jobs                                           batch                          true         Job
certificatesigningrequests        csr          certificates.k8s.io            false        CertificateSigningRequest
leases                                         coordination.k8s.io            true         Lease
endpointslices                                 discovery.k8s.io               true         EndpointSlice
events                            ev           events.k8s.io                  true         Event
ingresses                         ing          extensions                     true         Ingress
ingressclasses                                 networking.k8s.io              false        IngressClass
ingresses                         ing          networking.k8s.io              true         Ingress
networkpolicies                   netpol       networking.k8s.io              true         NetworkPolicy
runtimeclasses                                 node.k8s.io                    false        RuntimeClass
poddisruptionbudgets              pdb          policy                         true         PodDisruptionBudget
podsecuritypolicies               psp          policy                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io      false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io      false        ClusterRole
rolebindings                                   rbac.authorization.k8s.io      true         RoleBinding
roles                                          rbac.authorization.k8s.io      true         Role
priorityclasses                   pc           scheduling.k8s.io              false        PriorityClass
csidrivers                                     storage.k8s.io                 false        CSIDriver
csinodes                                       storage.k8s.io                 false        CSINode
storageclasses                    sc           storage.k8s.io                 false        StorageClass
volumeattachments                              storage.k8s.io                 false        VolumeAttachment





Previous                                                    Next                                                    Home