当前位置:网站首页>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
边栏推荐
- Bypass AV with golang
- Andorid 获取系统标题栏高度
- Graduation summary
- How QT exports data to PDF files (qpdfwriter User Guide)
- Improvement of RTP receiving and sending PS stream tool (II)
- 1380. Lucky numbers in the matrix
- Interpretation of new plug-ins | how to enhance authentication capability with forward auth
- Where can I find the English literature of the thesis (except HowNet)?
- pageoffice-之bug修改之旅
- MySQL advanced learning notes (III)
猜你喜欢

Architecture: load balancing

来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能

TypeError: Cannot read properties of undefined (reading ***)

MySQL advanced learning notes (III)

Many to one, one to many processing

Practical series - free commercial video material library
![MATLAB signal processing [Q & a notes-1]](/img/53/ae081820fe81ce28e1f04914678a6f.png)
MATLAB signal processing [Q & a notes-1]

Architecture: database architecture design
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

Monitor container runtime tool Falco
随机推荐
Install docker and use docker to install MySQL
What website can you find English literature on?
Nc50528 sliding window
Multiprocess programming (4): shared memory
在线预览Word文档
SQL query statement parameters are written successfully
多进程编程(二):管道
NC24840 [USACO 2009 Mar S]Look Up
Pat 1030 travel plan (30 points) (unfinished)
Architecture: database architecture design
maya渔屋建模
Multiprocess programming (II): Pipeline
NC20806 区区区间间间
开源了 | 文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
MySQL advanced learning notes (III)
多进程编程(五):信号量
helm 基础学习
MFC file operation
程序分析与优化 - 9 附录 XLA的缓冲区指派
How to specify const array in the global scope of rust- How to specify const array in global scope in Rust?