k8s由于资源比较多,组合起来参数众多,不适合用cli传参的形式。 因此用yaml文件的形式传参给k8s。 yaml文件相当于剧本,运维人员相当于制片人,k8s相当于导演,docker相当于剧务、pod详单于演员

pod的资源清单格式:

pod.yaml

apiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: default
labels:
app: myapp
spec:
containers:
- name: myapp-1
image: busybox:latest
- name: myapp-2
image: busybox:latest
command:
- "/bin/sh"
- "-c"
- "sleep 10"

创建资源:

kubectl apply -f pod.yaml

deployments的资源清单格式:

apiVersion: # 可以通过cli获取:kubectl  api-resources |grep [deployments|svc|pods]
kind: Deployment # 资源类型:Deployment|Pods|Ingress|Services|...
metadata: # 资源元数据
name: my-nginx # 资源名称
namespace: default
labels:
app: my-nginx
spec:
selector: # 标签选择器
matchLabels:
app: my-nginx # 选择带有my-nginx标签的资源
replicas: 2 # 副本数
template: # 副本模板。模板的内容(除了不需要apiVersion、kind之外,其它子字段都需要定义)
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: nginx
subPath: nginx.conf
volumes:
- name: nginx
configMap:
name: nginx-configmap
items:
- key: nginx_conf
path: nginx.conf

service的yaml清单格式:

apiVersion: networking.k8s.io/v1
kinf: Ingress
metadata:
name: ingress-entry
spec:
backend:
serviceName: nginx-service
servicePort: 80

---
apiVersion: v1
kind: Service
metadata:
name: nginx-service #定义service名称为nginx-service
labels:
app: nginx-service #为service打上app标签
spec:
type: NodePort #使用NodePort方式开通,在每个Node上分配一个端口作为外部访问入口
selector:
app: my-nginx
ports:
- port: 8000 #port是k8s集群内部访问service的端口,即通过clusterIP: port可以访问到某个service
targetPort: 80 #targetPort是pod的端口,从port和nodePort来的流量经过kube-proxy流入到后端pod的targetPort上,最后进入容器
nodePort: 32500 #nodePort是外部访问k8s集群中service的端口,通过nodeIP: nodePort可以从外部访问到某个service

configmap的资源清单格式:

apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-configmap
data:
nginx_conf: |-
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}