本文主要是介绍k8s实例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
k8s实例
- 1. 创建一个deployment副本数为3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本
- 2. 给一个应用扩容副本数为3
- 3. 创建一个pod,其中运行着nginx、redis、mamcached
- 4. 给一个pod创建一个service,并可以通过Cluster/NodePort访问
- 5. 创建deployment和service,使用busybox容器nslookup解析service
1. 创建一个deployment副本数为3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本
[root@master 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
[root@master test]# kubectl apply -f test.yml
deployment.apps/test created
[root@master 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
// 滚动更新
[root@master haproxy]# kubectl set image deploy/test test=dockerimage123/httpd:v2.0
deployment.apps/test image updated
[root@master 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
[root@master 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
[root@master haproxy]# kubectl rollout history deploy test
deployment.apps/test
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3 <none>
[root@master 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>
[root@master haproxy]# kubectl rollout undo deploy test //回滚到上一个版本
deployment.apps/test rolled back
[root@master 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
[root@master test]# kubectl create deploy test --image nginx
deployment.apps/test1 created
[root@master test]# kubectl get pod
test1-6d5588f8cc-4hgnt 1/1 Running 0 6m33s
[root@master 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
[root@master 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
[root@master test]# kubectl apply -f test.yml
pod/test created
[root@master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test3 3/3 Running 0 18m
4. 给一个pod创建一个service,并可以通过Cluster/NodePort访问
[root@master 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
[root@master test]# kubectl apply -f test.yml
pod/test unchanged
service/test created
[root@master 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
[root@master test]# curl 10.110.1100.198
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
[root@master test]# curl 192.168.182.150:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
5. 创建deployment和service,使用busybox容器nslookup解析service
[root@master test]# kubectl run test2 --image busybox -- sleep 500
pod/test2 created
[root@master test]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test2 1/1 Running 0 22s
[root@master 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
这篇关于k8s实例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!