close

Kubectl Example For Getting Resources Information And Imperative/Declarative Deploy Application

        Application And Pod Deployment In Kubernetes Cluster:

1/Using kubectl command:

– Primary CLI tool for controlling K8s cluster. Use for all operations (Create, update, query,…) any resource in cluster by directly interacting with API server on master node.

Example 1: Get all pod running with status for all namespaces

# kubectl get pods –all-namespaces

Example 2: Show logs of running container inside of a pod for troubleshooting

# kubectl logs –n <namespace> <container name>

– kubectl can used with option –o to specify format of output:

            +wide: output to stdout

            +json: output formatted with json

            +yaml: output formatted with yaml

*Show logs for troubleshooting kubelet service:

# journalctl -r -u kubelet

– Get all resources of a namespace (pods, services, replicaset,..)

# kubectl get all –all-namespaces

– Show details information of node:

# kubectl descirbe node <node name>

2/Application deployment in k8s:

– Deploy application in k8s cluster has 2 method:

+Imperative: To execute one operation on one object at a time (like run container by using docker run)

Ex: To deploy application nginx run command:

# kubectl create deployment nginx –image=nginx

+Declarative:

– Define components of application which need deployment as state in code.

– Using manifest file to specify configuration/state of each component for running up (like run multiple applications/containers by using docker-compose up).

– Manifest files can be created using yaml or json formatted file.

# kubectl apply –f deployment.yaml

Example 01: Imperative deploy application Hello Kubernetes using image download from registry: gcr.io/google-samples/node-hello:1.0 and exposed port for running container is 8080

– Create service object expose for this pod (using for external client access)

– Show detail information of this service. Endpoints and NodePort is public port using for external client access.

– Browsing to public IP of node running this pod with NodePort is 31324

Example 02: Declarative deploy apache web container

– Prepare deployment file with YAML formatted for apache application

apiVersion: v1

kind: Pod

metadata:

  name: apache-example

  labels:

    run: apache-example

spec:

  containers:

    – name: apache-ex

      image: httpd:2.4

      ports:

       – containerPort: 80

         protocol: TCP

– Check syntax of deployment file with option –dry-run=client and –validate when run kubectl apply

– Deployment pod using this manifest file

– Create service object expose for this pod (using for external client access)

– Show detail information of this service. Endpoints and NodePort is public port using for external client access.

– Browsing to default apache page by using public IP of node which run this pod with NodePort is 30496

– Copy new index page to apache root directory of container in pod

Tags : AutomationContainerDevOpsDockerKubernetesLinux-Unix

Leave a Response

error: Content is protected !!