当前位置:网站首页>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
边栏推荐
- Basic 10 of C language: array and pointer
- NC50528 滑动窗口
- Blue decides red - burst CS teamserver password
- Is the multitasking loss in pytoch added up or backward separately?
- [reading notes] phased summary of writing reading notes
- Form form instantiation
- 监控容器运行时工具Falco
- Hit the industry directly! The propeller launched the industry's first model selection tool
- Multi process programming (III): message queue
- MATLAB signal processing [Q & a notes-1]
猜你喜欢

Bloom filter

Cmake basic use

Chapter 3 of getting started with MySQL: database creation and operation

Digital twin smart factory develops digital twin factory solutions

Which software can translate an English paper in its entirety?

Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)

Interpretation of new plug-ins | how to enhance authentication capability with forward auth

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs

Interface difference test - diffy tool
随机推荐
请问大家在什么网站上能查到英文文献?
95 pages of smart education solutions 2022
JSON转换工具类
Multiprocess programming (I): basic concepts
Realization of mask recognition based on OpenCV
[reading notes] phased summary of writing reading notes
Additional: token; (don't read until you finish writing...)
Custom throttling function six steps to deal with complex requirements
Pat 1030 travel plan (30 points) (unfinished)
pod生命周期详解
Architecture: database architecture design
Unique line of "Gelu"
NC24840 [USACO 2009 Mar S]Look Up
Understanding and application of least square method
pageoffice-之bug修改之旅
【单片机项目实训】八路抢答器
maya渔屋建模
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
helm 基础学习
Where can I find foreign papers?