控制器
控制器用来管理Pod
使用StatefulSet控制器部署有状态应用
RS(ReplicaSet)
apiVersion: apps/v1
# 定义一个ReplicaSet控制器
kind: ReplicaSet
metadata:
name: frontend
# kubectl explain rs.spec
spec:
# 开启3个Pod实例
replicas: 3
# 选择器
selector:
# 使用标签选择
matchLabels:
# 当Pod有如下标签时,则认为该Pod属于本控制器
tier: frontend
# 定义Pod的模板
# kubectl explain rs.spec.template
template:
metadata:
# 定义Pod的标签
labels:
# 标签如下
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v3
# 设置该容器内部环境变量
env:
# 设置GET_HOSTS_FROM环境变量
- name: GET_HOSTS_FROM
# GET_HOSTS_FROM环境变量的值为dns
value: dns
# 设置容器要暴露的端口
ports:
# 要在pod的IP地址上暴露80端口
- containerPort: 80
Deployment
RS控制器的增强版
Deployment通过RS来管理Pod
apiVersion: apps/v1
kind: Deployment
metadata:
# deployment名字
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
# 定义Pod的模板
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.8
ports:
- containerPort: 80
kubectl get deployment,rs,pod -o wide
# NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
# deployment.apps/nginx-deployment 3/3 3 3 3m55s nginx nginx:1.8 app=nginx
# NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
# replicaset.apps/nginx-deployment-64c9d67564 3 3 3 3m55s nginx nginx:1.8 app=nginx,pod-template-hash=64c9d67564
# NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
# pod/nginx-deployment-64c9d67564-6drrs 1/1 Running 0 3m55s 172.19.2.18 k8s-node02 <none> <none>
# pod/nginx-deployment-64c9d67564-6vn9t 1/1 Running 0 3m55s 172.19.1.29 k8s-node01 <none> <none>
# pod/nginx-deployment-64c9d67564-qc4vt 1/1 Running 0 3m55s 172.19.2.17 k8s-node02 <none> <none>
DaemonSet
每个Node上都只运行一个Pod实例
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: app-daemonset
labels:
app: daemonset
spec:
selector:
matchLabels:
name: app-daemonset
template:
metadata:
labels:
name: app-daemonset
spec:
containers:
- name: container-daemonset
image: busybox
imagePullPolicy: IfNotPresent
command: ["ping", "www.baidu.com"]
kubectl get daemonset,pod -o wide
# NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE CONTAINERS IMAGES SELECTOR
# daemonset.apps/app-daemonset 2 2 2 2 2 <none> 31s container-daemonset busybox name=app-daemonset
# NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
# pod/app-daemonset-g8msw 1/1 Running 0 31s 172.19.1.30 k8s-node01 <none> <none>
# pod/app-daemonset-p84rk 1/1 Running 0 31s 172.19.2.19 k8s-node02 <none> <none>
Job
# kubectl explain job.apiVersion
apiVersion: batch/v1
kind: Job
metadata:
name: app-job
spec:
template:
metadata:
name: container-job
spec:
containers:
- name: job
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "echo run a job"]
# One of Always, OnFailure, Never. Default to Always.
restartPolicy: Never
kubectl get job,pod -o wide
# NAME COMPLETIONS DURATION AGE CONTAINERS IMAGES SELECTOR
# job.batch/app-job 1/1 2s 2m59s job busybox controller-uid=5e6f33cd-7146-454f-97a8-3bd971659a56
# NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
# pod/app-job-d2jjc 0/1 Completed 0 2m59s 172.19.1.32 k8s-node01 <none> <none>
kubectl logs app-job-d2jjc
# run a job
CronJob
管理基于时间的Job
- 在给定时间点只运行一次
- 周期性地在给定时间点运行
# kubectl explain cronjob.apiVersion
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: app-cronjob
spec:
# schedule必须字段,指定运行时间
schedule: "*/1 * * * *"
# jobTemplate必须字段
jobTemplate:
spec:
template:
spec:
containers:
- name: cronjob
image: busybox
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "-c", "date"]
restartPolicy: OnFailure
kubectl get cronjob,job,pod
# NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
# cronjob.batch/app-cronjob */1 * * * * False 1 2s 76s
# NAME COMPLETIONS DURATION AGE
# job.batch/app-cronjob-1606665060 1/1 2s 53s
# job.batch/app-cronjob-1606665120 1/1 1s 2s
# NAME READY STATUS RESTARTS AGE
# pod/app-cronjob-1606665060-m7xcz 0/1 Completed 0 53s
# pod/app-cronjob-1606665120-wbndr 0/1 Completed 0 2s
kubectl logs app-cronjob-1606665060-m7xcz
# Sun Nov 29 15:51:09 UTC 2020
kubectl logs app-cronjob-1606665120-wbndr
# Sun Nov 29 15:51:59 UTC 2020
StatefulSet
部署有状态应用
无头service:ClusterIP:none
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 60000
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx-statefulset
spec:
serviceName: nginx
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19-alpine
ports:
- containerPort: 80
kubectl get pod,statefulset,svc -o wide
# NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
# pod/nginx-statefulset-0 1/1 Running 0 20s 172.19.1.85 k8s-node01 <none> <none>
# pod/nginx-statefulset-1 1/1 Running 0 18s 172.19.2.45 k8s-node02 <none> <none>
# pod/nginx-statefulset-2 1/1 Running 0 17s 172.19.1.86 k8s-node01 <none> <none>
# NAME READY AGE CONTAINERS IMAGES
# statefulset.apps/nginx-statefulset 3/3 20s nginx nginx:1.19-alpine
# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
# service/kubernetes ClusterIP 172.18.0.1 <none> 443/TCP 3d6h <none>
# service/nginx ClusterIP None <none> 60000/TCP 20s app=nginx
每个Pod都会有唯一的主机名,唯一域名:
格式:主机名称.service名称.名称空间.svc.cluster.local
例如:nginx-statefulset-0.nginx.default.svc.cluster.local