当前位置:网站首页>Kubernetes simple introduction to writing YML
Kubernetes simple introduction to writing YML
2022-07-03 00:28:00 【dream21st】
To write kubernetes Of yml Resource list has 5 Top level fields :apiVersion、kind、metadata、spec、status.
# If there is no given group name , Then the default is core, have access to kubectl apiversions # Get current k8s All on the version apiVersion Version information ( Each version may not Same as )
apiVersion: group/apiversion
# Resource categories
kind:
# Resource metadata
metadata:
name
namespace
lables
annotations # The main purpose is to facilitate users to read and find
# Desired state (disired state)
spec:
# current state , This field has Kubernetes Self maintenance , Users cannot define
status:
Use kubectl Commands can be viewed apiVersion Version information of :
[[email protected] ~]# kubectl api-versions
Get the field setting help document
[[email protected] ~]# kubectl explain pod
The resource list can be roughly divided into the following types :
apiVersion <string> # Represents string type
metadata <Object> # Indicates that you need to nest multiple fields
labels <map[string]string> # By k:v The mapping of components
finalizers <[]string> # Represents a list of strings
ownerReferences <[]Object> # Represents a list of objects
hostPID <boolean> # Boolean type
priority <integer> # integer
name <string> -required- # If the type is followed by -required-, Indicates a required field
1 establish nameSpace
Write a yml file , The name of the document is dream21thnamespaces.yml, The contents of the document are as follows :
apiVersion: v1
kind: Namespace
metadata:
name: dream21th
Upload to server , Execute the following instructions , You can see that the created name is dream21th Of 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
Execute the following instructions to delete dream21th Of namespace:
[[email protected] namespaces]# kubectl delete -f dream21thnamespaces.yml
namespace "dream21th" deleted
2 establish pod
Write a yml file , The name of the document is dream21thtomcatpod.yml, The contents of the document are as follows :
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
Upload to server , Execute the following instructions :
[[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
Image Download Strategy 、 Restart strategy :
imagePullPolicy:
Always: Always pull pull
IfNotPresent: If there is a mirror image locally , Use local , If there is no image locally , Download mirroring .
Never: Use only local images , Never pull
restartPolicy:
Always: Just exit and restart .
OnFailure: When you fail to exit (exit code Not for 0) To restart
Never: Never restart
Delete the deployed above 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 establish deployment
Write a yml file , The name of the document is dream21thtomcatdeployment.yml, The contents of the document are as follows :
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
Upload to server , Execute the following instructions :
[[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
Be careful : stay Deployment You have to write matchLables;selector.matchLabels.app The value of and template.metadata.labels.app Agreement .
It can be verified by the following instructions :
[[email protected] deployment]# curl 10.244.2.5:8080
You can delete the deployed above through the following instructions 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 establish service
Write a yml file , The name of the document is dream21thtomcatservice.yml, The contents of the document are as follows :
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
Execute the following instructions , And verify :
[[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 Parameters and Service type :
port : visit service Port used
targetPort :Pod Container port in
NodePort: adopt Node Realize the access of external network users k8s Within cluster service(30000-32767)
ClusterIP: Default , Assign a virtual machine that can be accessed within the cluster IP
NodePort: At every Node Assign a port on as an external access entry
LoadBalancer: Work in a particular Cloud Provider On , for example Google Cloud,AWS,OpenStack
ExternalName: It refers to the introduction of services outside the cluster into the cluster , That is to realize the cluster inside pod And services outside the cluster communicate
Execute the following instructions to delete the deployed svc and deployment.
[[email protected] service]# kubectl delete -f dream21thtomcatservice.yml
deployment.apps "dream21th-tomcat-deployment" deleted
service "dream21th-tomcat-service" deleted
边栏推荐
猜你喜欢
setInterval定时器在ie不生效原因之一:回调的是箭头函数
DotNet圈里一个优秀的ORM——FreeSql
MySQL advanced learning notes (III)
MySQL 23道经典面试吊打面试官
CMake基本使用
UART、RS232、RS485、I2C和SPI的介绍
Monitor container runtime tool Falco
写论文可以去哪些网站搜索参考文献?
[shutter] Introduction to the official example of shutter Gallery (project introduction | engineering construction)
Automated defect analysis in electronic microscopic images
随机推荐
Chinatelecom has maintained a strong momentum in the mobile phone user market, but China Mobile has opened a new track
多进程编程(二):管道
[MCU project training] eight way answering machine
Nc20806 District interval
Bigder:32/100 测试发现的bug开发认为不是bug怎么处理
What is the standard format of a 2000-3000 word essay for college students' classroom homework?
Luogu_ P1149 [noip2008 improvement group] matchstick equation_ Enumeration and tabulation
国外的论文在那找?
AcWing_188. 武士风度的牛_bfs
Seckill system design
Slf4j + logback logging framework
TypeError: Cannot read properties of undefined (reading ***)
Array de duplication
NC50965 Largest Rectangle in a Histogram
What are the recommended thesis translation software?
Andorid 获取系统标题栏高度
百数不断创新,打造自由的低代码办公工具
Form form instantiation
Don't want teachers to see themselves with cameras in online classes? Virtual camera you deserve!
About the practice topic of screen related to unity screen, unity moves around a certain point inside