k8s实例
程序员文章站
2022-03-12 11:58:30
...
k8s实例
1. 创建一个deployment副本数为3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本
[[email protected] test]# cat test.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
labels:
app: test
spec:
replicas: 3
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- image: dockerimages123/httpd:v0.1
name: test
imagePullPolicy: IfNotPresent
[[email protected] test]# kubectl apply -f test.yml
deployment.apps/test created
[[email protected] haproxy]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-85cd869cd6-7524t 1/1 Running 0 4m49s
test-85cd869cd6-8khgw 1/1 Running 0 4m49s
test-85cd869cd6-kc7fw 1/1 Running 0 4m49s
// 滚动更新
[[email protected] haproxy]# kubectl set image deploy/test test=dockerimage123/httpd:v2.0
deployment.apps/test image updated
[[email protected] haproxy]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-6cfbd9b7f-5dxjk 1/1 Running 0 13s
test-6cfbd9b7f-8kbs8 1/1 Running 0 11s
test-6cfbd9b7f-sv9km 1/1 Running 0 14s
test-85cd869cd6-7524t 1/1 Terminating 0 9m24s
test-85cd869cd6-8khgw 1/1 Terminating 0 9m24s
test-85cd869cd6-kc7fw 1/1 Terminating 0 9m24s
[[email protected] haproxy]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-6cfbd9b7f-5dxjk 1/1 Running 0 103s
test-6cfbd9b7f-8kbs8 1/1 Running 0 101s
test-6cfbd9b7f-sv9km 1/1 Running 0 104s
[[email protected] haproxy]# kubectl rollout history deploy test
deployment.apps/test
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
[[email protected] haproxy]# kubectl rollout history deploy test --revision=3
deployment.apps/test with revision #3 //查看上一个版本的信息
Pod Template:
Labels: app=test
pod-template-hash=6cfbd9b7f
Containers:
test:
Image: dockerimages123/httpd:v2.0
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
[[email protected] haproxy]# kubectl rollout undo deploy test //回滚到上一个版本
deployment.apps/test rolled back
[[email protected] haproxy]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-6cfbd9b7f-5dxjk 1/1 Running 0 32m
test-6cfbd9b7f-8kbs8 1/1 Running 0 32m
test-6cfbd9b7f-sv9km 1/1 Running 0 32m
2. 给一个应用扩容副本数为3
[[email protected] test]# kubectl create deploy test --image nginx
deployment.apps/test1 created
[[email protected] test]# kubectl get pod
test1-6d5588f8cc-4hgnt 1/1 Running 0 6m33s
[[email protected] test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-5f6778868d-4t9xd 1/1 Running 0 2m23s
test1-6d5588f8cc-4hgnt 1/1 Running 0 8m51s
test1-6d5588f8cc-b9qsw 1/1 Running 0 49s
test1-6d5588f8cc-vkvh5 1/1 Running 0 49s
3. 创建一个pod,其中运行着nginx、redis、mamcached
[[email protected] test]# cat test.yml
---
apiVersion: v1
kind: Pod
metadata:
name: test
labels:
app: test
spec:
containers:
- image: nginx
name: nginx
- image: redis
name: redis
- image: memcached
name: memcached
[[email protected] test]# kubectl apply -f test.yml
pod/test created
[[email protected] test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test3 3/3 Running 0 18m
4. 给一个pod创建一个service,并可以通过Cluster/NodePort访问
[[email protected] test]# cat test.yml
---
apiVersion: v1
kind: Pod
metadata:
name: test
labels:
app: test
spec:
containers:
- image: nginx
name: nginx
---
apiVersion: v1
kind: Service
metadata:
name: test1
spec:
ports:
- port: 80
targetPort: 80
nodePort: 30001
selector:
app: test1
type: NodePort
[[email protected] test]# kubectl apply -f test.yml
pod/test unchanged
service/test created
[[email protected] test]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/test 1/1 Running 0 50s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d1h
service/test4 NodePort 10.110.1100.198 <none> 80:30001/TCP 50s
[[email protected] test]# curl 10.110.1100.198
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
[[email protected] test]# curl 192.168.182.150:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
5. 创建deployment和service,使用busybox容器nslookup解析service
[[email protected] test]# kubectl run test2 --image busybox -- sleep 500
pod/test2 created
[[email protected] test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test2 1/1 Running 0 22s
[[email protected] test]# kubectl exec -it test2 -- /bin/sh
/ # nslookup kubernetes
Server: 10.96.0.10
Address: 10.96.0.10:53
Name: kubernetes.default.svc.cluster.local
Address: 10.96.0.1
*** Can't find kubernetes.svc.cluster.local: No answer
*** Can't find kubernetes.cluster.local: No answer
*** Can't find kubernetes.example.com: No answer
*** Can't find kubernetes.default.svc.cluster.local: No answer
*** Can't find kubernetes.svc.cluster.local: No answer
*** Can't find kubernetes.cluster.local: No answer
*** Can't find kubernetes.example.com: No answer
上一篇: 手把手教你使用小程序创建画布时钟(实例)
下一篇: 浅析小程序怎么实现登录功能