Kubernetes (often abbreviated as k8s) is an open-source container orchestration platform designed by Google and donated to the Cloud Native Computing Foundation (CNCF). It aims to provide a standardized container deployment process that makes deploying, scaling, and managing applications easier and more efficient. Kubernetes automates and standardizes the deployment, scaling, and management of containerized applications. It allows you to run and manage containerized applications across a cluster without needing to worry about the specific servers they run on. Below, Brother V will introduce the basic operational steps of Kubernetes to help beginners get started quickly.
Kubernetes Configuration
Kubernetes configurations are typically defined using YAML files, which describe the desired state of resources. For example, a simple Deployment configuration might look like this:
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: myapp:1.0.0
ports:
- containerPort: 8080
Kubernetes Deployment
When deploying applications, you can use the kubectl command-line tool or the Kubernetes API. For example, you can deploy the Deployment above using the following command:
kubectl apply -f my-app-deployment.yaml
Suppose you have a web application that you need to deploy to Kubernetes. These are the steps you would typically follow:
-
Containerize your application: First, you need to containerize your application and create a Docker image.
-
Write a Deployment configuration: Next, you write a Deployment YAML file that specifies the number of container replicas to run and the container image to use.
-
Deploy your application: Use the
kubectl apply -f deployment.yamlcommand to deploy your application to Kubernetes. -
Create a Service: To allow external access to your application, you create a Service that routes traffic to the Pods in your Deployment.
-
Scale your application: If traffic increases, you can scale your application by modifying the number of replicas in your Deployment.
-
Logging and monitoring: You configure log collection and monitoring tools to track your application's performance and logs.
-
Update your application: When you need to update your application, you update the Docker image and update the Deployment to use the new image, and Kubernetes will smoothly replace the old Pods.
This is an Example
Below is a simplified case study demonstrating how to deploy a simple web application to a Kubernetes cluster. This case will cover the entire process from containerizing the application to deploying and scaling it on Kubernetes.
Step 1: Prepare a Kubernetes Cluster
Ensure you have a running Kubernetes cluster. You can use Minikube to create a single-node Kubernetes cluster on your local machine, or use a managed Kubernetes service provided by a cloud provider, such as Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), etc.
Step 2: Write the Dockerfile
Assuming your web application is already ready, you need to create a Docker image for it. Create a file named Dockerfile and add the following content (using a simple Node.js application as an example):
FROM node:14Create working directory
WORKDIR /usr/src/app
Install application dependencies
COPY package*.json ./
RUN npm install
Copy application source code
COPY . .
Expose container port
EXPOSE 8080
Run the application
CMD [“node”, “server.js”]
Step 3: Build the Docker Image
In the directory containing the Dockerfile, run the following command to build your Docker image:
docker build -t my-web-app:1.0 .
Ensure Docker is running, and that you have successfully built the image.
Step 4: Push the Docker Image to a Container Registry
Push the image to Docker Hub or another container registry so that Kubernetes can pull it:
docker login
docker push username/my-web-app:1.0
Replace username with your Docker Hub username.
Step 5: Write the Kubernetes Deployment Configuration
Create a file named web-app-deployment.yaml and add the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-app
image: username/my-web-app:1.0
ports:
- containerPort: 8080
Replace username with your Docker Hub username.
Step 6: Create the Deployment
Use the kubectl command-line tool to apply the Deployment configuration to your Kubernetes cluster:
kubectl apply -f web-app-deployment.yaml
Step 7: Create a Service
To access your web application from outside the cluster, you need to create a Service:
apiVersion: v1
kind: Service
metadata:
name: web-app-service
spec:
selector:
app: web-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Apply this Service configuration:
kubectl apply -f web-app-service.yaml
If you are using Minikube, you can use the minikube service command to access the service:
minikube service web-app-service
Step 8: Scale the Deployment
If your application traffic increases and you need to scale the number of instances, you can edit the Deployment and update the number of replicas:
kubectl scale deployment web-app-deployment --replicas=5
Step 9: Check Application Status
You can use the following commands to check the status of your Pods and Services:
kubectl get pods
kubectl get services
Step 10: Logging and Monitoring
To debug and monitor your application, you can view the logs of a Pod:
kubectl logs -f pod-name
Replace pod-name with the name of your Pod.
Step 11: Update Your Application
If you need to update your application, you can build a new Docker image, push it to the registry, then update your Deployment to use the new image:
kubectl set image deployment/web-app-deployment web-app=username/my-web-app:1.1
Replace 1.1 with your new image tag.
By following these steps, beginners can experience the entire process of containerizing an application, pushing it to a container registry, and deploying, scaling, and updating it on Kubernetes. This provides a great starting point for further learning and exploration of Kubernetes.
This is a discussion topic separated from the original post at https://juejin.cn/post/7368665381731352591