容器雲平臺No.3~kubernetes使用

今天是是第三篇,接着上一篇繼續html

首先,經過kubectl能夠看到,三個節點都正常運行

[root@k8s-master001 ~]# kubectl  get no 
NAME            STATUS   ROLES    AGE   VERSION
k8s-master001   Ready    master   16h   v1.19.0
k8s-master002   Ready    master   16h   v1.19.0
k8s-master003   Ready    master   16h   v1.19.0

如今來部署第一個服務,這裏以nginx爲例

[root@k8s-master001 ~]# kubectl  run  nginx --image=nginx --port=80
pod/nginx created

能夠看到,咱們再k8s集羣上建立了一個nginx應用,而後咱們經過以下命令查看狀態,發現如今nginx狀態爲Pendingnode

[root@k8s-master001 ~]# kubectl  get po
NAME    READY   STATUS    RESTARTS   AGE
nginx   0/1     Pending   0          7s

如今咱們使用kubectl describe命令來查看更多信息nginx

[root@k8s-master001 ~]# kubectl  describe po nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         <none>
Labels:       run=nginx
Annotations:  <none>
Status:       Pending
IP:           
IPs:          <none>
Containers:
  nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-6gd92 (ro)
Conditions:
  Type           Status
  PodScheduled   False 
Volumes:
  default-token-6gd92:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-6gd92
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason            Age   From  Message
  ----     ------            ----  ----  -------
  Warning  FailedScheduling  15s         0/3 nodes are available: 3 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.
  Warning  FailedScheduling  14s         0/3 nodes are available: 3 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.

從輸出信息能夠看到最後兩個事件3 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate.api

什麼意思呢?什麼意思呢?

這個提示表示,沒有節點能夠接受調度。bash

爲何會這樣呢?

由於咱們如今三個節點都是master節點,尚未添加node節點。默認狀況下,master打了污點(taint,後續再介紹),master節點不接收調度。
因爲咱們這裏是測試環境,沒有多餘的機器來做爲node節點,能夠手動刪除master的污點,讓master節點接收調度。
經過以下命令能夠查看taint信息:網絡

[root@k8s-master001 ~]# kubectl get no -o yaml | grep taint -A 5
          f:taints: {}
      manager: kube-controller-manager
      operation: Update
      time: "2020-09-10T09:10:40Z"
    - apiVersion: v1
      fieldsType: FieldsV1
--
    taints:
    - effect: NoSchedule
      key: node-role.kubernetes.io/master
  status:
    addresses:
    - address: 10.26.25.20
--
          f:taints: {}
      manager: kube-controller-manager
      operation: Update
      time: "2020-09-10T09:30:25Z"
    - apiVersion: v1
      fieldsType: FieldsV1
--
    taints:
    - effect: NoSchedule
      key: node-role.kubernetes.io/master
  status:
    addresses:
    - address: 10.26.25.21
--
          f:taints: {}
      manager: kube-controller-manager
      operation: Update
      time: "2020-09-10T09:35:43Z"
    - apiVersion: v1
      fieldsType: FieldsV1
--
    taints:
    - effect: NoSchedule
      key: node-role.kubernetes.io/master
  status:
    addresses:
    - address: 10.26.25.22

刪除污點node-role.kubernetes.io/master,以下所示

[root@k8s-master001 ~]# kubectl taint nodes --all node-role.kubernetes.io/master-
node/k8s-master001 untainted
node/k8s-master002 untainted
node/k8s-master003 untainted

而後如今在看nginx的狀態,已經變爲ContainerCreating,這表示已經分配到節點,開始建立nginx的pod了app

[root@k8s-master001 ~]# kubectl  get po
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          3m11s

使用kubectl get po -o wide查看,如今nginx已經正常運行了,並且能夠看到,nginx如今被分配到 k8s-master001節點上,Pod IP是10.244.0.4 運維

[root@k8s-master001 ~]# kubectl  get po -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE            NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          56m   10.244.0.4   k8s-master001   <none>           <none>

如今來訪問nginx,熟悉的200出現了~~

[root@k8s-master001 ~]# curl -I  10.244.0.4
HTTP/1.1 200 OK
Server: nginx/1.19.2
Date: Fri, 11 Sep 2020 02:22:41 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Aug 2020 14:50:35 GMT
Connection: keep-alive
ETag: "5f32b03b-264"
Accept-Ranges: bytes

可是若是在非集羣節點上訪問10.244.0.4,好比在本身的電腦上訪問curl

[~/b/] : curl -I 10.244.0.4
curl: (55) getpeername() failed with errno 22: Invalid argument

容器雲平臺No.3~kubernetes使用

如今就來解決這個問題,

一、咱們刪掉原來建立的nginx pod

[root@k8s-master001 ~]# kubectl  delete po nginx 
pod "nginx" deleted

二、建立nginx.yaml文件

[root@k8s-master001 ~]# cat nginx.yaml 
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  serviceName: nginx
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      terminationGracePeriodSeconds: 180
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          name: port
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx

三、執行kubectl apply -f nginx.yaml部署

[root@k8s-master001 ~]# kubectl  apply -f nginx.yaml 
statefulset.apps/nginx created
service/nginx created

[root@k8s-master001 ~]# kubectl  get po,ep,svc
NAME          READY   STATUS    RESTARTS   AGE
pod/nginx-0   1/1     Running   0          24s

NAME                   ENDPOINTS                                            AGE
endpoints/kubernetes   10.26.25.20:6443,10.26.25.21:6443,10.26.25.22:6443   17h
endpoints/nginx        10.244.2.3:80                                        23s

NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        17h
service/nginx        NodePort    10.106.27.213   <none>        80:30774/TCP   23s

如今能看到,建立了一個名爲nginx的service,把nginx默認的80端口映射到了30774
訪問集羣任意節點的IP:32650,這裏是10.26.25.20:30774ide

在集羣節點上訪問:

[root@k8s-master001 ~]# curl -I   10.26.25.20:30774
HTTP/1.1 200 OK
Server: nginx/1.19.2
Date: Fri, 11 Sep 2020 02:53:55 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Aug 2020 14:50:35 GMT
Connection: keep-alive
ETag: "5f32b03b-264"
Accept-Ranges: bytes

在筆記本電腦上訪問:

[~/b/wechatimages] : curl -I   10.26.25.20:30774
HTTP/1.1 200 OK
Server: nginx/1.19.2
Date: Fri, 11 Sep 2020 02:54:24 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 11 Aug 2020 14:50:35 GMT
Connection: keep-alive
ETag: "5f32b03b-264"
Accept-Ranges: bytes

若是仍是
容器雲平臺No.3~kubernetes使用
能夠先行了解kubernetes的pod,endpoint,service等概念。。。後續文章也會陸續講到。。。

注:文中圖片來源於網絡,若有侵權,請聯繫我及時刪除。Tips: 更多好文章,請關注公*號「菜鳥運維雜談」!!!

相關文章
相關標籤/搜索