1. 程式人生 > >Kubernetes相關操作記錄

Kubernetes相關操作記錄

跟著學習操作了一遍,還不是很瞭解,先粗糙記錄下 參考並感謝 三臺ubuntu虛擬機器並安裝好了docker,後續的所有操作都是使用root賬戶。 虛擬機器具體資訊如下表:

系統型別 IP地址 節點角色 Hostname
ubuntu16.04 168.174.135 worker server01
ubuntu16.04 168.174.136 master server02
ubuntu16.04 168.174.137 worker server03

kubectl version

kubectl get pods

kubectl run kubernetes-bootcamp --image=jocatalin/kubernetes-bootcamp:v1 --port=8080

kubectl get deployments

kubectl get pods -o wide

kubectl describe pods kubernetes-bootcamp-6b7849c495-z6mxx

[email protected]:~# curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/kubernetes-bootcamp-6b7849c495-z6mxx/
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6b7849c495-z6mxx | v=1

擴縮容

[email protected]:~# kubectl get deploy
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1         1         1            1           20m

擴容命令

kubectl scale deploy kubernetes-bootcamp --replicas=4

[email protected]rver02:~# kubectl scale deploy kubernetes-bootcamp --replicas=4
deployment "kubernetes-bootcamp" scaled

[email protected]:~# kubectl get deploy
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   4         4         4            2           21m

ContainerCreating中

[email protected]:~# kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
kubernetes-bootcamp-6b7849c495-4n9gd   1/1       Running             0          1m
kubernetes-bootcamp-6b7849c495-c9d6q   0/1       ContainerCreating   0          1m
kubernetes-bootcamp-6b7849c495-txpfz   0/1       ContainerCreating   0          1m
kubernetes-bootcamp-6b7849c495-z6mxx   1/1       Running             0          22m
[email protected]:~# kubectl get pods -o wide
NAME                                   READY     STATUS              RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-6b7849c495-4n9gd   1/1       Running             0          2m        172.20.40.193   192.168.174.137
kubernetes-bootcamp-6b7849c495-c9d6q   0/1       ContainerCreating   0          2m        <none>          192.168.174.135
kubernetes-bootcamp-6b7849c495-txpfz   0/1       ContainerCreating   0          2m        <none>          192.168.174.136
kubernetes-bootcamp-6b7849c495-z6mxx   1/1       Running             0          22m       172.20.40.192   192.168.174.137
[email protected]:~# kubectl get pods -o wide
NAME                                   READY     STATUS    RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-6b7849c495-4n9gd   1/1       Running   0          8m        172.20.40.193   192.168.174.137
kubernetes-bootcamp-6b7849c495-c9d6q   1/1       Running   0          8m        172.20.188.0    192.168.174.135
kubernetes-bootcamp-6b7849c495-txpfz   1/1       Running   0          8m        172.20.6.0      192.168.174.136
kubernetes-bootcamp-6b7849c495-z6mxx   1/1       Running   0          29m       172.20.40.192   192.168.174.137

縮容,則將replica減小即可

kubectl scale deploy kubernetes-bootcamp --replicas=2

兩個pods在Terminating中

[email protected]:~# kubectl get pods -o wide
NAME                                   READY     STATUS        RESTARTS   AGE       IP              NODE
kubernetes-bootcamp-7689dc585d-6glz5   1/1       Terminating   0          7m        172.20.40.194   192.168.174.137
kubernetes-bootcamp-7689dc585d-8sb2m   1/1       Terminating   0          7m        172.20.188.2    192.168.174.135
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running       0          7m        172.20.6.1      192.168.174.136
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running       0          7m        172.20.188.1    192.168.174.135

完成縮容

[email protected]:~# kubectl get pods -o wide
NAME                                   READY     STATUS    RESTARTS   AGE       IP             NODE
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running   0          8m        172.20.6.1     192.168.174.136
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running   0          8m        172.20.188.1   192.168.174.135

更新映象

kubectl set image deploy kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

[email protected]:~# kubectl set image deploy kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment "kubernetes-bootcamp" image updated

檢視更新結果 kubectl rollout status deploy kubernetes-bootcamp

[email protected]:~# kubectl rollout status deploy kubernetes-bootcamp
Waiting for rollout to finish: 3 of 4 updated replicas are available...
deployment "kubernetes-bootcamp" successfully rolled out

kubectl describe deploy 該命令可以檢視image的版本變為v2

回退更新

kubectl rollout undo deploy kubernetes-bootcamp

通過配置檔案來管理 /root/services/nginx-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
    - name: nginx
      image: nginx:1.7.9
      ports:
      - containerPort: 80

