当前位置:网站首页>kubernetes编写yml简单入门
kubernetes编写yml简单入门
2022-07-02 23:09:00 【dream21st】
编写kubernetes的yml资源清单有5个顶级的字段组成:apiVersion、kind、metadata、spec、status。
# 如果没有给定 group 名称,那么默认为 core,可以使用 kubectl apiversions # 获取当前 k8s 版本上所有的 apiVersion 版本信息( 每个版本可能不 同 )
apiVersion: group/apiversion
#资源类别
kind:
#资源元数据
metadata:
name
namespace
lables
annotations # 主要目的是方便用户阅读查找
# 期望的状态(disired state)
spec:
# 当前状态,本字段有 Kubernetes 自身维护,用户不能去定义
status:
使用kubectl命令可以查看apiVersion的各个版本信息:
[[email protected] ~]# kubectl api-versions
获取字段设置帮助文档
[[email protected] ~]# kubectl explain pod
资源清单中大致可以分为如下几种类型:
apiVersion <string> #表示字符串类型
metadata <Object> #表示需要嵌套多层字段
labels <map[string]string> #表示由k:v组成的映射
finalizers <[]string> #表示字串列表
ownerReferences <[]Object> #表示对象列表
hostPID <boolean> #布尔类型
priority <integer> #整型
name <string> -required- #如果类型后面接 -required-,表示为必填字段
1 创建nameSpace
编写一个yml文件,文件的名称为dream21thnamespaces.yml,文件的内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dream21th
上传到服务器,执行下面指令,可以看到创建的名称为dream21th的namespace:
[[email protected] namespaces]# kubectl apply -f dream21thnamespaces.yml
namespace/dream21th created
[[email protected] namespaces]# kubectl get ns
NAME STATUS AGE
default Active 119d
dream21th Active 7s
kube-node-lease Active 119d
kube-public Active 119d
kube-system Active 119d
执行下面指令删除dream21th的namespace:
[[email protected] namespaces]# kubectl delete -f dream21thnamespaces.yml
namespace "dream21th" deleted
2 创建pod
编写一个yml文件,文件的名称为dream21thtomcatpod.yml,文件的内容如下:
apiVersion: v1
kind: Pod
metadata:
name: dream21th-tomcat-pod
labels:
app: dream21th-tomcat-pod
spec:
containers:
- name: dream21th-tomcat-pod
image: tomcat:9.0.20-jre8-alpine
imagePullPolicy: IfNotPresent
restartPolicy: Always
上传到服务器,执行下面指令:
[[email protected] pod]# kubectl apply -f dream21thtomcatpod.yml
pod/dream21th-tomcat-pod created
[[email protected] pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dream21th-tomcat-pod 1/1 Running 0 7s
[[email protected] pod]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dream21th-tomcat-pod 1/1 Running 0 14s 10.244.1.8 k8s-work1 <none> <none>
[[email protected] pod]# curl 10.244.1.8:8080
镜像下载策略、重启策略:
imagePullPolicy:
Always:总是拉取 pull
IfNotPresent:如果本地有镜像,使用本地,如果本地没有镜像,下载镜像。
Never:只使用本地镜像,从不拉取
restartPolicy:
Always:只要退出就重启。
OnFailure:失败退出时(exit code不为0)才重启
Never:永远不重启
删除上面部署的pod:
[[email protected] pod]# kubectl delete -f dream21thtomcatpod.yml
pod "dream21th-tomcat-pod" deleted
[[email protected] pod]# kubectl get pod
No resources found in default namespace.
3 创建deployment
编写一个yml文件,文件的名称为dream21thtomcatdeployment.yml,文件的内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dream21th-tomcat-deployment
labels:
app: dream21th-tomcat-deployment
spec:
replicas: 3
template:
metadata:
name: dream21th-tomcat-deployment
labels:
app: dream21th-tomcat
spec:
containers:
- name: dream21th-tomcat-deployment
image: tomcat:9.0.20-jre8-alpine
imagePullPolicy: IfNotPresent
restartPolicy: Always
selector:
matchLabels:
app: dream21th-tomcat
上传到服务器,执行下面指令:
[[email protected] deployment]# kubectl apply -f dream21thtomcatdeployment.yml
deployment.apps/dream21th-tomcat-deployment created
[[email protected] deployment]# kubectl get pods
NAME READY STATUS RESTARTS AGE
dream21th-tomcat-deployment-5fc846c9bc-5rvkq 1/1 Running 0 10s
dream21th-tomcat-deployment-5fc846c9bc-j95mc 1/1 Running 0 10s
dream21th-tomcat-deployment-5fc846c9bc-ks88d 1/1 Running 0 10s
[[email protected] deployment]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dream21th-tomcat-deployment-5fc846c9bc-5rvkq 1/1 Running 0 18s 10.244.2.5 k8s-work2 <none> <none>
dream21th-tomcat-deployment-5fc846c9bc-j95mc 1/1 Running 0 18s 10.244.1.10 k8s-work1 <none> <none>
dream21th-tomcat-deployment-5fc846c9bc-ks88d 1/1 Running 0 18s 10.244.1.9 k8s-work1 <none> <none>
[[email protected] deployment]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
dream21th-tomcat-deployment 3/3 3 3 28s
[[email protected] deployment]# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
dream21th-tomcat-deployment 3/3 3 3 33s dream21th-tomcat-deployment tomcat:9.0.20-jre8-alpine app=dream21th-tomcat
注意:在Deployment中必须写matchLables;selector.matchLabels.app的值要和template.metadata.labels.app一致。
可以通过下面的指令进行验证:
[[email protected] deployment]# curl 10.244.2.5:8080
可以通过下面的指令删除上面部署的deployment:
[[email protected] deployment]# kubectl delete -f dream21thtomcatdeployment.yml
deployment.apps "dream21th-tomcat-deployment" deleted
[[email protected] deployment]# kubectl get pods
No resources found in default namespace.
4 创建service
编写一个yml文件,文件的名称为dream21thtomcatservice.yml,文件的内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: dream21th-tomcat-deployment
labels:
app: dream21th-tomcat-deployment
spec:
replicas: 3
template:
metadata:
name: dream21th-tomcat-deployment
labels:
app: dream21th-tomcat
spec:
containers:
- name: dream21th-tomcat-deployment
image: tomcat:9.0.20-jre8-alpine
imagePullPolicy: IfNotPresent
restartPolicy: Always
selector:
matchLabels:
app: dream21th-tomcat
---
apiVersion: v1
kind: Service
metadata:
name: dream21th-tomcat-service
spec:
selector:
app: dream21th-tomcat
ports:
- port: 8888
targetPort: 8080
nodePort: 30089
protocol: TCP
type: NodePort
执行下面指令,并验证:
[[email protected] service]# kubectl apply -f dream21thtomcatservice.yml
deployment.apps/dream21th-tomcat-deployment created
service/dream21th-tomcat-service created
[[email protected] service]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dream21th-tomcat-service NodePort 10.96.13.168 <none> 8888:30089/TCP 7s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 119d
[[email protected] service]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
dream21th-tomcat-deployment 3/3 3 3 18s
[[email protected] service]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dream21th-tomcat-deployment-5fc846c9bc-5jhd9 1/1 Running 0 24s 10.244.2.6 k8s-work2 <none> <none>
dream21th-tomcat-deployment-5fc846c9bc-p26kj 1/1 Running 0 24s 10.244.1.12 k8s-work1 <none> <none>
dream21th-tomcat-deployment-5fc846c9bc-xpnd5 1/1 Running 0 24s 10.244.1.11 k8s-work1 <none> <none>
[[email protected] service]# curl 192.168.43.103:30089
Service参数和Service类型:
port :访问service使用的端口
targetPort :Pod中容器端口
NodePort: 通过Node实现外网用户访问k8s集群内service(30000-32767)
ClusterIP:默认,分配一个集群内部可以访问的虚拟IP
NodePort:在每个Node上分配一个端口作为外部访问入口
LoadBalancer:工作在特定的Cloud Provider上,例如Google Cloud,AWS,OpenStack
ExternalName:表示把集群外部的服务引入到集群内部中来,即实现了集群内部pod和集群外部的服务 进行通信
执行下面的指令删除上面部署的svc和deployment。
[[email protected] service]# kubectl delete -f dream21thtomcatservice.yml
deployment.apps "dream21th-tomcat-deployment" deleted
service "dream21th-tomcat-service" deleted
边栏推荐
- Multiprocess programming (4): shared memory
- pod生命周期详解
- Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
- 写论文可以去哪些网站搜索参考文献?
- 程序分析与优化 - 9 附录 XLA的缓冲区指派
- 布隆过滤器
- JSON转换工具类
- Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
- 在线预览Word文档
- Many to one, one to many processing
猜你喜欢
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
JS interviewer wants to know how much you understand call, apply, bind no regrets series
[shutter] shutter open source project reference
Multiprocess programming (II): Pipeline
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
Explain in detail the significance of the contour topology matrix obtained by using the contour detection function findcontours() of OpenCV, and how to draw the contour topology map with the contour t
Architecture: load balancing
redis21道经典面试题,极限拉扯面试官
95 pages of smart education solutions 2022
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
随机推荐
Explain in detail the process of realizing Chinese text classification by CNN
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
Andorid 获取系统标题栏高度
How to write the design scheme of the thesis?
Chapter 4 of getting started with MySQL: data types stored in data tables
Nc50528 sliding window
pageoffice-之bug修改之旅
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
Multiprocess programming (V): semaphores
NC50528 滑动窗口
FRP reverse proxy +msf get shell
AcWing_188. 武士风度的牛_bfs
Slf4j + logback logging framework
多进程编程(二):管道
Digital twin visualization solution digital twin visualization 3D platform
论文的英文文献在哪找(除了知网)?
maya渔屋建模
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
Multiprocess programming (4): shared memory
Confluence的PDF导出中文文档异常显示问题解决