Understanding the Kubernetes API with examples

Category : Kubernetes | Sub Category : Learn Kubernetes | By Prasad Bonam Last updated: 2023-11-22 07:28:20 Viewed : 216


Lets go through some practical examples of using the Kubernetes API with the kubectl command-line tool. Note that these examples assume you have kubectl configured to connect to a Kubernetes cluster.

  1. List Pods:

    • Use the following command to list all the pods in the default namespace:
      bash
      kubectl get pods
  2. Describe a Pod:

    • Get detailed information about a specific pod, including its configuration and events:
      bash
      kubectl describe pod <pod-name>
  3. Create a Pod:

    • Create a simple pod using a YAML definition. Save the following content in a file named my-pod.yaml and then apply it:
      yaml
      apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: nginx
      Apply the configuration:
      bash
      kubectl apply -f my-pod.yaml
  4. Update a Pod:

    • Modify the pod definition and apply the changes:
      yaml
      apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - name: my-container image: nginx:latest
      Apply the changes:
      bash
      kubectl apply -f my-pod.yaml
  5. Delete a Pod:

    • Delete a pod using its name:
      bash
      kubectl delete pod <pod-name>
  6. List Services:

    • List all the services in the default namespace:
      bash
      kubectl get services
  7. Expose a Deployment:

    • Create a simple deployment and expose it as a service:
      yaml
      apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:latest --- apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80
      Apply the configuration:
      bash
      kubectl apply -f my-deployment.yaml
  8. Scale a Deployment:

    • Scale the deployment to 5 replicas:
      bash
      kubectl scale deployment my-deployment --replicas=5
  9. Rolling Update:

    • Perform a rolling update of a deployment with a new image:
      bash
      kubectl set image deployment/my-deployment nginx=nginx:1.17 --record
  10. Namespace Operations:

    • Create a new namespace:
      bash
      kubectl create namespace my-namespace
    • List pods in a specific namespace:
      bash
      kubectl get pods --namespace=my-namespace

These examples demonstrate basic operations using the Kubernetes API with kubectl. You can adapt and extend these commands for more complex scenarios and configurations as needed. Understanding these operations is crucial for managing and deploying applications on a Kubernetes cluster.

Search
Related Articles

Leave a Comment: