Kubernetes基礎:使用Deployment創建Pod

隨着RC的退出,Deployment作爲Pod的控制器之一使用地越來越廣泛,這篇文章以nginx服務爲例,介紹一下使用的方式。

事前準備

本文使用Kubernetes 1.17.2,可參看下文進行快速環境搭建:

[root@host131 ansible]# kubectl get node -o wide
NAME              STATUS   ROLES    AGE     VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
192.168.163.131   Ready    <none>   2m25s   v1.17.2   192.168.163.131   <none>        CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   docker://19.3.5
[root@host131 ansible]# 

Deployment配置文件

使用如下yaml文件用於設定Deployment

[root@host131 nginx]# cat deployment.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-host
        image: nginx:1.17.8-alpine
        ports:
        - containerPort: 80
...
[root@host131 nginx]# 
  • 注意事項:matchLabels的內容需要與template中metadata的labels內容匹配,不過不用擔心,在創建時會進行檢查,比如將matchLabels改爲app: nginx-app1,創建時會提示如下錯誤信息
[root@host131 nginx]# kubectl create -f deployment.yml 
The Deployment "nginx-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"nginx-app1"}: `selector` does not match template `labels`
[root@host131 nginx]#

Service配置文件

使用如下Service配置文件

[root@host131 nginx]# cat service.yml 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx-service-app
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30000
  selector:
    app: nginx-app
...
[root@host131 nginx]# 
  • 注意事項:selector關聯的需要是Deployment中template定義的label。

生成Deployment

使用Deployment的配置文件生成Deployment

[root@host131 nginx]# kubectl create -f deployment.yml 
deployment.apps/nginx-deployment created
[root@host131 nginx]#

結果確認

[root@host131 nginx]# kubectl get deployment -o wide |grep nginx
nginx-deployment   1/1     1            1           34s    nginx-host   nginx:1.17.8-alpine                  app=nginx-app
[root@host131 nginx]# 
[root@host131 nginx]# kubectl get pods -o wide |grep nginx
nginx-deployment-7c5b8fc568-wkh9b   1/1     Running   0          51s    10.254.152.6   192.168.163.131   <none>           <none>
[root@host131 nginx]# 
[root@host131 nginx]# 
[root@host131 nginx]# kubectl exec -it nginx-deployment-7c5b8fc568-wkh9b sh
/ # hostname
nginx-deployment-7c5b8fc568-wkh9b
/ #

生成Service

[root@host131 nginx]# kubectl create -f service.yml 
service/nginx-service created
[root@host131 nginx]#

結果確認

[root@host131 nginx]# kubectl get service -o wide |grep nginx
nginx-service   NodePort    10.254.77.182   <none>        80:30000/TCP   9s      app=nginx-app
[root@host131 nginx]# 

nginx使用:集羣地址訪問

在集羣所在的集羣中,可以使用集羣地址+80端口進行訪問

[root@host131 nginx]# curl http://10.254.77.182
<!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@host131 nginx]#

也可以利用NodePort端口結合宿主機IP進行訪問

[root@host131 nginx]# curl http://192.168.163.131:30000
<!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@host131 nginx]# 

當然這種方式也可以直接使用瀏覽器進行確認
在這裏插入圖片描述

總結

Deployment作爲Pod的控制器,可以對Pod進行創建、滾動升級等常見操作,這篇文章以nginx服務爲例進行說明,介紹了Deployment和Service在Kubernetes中最常見的使用方式之一。

發佈了1086 篇原創文章 · 獲贊 1301 · 訪問量 403萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章