Prerequisites
- Download and install Docker Desktop, see the introduction and installation for details .
- Complete the containerization of the application in the second part .
- Make sure Kubernetes is enabled in your Docker Desktop:
- Mac : Click the Docker icon in the menu bar, navigate to Preferences , and make sure there is a green light next to "Kubernetes".
- Windows : Click the Docker icon in the system tray, navigate to Settings , and make sure there is a green light next to "Kubernetes".
If Kubernetes is not running, follow the instructions in the Orchestration overview of this tutorial to complete the setup.
Introduction
Now that we have demonstrated that the various components of the application run as independent containers, we can now arrange for them to be managed by an orchestrator like Kubernetes. Kubernetes provides many tools for scaling, networking, securing, and maintaining your containerized applications that are beyond the capabilities of the container itself.
In order to verify whether our containerized application can work well on Kubernetes, we will use the built-in Kubernetes environment of Docker Desktop on the development machine to deploy our application, and then hand it over to a complete Kubernetes cluster in the production environment Run on. The Kubernetes environment created by Docker Desktop is fully functional, which means that it has all the Kubernetes features that your application will enjoy on a real cluster, and can be easily accessed through your development machine.
Describe the application using Kubernetes YAML
All containers in Kubernetes are arranged as pods , which are groups of containers in the same location that share some resources. In addition, in actual applications, we almost never create individual pods; instead, most of our workloads are scheduled as deployments , which are scalable groups of pods that are automatically maintained by Kubernetes. Finally, all Kubernetes objects can and should be described in a manifest called a Kubernetes YAML file. These YAML files describe all the components and configurations of a Kubernetes application and can be used to easily create and destroy applications in any Kubernetes environment.
-
You have written a very basic Kubernetes YAML file in the Orchestration overview section of this tutorial . Now, let's write a slightly more complicated YAML file to run and manage our bulletin board application. The following content into a named
bb.yaml
file:apiVersion: apps/v1 kind: Deployment metadata: name: bb-demo namespace: default spec: replicas: 1 selector: matchLabels: bb: web template: metadata: labels: bb: web spec: containers: - name: bb-site image: bulletinboard:1.0 --- apiVersion: v1 kind: Service metadata: name: bb-entrypoint namespace: default spec: type: NodePort selector: bb: web ports: - port: 8080 targetPort: 8080 nodePort: 30001
In this Kubernetes YAML file, there are two objects to
---
separate:- One
部署(Deployment)
, describing a group of identical pods that can be scaled. In this case, you will get a副本(replica)
, i.e. copy of your pod, and the pod (in thetemplate:
description of the key) only one container, the container based on the present course of the previous step inbulletinboard:1.0
the mirror. - A
NodePort
service that will flow from the 30001 port forwarding on your host to the 8080 port in which it is routed to the pods, allowing you to reach your network from a bulletin board application.
Also, please note that although Kubernetes YAML may seem long and complicated at first, it almost always follows the same pattern:
apiVersion
, Indicating the Kubernetes API that parses the objectkind
To indicate what type of object it ismetadata
To apply things like names to objectsspec
, Specify all the parameters and configurations of the object
- One
Deploy and check the application
-
In the terminal, navigate to your created
bb.yaml
position, and deploy the application to Kubernetes:kubectl apply -f bb.yaml
You will see the output shown below, indicating that the Kubernetes object has been successfully created:
deployment.apps/bb-demo created service/bb-entrypoint created
-
Make sure everything is in order by listing the deployment:
kubectl get deployments
If all goes well, your deployment should be listed as follows:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE bb-demo 1 1 1 1 48s
This means that "all" of the pods you requested in YAML are up and running. Do the same check for your service:
kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE bb-entrypoint NodePort 10.106.145.116 <none> 8080:30001/TCP 53s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 138d
In addition to the default
kubernetes
out of the service, we also see thebb-entrypoint
service that accepts traffic on port 30001 / TCP. -
Open your browser and visit your bulletin board
localhost:30001
; you will see your bulletin board, just like when we run it as a standalone container in the second part of the Docker quickstart . -
Once satisfied, remove your application:
kubectl delete -f bb.yaml
in conclusion
So far, we have successfully deployed our application to a fully functional Kubernetes environment on the development machine using Docker Desktop. We have not done much work on Kubernetes, but now the door has been opened;
we have not done much work on Kubernetes, but the door has been opened; you can start adding other components to your application and take advantage of All the features and functions of Kubernetes are on your own machine.
In addition to deploying to Kubernetes, we also describe the application as a Kubernetes YAML file. This simple text file contains everything we need to create our application in the running state. We can check it into version control and share it with colleagues, allowing us to easily distribute the application to other clusters (such as test and production clusters that may appear after the development environment).
Kubernetes references
For further documentation of all the new Kubernetes objects used in this article, please see:
Kubernetes Chinese document: https://kubernetes.io/zh/docs/home/
Author: Docker's official website
translator: Technical Zemin
Publisher: Technical Verses
links: English text