執行命令建立nginx pod kubectl create -f /root/services/nginx-pod.yaml

[email protected]:~/services# kubectl create -f /root/services/nginx-pod.yaml 
pod "nginx" created

檢視;nginx在建立中ContainerCreating

[email protected]:~/services# kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running             0          19m
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running             0          19m
nginx                                  0/1       ContainerCreating   0          15s

訪問驗證 開啟一個kubectl proxy來訪問:驗證返回nginx的歡迎頁,驗證成功

[email protected]:~# curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/nginx/
<!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的deployment /root/services/nginx-deployment.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  template:
    metadata: 
      labels: 
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
          - containerPort: 80
[email protected]:~/services# kubectl create -f /root/services/nginx-deployment.yaml 
deployment "nginx-deployment" created
[email protected]:~/services# 
[email protected]:~/services# kubectl get deploy
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2         2         2            2           1h
nginx-deployment      2         2         2            1           15s

ContainerCreating中

[email protected]:~/services# kubectl get pods
NAME                                   READY     STATUS              RESTARTS   AGE
kubernetes-bootcamp-7689dc585d-ltcrd   1/1       Running             0          34m
kubernetes-bootcamp-7689dc585d-psrsf   1/1       Running             0          34m
nginx                                  1/1       Running             0          15m
nginx-deployment-6c54bd5869-44ndp      1/1       Running             0          1m
nginx-deployment-6c54bd5869-6nxww      0/1       ContainerCreating   0          1m
[email protected]:~/services# 
[email protected]:~/services# kubectl get pods -l app=nginx
NAME                                READY     STATUS              RESTARTS   AGE
nginx-deployment-6c54bd5869-44ndp   1/1       Running             0          2m
nginx-deployment-6c54bd5869-6nxww   0/1       ContainerCreating   0          2m

兩個都執行起來了

[email protected]:~/services# kubectl get pods -l app=nginx
NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-6c54bd5869-44ndp   1/1       Running   0          4m
nginx-deployment-6c54bd5869-6nxww   1/1       Running   0          4m

kube-proxy 安裝完成後,kubectl get services檢視有哪些服務

[email protected]:~# kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.68.0.1    <none>        443/TCP   3h

檢視這個service的詳細說明

kubectl describe service kubernetes
[email protected]:~# kubectl describe service kubernetes
Name:              kubernetes
Namespace:         default
Labels:            component=apiserver
                   provider=kubernetes
Annotations:       <none>
Selector:          <none>
Type:              ClusterIP
IP:                10.68.0.1
Port:              https  443/TCP
TargetPort:        6443/TCP
Endpoints:         192.168.174.136:6443
Session Affinity:  ClientIP
Events:            <none>

kube-proxy有什麼方便 kubectl expose deploy kubernetes-bootcamp --type="NodePort" --target-port=8080 --port=80

[email protected]:~# kubectl expose deploy kubernetes-bootcamp --type="NodePort" --target-port=8080 --port=80
service "kubernetes-bootcamp" exposed

此時,多了一個service

[email protected]:~# kubectl get services
NAME                  TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes            ClusterIP   10.68.0.1     <none>        443/TCP        3h
kubernetes-bootcamp   NodePort    10.68.70.33   <none>        80:34824/TCP   37s
[email protected]:~# curl 192.168.174.135:34824
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7689dc585d-ltcrd | v=2
[email protected]:~# 
[email protected]:~# 
[email protected]:~# curl 192.168.174.137:34824
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7689dc585d-psrsf | v=2
[email protected]:~# 
[email protected]:~# curl 192.168.174.136:34824
curl: (7) Failed to connect to 192.168.174.136 port 34824: Connection refused

指定NodePort nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  ports:
  - port: 8080
    targetPort: 80
    nodePort: 20000
  selector:
    app: nginx
  type: NodePort
[email protected]:~/services# kubectl create -f /root/services/nginx-service.yaml 
service "nginx-service" created
[email protected]:~/services# kubectl get services
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.68.0.1       <none>        443/TCP          3h
kubernetes-bootcamp   NodePort    10.68.70.33     <none>        80:34824/TCP     11m
nginx-service         NodePort    10.68.121.152   <none>        8080:20000/TCP   1m

service簡寫svc

[email protected]:~/services# kubectl get svc
NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.68.0.1       <none>        443/TCP          3h
kubernetes-bootcamp   NodePort    10.68.70.33     <none>        80:34824/TCP     12m
nginx-service         NodePort    10.68.121.152   <none>        8080:20000/TCP   1m

訪問驗證

[email protected]:~# curl 192.168.174.135:20000
<!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>