部署你的第一个应用程序到k8s集群

    技术2025-05-31  24

    部署你的第一个应用程序到k8s集群

    看到这里,求知欲饥渴难耐的你一定在想,怎么部署的我们应用程序到集群里面去呢?来个简单的,只需要两步:(这里本文使用nginx镜像当我们的应用程序,因为nginx 简单,运行起来后直接可以用浏览器访问网页了。)

    第一步:在master 节点上创建一个deployment

    kubectl create deployment nginx --image=nginx

    效果如下,可以看到一个叫nginx的deployment创建成功了。

    root@ubuntu:/home/cong# kubectl create deployment nginx --image=nginx deployment.apps/nginx created root@ubuntu:/home/cong# kubectl get deployments NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE nginx     1         1         1            1           11m

       第二步:创建一个service

    kubectl create service nodeport nginx --tcp 80:80

     效果如下,可以看到一个叫nginx的service创建成功了,这里kubectl get svc是kubectl get services的简写。

    root@ubuntu:/home/cong# kubectl create service nodeport nginx --tcp 80:80 service/nginx created root@ubuntu:/home/cong# kubectl get svc NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        3d nginx        NodePort    10.107.237.157   <none>        80:30601/TCP   11s

    在slave节点上执行下面的命令验证一下nginx有没有部署成功。

    curl localhost:30601   或者   curl kube-slave:30601

     效果如下:

    root@ubuntu:/home/cong# curl localhost:30601 <!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>

    用浏览器打开试试,nginx的首页显示出来了。

    简单吧,是不是信心大增了?!

    为了练习更多复杂的命令,我们将上面建好的deployments/nginx, services/nginx 删除先,命令如下:

    root@ubuntu:/home/cong# kubectl delete deployments/nginx services/nginx deployment.extensions "nginx" deleted service "nginx" deleted

     

    Processed: 0.019, SQL: 9