k8s实战
k8s实战创建一个deployment副本数3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本[root@master ~]# vi test.ymlapiVersion: apps/v1kind: Deploymentmetadata:labels:app: test01name: testspec:replicas: 3selector:matchLabels:app: te
·
k8s实战
创建一个deployment副本数3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本
[root@master ~]# vi test.yml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: test01
name: test
spec:
replicas: 3
selector:
matchLabels:
app: test01
template:
metadata:
labels:
app: test01
spec:
containers:
- image: best2001/httpd
name: test
[root@master ~]# kubectl apply -f test.yml
deployment.apps/test created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-d9b87b475-946kv 1/1 Running 0 6m39s
test-d9b87b475-j2lml 1/1 Running 0 6m39s
test-d9b87b475-lqp2b 1/1 Running 0 6m39s
[root@master ~]# kubectl set image deploy/test test=nginx:v0.1
deployment.apps/test image updated
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-5cfd585974-z958p 0/1 ContainerCreating 0 6s
test-d9b87b475-946kv 1/1 Running 0 21m
test-d9b87b475-j2lml 1/1 Running 0 21m
test-d9b87b475-lqp2b 1/1 Running 0 21m
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
test 3/3 1 3 22m
[root@master ~]# kubectl rollout history deployment test
deployment.apps/test
REVISION CHANGE-CAUSE
1 <none>
2 <none>
[root@master ~]# kubectl rollout history deployment test --revision=2
deployment.apps/test with revision #2
Pod Template:
Labels: app=test01
pod-template-hash=5cfd585974
Containers:
test:
Image: nginx:v0.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
[root@master ~]# kubectl rollout undo deployment test --to-revision=1
deployment.apps/test rolled back
[root@master ~]# kubectl rollout history deployment test
deployment.apps/test
REVISION CHANGE-CAUSE
2 <none>
3 <none>
给一个应用扩容副本数为3
[root@master ~]# kubectl scale deploy/test --replicas=3
deployment.apps/test scaled
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-d9b87b475-946kv 1/1 Running 0 28m
test-d9b87b475-j2lml 1/1 Running 0 28m
test-d9b87b475-lqp2b 1/1 Running 0 28m
test-c9dcccb69-l6wg5 1/1 Terminating 0 119s
test-c9dcccb69-wqjfk 1/1 Terminating 0 119s
创建一个pod,其中运行着nginx、redis、memcached 3个容器
[root@master ~]# vi test1.yml
apiVersion: v1
kind: Pod
metadata:
name: test
labels:
app: test01
spec:
containers:
- image: nginx
name: nginx
- image: redis
name: redis
- image: memcached
name: memcached
[root@master ~]# kubectl apply -f test1.yml
pod/test created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test 3/3 Running 0 3m55s
给一个pod创建service,并可以通过ClusterlP/NodePort访问
[root@master ~]# vim test2.yml
---
apiVersion: v1
kind: Pod
metadata:
name: test
labels:
app: test01
spec:
containers:
- image: best2001/nginx:v0.3
name: nginx
---
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: default
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: test01
type: NodePort
[root@master kubenetres]# kubectl apply -f test.yml
pod/test created
service/nginx created
[root@master kubenetres]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/test 1/1 Running 0 16s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx NodePort 10.100.135.23 <none> 80:31654/TCP 16s
#ClusterIP访问
[root@master kubenetres]# curl 10.100.135.23
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
···省略部分····
#NodePort访问
[root@master kubenetres]# curl 192.168.200.140:31654
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
···省略部分····
创建deployment和service,使用busybox容器nslookup解析service
[root@master kubenetres]# kubectl run test1 --image=busybox:1.28.4 -- sleep 24h
pod/test1 created
[root@master kubenetres]# kubectl get pods
NAME READY STATUS RESTARTS AGE
test 1/1 Running 0 10m
[root@master kubenetres]# kubectl exec -it test1 -- /bin/sh
/ # nslookup kubernetes
Server: 10.96.1.19
Address 1: 10.96.1.19 kube-dns.kube-system.svc.cluster.local
Name: kubernetes
Address 1: 10.96.1.1 kubernetes.default.svc.cluster.local
[root@master kubenetres]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.1.1 <none> 443/TCP 3d21h
更多推荐
已为社区贡献5条内容
所有评论(0)