当前位置:网站首页>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
边栏推荐
- 论文的设计方案咋写?
- Bigder: how to deal with the bugs found in the 32/100 test if they are not bugs
- Where can I check the foreign literature of economics?
- Monitor container runtime tool Falco
- Where can I find the English literature of the thesis (except HowNet)?
- Angled detection frame | calibrated depth feature for target detection (with implementation source code)
- [shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)
- What are the recommended thesis translation software?
- 1380. Lucky numbers in the matrix
- Form form instantiation
猜你喜欢

Many to one, one to many processing
![[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)](/img/c5/2f65d37682607aab98443d7f1ba775.jpg)
[shutter] shutter photo wall (center component | wrap component | clickrrect component | stack component | positioned component | button combination component)

sysdig分析容器系统调用

Interface difference test - diffy tool

Is the multitasking loss in pytoch added up or backward separately?

Digital collection trading website domestic digital collection trading platform
![[target detection] r-cnn, fast r-cnn, fast r-cnn learning](/img/f0/df285f01ffadff62eb3dcb92f2e04f.jpg)
[target detection] r-cnn, fast r-cnn, fast r-cnn learning
![洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表](/img/4a/ab732c41ea8a939fa0983fec475622.png)
洛谷_P1149 [NOIP2008 提高组] 火柴棒等式_枚举打表

What are the projects of metauniverse and what are the companies of metauniverse

Explain in detail the process of realizing Chinese text classification by CNN
随机推荐
毕业总结
Go自定义排序
Question e: merged fruit -noip2004tgt2
What are the recommended thesis translation software?
Go custom sort
Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
TypeError: Cannot read properties of undefined (reading ***)
Custom throttling function six steps to deal with complex requirements
95 pages of smart education solutions 2022
Implement the foreach method of array
sysdig分析容器系统调用
Angled detection frame | calibrated depth feature for target detection (with implementation source code)
Define MySQL function to realize multi module call
Pytorch里面多任务Loss是加起来还是分别backward?
秒杀系统设计
Pageoffice - bug modification journey
MFC gets the current time
S12. Verify multi host SSH mutual access script based on key
What is the standard format of a 2000-3000 word essay for college students' classroom homework?
MySQL advanced learning notes (III)