init contaienr
它是一种专用的容器,在Pod内的应用容器启动之前运行,并包括一些应用镜像中不存在的实用工具和安装脚本。
Init 容器支持应用容器的全部字段和特性,包括资源限制、数据卷和安全设置。 然而,Init 容器对资源请求和限制的处理稍有不同,在下面 资源 处有说明。
同时 Init 容器不支持 Readiness Probe,因为它们必须在 Pod 就绪之前运行完成。
如果为一个 Pod 指定了多个 Init 容器,这些容器会按顺序逐个运行。每个 Init 容器必须运行成功,下一个才能够运行。当所有的 Init 容器运行完成时,Kubernetes 才会为 Pod 初始化应用容器并像平常一样运行。
cat << EOF > initcontainers.yaml
apiVersion: v1
kind: Pod
metadata:
name: init-test
labels:
app: myapp
spec:
containers:
- name: container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-test
image: busybox:1.28
command: ['sh', '-c', 'echo init containers test && sleep 10']
EOF
rancher上启动pod添加sidecar,添加initcontainer
replicaset
ReplicaSet通过selector标识创建的pod。yaml文件里的字段replicas是指控制器应该维护的Pod副本数量。当ReplicSet需要创建新的Pod副本会使用yaml里的pod模板,也是就template字段。
注意labels:如果创建副本指定labels,那么如果创建的labels的pod或容器超过labels数量,就会被删除
cat << EOF > replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
labels:
app: rs-nginx
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
app: pod-nginx
template:
metadata:
labels:
app: pod-nginx
spec:
containers:
- name: nginx
image: nginx
EOF
[rancher@master1 ~]$ kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
nginx 3 3 3 66s
deployment
Deployment是一个更高级别的概念用于部署无状态服务,通过管理ReplicaSet为Pod提供新的功能,比如:更新、回滚等。
创建完deployment后会自动创建replicasets,官网链接
# run
[rancher@master1 test]$ kubectl run --image=nginx --replicas=2 --image-pull-policy=IfNotPresent --labels=app=nginx test
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/test created
# 查
[rancher@master1 test]$ kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
test 2/2 2 2 75s
[rancher@master1 test]$ kubectl get replicasets.apps
NAME DESIRED CURRENT READY AGE
nginx 3 3 3 47m
test-6f596c6495 2 2 2 105s
[rancher@master1 test]$ kubectl describe deployments.apps nginx-deployment
# 更新,指定deployment容器的名字
[rancher@master1 test]$ kubectl --record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1
deployment.apps/nginx-deployment image updated
deployment.apps/nginx-deployment image updated
# 查看每一次更新版本的操作
[rancher@master1 test]$ kubectl rollout history deployment nginx-deployment --revision=2
# 回滚
[rancher@master1 test]$ kubectl rollout undo deployment nginx-deployment
deployment.apps/nginx-deployment rolled back
# 升级
[rancher@master1 test]$ kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:latest --record=true
deployment.apps/nginx-deployment image updated
# 检查正在进行或进行完成的状态
[rancher@master1 test]$ kubectl rollout status deployment nginx-deployment
deployment "nginx-deployment" successfully rolled out
# 缩放
kubectl scale deployment.v1.apps/nginx-deployment --replicas=10
```yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
<a name="daemonset"></a>
## daemonset
DaemonSet确保所有(或部分)节点上运行一个Pod的副本。当集群加入新的节点时,会自动在新的节点上创建Pod的副本。当节点从群集中删除时,这些Pod会被回收。删除DaemonSet将清除其创建的Pod。典型的应用包括:
-
日志收集,比如 fluentd,logstash 等
-
系统监控,比如 Prometheus Node Exporter,collectd,New Relic agent,Ganglia gmon等
-
系统程序,比如 kube-proxy, glusterd, ceph 等
```bash
[rancher@master1 test]$ kubectl delete -f daemonset.yml
daemonset.apps "fluentd-elasticsearch" deleted
apiVer1sion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
# 设置了宽容,在某种情况下,可以调度或不调度master,这里是容忍master可以运行pod
# this toleration is to have the daemonset runnable on master nodes
# remove it if your masters can't run pods
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: quay.azk8s.cn/fluentd_elasticsearch/fluentd:v2.5.2
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
statefulset
StatefulSet控制器用于部署有状态应用程序,并且为每个Pod维护一个永久性标识符。
哪些需求或者应用程序会使用到StatefulSet控制器?
稳定的持久化存储,即 Pod 重新调度后还是能访问到相同的持久化数据,基于 PVC 来实现
稳定的网络标志,即 Pod 重新调度后其 PodName 和 HostName 不变,基于 Headless Service(即没有 Cluster IP 的 Service)来实现
有序部署与扩缩容:即 Pod 是有顺序的在部署或者扩缩容的时候要依据定义的顺序依次依序进行(即从 0 到 N-1,在下一个 Pod 运行之前所有之前的 Pod 必须都是 Running 和 Ready 状态)
有序的滚动更新:StatefulSet控制器将删除并重新创建StatefulSet中的每个Pod。它将以Pod终止的顺序进行(从最大顺序到最小顺序),每次更新一个Pod。它会等到一个更新完成在去更新下一个Pod ```yml apiVersion: v1 kind: Service metadata: name: nginx labels: app: nginx spec: ports:
- port: 80 name: web clusterIP: None selector: app: nginx
apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: selector: matchLabels: app: nginx # has to match .spec.template.metadata.labels serviceName: “nginx” replicas: 3 # by default is 1 template: metadata: labels: app: nginx # has to match .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
<a name="job"></a>
## job
Jobs创建一个或多个Pod并确保指定数量的Pod成功终止。当pod成功完成后,Job会跟踪成功的完成情况。达到指定数量的成功完成时,任务(即Jobs)完成。注意:删除作业将清理它创建的Pod。
```yml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
cronjob
CronJob 用于创建具有时间安排的任务。类似于linux系统的Crontab,都是通过周期表来运行Job。注意:CronJob schedule:时间均基于的kube-controller-manager的时区。
典型的用法包括:
需要指定时间点调度 Job 运行
创建周期性运行的 Job,例如:数据库备份、发送邮件。
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure