Deploying Applications with Kubernetes
Creating a Kubernetes Deployment
To deploy an application with Kubernetes Deployment, you need to create a Deployment YAML file. Here's an example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: labex/my-app:v1
ports:
- containerPort: 80
This Deployment will create three replicas of the labex/my-app:v1
container, and expose port 80.
Deploying the Application
To deploy the application, save the YAML file and run the following command:
kubectl apply -f deployment.yaml
This will create the Deployment and the associated ReplicaSet and Pods.
Verifying the Deployment
You can verify the Deployment status using the following commands:
kubectl get deployments
kubectl get replicasets
kubectl get pods
This will show the current state of the Deployment, ReplicaSet, and Pods.
Scaling the Deployment
To scale the Deployment, you can update the replicas
field in the YAML file and apply the changes:
spec:
replicas: 5
Then, run kubectl apply -f deployment.yaml
to update the Deployment.
Updating the Application
To update the application, you can change the container image in the Deployment YAML file and apply the changes:
spec:
template:
spec:
containers:
- name: my-app
image: labex/my-app:v2
Then, run kubectl apply -f deployment.yaml
to update the Deployment. Kubernetes will perform a rolling update, replacing the old Pods with the new Pods.