Kubernetes实战(十一)——创建第一个k8s应用

部署之前先来回顾一下K8s中的核心概念:

Replication Controller ,RC

Kubernetes实战(十一)——创建第一个k8s应用

Replica Set ,RS

Kubernetes实战(十一)——创建第一个k8s应用

Deployment

Kubernetes实战(十一)——创建第一个k8s应用

Service

Kubernetes实战(十一)——创建第一个k8s应用

1.创建第一个k8s应用

1.创建一个测试用的deployment

[root@linux-node1 ~]# kubectl run net-test --image=alpine --replicas=2 sleep 360000
deployment.apps "net-test" created

2.查看获取IP情况

[root@linux-node1 ~]# kubectl get pod -o wide
NAME                        READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-dk28b   1/1       Running   0          2m        10.2.91.2   192.168.56.12
net-test-5767cb94df-fh4vr   1/1       Running   0          2m        10.2.38.2   192.168.56.13

3.测试联通性

[root@linux-node1 ~]# ping -c 2 10.2.38.2
PING 10.2.38.2 (10.2.38.2) 56(84) bytes of data.
64 bytes from 10.2.38.2: icmp_seq=1 ttl=63 time=0.291 ms
64 bytes from 10.2.38.2: icmp_seq=2 ttl=63 time=1.31 ms


2.创建nginx deployment

1.创建一个nginx的deployment

[root@linux-node1 ~]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
 labels:
   app: nginx
spec:
 replicas: 3
 selector:
   matchLabels:
     app: nginx
 template:
   metadata:
     labels:
       app: nginx
   spec:
     containers:
     - name: nginx
       image: nginx:1.13.12
       ports:
       - containerPort: 80

[root@linux-node1 ~]# kubectl create -f nginx-deployment.yaml
deployment.apps "nginx-deployment" created

2.查看deployment及详细信息

[root@linux-node1 ~]# kubectl get deployment
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
net-test           2         2         2            2           7m
nginx-deployment   3         3         3            0           1m
[root@linux-node1 ~]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Sun, 23 Sep 2018 22:24:39 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision=1
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total

3.查看pod信息

[root@linux-node1 ~]# kubectl get pod -o wide
NAME                                READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-dk28b           1/1       Running   0          12m       10.2.91.2   192.168.56.12
net-test-5767cb94df-fh4vr           1/1       Running   0          12m       10.2.38.2   192.168.56.13
nginx-deployment-6c45fc49cb-42sgq   1/1       Running   0          6m        10.2.91.3   192.168.56.12
nginx-deployment-6c45fc49cb-f9sj5   1/1       Running   0          6m        10.2.38.3   192.168.56.13
nginx-deployment-6c45fc49cb-tmgld   1/1       Running   0          6m        10.2.38.4   192.168.56.13

4.访问nginx

[root@linux-node1 ~]# curl --head http://10.2.91.3
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Sun, 23 Sep 2018 14:35:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

5.更新deployment,常用于升降级

[root@linux-node1 ~]# kubectl set image deployment/nginx-deployment nginx=nginx:1.12.2 --record
deployment.apps "nginx-deployment" image updated

[root@linux-node1 ~]# kubectl get deployment -o wide
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES         SELECTOR
net-test           2         2         2            2           20m       net-test     alpine         run=net-test
nginx-deployment   3         4         1            3           14m       nginx        nginx:1.12.2   app=nginx

#查看版本是否降级
[root@linux-node1 ~]# curl --head http://10.2.91.4
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sun, 23 Sep 2018 14:41:21 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Jul 2017 13:29:18 GMT
Connection: keep-alive
ETag: "5964d2ae-264"
Accept-Ranges: bytes

