1.busybox
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
namespace: default
spec:
selector:
matchLabels:
app: busybox
replicas: 2
template:
metadata:
labels:
run: busybox
spec:
containers:
- name: busybox
image: busybox:latest
args:
- /bin/sh
- -c
- sleep 10; touch /tmp/healthy; sleep 30000
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 10
periodSeconds: 5
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox
namespace: default
spec:
selector:
matchLabels:
app: busybox
replicas: 2
template:
metadata:
labels:
run: busybox
spec:
containers:
- name: busybox
image: busybox:latest
args:
- /bin/sh
- -c
- sleep 10; touch /tmp/healthy; sleep 30000
readinessProbe:
exec:
command:
- cat
- /tmp/healthy
initialDelaySeconds: 10
periodSeconds: 5
2.蓝绿
在进行蓝/绿部署时,应用程序的一个新副本(绿)将与现有版本(蓝)一起部署。然后更新应用程序的入口/路由器以切换到新版本(绿)。然后,您需要等待旧(蓝)版本来完成所有发送给它的请求,但是大多数情况下,应用程序的流量将一次更改为新版本;Kubernetes不支持内置的蓝/绿部署。目前最好的方式是创建新的部署,然后更新应用程序的服务(如service)以指向新的部署;蓝绿部署是不停老版本,部署新版本然后进行测试,确认OK后将流量逐步切到新版本。蓝绿部署无需停机,并且风险较小。
- demo-blue1.yaml
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-blue1
namespace: default
labels:
app: demo
version: v1
spec:
replicas: 1
revisionHistoryLimit: 3
strategy:
rollingUpdate:
maxSurge: 30%
maxUnavailable: 30%
selector:
matchLabels:
app: demo
version: v1
template:
metadata:
labels:
app: demo
version: v1
spec:
containers:
- name: demo-blue1
image: opcache/devops-go-sample:hello1
resources:
limits:
cpu: 5m
memory: 10Mi
requests:
cpu: 5m
memory: 10Mi
livenessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 30
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 5
ports:
- name: http
containerPort: 8080
protocol: TCP
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-blue1
namespace: default
labels:
app: demo
version: v1
spec:
replicas: 1
revisionHistoryLimit: 3
strategy:
rollingUpdate:
maxSurge: 30%
maxUnavailable: 30%
selector:
matchLabels:
app: demo
version: v1
template:
metadata:
labels:
app: demo
version: v1
spec:
containers:
- name: demo-blue1
image: opcache/devops-go-sample:hello1
resources:
limits:
cpu: 5m
memory: 10Mi
requests:
cpu: 5m
memory: 10Mi
livenessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 30
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 5
ports:
- name: http
containerPort: 8080
protocol: TCP
- demo-green2.yaml
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-green2
namespace: default
labels:
app: demo
version: v2
spec:
replicas: 1
revisionHistoryLimit: 3
strategy:
rollingUpdate:
maxSurge: 30%
maxUnavailable: 30%
selector:
matchLabels:
app: demo
version: v2
template:
metadata:
labels:
app: demo
version: v2
spec:
containers:
- name: demo-green2
image: opcache/devops-go-sample:hello2
livenessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 30
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 5
ports:
- name: http
containerPort: 8080
protocol: TCP
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo-green2
namespace: default
labels:
app: demo
version: v2
spec:
replicas: 1
revisionHistoryLimit: 3
strategy:
rollingUpdate:
maxSurge: 30%
maxUnavailable: 30%
selector:
matchLabels:
app: demo
version: v2
template:
metadata:
labels:
app: demo
version: v2
spec:
containers:
- name: demo-green2
image: opcache/devops-go-sample:hello2
livenessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 30
successThreshold: 1
failureThreshold: 5
readinessProbe:
httpGet:
path: /
port: 8080
scheme: HTTP
initialDelaySeconds: 3
timeoutSeconds: 5
periodSeconds: 10
successThreshold: 1
failureThreshold: 5
ports:
- name: http
containerPort: 8080
protocol: TCP
- Service-v1.yaml
yaml
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: default
labels:
app: demo
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
selector:
app: demo
version: v1
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: default
labels:
app: demo
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
selector:
app: demo
version: v1
- Service-v2.yaml
yaml
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: default
labels:
app: demo
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
selector:
app: demo
version: v2
apiVersion: v1
kind: Service
metadata:
name: demo
namespace: default
labels:
app: demo
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
selector:
app: demo
version: v2
- 执行
shell
kubectl apply -f demo-blue1.yaml
kubectl apply -f demo-green2.yaml
kubectl apply -f Service-v1.yaml
#获取SVC IP
kubectl get rc,pods,svc,ingress -A -o wide | grep demo
kubectl apply -f demo-blue1.yaml
kubectl apply -f demo-green2.yaml
kubectl apply -f Service-v1.yaml
#获取SVC IP
kubectl get rc,pods,svc,ingress -A -o wide | grep demo
- 验证
for i in `seq 1 100000`; do curl clusterIP:8080/?a=$i ;echo " "; done
for i in `seq 1 100000`; do curl clusterIP:8080/?a=$i ;echo " "; done
3.暴露多个端口
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: demotest1
namespace: default
spec:
selector:
matchLabels:
app: demotest1
replicas: 2
template:
metadata:
labels:
app: demotest1
spec:
containers:
- name: demotest1
image: docker pull opcache/devops-go-sample:hello1
ports:
- containerPort: 8080
- containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
name: demotest1
namespace: default
spec:
type: NodePort
ports:
- name: http-test1
port: 8080
protocol: TCP
nodePort: 8080
- name: http-test2
port: 8081
protocol: TCP
nodePort: 8081
selector:
app: demotest1
apiVersion: apps/v1
kind: Deployment
metadata:
name: demotest1
namespace: default
spec:
selector:
matchLabels:
app: demotest1
replicas: 2
template:
metadata:
labels:
app: demotest1
spec:
containers:
- name: demotest1
image: docker pull opcache/devops-go-sample:hello1
ports:
- containerPort: 8080
- containerPort: 8081
---
apiVersion: v1
kind: Service
metadata:
name: demotest1
namespace: default
spec:
type: NodePort
ports:
- name: http-test1
port: 8080
protocol: TCP
nodePort: 8080
- name: http-test2
port: 8081
protocol: TCP
nodePort: 8081
selector:
app: demotest1