什么是Pod
- Pod是K8S最小调度单位。
- Pod可以由一个或者多个容器组合而成。
- Pod容器共享Volume
- 很少会直接创建一个Pod,在大多数情况下,会通过RC,RS,Deployment,DaemonSet,Job等控制器完成对一组Pod副本的创建,调度以及生命周期的自动控制任务。
通过Deployment部署一个nginx
- 新建并且编辑nignx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
- 创建Deployment
kubectl create -f nignx-deployment.yaml
# 执行一下命令可以查看结果
kubectl get deployments
kubectl get rs
kubectl get pod -o wide
# [root@k8s-master k8s-yaml]# kubectl get pods
# NAME READY STATUS RESTARTS AGE
# nginx-deployment-59c9f8dff-kbdg6 1/1 Running 0 47s
# nginx-deployment-59c9f8dff-lwnj2 1/1 Running 0 47s
# nginx-deployment-59c9f8dff-whjt8 1/1 Running 0 47s
创建Service
apiVersion: v1
kind: Service
metadata:
name: nginx
spec:
ports:
- port: 8888
targetPort: 80
selector:
app:nginx
- NodePort类型:在node上打开一个端口,将该端口得流量导入到kube-proxy,然后kube-proxy进一步到对应得pod
apiVersion: v1
kind: Service
metadata:
name: nodeportservice
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 8889
targetPort: 80
ConfigMap
- 生产为容器内的环境变量
- 设置容器启动命令的启动参数
- 以Volume的形式挂载为容器内部的文件或者目录
创建
- 通过YAML配置文件方式创建
# configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm-var
data:
level: info
env: development
[root@k8s-master k8s-yaml]# kubectl create -f configmap.yaml
configmap/cm-var created
[root@k8s-master k8s-yaml]# kubectl get cm
NAME DATA AGE
cm-var 2 20s
kubectl describe configmap
Pod中使用ConfigMap
# pod-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod
spec:
containers:
- name: cn-test
image: busybox
command: ["/bin/sh", "-c", "env | grep | APP"]
env:
- name: APPLOGLEVEL # 定义环境变量的名称
valueFrom:
configMapKeyRef:
name: cm-var # 环境变量的值取自以上configmap.yaml定义的cm-var
key: level # key值为level
- name: APPENV
valueFrom:
configMapKeyRef:
name: cm-var
key: env
- k8s在1.6的版本开始,引入了一个新的字段envFrom
# pod-envFrom-configmap.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod
spec:
containers:
- name: cn-test
image: busybox
command: ["/bin/sh", "-c", "env"]
envFrom:
- configMapRef:
name: cm-var
限制条件
- ConfigMap必须在Pod之前创建
- Config受NameSpace的限制,必须同一个NameSpace下
- 支持被Api Server管理的Pod才能使用【比如静态pod无法使用】
容器内获取Pod的信息
# pod-dapi.yaml
apiVersion: v1
kind: Pod
metadata:
name: cm-test-pod
spec:
containers:
- name: cn-test
image: busybox
command: ["/bin/sh", "-c", "env"]
env:
- name: podName # 定义环境变量的名称
valueFrom:
fieldRef:
podName: metadata.name
- name: podId
valueFrom:
fieldRef:
podId: status.podIP
Pod升级和回滚
- Deployment的升级
Nignx由1.7.9升级到1.9.1
# deployment-nginx.yaml
apiVersion: v1
kind: Deplopment
metadata:
name: deployment-nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containersPort: 80
# 创建
# kubeclt create -f deployment-nginx.yaml
# 升级
# kebectl set image deplopment/deplopment-nginx nginx=nginx:1.9.1
- Deplopment的回滚
# 查看回滚命令
kubect rollout history deplopment/deployment-nginx
# 回滚到上一版本
kubectl rollout undo deployment/deployment-nginx
Pod的扩容
- 手动扩容
通过执行kubectl scale命令或者通过restful api对一个deployment/rc/rs进行pod的副本数量的设置。
- 以上面deployment-nginx.yaml为例[yaml中设置启动3个]
kubectl scale deplopment deployment-nginx --replicase = 5
# 如果设置的replicase 小于初始化的,会“杀掉”一些运行中的Pod.
- 自动扩容
需要用户根据某个性能指标或者自定义业务指标,并且指定pod的副本数量范围,系统将在这个范围内根据性能指标的变化进行调整。
-
HPA的资源对象处于api的“autoscaling”中,目前有v1和v2两个版本,v1只支持基于CPU使用率的自动扩缩容。v2版本则支持基于任意指标的扩容配置,包括基于资源使用率,pod指标
-
v1版本需要预先安装Heapster组件或者Metrics Server 用于采集pod的CPU使用率
apiVserion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: v1-auto-nginx
spec:
scaleTargetRef: # 目标作用对象,可以是deplopment,rs,rc
apiVsersion: apps/v1
kind: Deplopment
name: deplopment-nginx
minReplicas: 1
maxReplicas: 10
targetCPUUtilizationPercentage: 50 # 期望每个pod的cpu使用率都是50%
- v2版本
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
name: v2-auto-nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: deployment
name: deplopment-nginx
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50