Previous We talked about the use of container Docker to create and publish a bulletin board application, which uses a Kubernetes orchestration tools to manage container. Kubernetes in a scalable, networking, security and maintenance of the application container provides many tools interface, which docker has been far beyond the capabilities of the container itself.
To test our applications on Kubernetes is operating correctly, we will use the built-in Kubernetes Docker Desktop environment on the development machine to deploy our application, which is then delivered to the production of complete Kubernetes cluster operation. Docker Desktop features in Kubernetes and production Kubernetes cluster is the same, so, even in the development environment, your application can also enjoy all the features of the production Kubernetes cluster.
To create an application using YAML
1, create a file named bb.yaml, and shown in Figure 1.1.
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
Figure 1.1
Kubernetes the above two objects with YAML file structure, "---" as a separator, if only a YAML file object, the separator may be omitted.
Deployment object description to create a pod group, the number of copies of the pod receptacle is a group, then based on the image bulletinboard: 1.0 to create.
Service对象描述了创建一个NodePort类型的服务,该服务用于将流量从宿主机上的30001端口路由到pods内容器的8080端口,允许您从宿主机的IP:30001访问公告板应用程序。
Kubernetes YAML一开始看起来很长很复杂,但它几乎总是遵循相同的模式
(1)、apiVersion用于指定Kubernetes API的版本
(2)、kind用于指定资源类型,可以是Deployment、Service、Namespace、ConfigMap、ServiceAccount等。
(3)、metadata,指定Pod的元数据信息,包括name、namespace、labels等。
(4)、spec,指定容器container、存储storage、卷volume以及其他kubernetes对象所需的配置。
发布并测试您的应用
1、在bb.yaml所在目录,执行如下命令。
# kubectl apply -f bb.yaml
当您看到如下输出,则表示应用发布成功。
deployment.apps/bb-demo created
service/bb-entrypoint created
2、执行如下命令,确保部署正常。
# kubectl get deployments
如果输出信息如下,则表示运行正常。
NAME READY UP-TO-DATE AVAILABLE AGE
bb-demo 1/1 1 1 80m
3、执行如下命令查看service服务信息
# kubectl get services
输出信息如下
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
bb-entrypoint NodePort 10.99.53.144 <none> 8080:30001/TCP 20s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 24h
See service service bb-entrypoint we publish, provide external access to port 30001, NodePort default port range: 30000-32767, can kube-apiserver command (the command apiserver container) that output
--service-node-port-range portRange
A port range to reserve for services with NodePort visibility. Example: '30000-32767'. Inclusive at both ends of the range. (default 30000-32767)
4, through http: // localhost: 30001 to access your application, you will see the following interface, as shown in Figure 1.2, it means that application deployment is successful, the next step is to test, build, release, sharing process.
Figure 1.2
5, delete the application, you can make the following command
# Kubectl delete -F bb.yaml
or
# kubectl delete deploy bb-demo
# kubectl delete service bb-entrypoint
As shown in Figure 1.3.
Figure 1.3
So far, we have successfully used Docker Desktop to deploy our application to the development of the machine Kubernetes environment. We have not done too much work on Kubernetes, but now the door has been opened, you can start adding other components in your application, and use all the features of Kubernetes. In addition, we should also strengthen the YAML file learning to understand.
Note: YAML file can be generated based on an existing template deploy running, pod, service, and then be modified based on this template.