kubernetes容器集群管理启动一个测试示例

创建nginx

创建3个nginx副本
[root@master bin]# kubectl run nginx --image=nginx --replicas=3
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/nginx created
查看创建的容器
[root@master bin]# kubectl get pod
NAME                     READY   STATUS              RESTARTS   AGE
nginx-7bb7cd8db5-4h2wn   0/1     ImagePullBackOff    0          67s
nginx-7bb7cd8db5-kgsxt   0/1     ContainerCreating   0          67s
nginx-7bb7cd8db5-tjw54   0/1     ImagePullBackOff    0          67s
查看所有资源对象
[root@master bin]# kubectl get pod
NAME                     READY   STATUS              RESTARTS   AGE
nginx-7bb7cd8db5-4h2wn   0/1     ImagePullBackOff    0          67s
nginx-7bb7cd8db5-kgsxt   0/1     ContainerCreating   0          67s
nginx-7bb7cd8db5-tjw54   0/1     ImagePullBackOff    0          67s
[root@master bin]# kubectl get all
NAME                         READY   STATUS             RESTARTS   AGE
pod/nginx-7bb7cd8db5-4h2wn   0/1     ImagePullBackOff   0          2m47s
pod/nginx-7bb7cd8db5-kgsxt   0/1     ImagePullBackOff   0          2m47s
pod/nginx-7bb7cd8db5-tjw54   0/1     ImagePullBackOff   0          2m47s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.10.10.1   <none>        443/TCP   9d

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/nginx   0/3     3            0           2m47s

NAME                                     DESIRED   CURRENT   READY   AGE
replicaset.extensions/nginx-7bb7cd8db5   3         3         0       2m47s

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   0/3     3            0           2m47s

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-7bb7cd8db5   3         3         0       2m47s
[root@master bin]# kubectl get all
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-7bb7cd8db5-4h2wn   1/1     Running   0          11m
pod/nginx-7bb7cd8db5-kgsxt   1/1     Running   0          11m
pod/nginx-7bb7cd8db5-tjw54   1/1     Running   0          11m

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.10.10.1   <none>        443/TCP   9d

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
deployment.extensions/nginx   3/3     3            3           11m

NAME                                     DESIRED   CURRENT   READY   AGE
replicaset.extensions/nginx-7bb7cd8db5   3         3         3       11m

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   3/3     3            3           11m

NAME                               DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-7bb7cd8db5   3         3         3       11m
[root@master bin]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7bb7cd8db5-4h2wn   1/1     Running   0          12m
nginx-7bb7cd8db5-kgsxt   1/1     Running   0          12m
nginx-7bb7cd8db5-tjw54   1/1     Running   0          12m
查看容器运行主机位置
[root@master bin]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP            NODE              NOMINATED NODE   READINESS GATES
nginx-7bb7cd8db5-4h2wn   1/1     Running   0          13m   172.17.54.2   192.168.238.128   <none>           <none>
nginx-7bb7cd8db5-kgsxt   1/1     Running   0          13m   172.17.10.3   192.168.238.129   <none>           <none>
nginx-7bb7cd8db5-tjw54   1/1     Running   0          13m   172.17.10.2   192.168.238.129   <none>           <none>
发布服务
[root@master bin]# kubectl expose deployment nginx --port=88 --target-port=80 --type=NodePort
service/nginx exposed
[root@master bin]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7bb7cd8db5-4h2wn   1/1     Running   0          17m
nginx-7bb7cd8db5-kgsxt   1/1     Running   0          17m
nginx-7bb7cd8db5-tjw54   1/1     Running   0          17m
[root@master bin]# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.10.10.1    <none>        443/TCP        9d
nginx        NodePort    10.10.10.40   <none>        88:30879/TCP   71s
node节点访问测试
[root@node01 ~]# curl 10.10.10.40:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

浏览器访问测试

查看日志
[root@master bin]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7bb7cd8db5-4h2wn   1/1     Running   0          22m
nginx-7bb7cd8db5-kgsxt   1/1     Running   0          22m
nginx-7bb7cd8db5-tjw54   1/1     Running   0          22m
[root@master bin]# kubectl logs nginx-7bb7cd8db5-4h2wn 
172.17.10.0 - - [17/Jul/2019:00:25:12 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
查看已经部署的容器
[root@master ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   3/3     3            3           2d
删除部署的容器
[root@master ~]# kubectl delete deployment nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl get deployment         
No resources found.

posted on 2019-07-20 20:12  yinshoucheng  阅读(282)  评论(0编辑  收藏  举报

导航