apiVersion #API对象版本,可通过`kubectl api-versions`命令查看
kind #资源类型,区分大小写,可通过`kubectl api-resources`命令查看,这里使用Deployment对象
metadata #是该资源的元数据,name是必需的元数据项
spec# 部分是该Deployment的规格说明
replicas#指明副本数量,默认为1
template#定义Pod的模板,这是配置文件的重要部分
metadata#定义Pod的元数据,至少要定义一个label。label的key和value可以任意指定
spec # 描述Pod的规格,此部分定义Pod中每一个容器的属性,name和image是必需的
status
这里直接使用nginx官方镜像
方式一
kubectl create deployment nginx-deployment --image=nginx --replicas=2 --port=80
方式二
kubectl apply -f nginx-deployment.yaml
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
方式一
kubectl expose deployment nginx-deployment --type=NodePort --port=80
方式二
kubectl create -f nginx-service.yaml
# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
name: nginx-service
spec:
type: NodePort
ports:
- port: 80
selector:
app: nginx # 名称与deployment中定义的app名称保持一致
我们这里设置了replicas=2,会产生两个容器(名称为 k8s_nginx_nginx-deployment-开头), 分别进入两个容器,增加一个文件
echo 'aaa' >/usr/share/nginx/html/a.html
查询nodePort
kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 157m
nginx-deployment NodePort 10.106.195.35 <none> 80:32352/TCP 6m51s
如上nodePort为32352
测试
http://localhost:32352/a.html
kubectl delete -n default deployment nginx-deployment
kubectl delete -n default service nginx-service
kubectl delete -n default ingress test-ingress
k8s ingress 两种部署方式nodePort和hostNetwork
https://kubernetes.io/zh/docs/reference/
历史版本
https://v1-20.docs.kubernetes.io/docs/reference/generated/kubernetes-api/v1.20/#deployment-v1-apps