1. Production, few will run a self-contained pod, usually by the controller to create pod, its configuration file is embedded in the way create a pod .
pod controller : ReplicaSet, Deployment, DaemonSet, Job , Cronjob, StatefulSet
ReplicaSet : on behalf of the user to create a specified number of copies of the number of pod, pod number of copies to ensure the state in line with expectations, and supports rolling automatic expansion and volume reduction function.
ReplicaSet mainly consists of three components:
a user desired number of copies pod;. . b tag selector to determine which pod to his own management; c.pod resource template, when less than the existing number of pod, pod will be based on the new resource template.
Help users manage resources pod stateless, accurately reflect the number of user-defined target, but RelicaSet not directly use the controller, but the use of Deployment;
Deployment : work on ReplicaSet, stateless application for managing, at present, the best controller support rolling updates and rollbacks function, it also provides declarative configuration;.
DaemonSet : to ensure that each node in the cluster is running only a copy of a particular pod, typically used to implement system-level background tasks, such as ELK responsible for collecting logs filebeat, features: Service is stateless, the service must be a daemon;
The Job : simply complete exit immediately without restarting or reconstruction;
Cronjob : periodic task control, do not need to continue running in the background;
StatefulSet : Management stateful applications.
2. ReplicaSet (RS) Example
RS-demo.yaml CAT apiVersion: Apps / V1 kind: ReplicaSet Metadata: name: RS-MyApp namespace: default spec: Replicas: 2 Selector: matchLabels: RUN: MyApp Release: Canary Template: Metadata: name: Whatever Labels: RUN: myapp Release: Canary env: the Test spec: Containers: - name: nginx-Web Image: ikubernetes / myapp: v1 the ports: - name: HTTP containerPort: 80 template: name resource defined in the template actually does not take effect, after the pod up and running, real name The controller is a random string name + Create -f RS-demo.yaml kubectl kubectl GET PODS -o Wide NAME RESTARTS of AGE the IP NODEs the STATUS READY RS-MyApp-2hxc9 1/1 0 Running 74S 10.244.2.7 K8S node2- RS-d6845-MyApp 1/1 0 Running 74S K8S-node1 10.244.1.9 curl 10.244.2.7 the Hello MyApp | Version: v1 | <a href="hostname.html"> the Name </a> Pod # replicatset edit configuration file that is not what we created manually, but apiserver maintain, modify the number of copies kubectl Edit rs myapp # can also be upgraded version, the change v1 v2, but only after the pod reconstruction, such as adding or deleting Pod, will be updated to version v2
3. Deployment controller
图解:通过Deployment控制器来动态更新pod版本,Deployment下有众多replicatset,但只有一个是激活的,更改配置文件中的镜像版本,就会一个一个的删除replicatset v1版本中的Pod,自动新创建的pod就会变成v2版本,当pod全部变成v2版本后,replicatset v1不会被删除,这样一旦发现v2版本有问题,还可以回退到v1版本,通常deployment默认保留10个版本的replicatset.
kubectl explain deploy # 文档是落后于k8s版本的 deploy示例: cat deploy-demo.yaml apiVersion: apps/v1 kind: Deployment metadata: name: myapp-deploy namespace: default spec: replicas: 2 selector: matchLabels: app: myapp release: canary template: metadata: labels: app: myapp release: canary spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 # apply:声明式更新和创建,可以应用多次,create只能用一次 kubectl apply -f deploy-demo.yaml kubectl get deploy # 会看见一个名为myapp-deploy的deploy生成 kubectl get rs NAME DESIRED CURRENT READY AGE myapp-deploy-574965d786 2 2 2 93s kubectl get pods NAME READY STATUS RESTARTS AGE myapp-deploy-574965d786-5x42g 1/1 Running 0 70s myapp-deploy-574965d786-dqzpd 1/1 Running 0 70s # 默认滚动策略是RollingUpdate,查看滚动更新的历史 kubectl rollout history deployment myapp-deploy # 如果要修改副本数,则编辑deploy-demo.yaml修改副本数,或者: kubectl patch deployment myapp-deploy -p '{"spec":{"replicas":5}}' # 给更新策略打补丁: kubectl patch deployment myapp-deploy -p '{"spec":{"strategy":{"rollingUpdate":{"maxSurge":1,"maxUnavailable":0}}}}' # maxSurge:pod的数量最多可超出期望值多少个;maxUnavailable:最多不可用的pod有多少个. # 金丝雀发布,先发布一个,此时多了一个pod,现在有6个 # 用set image命令将镜像myapp升级为v3版本,并且将myapp-deploy控制器标记为暂停, # 被暂停的资源不会被控制器使用,可以使"kubectl rollout resume"命令恢复已暂停资源 kubectl set image deployment myapp-deploy myapp=ikubernetes/myapp:v3 && kubectl rollout pause deployment myapp-deploy kubectl get pods -l app=myapp -w # resume:继续,重新开始,可以看到继续更新,删一个更新一个 kubectl rollout status deployment myapp-deploy kubectl rollout resume deployment myapp-deploy # 查看副本集的详细信息 kubectl get rs -o wide # 版本回滚 kubectl rollout history deployment myapp-deploy kubectl rollout undo deployment myapp-deploy --to-revision=1
4.DaemonSet示例
# node1、node2下载filebeat镜像 docker pull ikubernetes/filebeat:5.6.5-alpine cat ds-demo.yaml apiVersion: apps/v1 kind: Deployment metadata: name: redis namespace: default spec: replicas: 1 selector: matchLabels: app: redis role: logstor template: metadata: labels: app: redis role: logstor spec: containers: - name: redis image: redis:4.0-alpine ports: - name: redis containerPort: 6379 --- # 减号隔离不同资源定义 apiVersion: apps/v1 kind: DaemonSet metadata: name: filebeat-ds namespace: default spec: selector: matchLabels: app: filebeat release: stable template: metadata: labels: app: filebeat release: stable spec: containers: - name: filebeat image: ikubernetes/filebeat:5.6.5-alpine env: - name: REDIS_HOST value: redis.default.svc.cluster.local - name: REDIS_LOG_LEVEL value: info # REDIS_HOST这个键值对是filebeat-ds向filebeat配置文件传的环境变量 kubectl apply -f ds-demo.yaml # 暴露redis端口 kubectl expose deployment redis --port=6379 # 进入redis kubectl exec -it redis-664bbc646b-sg6wk -- /bin/sh /data # redis-cli -h redis.default.svc.cluster.local # 进入filebeat kubectl exec -it filebeat-ds-bszfz -- /bin/sh nslookup redis.default.svc.cluster.local # daemon-set也支持滚动更新 kubectl set image daemonsets filebeat-ds filebeat=ikubernetes/filebeat:5.5.7-alpine kubectl explain pods.spec # 有一个字段hostNetwork,可以让容器直接共享宿主机的网络 注:不同pod之间通信,filebeat向redis发送日志靠的是service
参考博客:http://blog.itpub.net/28916011/viewspace-2214692/