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.
List Pods:
bashkubectl get pods
Describe a Pod:
bashkubectl describe pod <pod-name>
Create a Pod:
my-pod.yaml
and then apply it:yamlapiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
Apply the configuration:bashkubectl apply -f my-pod.yaml
Update a Pod:
yamlapiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx:latest
Apply the changes:bashkubectl apply -f my-pod.yaml
Delete a Pod:
bashkubectl delete pod <pod-name>
List Services:
bashkubectl get services
Expose a Deployment:
yamlapiVersion: 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:bashkubectl apply -f my-deployment.yaml
Scale a Deployment:
bashkubectl scale deployment my-deployment --replicas=5
Rolling Update:
bashkubectl set image deployment/my-deployment nginx=nginx:1.17 --record
Namespace Operations:
bashkubectl create namespace my-namespace
bashkubectl 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.