#查看回滚记录
[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment
deployments "nginx-deployment"
REVISION  CHANGE-CAUSE
1         <none>
2         kubectl set image deployment/nginx-deployment nginx=nginx:1.12.2 --record=true

6.快速回滚到上一版本

#查看具体某一个版本的历史
[root@linux-node1 ~]# kubectl rollout history deployment/nginx-deployment --revision=1
deployments "nginx-deployment" with revision #1
Pod Template:
 Labels: app=nginx
pod-template-hash=2701970576
 Containers:
  nginx:
   Image: nginx:1.13.12
   Port: 80/TCP
   Host Port: 0/TCP
   Environment: <none>
   Mounts: <none>
 Volumes: <none>

#快速回滚到上一个版本
[root@linux-node1 ~]# kubectl rollout undo deployment/nginx-deployment
deployment.apps "nginx-deployment"

[root@linux-node1 ~]# curl --head http://10.2.38.7
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Sun, 23 Sep 2018 14:49:24 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

Pod的IP每次都会变化,因此我们需要创建一个service,通过VIP来访问。


3.创建nginx service

1.创建一个nginx的service

[root@linux-node1 ~]# cat nginx-service.yaml
kind: Service
apiVersion: v1
metadata:
 name: nginx-service
spec:
 selector:
   app: nginx
 ports:
 - protocol: TCP
   port: 80
   targetPort: 80

[root@linux-node1 ~]# kubectl create -f nginx-service.yaml
service "nginx-service" created

2.访问service

[root@linux-node1 ~]# kubectl get service -o wide
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE       SELECTOR
kubernetes      ClusterIP   10.1.0.1       <none>        443/TCP   9h        <none>
nginx-service   ClusterIP   10.1.179.125   <none>        80/TCP    2m        app=nginx

#在node2上访问VIP没有问题
[root@linux-node2 ~]# curl --head http://10.1.179.125
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Sun, 23 Sep 2018 14:57:21 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

#通过ipvs转发实现负载均衡
[root@linux-node2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr persistent 10800
 -> 192.168.56.11:6443           Masq    1      0          0    
TCP  10.1.179.125:80 rr
 -> 10.2.38.6:80                 Masq    1      0          0    
 -> 10.2.38.7:80                 Masq    1      0          0    
 -> 10.2.91.6:80                 Masq    1      0          1  

3.快速扩容

[root@linux-node1 ~]# kubectl scale deployment nginx-deployment --replicas 5
deployment.extensions "nginx-deployment" scaled
[root@linux-node1 ~]# kubectl get pod -o wide
NAME                                READY     STATUS    RESTARTS   AGE       IP          NODE
net-test-5767cb94df-dk28b           1/1       Running   0          42m       10.2.91.2   192.168.56.12
net-test-5767cb94df-fh4vr           1/1       Running   0          42m       10.2.38.2   192.168.56.13
nginx-deployment-6c45fc49cb-42mqn   1/1       Running   0          14s       10.2.38.8   192.168.56.13
nginx-deployment-6c45fc49cb-bsr2k   1/1       Running   0          13m       10.2.38.7   192.168.56.13
nginx-deployment-6c45fc49cb-qv9rd   1/1       Running   0          13m       10.2.91.6   192.168.56.12
nginx-deployment-6c45fc49cb-rqn6x   1/1       Running   0          13m       10.2.38.6   192.168.56.13
nginx-deployment-6c45fc49cb-sq4cw   1/1       Running   0          14s       10.2.91.7   192.168.56.12

[root@linux-node2 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
 -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr persistent 10800
 -> 192.168.56.11:6443           Masq    1      0          0        
TCP  10.1.179.125:80 rr
 -> 10.2.38.6:80                 Masq    1      0          0        
 -> 10.2.38.7:80                 Masq    1      0          0        
 -> 10.2.38.8:80                 Masq    1      0          0        
 -> 10.2.91.6:80                 Masq    1      0          0        
 -> 10.2.91.7:80                 Masq    1      0          0  

anzhihe 安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/3055.html | ☆★★每天进步一点点,加油!★★☆ | 

您可能还感兴趣的文章!

发表评论

电子邮件地址不会被公开。 必填项已用*标注