k8s之pod控制器

匿名 (未验证) 提交于 2019-12-02 23:43:01

a.用户期望的pod副本数量; b.标签选择器,判断哪个pod归自己管理; c.pod资源模板,当现存的pod数量不足,会根据pod资源模板进行新建. 

cat rs-demo.yaml 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: test     spec:       containers:       - name: nginx-web         image: ikubernetes/myapp:v1         ports:         - name: http           containerPort: 80 template:资源模板中定义的name其实不生效,pod运行起来之后,真正的名字是控制器名+随机字符串 kubectl create -f rs-demo.yaml kubectl get pods -o wide NAME             READY   STATUS      RESTARTS   AGE     IP           NODE      rs-myapp-2hxc9   1/1     Running     0          74s     10.244.2.7   k8s-node2 rs-myapp-d6845   1/1     Running     0          74s     10.244.1.9   k8s-node1 curl 10.244.2.7 Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a> # 编辑replicatset的配置文件,这个文件不是我们手工创建的,而是apiserver维护的,修改副本数 kubectl edit rs myapp # 也可以升级版本,将v1改成v2,但只有pod重建后,比如增加或删除Pod,才会更新成v2版本 

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 

# 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 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!