跳到主要内容

14 篇博文 含有标签「K8s」

查看所有标签

4. Pod生命周期管理

· 阅读需 2 分钟

4.1 Liveness command

通过命令行实现Liveness检查

# 编写Yaml
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-exec
spec:
containers:
- name: liveness
image: radial/busyboxplus
imagePullPolicy: IfNotPresent
args:
- /bin/sh
- -c
- touch /tmp/healthy; sleep 60; rm -rf /tmp/healthy; sleep 600
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 5
periodSeconds: 5

# 验证方法
kubectl exec -it liveness-exec -- ls /tmp
kubectl get pod

4.2 Liveness HTTP request

通过HTTP Request实现Liveness健康检查

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
labels:
test: liveness
name: liveness-http
spec:
containers:
- name: liveness
image: seedoflife/liveness
imagePullPolicy: IfNotPresent
args:
- /server
livenessProbe:
httpGet:
path: /healthz
port: 8080
httpHeaders:
- name: X-Custom-Header
value: Awesome
initialDelaySeconds: 3
periodSeconds: 3

# 验证方法
curl -v <Pod IP>:<Port>
kubectl get pod

4.3 ReadinessProbe + Service

通过tcpSocket实现ReadInessProbe + Service的健康检查

# 编写Yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: service-health
spec:
replicas: 1
selector:
matchLabels:
app: service-health
template:
metadata:
labels:
app: service-health
spec:
containers:
- name: service-health
image: python:2.7
imagePullPolicy: IfNotPresent
command: ["/bin/bash","-c","echo $(hostname) > index.html && sleep 30 && python -m SimpleHTTPServer 8080"]
ports:
- containerPort: 8080
readinessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 10
periodSeconds: 10

# 创建Service
kubectl expose deployment service-health --port=8080

# 验证方法
kubectl get service
kubectl scale deployment service-health --replicas=4
curl <ClusterIP>:<Port>

6. Pod的节点分配

· 阅读需 2 分钟

6.1 nodeName

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
nodeName: node01

6.2 nodeSelector

# 设置节点的Labels
kubectl label nodes node01 disktype=ssd

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
env: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
nodeSelector:
disktype: ssd

6.3 Affinity and anti-affinity

6.3.1 Node affinity

硬性要求

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity: # 使用node亲和进行判定
requiredDuringSchedulingIgnoredDuringExecution: # 硬性要求
nodeSelectorTerms: # 节点选择器的条件
- matchExpressions: # 定义匹配的表达式
- key: app # label的key
operator: In # 定义判断方式多种:In,NotIn,Exists,DoesNotexist,Gt,Lt。
values: # label的values
- nginx1
containers:
- name: with-node-affinity
image: nginx

软性要求

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution: # 软性要求
- weight: 1 # 设置权重,分数越高优先级越高
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: with-node-affinity
image: nginx

偏好要求

# 设置节点的Labels
kubectl label nodes node02 disktype=ssd
kubectl label nodes node01 app=nginx1
kubectl label nodes node02 app=nginx2

# 编写Yaml文件
apiVersion: v1
kind: Pod
metadata:
name: with-node-affinity
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: app
operator: In
values:
- nginx1
- nginx2
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
preference:
matchExpressions:
- key: disktype
operator: In
values:
- ssd
containers:
- name: with-node-affinity
image: nginx

6.3.2 Inter-pod affinity and anti-affinity

创建redis

# 编写Yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-cache
spec:
selector:
matchLabels:
app: store
replicas: 3
template:
metadata:
labels:
app: store
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname" # 拓扑域,用来判定拥有哪些标签的节点是同一个位置
containers:
- name: redis-server
image: redis:3.2-alpine

创建web-server

# 编写Yaml文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-server
spec:
selector:
matchLabels:
app: web-store
replicas: 3
template:
metadata:
labels:
app: web-store
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- web-store
topologyKey: "kubernetes.io/hostname"
podAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- store
topologyKey: "kubernetes.io/hostname"
containers:
- name: web-app
image: nginx:1.12-alpine

4-5. Kubernetes API、Resources与Namespaces

· 阅读需 1 分钟

4. Kubernetes API与Resources

4.1 api-resources

查看Kubernetes的资源

kubectl api-resources

4.2 api-versions

查看Kubernetes api-version

kubectl api-versions

kubectl explain namespace

5. Namespaces

5.1 查看当前集群下的namespace

kubectl get namespaces

5.2 Namespaces的创建与删除

# 创建
kubectl create namespace test

# 删除
kubectl delete namespaces demo

# yaml创建
cat << EOF > my-namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: demo
EOF

# 执行yaml文件创建namespaces
kubectl apply -f ./my-namespace.yaml

# 通过yaml文件删除namespaces
kubectl delete -f my-namespace.yaml