一、deployment部署pod
备注:// 部署pod到指定节点
在启动Pod的yaml文件中与containers同级别的位置添加如下两行即可
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: docker.io/nginx ports: - containerPort: 80 resources: limits: cpu: 100m requests: cpu: 100m
2、centos的deployment
kind: Deployment metadata: name: qperf-server labels: app: qperf-server spec: replicas: 1 selector: matchLabels: app: qperf-server template: metadata: labels: app: qperf-server spec: nodeName: node02 containers: - name: centos image: centos:latest command: ["/bin/sh","-c","while true;do sleep 1;done"]
二、用pod创建一个简单的pod
Ⅰ、创建一个nginx 的pod
apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx-container image: nginx ports: - containerPort: 80
Ⅱ、创建一个busybox的pod
apiVersion: v1 kind: Pod metadata: name: busybox labels: app: busybox spec: containers: - name: busybox image: busybox command: ['sh', '-c', 'echo The app is running! && sleep 3600']
三、kubectl常用命令
Ⅰ、不进入容器执行命令
kubectl exec -it podName -c containerName -n namespace -- shell comand 例:[root@k8s-master ~]# kubectl exec -it mypod-nginx -c nginx -- mkdir -p /usr/local/wuchang
Ⅱ、查看阿皮、版本
kubectl api-versions
四、扩容和缩容
1、扩容:通过执行扩容命令,对某个deployment直接进行扩容
kubectl scale deployment nginx-deployment --replicas=4
2、缩容:当要缩容,减少副本数量即可
kubectl scale deployment nginx-deployment --replicas=2
3、pod的重启
Deployment 对象并不是直接操控的 Pod 对象,而是操控的 ReplicaSet 对象,而 ReplicaSet 对象就是由副本的数目的定义和Pod 模板组成的。
所以这条命令分别是将ReplicaSet 的数量 scale 到 0,然后又 scale 到 1
kubectl scale deployment esb-admin --replicas=0 -n {namespace} kubectl scale deployment esb-admin --replicas=1 -n {namespace}
4、查看pod的日志
1、pod若处于运行状态,则通过kubectl logs 即可
[root@node-1 ~]# kubectl logs pod_name -c container_name -n Name_Space kubectl logs -f <pod_name>
2、若pod处于init状态,则需要通过docker ps查看
通过docker ps 获取该pod的中的CONTAINER ID [root@node-1 ~]# docker ps | grep pod_name [root@node-1 ~]# docker logs CONTAINER_ID
五、镜像的更新升级和回滚
1、set images更新
set images:kubectl set image deploy nginxdeploy *=docker.io/jwilder/nginx-proxy
2、patch更新
kubectl patch deployment image-deployment --patch '{"spec": {"template": {"spec": {"containers": [{"name": "nginx","image":"registry.cn-beijing.aliyuncs.com/mrvolleyball/nginx:v1"}]}}}}'
3、回滚
kubectl rollout history deployment web 查看可以回滚的版本 kubectl rollout undo deployment web 回滚到上一版本
六、postStart和preStop
postStart:容器创建成功后,运行前的任务
apiVersion: v1 kind: Pod metadata: name: lifecycle-demo spec: containers: - name: lifecycle-demo-container image: nginx lifecycle: postStart: exec: command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:容器被中止前执行
preStop:pod销毁前执行 preStop: exec: command: ["/usr/sbin/nginx","-s","quit"]
七、探针
1、readinessProbe(就绪检测):判断返回结果是否符合预期
readinessProbe: httpGet: port: 80 path: /index1.html //如果存在就能就绪,不存在则无法就绪
initialDelaySeconds: 2 #容器启动后2S开始探测 periodSeconds: 5 #每次探测的间隔时间5S,单位为秒
2、livenessProbe:判断退出状态码是否是0
apiVersion: v1 kind: Pod metadata: name: liveness-exec-pod spec: containers: - name: liveness-exec-pod image: busybox:latest imagePullPolicy: IfNotPresent command: ["/bin/sh","-c","touch /tmp/healthy;sleep 30;rm -rf /tmp/healthy;sleep 3600"] livenessProbe: exec: command: ["test","-e","/tmp/healthy"] //检测是否存在,存在返回0 initialDelaySeconds: 2 #容器启动后2S开始探测 periodSeconds: 5 #每次探测的间隔时间5S,单位为秒