操作小练习
Service的端口号暴露写法
---
apiVersion: v1
kind: Service
metadata:
name: kube-node-service
labels:
name: kube-node-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 8081
protocol: TCP
nodePort: 32143
selector:
app: web
1、创建一个deployment副本数3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本
[root@master ~]
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: jiejiehao/httpd:v3
imagePullPolicy: IfNotPresent
[root@master ~]
deployment.apps/web configured
[root@master ~]
NAME READY UP-TO-DATE AVAILABLE AGE
web 3/3 3 3 47h
[root@master ~]
NAME READY STATUS RESTARTS AGE
web-5bbdcb8db8-blkpx 1/1 Running 0 5s
web-5bbdcb8db8-k6wll 1/1 Running 0 3s
web-5bbdcb8db8-pgfgb 1/1 Running 0 9s
//升级(kubectl set image deployment.apps/{deployment名称} {镜像名称}:={镜像名称}:{版本})
[root@master ~]
deployment.apps/web image updated
[root@master ~]
NAME READY STATUS RESTARTS AGE
web-58b97c8959-b9btn 1/1 Running 0 17s
web-58b97c8959-bw7wc 1/1 Running 0 14s
web-58b97c8959-t85hf 1/1 Running 0 15s
web-5bbdcb8db8-blkpx 1/1 Terminating 0 2m38s
web-5bbdcb8db8-k6wll 1/1 Terminating 0 2m36s
web-5bbdcb8db8-pgfgb 1/1 Terminating 0 2m42s
//回滚默认情况下, Deployment 的上线记录都会保留在系统中,以便可以随时回滚,查看 Deployment 的上线历史记录
[root@master ~]
deployment.apps/web
REVISION CHANGE-CAUSE
7 <none>
8 <none>
//回滚到上一个版本
[root@master ~]
deployment.apps/web rolled back
2、给一个应用扩容副本数为5
[root@master ~]
deployment.apps/web scaled
[root@master ~]
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/web 5/5 5 5 47h
NAME READY STATUS RESTARTS AGE
pod/web-5bbdcb8db8-5jjts 1/1 Running 0 41s
pod/web-5bbdcb8db8-cv7np 1/1 Running 0 13s
pod/web-5bbdcb8db8-dvvjq 1/1 Running 0 13s
pod/web-5bbdcb8db8-ntpkn 1/1 Running 0 43s
pod/web-5bbdcb8db8-v8sxm 1/1 Running 0 42s
3、创建一个pod,其中运行着nginx、redis、memcached 3个容器
[root@master ~]
apiVersion: v1
kind: Pod
metadata:
name: test
labels:
app: test
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
- name: redis
image: redis
imagePullPolicy: IfNotPresent
- name: memcached
image: memcached
imagePullPolicy: IfNotPresent
[root@master ~]
pod/test created
[root@master ~]
NAME READY STATUS RESTARTS AGE
test 3/3 Running 0 11m
4、给一个pod创建service,并可以通过ClusterlP/NodePort访问
[root@master ~]
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web1
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: web1
template:
metadata:
labels:
app: web1
spec:
containers:
- name: web1
image: jiejiehao/httpd:v1
---
apiVersion: v1
kind: Service
metadata:
name: web1
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
nodePort: 30000
selector:
app: web1
type: NodePort
[root@master ~]
error: must specify one of -f and -k
[root@master ~]
deployment.apps/web1 unchanged
service/web1 unchanged
[root@master ~]
NAME READY STATUS RESTARTS AGE
pod/web1-99dd54ccd-jw9w6 1/1 Running 1 33h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/web1 NodePort 10.105.63.49 <none> 80:30000/TCP 33s
[root@master ~]
test page on v1
[root@master ~]
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready control-plane,master 8d v1.20.0 192.168.47.115 <none> Red Hat Enterprise Linux 8.2 (Ootpa) 4.18.0-193.el8.x86_64 docker://20.10.12
node1 Ready <none> 8d v1.20.0 192.168.47.120 <none> Red Hat Enterprise Linux 8.2 (Ootpa) 4.18.0-193.el8.x86_64 docker://20.10.12
node2 Ready <none> 8d v1.20.0 192.168.47.121 <none> Red Hat Enterprise Linux 8.2 (Ootpa) 4.18.0-193.el8.x86_64 docker://20.10.12
[root@master ~]
test page on v1
[root@master ~]
test page on v1
[root@master ~]
test page on v1
5、创建deployment和service,使用busybox容器nslookup解析service
[root@master ~]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd NodePort 10.103.50.196 <none> 80:30263/TCP 5d22h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8d
web1 NodePort 10.105.63.49 <none> 80:30000/TCP 33h
[root@master ~]
If you don't see a command prompt, try pressing enter.
/
test page on v1
/
test page on v1
/
Server: 10.96.0.10
Address: 10.96.0.10:53
Name: web1.default.svc.cluster.local
Address: 10.105.63.49
*** Can't find web1.default.svc.cluster.local: No answer