K8S中deployment和service案例
文章目录案例要求:1、案例一1 创建deployment,在还原到之前版本2、案例二:副本数扩容3、案例三多端口实例案例要求:1、创建一个deployment副本数为2,在滚动更新镜像版本,并记录这个更新记录,最后在还原到上一个版本。2、给这个应用扩容副本数到4。3、创建一个pod,pod里面运行3个容器,nginx、tomcat、redis。4、给一个pod创建service,并且可以通过Clu
·
案例要求:
1、创建一个deployment副本数为2,在滚动更新镜像版本,并记录这个更新记录,最后在还原到上一个版本。
2、给这个应用扩容副本数到4。
3、创建一个pod,pod里面运行3个容器,nginx、tomcat、redis。
4、给一个pod创建service,并且可以通过Clusterip/NodePort方式访问。
1、案例一
1 创建deployment,在还原到之前版本
[root@k8s-master ~]# kubectl create deployment xin --image=nginx --dry-run=client -o yaml > xin.yaml
[root@k8s-master ~]# vim xin.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: xin
name: xin
spec:
replicas: 2
selector:
matchLabels:
app: xin
template:
metadata:
labels:
app: xin
spec:
containers:
- image: nginx:1.14
name: nginx
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
xin-5958d7557f-7l4cw 1/1 Running 0 3m50s
xin-5958d7557f-9jj68 1/1 Running 0 3m18s
[root@k8s-master ~]# kubectl rollout history deployment # 记录历史发布的版本
deployment.apps/xin
REVISION CHANGE-CAUSE
1 <none>
[root@k8s-master ~]# vim xin.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: xin
name: xin
spec:
replicas: 2
selector:
matchLabels:
app: xin
template:
metadata:
labels:
app: xin
spec:
containers:
- image: nginx:1.17 # 更改nginx版本
name: nginx
[root@k8s-master ~]# kubectl get pod
[root@k8s-master ~]# kubectl rollout history deployment
deployment.apps/xin
REVISION CHANGE-CAUSE
1 <none>
2 <none>
[root@k8s-master ~]# kubectl rollout undo deployment xin --to-revision=1
deployment.apps/xin rolled back
[root@k8s-master ~]# kubectl rollout history deployment
deployment.apps/xin
REVISION CHANGE-CAUSE
2 <none>
3 <none>
2、案例二:
副本数扩容
[root@k8s-master ~]# kubectl scale deployment xin --replicas=4 # 新增副本数,也可以直接到yaml文件中改replicas的值
[root@k8s-master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
xin-bf9b569b9-2slgt 1/1 Running 0 13m
xin-bf9b569b9-fqhlb 1/1 Running 0 13m
xin-bf9b569b9-m9hzd 1/1 Running 0 8s
xin-bf9b569b9-txpbl 1/1 Running 0 8s
3、案例三
多端口实例
[root@k8s-master ~]# kubectl create deployment zz --image=nginx --image=redis --image=tomcat --dry-run=client -o yaml > zhao.yaml
[root@k8s-master ~]# vim zz.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: sz
name: zz
spec:
replicas: 1
selector:
matchLabels:
app: sz
template:
metadata:
labels:
app: sz
spec:
containers:
- image: nginx
name: http
- image: tomcat
name: web
- image: redis
name: db
[root@k8s-master ~]# kubectl apply -f zz.yaml
[root@k8s-master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
xin-54f665d556-jm2pn 1/1 Running 0 3h29m 10.244.169.168 k8s-node2 <none> <none>
xin-54f665d556-rg6cm 1/1 Running 0 3h29m 10.244.36.97 k8s-node1 <none> <none>
zz-7d5b8995b8-g8s5r 3/3 Running 0 58m 10.244.36.117 k8s-node1 <none> <none>
[root@k8s-master ~]# kubectl exec -it -c web zz-7d5b8995b8-g8s5r -- bash #进入apache容器中
root@zz-7d5b8995b8-g8s5r:/usr/local/tomcat# cd webapps
root@zz-7d5b8995b8-g8s5r:/usr/local/tomcat/webapps# mkdir ROOT #创建apache发布目录
root@zz-7d5b8995b8-g8s5r:/usr/local/tomcat/webapps# cd ROOT/
root@zz-7d5b8995b8-g8s5r:/usr/local/tomcat/webapps/ROOT# echo 123 > index.html
root@zz-7d5b8995b8-g8s5r:/usr/local/tomcat/webapps/ROOT# exit
[root@k8s-master ~]# curl 10.244.36.117:8080
123
[root@k8s-master ~]# curl -I 10.244.36.117:8080 #容器内访问tomcat
HTTP/1.1 200
Accept-Ranges: bytes
ETag: W/"4-1652434459188"
Last-Modified: Fri, 13 May 2022 09:34:19 GMT
Content-Type: text/html
Content-Length: 4
Date: Fri, 13 May 2022 09:37:03 GMT
[root@k8s-master ~]# curl -I 10.244.36.117:80 #容器内访问nginx
HTTP/1.1 200 OK
Server: nginx/1.21.5
Date: Fri, 13 May 2022 10:08:59 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 28 Dec 2021 15:28:38 GMT
Connection: keep-alive
ETag: "61cb2d26-267"
Accept-Ranges: bytes
4、部署service端,在容器外进行访问
[root@k8s-master ~]# kubectl expose deployment zhao --port=80 --type=NodePort --dry-run=client -o yaml > zhao-service.yaml
[root@k8s-master ~]# vim zhao-service.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: sz
name: zhao
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30003
- name: web
port: 8080
protocol: TCP
targetPort: 8080
nodePort: 30004
- name: db
port: 6379
protocol: TCP
targetPort: 6379
nodePort: 30005
selector:
app: sz
type: NodePort
[root@k8s-master ~]# kubectl get pod,service -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/xin-54f665d556-jm2pn 1/1 Running 0 4h9m 10.244.169.168 k8s-node2 <none> <none>
pod/xin-54f665d556-rg6cm 1/1 Running 0 4h9m 10.244.36.97 k8s-node1 <none> <none>
pod/zz-7d5b8995b8-g8s5r 3/3 Running 0 98m 10.244.36.117 k8s-node1 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d3h <none>
service/zhao NodePort 10.111.136.204 <none> 80:30003/TCP,8080:30004/TCP,6379:30005/TCP 33m app=sz
# 使用浏览器访问:
1、任意node节点+指定nodePort端口
更多推荐
已为社区贡献8条内容
所有评论(0)