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

[root@k8s-master ~]# cat test.yaml 
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: web
spec: 
  replicas: 3  
  revisionHistoryLimit: 5 
  strategy: 
    rollingUpdate: 
      maxSurge: 50%
      maxUnavailable: 50%
    type: RollingUpdate
  selector: 
    matchLabels: 
      app: httpd
  template: 
    metadata: 
      labels: 
        app: httpd
    spec: 
      containers: 
      - name: httpd
        image: xxkk/httpd:v1.0
        imagePullPolicy: IfNotPresent

[root@k8s-master ~]# kubectl apply -f test.yaml 
deployment.apps/web created
[root@k8s-master ~]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-76fd645644-2rlvx   1/1     Running   0          8s
web-76fd645644-4dsdr   1/1     Running   0          8s
web-76fd645644-97r6n   1/1     Running   0          8s
[root@k8s-master ~]# kubectl rollout history deployment/web
deployment.apps/web 
REVISION  CHANGE-CAUSE
1         <none>

[root@k8s-master ~]# vi test.yaml 
......
containers: 
      - name: httpd
        image: xxkk/httpd:v2.0  //更改镜像
        imagePullPolicy: IfNotPresent
 ......
[root@k8s-master ~]# kubectl apply -f test.yaml 
deployment.apps/web configured
[root@k8s-master ~]# kubectl get pods
NAME                  READY   STATUS    RESTARTS   AGE
web-55fcc788f-ct5m6   1/1     Running   0          8m
web-55fcc788f-f4q4v   1/1     Running   0          8m
web-55fcc788f-h5zzt   1/1     Running   0          8m

# 回滚至版本1
[root@k8s-master ~]# kubectl rollout history deployment/web
deployment.apps/web 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

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

2. 给一个应用扩容副本为4

[root@k8s-master ~]# vi test.yaml 
[root@k8s-master ~]# cat test.yaml 
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: web
spec: 
  replicas: 4   #修改为4

[root@k8s-master ~]# kubectl apply -f test.yaml 
deployment.apps/web configured
[root@k8s-master ~]# kubectl get pods
NAME                   READY   STATUS        RESTARTS   AGE
web-55fcc788f-7w9dp    1/1     Running       0          51s
web-55fcc788f-jj47k    1/1     Running       0          51s
web-55fcc788f-vhllq    1/1     Running       0          51s
web-55fcc788f-xw986    1/1     Running       0          51s


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

[root@k8s-master ~]# vi test1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test3
  labels: 
    app: test3
spec: 
  containers:
  - image: nginx
    name: nginx
  - image: redis
    name: redis
  - image: memcached
    name: memcached

[root@k8s-master ~]# kubectl apply -f test1.yaml 
pod/test3 created

[root@k8s-master ~]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
test3   3/3     Running   0          72s

4.给一个pod创建service,并可以通过ClusterIP/NodePort

[root@k8s-master ~]# vi hostwork.yaml
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: myapp-deploy 
  namespace: default 
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: myapp 
      release: v1
  template:
    metadata: 
      labels: 
        app: myapp 
        release: v1 
    spec: 
      containers: 
      - name: myapp
        image: xxkk/file_httpd:v0.2
        imagePullPolicy: IfNotPresent
---
apiVersion: v1 
kind: Service 
metadata: 
  name: myapp-nodeport
  namespace: default
spec: 
  type: NodePort  
  selector: 
    app: myapp
    release: v1
  ports: 
  - name: httpd
    port: 80
    targetPort: 80
    nodePort: 31250

[root@k8s-master ~]# kubectl apply -f hostwork.yaml 
deployment.apps/myapp-deploy created
service/myapp-nodeport configured
[root@k8s-master ~]# kubectl get pod,svc
NAME                                READY   STATUS        RESTARTS   AGE
pod/myapp-deploy-5bc8b6987d-2lhfz   1/1     Running       0          69s
pod/web-55fcc788f-7w9dp             0/1     Terminating   0          4m50s
pod/web-55fcc788f-xw986             0/1     Terminating   0          4m50s

NAME                     TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP        7d22h
service/myapp-nodeport   NodePort    10.97.249.121   <none>        80:31250/TCP   43m
[root@k8s-master ~]# curl 10.97.249.121
<html><body><h1>It works!</h1></body></html>
[root@k8s-master ~]# curl 192.168.72.142:31250
<html><body><h1>It works!</h1></body></html>

5. 创建deployment和service,使用centos容器Nslookup解析service

[root@k8s-master ~]# cat hostwork.yaml
apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: myapp-deploy 
  namespace: default 
spec: 
  replicas: 1
  selector: 
    matchLabels: 
      app: myapp 
      release: v1
  template:
    metadata: 
      labels: 
        app: myapp 
        release: v1 
    spec: 
      containers: 
      - name: myapp
        image: xxkk/file_httpd:v0.2
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata: 
  name: myapp-externalname
  namespace: default
spec: 
  type: ExternalName 
  externalName: web.test.example.com
[root@k8s-master ~]# kubectl apply -f hostwork.yaml 
deployment.apps/myapp-deploy created
service/myapp-externalname created
[root@k8s-master ~]# kubectl get pod,svc
NAME                                READY   STATUS    RESTARTS   AGE
pod/myapp-deploy-5bc8b6987d-hnzjw   1/1     Running   0          102s

NAME                         TYPE           CLUSTER-IP   EXTERNAL-IP            PORT(S)   AGE
service/kubernetes           ClusterIP      10.96.0.1    <none>                 443/TCP   7d22h
service/myapp-externalname   ExternalName   <none>       web.test.example.com   <none>    102s

# 辨析一个busybox资源定义文件
[root@k8s-master ~]# cat busybox.yaml 
apiVersion: v1
kind: Pod
metadata:
  labels:
    app: busybox-pod
  name: test-busybox
spec:
  containers:
  - command:
    - sleep
    - "3600"
    image: busybox
    imagePullPolicy: Always
    name: test-busybox
[root@k8s-master ~]# kubectl apply -f busybox.yaml 
pod/test-busybox created
[root@k8s-master ~]# kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
myapp-deploy-5bc8b6987d-hnzjw   1/1     Running   0          3m56s
test-busybox                    1/1     Running   0          26s

[root@k8s-master ~]# kubectl exec -it test-busybox /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
Server:         10.96.0.10
Address:        10.96.0.10:53

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

*** Can't find myapp-externalname.svc.cluster.local: No answer
*** Can't find myapp-externalname.cluster.local: No answer
*** Can't find myapp-externalname.localdomain: No answer
*** Can't find myapp-externalname.default.svc.cluster.local: No answer
*** Can't find myapp-externalname.svc.cluster.local: No answer
*** Can't find myapp-externalname.cluster.local: No answer
*** Can't find myapp-externalname.localdomain: No answer
Logo

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

更多推荐