1. 程式人生 > >Docker叢集管理工具-Kubernetes部署記錄

Docker叢集管理工具-Kubernetes部署記錄

1)首先部署nginx pod 和複製器---------------------------------------------------------------------

[[email protected] ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
docker.io/nginx     latest              3448f27c273f        8 days ago          109.4 MB

通過下面命令發現apiVersion版本是v1
[
[email protected]
~]# curl -s -L http://182.48.115.237:8080/api/v1beta1/version | python -mjson.tool { "apiVersion": "v1", ....... } 開始建立pod單元 [[email protected] ~]# mkdir -p /home/kubermange && cd /home/kubermange [[email protected] kubermange]# vim nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: nginx-controller spec: replicas: 2 #即2個備份 selector: name: nginx template: metadata: labels: name: nginx spec: containers: - name: nginx image: docker.io/nginx ports: - containerPort: 80 [
[email protected]
kubermange]# kubectl -s http://182.48.115.237:8080 create -f nginx-rc.yaml replicationcontroller "nginx-controller" created 由於kubernetes要去gcr.io下載gcr.io/google_containers/pause映象,然後下載nginx映象,所以所建立的Pod需要等待一些時間才能處於running狀態。 然後檢視pods清單 [[email protected] kubermange]# kubectl -s http://k8s-master:8080 get pods NAME READY STATUS RESTARTS AGE nginx-controller-f0j9c 0/1 ContainerCreating 0 1m nginx-controller-v219k 0/1 ContainerCreating 0 1m 可以使用describe 命令檢視pod所分到的節點: [
[email protected]
kubermange]# kubectl -s http://182.48.115.237:8080 describe pod nginx-controller-f0j9c Name: nginx-controller-f0j9c Namespace: default Node: k8s-node-1/182.48.115.238 ....... 同理,檢視另一個pod [[email protected] kubermange]# kubectl -s http://182.48.115.237:8080 describe pod nginx-controller-v219k Name: nginx-controller-v219k Namespace: default Node: k8s-node-2/182.48.115.239 ....... 由上可以看出,這個複製器啟動了兩個Pod,分別執行在182.48.115.238和182.48.115.239這兩個節點上了。到這兩個節點上檢視,發現已經有nginx應用容器建立了。 2)部署節點內部可訪問的nginx service------------------------------------------------------------------------ Service的type有ClusterIP和NodePort之分,預設是ClusterIP,這種型別的Service只能在叢集內部訪問。配置檔案如下: [[email protected] kubermange]# vim nginx-service-clusterip.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-clusterip spec: ports: - port: 8001 targetPort: 80 protocol: TCP selector: name: nginx 然後執行下面的命令建立service: [[email protected] kubermange]# kubectl -s http://182.48.115.237:8080 create -f ./nginx-service-clusterip.yaml service "nginx-service-clusterip" created [[email protected] kubermange]# kubectl -s http://182.48.115.237:8080 get service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 2h nginx-service-clusterip 10.254.163.249 <none> 8001/TCP 24s 驗證service的可訪問性(訪問節點): 上面的輸出告訴我們這個Service的Cluster IP是10.254.163.249,埠是8001。那麼我們就來驗證這個PortalNet IP的工作情況: ssh登入到節點機上驗證(可以提前做ssh無密碼登入的信任關係,當然也可以不做,這樣驗證時要手動輸入登入密碼) [[email protected] kubermange]# ssh 182.48.115.238 curl -s 10.254.163.249:8001 //或者直接到節點機上執行"curl -s 10.254.163.249:8001" The authenticity of host '182.48.115.238 (182.48.115.238)' can't be established. ECDSA key fingerprint is 4c:24:35:e0:35:00:86:05:94:a2:9e:f9:22:b0:90:b7. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '182.48.115.238' (ECDSA) to the list of known hosts. [email protected]'s password: <!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> 同理驗證到另外一個節點機上的service的可訪問性也是ok的 [[email protected] kubermange]# ssh 182.48.115.239 curl -s 10.254.163.249:8001 由此可見,從前面部署複製器的部分可以知道nginx Pod執行在182.48.115.238和182.48.115.239這兩個節點上。 從這兩個節點上訪問我們的服務來體現Service Cluster IP在所有叢集節點的可到達性。 3)部署外部可訪問的nginx service------------------------------------------------------------------- 下面我們建立NodePort型別的Service,這種型別的Service在叢集外部是可以訪問。下表是本文用的配置檔案: [[email protected] kubermange]# vim nginx-service-nodeport.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-nodeport spec: ports: - port: 8000 targetPort: 80 protocol: TCP type: NodePort selector: name: nginx 執行下面的命令建立service: [[email protected] kubermange]# kubectl -s http://182.48.115.237:8080 create -f ./nginx-service-nodeport.yaml service "nginx-service-nodeport" created [[email protected] kubermange]# kubectl -s http://182.48.115.237:8080 get service NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes 10.254.0.1 <none> 443/TCP 2h nginx-service-clusterip 10.254.163.249 <none> 8001/TCP 13m nginx-service-nodeport 10.254.146.68 <nodes> 8000:31298/TCP 22s 使用下面的命令獲得這個service的節點級別的埠: [[email protected] kubermange]# kubectl -s http://182.48.115.237:8080 describe service nginx-service-nodeport 2>/dev/null | grep NodePort Type: NodePort NodePort: <unset> 31298/TCP 驗證service的可訪問性(訪問節點): 上面的輸出告訴我們這個Service的節點級別埠是31298。下面我們驗證這個Service的工作情況: [[email protected] kubermange]# curl 182.48.115.238:31298 <!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> 同理驗證到另外一個節點機上的service的可訪問性也是ok的 [[email protected] kubermange]# curl 182.48.115.239:31298 ---------------------------------------------------------- 登入另外兩個節點機上,發現已經建立了nginx應用容器 [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 620d9171a42c docker.io/nginx "nginx -g 'daemon off" 19 minutes ago Up 19 minutes k8s_nginx.3d610115_nginx-controller-f0j9c_default_eaa0497b-3be5-11e7-a8a1-52540030ba6d_a6cde2e2 01facbbbe7cb registry.access.redhat.com/rhel7/pod-infrastructure:latest "/pod" 19 minutes ago Up 19 minutes k8s_POD.a8590b41_nginx-controller-f0j9c_default_eaa0497b-3be5-11e7-a8a1-52540030ba6d_d2dd947d [[email protected] ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 308749352e55 docker.io/nginx "nginx -g 'daemon off" 30 minutes ago Up 30 minutes k8s_nginx.3d610115_nginx-controller-v219k_default_eaa02644-3be5-11e7-a8a1-52540030ba6d_7d54d433 cde94e406f9a registry.access.redhat.com/rhel7/pod-infrastructure:latest "/pod" 30 minutes ago Up 30 minutes