k8s常用yaml文件模板
关于外部访问
apiVersion: apps/v1 #apiVersion是当前配置格式的版本
kind: Deployment #kind是要创建的资源类型
metadata: #metadata是该资源的元数据,name是必须的元数据项
name: flaskapp-1
spec:
selector: # .spec.selector 必须匹配 .spec.template.metadata.labels,否则它将被API拒绝。如果 .spec.selector 没有被指定, .spec.selector.matchLabels 默认是 .spec.template.metadata.labels
matchLabels:
run: flaskapp-1
replicas: 1
template: #定义Pod
metadata: #metadata定义Pod的元数据,至少要顶一个label,label的key和value可以任意指定
labels:
run: flaskapp-1
spec:
containers:
- name: flaskapp-1
image: jcdemo/flaskapp #image指定镜像的地址
ports:
- containerPort: 5000 #暴露给service的地址
apiVersion: v1
kind: Service
metadata:
name: flaska
labels:
run: flaskapp-1
spec:
type: NodePort # 暴露service的三种方式 NodePort,LoadBalancer 和 Ingress
ports: #将Service 的 30005 端口映射到 Pod 的 5000 端口,使用 TCP 协议
- port: 5000
name: flaskapp-port
targetPort: 5000
protocol: TCP
nodePort: 30005
selector: # selector 指明挑选那些 label 为 run: xx 的 Pod 作为 Service 的后端
run: flaskapp-1
关于滚动更新
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 #选择副本数量
minReadySeconds: 10 # 从容器启动到应用正常提供服务
strategy: # k8s 默认的 strategy 就是 RollingUpdate
type: RollingUpdate
rollingUpdate:
maxSurge: 1 # 更新时允许最大激增的容器数,默认 replicas 的 1/4 向上取整
maxUnavailable: 0 # 更新时允许最大 unavailable 容器数,默认 replicas 的 1/4 向下取整
selector: #选择要管理的pod
matchLabels: # matchLabels用于定义一组Label,与直接写在Selector中作用相同
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
关于存活探针
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
args: #启动参数
- /bin/sh
- -c
- touch /tmp/healthy; sleep 30; rm -rf /tmp/healthy; sleep 600 #创建文件后休眠然后删除文件后继续休眠
image: busybox
livenessProbe: #绑定liveness在容器下
exec:
command:
- cat
- /tmp/healthy #检查文件存在,如存在返回值为0
initialDelaySeconds: 5 #检测延迟
periodSeconds: 5 #重试循环时间
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: mirrorgooglecontainers/liveness
args:
- /server
livenessProbe:
httpGet:
path: /healthz
port: 8080
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod
spec:
containers:
- name: cm-container
image: busybox
args: ["/bin/sh", "-c", "env"]
env:
- name: special-env #设置key
valueFrom:
configMapKeyRef:
name: specialconfig-2 #选择configmap
key: key1 #导入value
envFrom: #导入的env环境变量
- configMapRef:
name: specialconfig-2 #选择configmap
restartPolicy: Never #重启策略
apiVersion: v1
kind: Pod
metadata:
name: cmpod2
spec:
containers:
- name: cmpod2
image: busybox
args: [ "/bin/sh", "-c", "sleep 3000" ]
volumeMounts:
- name: db #选择volume
mountPath: "/etc/db" #选择目录
readOnly: true
volumes: #选择configmap以volume方式挂载
- name: db
configMap:
name: specialconfig-2
apiVersion: v1
kind: Pod
metadata:
name: spod2
spec:
containers:
- image: busybox
name: spod2
args: ["/bin/sh","-c","sleep 3000"]
volumeMounts:
- name: secrets #选择volume
mountPath: "/etc/secret" #选择目录
readOnly: true
volumes:
- name: secrets
secret:
secretName: mysecret #选择secrets
items:
- key: password #选择key
path: my-group/my-passwd #设定目录
apiVersion: v1
kind: Pod
metadata:
name: em
spec:
containers:
- image: ubuntu
name: test-container
volumeMounts: #挂载emptyDir
- mountPath: /cache #挂载目录
name: cache-volume #数据卷名称
args:
- /bin/sh
- -c
- sleep 30000
volumes: #创建数据卷
- name: cache-volume
emptyDir: {}
apiVersion: v1
kind: PersistentVolume #选择PV
metadata:
name: mypv
spec:
capacity: #设置容量
storage: 1Gi
accessModes: #读写规则
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle #回收策略
( storageClassName: value #设置存储级别供给statefulset调用)
nfs: #pv格式
path: /nfs/pv1 #NFS服务器上提供的路径
server: 192.168.8.202 #NFS服务器地址
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mypvc
spec:
accessModes:
- ReadWriteOnce
volumeName: mypv #选择PV
resources:
requests:
storage: 1Gi
- 点赞
- 收藏
- 关注作者
评论(0)