编写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的各个版本信息:
[root@k8s-master ~]# kubectl api-versions
获取字段设置帮助文档
[root@k8s-master ~]# 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:
[root@k8s-master namespaces]# kubectl apply -f dream21thnamespaces.yml
namespace/dream21th created
[root@k8s-master 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:
[root@k8s-master 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
上传到服务器,执行下面指令:
[root@k8s-master pod]# kubectl apply -f dream21thtomcatpod.yml
pod/dream21th-tomcat-pod created
[root@k8s-master pod]# kubectl get pod
NAME READY STATUS RESTARTS AGE
dream21th-tomcat-pod 1/1 Running 0 7s
[root@k8s-master 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>
[root@k8s-master pod]# curl 10.244.1.8:8080
镜像下载策略、重启策略:
imagePullPolicy:
Always:总是拉取 pull
IfNotPresent:如果本地有镜像,使用本地,如果本地没有镜像,下载镜像。
Never:只使用本地镜像,从不拉取
restartPolicy:
Always:只要退出就重启。
OnFailure:失败退出时(exit code不为0)才重启
Never:永远不重启
删除上面部署的pod:
[root@k8s-master pod]# kubectl delete -f dream21thtomcatpod.yml
pod "dream21th-tomcat-pod" deleted
[root@k8s-master 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
上传到服务器,执行下面指令:
[root@k8s-master deployment]# kubectl apply -f dream21thtomcatdeployment.yml
deployment.apps/dream21th-tomcat-deployment created
[root@k8s-master 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
[root@k8s-master 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>
[root@k8s-master deployment]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
dream21th-tomcat-deployment 3/3 3 3 28s
[root@k8s-master 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一致。
可以通过下面的指令进行验证:
[root@k8s-master deployment]# curl 10.244.2.5:8080
可以通过下面的指令删除上面部署的deployment:
[root@k8s-master deployment]# kubectl delete -f dream21thtomcatdeployment.yml
deployment.apps "dream21th-tomcat-deployment" deleted
[root@k8s-master 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
执行下面指令,并验证:
[root@k8s-master service]# kubectl apply -f dream21thtomcatservice.yml
deployment.apps/dream21th-tomcat-deployment created
service/dream21th-tomcat-service created
[root@k8s-master 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
[root@k8s-master service]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
dream21th-tomcat-deployment 3/3 3 3 18s
[root@k8s-master 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>
[root@k8s-master 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。
[root@k8s-master service]# kubectl delete -f dream21thtomcatservice.yml
deployment.apps "dream21th-tomcat-deployment" deleted
service "dream21th-tomcat-service" deleted