Kubernetes Pods and Kubectl Commands

Working with Pods and Kubectl Commands Contents

  1. Creating Your First Pod with Kubectl
  2. Basic Kubectl Commands
  3. Inspecting Kubernetes Objects Using Kubectl
  4. Imperative vs Declarative Commands
  5. YAML Syntax
  6. Name and Metadata
  7. Labels and Label Selectors
  8. Annotations
  9. Kubernetes Namespace

Working with Kubernetes Pods

A Kubernetes Pod is a group of one or more containers, tied together for the purposes of administration and networking.

Listing Kubernetes Objects

To get a list of existing Kubernetes objects, including Deployments, ReplicaSets, Pods, and Services, use the following command: sh

kubectl get all

Creating a [Naked] Pod

To create a new Pod named mynginx with an NGINX image, use: sh

kubectl run mynginx –image nginx –port 80 Note that mynginx is the name of the pod.

Pod States

Pod states typically transition through the following stages:

  • Pending
  • Creating
  • Running

Viewing an Object

There are several commands for printing information about an object:

  • get: Prints basic information about matching objects. Use kubectl get -h to see a list of options.
  • describe: Prints detailed aggregated information about matching objects.
  • logs: Prints the stdout and stderr for a container running in a Pod. Getting the List of All Pods To list all pods, use: sh

kubectl get pods

For more detailed information including the internal IP assigned to the pod and nominated node, use: sh

kubectl get pod/mynginx -o wide

kubectl get pod mynginx -o wide

Viewing Log of a Particular Pod

To view the logs of a specific container within a pod: sh

kubectl logs mynginx -c <container-name> Getting the Current State of the Pods To get detailed information about the current state of a pod, similar to docker inspect, use: sh

kubectl describe pod/mynginx

Accessing the NGINX Server in Container

To start a new shell process in the Kubernetes pod container and access the NGINX server: sh

kubectl exec -it mynginx -c <container-name> — sh # curl localhost Accessing the NGINX Server Using the Pod’s IP Address From the container of another pod: sh

kubectl get pods -o wide # note the IP address of the pod kubectl run -it myclient –image=ubuntu — bash # apt update # apt install curl # curl http://<IP-of-pod> Accessing the NGINX Server from the Host Machine To forward a port from the host machine to a pod: sh

kubectl port-forward pod/mynginx 8080:80 In a new terminal window: sh

curl http://localhost:8080

Deleting a Pod

To delete a pod: sh

kubectl delete pod/mynginx

Declarative Commands

Imperative

  • Involves using verb-based commands like kubectl run, kubectl create, kubectl expose, kubectl delete, kubectl scale, and kubectl edit.
  • Suitable for testing and interactive experimentation.

Declarative

  • Objects are written in YAML files and deployed using kubectl create or kubectl apply.
  • Best suited for production environments.

YAML Document Example

yaml

apiVersion: v1

kind: Pod

metadata:

name: mynginx

labels:

app: web

spec:

containers:

name: nginx-con

image: nginx ports:

  • containerPort: 80
  • Required Fields in YAML
  • In the YAML file for the Kubernetes object you want to create, set values for the following fields (also known as root keys):
  • apiVersion: Which version of the Kubernetes API you’re using to create this object.
  • kind: What kind of object you want to create.
  • metadata: Data that helps uniquely identify the object, including a name string, UID, and optional namespace.
  • spec: The desired state for the object.
  • To apply a configuration from a YAML file: sh

kubectl apply -f pod.yaml

Advantages of Using YAML for Kubernetes Definitions

  • Convenience: No need to add all of your parameters to the command line.
  • Maintenance: YAML files can be added to source control, so you can track changes.
  • Flexibility: You can create much more complex structures using YAML than you can on the command line.

Getting YAML from Existing Objects

To get the detailed live configuration of an existing object in YAML format: sh

kubectl get pod/mynginx -o yaml

“Mastering Kubernetes is a journey, not a destination. Every command you run and every YAML file you write is a step towards becoming a true cloud-native engineer. Embrace the learning process and watch your skills transform.”

Caution: Please do not click links or open attachments unless you recognize the sender and know the content is safe.

-Thank you, happy coding !!

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *