1 创建一个deployment副本数3,然后滚动更新镜像版本,并记录这个更新记录,最后再回滚到上一个版本

[root@master mainfest]# cat test.yaml
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: zhaojie10/httpd:v0.1
        name: test
        imagePullPolicy: IfNotPresent
      
[root@master mainfest]# kubectl apply -f test.yaml 
deployment.apps/test created
[root@master mainfest]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
test-7745d6779-9mk6h   1/1     Running   0          6m9s
test-7745d6779-ldwbg   1/1     Running   0          6m9s
test-7745d6779-psv5g   1/1     Running   0          6m9s

//升级版本
[root@master mainfest]# kubectl set image deploy/test test=zhaojie10/httpd:v0.1
deployment.apps/test image updated

[root@master mainfest]# kubectl get pod		//正在升级中(蓝绿部署)
NAME                     READY   STATUS              RESTARTS   AGE
test-7697c79b78-5rn9f   0/1     ContainerCreating   0          14s
test-7745d6779-9mk6h    1/1     Running             0          7m41s
test-7745d6779-ldwbg    1/1     Running             0          7m41s
test-7745d6779-psv5g    1/1     Running             0          7m41s

[root@master mainfest]# kubectl get pod		//已经升级完成
NAME                     READY   STATUS    RESTARTS   AGE
test-7697c79b78-5rn9f   1/1     Running   0          3m22s
test-7697c79b78-9fs4n   1/1     Running   0          89s
test-7697c79b78-wb95c   1/1     Running   0          88s

//回滚到上一个版本
[root@master mainfest]# kubectl rollout history deploy test	//查看上线记录
deployment.apps/test
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

[root@master mainfest]# kubectl rollout undo deploy	test --to-revision=1	//回滚到上一个版本
deployment.apps/test rolled back
[root@master mainfest]# kubectl get pod
NAME                    READY   STATUS    RESTARTS   AGE
test-7745d6779-5kcj5   1/1     Running   0          57s
test-7745d6779-hjs2h   1/1     Running   0          55s
test-7745d6779-z424c   1/1     Running   0          56s

//当前处于版本1,但是只使用了两个镜像,所以被覆盖,替换了。
[root@master ~]# kubectl rollout history deploy test
deployment.apps/web 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

2 给一个应用扩容副本数为3

[root@master mainfest]# kubectl create deploy test1 --image nginx:latest
deployment.apps/test1 created

[root@master mainfest]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
test1-65bf7bd56f-7qq9r   1/1     Running   0          17s

[root@master mainfest]# kubectl scale deploy/test1 --replicas 3
deployment.apps/test1 scaled
[root@master mainfest]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
test1-65bf7bd56f-7qq9r   1/1     Running   0          112s
test1-65bf7bd56f-qc29q   1/1     Running   0          67s
test1-65bf7bd56f-rskpf   1/1     Running   0          67s

3 创建一个pod,其中运行着nginx、redis、memcached 3个容器

[root@master mainfest]# cat  test2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test2
  labels: 
    app: test2
spec: 
  containers:
  - image: nginx
    name: nginx
  - image: redis
    name: redis
  - image: memcached
    name: memcached
    
[root@master mainfest]# kubectl apply -f test2.yaml 
pod/test2 created
[root@master mainfest]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
test2   3/3     Running   0          72s

4 给一个pod创建service,并可以通过ClusterlP/NodePort访问

[root@master mainfest]# cat test3.yaml
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: myapp
  namespace: default 
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: myapp 
      release: v1
  template:
    metadata: 
      labels: 
        app: myapp 
        release: v1 
    spec: 
      containers: 
      - name: myapp
        image: zhaojie10/httpd:v0.1
        imagePullPolicy: IfNotPresent
---
apiVersion: v1 
kind: Service 
metadata: 
  name: myapp
  namespace: default
spec: 
  type: NodePort  
  selector: 
    app: myapp
    release: v1
  ports: 
  - name: httpd
    port: 80
    targetPort: 80
    nodePort: 30001

//指定资源定义文件,部署资源
[root@master ~]# kubectl apply -f test3.yaml 
pod/test3 created
service/test3 created

//查看pods,service状态
[root@master ~]# kubectl get pod,svc
NAME                               READY   STATUS    RESTARTS   AGE
pod/myapp-deploy-79b6dccb8-h7mlh   1/1     Running   0          12s

NAME                         TYPE           CLUSTER-IP      EXTERNAL-IP          PORT(S)        AGE
service/kubernetes           ClusterIP      10.96.0.1       <none>               443/TCP        6d4h
service/myapp                NodePort       10.99.132.198   <none>               80:30001/TCP   12s

# 使用CLUSTER-IP访问
[root@master ~]# curl 10.99.132.198
<html><body><h1>It works!</h1></body></html>

# 使用NodePort访问
[root@master ~]# curl 192.168.58.110:30001
<html><body><h1>It works!</h1></body></html>

5 创建deployment和service,使用busybox容器nslookup解析service

[root@master ~]# cat busybox.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: busybox
  name: test4
spec:
  containers:
  - command:
    - sleep
    - "9000"
    image: busybox
    imagePullPolicy: Always
    name: test4

# 部署
[root@master ~]# kubectl apply -f busybox.yaml 
pod/test-busybox created

# 查看pod状态
[root@master ~]# kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
test4                           1/1     Running   0          19s

# 使用exec -it 与busybox进行交互
[root@master ~]# kubectl exec -it test4 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # nslookup myapp-externalname            //使用 nslookup 解析service
Server:         10.96.0.10
Address:        10.96.0.10:53

** server can't find myapp-deploy.default.svc.cluster.local: NXDOMAIN

*** Can't find myapp-deploy.svc.cluster.local: No answer
*** Can't find myapp-deploy.cluster.local: No answer
*** Can't find myapp-deploy.default.svc.cluster.local: No answer
*** Can't find myapp-deploy.svc.cluster.local: No answer
*** Can't find myapp-deploy.cluster.local: No answer

/ # 
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