YAML配置文件管理对象
对象管理:
kubectl create -f nginx-deployment.yaml
kubectl get deploy
kubectl get rs
kubectl get pods --show-labels
kubectl get pods -l app=nginx
kubectl set image deployment/nginx-deployment nginx=nginx:1.11
或者
kubectl edit deployment/nginx-deployment
或者
kubectl apply -f nginx-deployment.yaml
kubectl rollout status deployment/nginx-deployment
kubectl rollout history deployment/nginx-deployment
kubectl rollout history deployment/nginx-deployment --revision=3
kubectl rollout undo deployment/nginx-deployment
kubectl rollout undo deployment/nginx-deployment --to-revision=3
kubectl scale deployment nginx-deployment --replicas=10
kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
kubectl delete pods <pod> --grace-period=0 --force
Pod状态 创建/查询/更新/删除 重启策略 健康检查 数据持久化和共享 hostPort 问题定位
Pod管理-创建/查询/更新/删除
基本管理:
# 创建pod资源
kubectl create -f pod.yaml
# 查看pods
kubectl get pods pod-test
# 查看pod描述
kubectl describe pod pod-test
# 替换资源
kubectl replace -f pod.yaml -force
# 删除资源
kubectl delete pod pod-test
支持三种策略:
Always:当容器终止退出后,总是重启容器,默认策略。
OnFailure:当容器异常退出(退出状态码非0)时,才重启容器。
Never:当容器终止退出,从不重启容器。
Pod实例:vim pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-test
labels:
os: centos
spec:
containers:
- name: hello
image: centos:7
env:
- name: Test
value: "123456"
command: ["bash","-c","while true;do date;sleep 1;done"]
restartPolicy: OnFailure
Pod管理-健康检查:
提供Probe机制,有以下两种类型:
livenessProbe
如果检查失败,将杀死容器,然后根据Pod的重启策略来决定是否
重启。
readinessProbe
如果检查失败,Kubernetes会把Pod从服务代理的分发后端剔除。
Probe支持以下三种检查方法:
httpGet
发送HTTP请求,返回200-400范围状态码为成功。
exec
执行Shell命令返回状态码是0为成功。
tcpSocket
发起TCP Socket建立成功。
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.10
ports:
- containerPort: 80
livenessProbe:
httpGet:
path: /index.html
port: 80
Pod管理-数据持久化和共享:
apiVersion: v1
kind: Pod
metadata:
name: pod-test1
labels:
test: centos
spec:
containers:
# 第一个容器
- name: hello-write
image: centos:7
command: ["bash","-c","for i in {1..1000};do echo $i >> /data/hello;sleep 1;done"]
# 第二个容器
- name: hello-read
image: centos:7
command: ["bash","-c","for i in {1..1000};do cat $i >> /data/hello;sleep 1;done"]
volumeMounts:
- name: data
mountPath: /data
# 数据卷
volumes:
- name: data
hostPath:
path: /data
Pod端口映射:
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.10
ports:
- name: http
containerPort: 80
hostIP: 0.0.0.0
hostPort: 80
protocol: TCP
- name: https
containerPort: 443
hostIP: 0.0.0.0
hostPort: 443
protocol: TCP