1. 程式人生 > >K8s權威指南學習

K8s權威指南學習

  • 第一章 k8s入門(簡單的Java web專案)

    環境: VMware Workstation在本機虛擬一個64位的Centos7虛擬機器

    • 環境準備

      1. 關閉防火牆

        systemctl disable firewalld
        systemctl stop firewalld
        
      2. 安裝etcd和Kubernetes軟體(會自動安裝Docker軟體)

        yum install -y etcd kubernetes
        
      3. 按順序啟動所有服務

        systemctl start etcd
        systemctl start docker
        systemctl start kube-apiserver
        systemctl start kube-controller-manager
        systemctl start kube-scheduler
        systemctl start kubelet
        systemctl start kube-proxy
        
    • 啟動MySQL服務

      1. MySQL映象

        docker pull daocloud.io/library/mysql:latest
        
      2. 構建Mysql RC定義檔案(可以使用vim建立),命名mysql-rc.yaml 內容如下:

        apiVersion: v1
        kind: ReplicationController
        metadata:
          name: mysql
        	spec:
        	  replicas: 1
        	  selector:
        		app: mysql
        	  template:
        		metadata:
        		  labels:
        			app: mysql
        		spec:
        		  containers:
        		  - name: mysql
        			image: mysql
        			ports:
        			- containerPort: 3306
        			env:
        			- name: MYSQL_ROOT_PASSWORD
        			  value: "123456"
        
      3. 釋出到kubernetes叢集中,在master節點執行命令(注:此時可能出現問題,如:問題1:no resource found,問題2:status的狀態是ContainerCreating而不是running,如果出現此問題請看關於建立rc和pod出現的問問題

        $ kubectl create -f mysql-rc.yaml 
        replicationcontroller "mysql" created
        

        檢視建立的rc

        $ kubectl get rc 
        NAME DESIRED CURRENT READY AGE 
        mysql 1 1 1 14s
        

        檢視pod建立情況

        $ kubectl get pods 
        NAME READY STATUS RESTARTS AGE 
        mysql-srd1m 1/1 Running 0 41s
        
      4. 建立一個與之關聯的k8s Service-Mysql的定義檔案(檔名為mysql-svc.yaml),內容如下:

        apiVersion: v1
        kind: Service
        metadata:
        name: mysql
        spec:
        ports:
        - port: 3306
        selector:
        app: mysql
        

        執行kubectl命令,建立service

        kubectl create -f mysql-svc.yaml
        

        檢視剛剛建立的service

        $ kubectl get svc
        NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
        kubernetes   10.254.0.1     <none>        443/TCP    8d
        mysql        10.254.55.41   <none>        3306/TCP   13s
        
    • 啟動Tomcat服務

      1. 拉取Tomcat映象
        	docker pull kubeguide/tomcat-app:v2
        
      2. 構建Tomcat RC定義檔案,命名myweb-rc.yaml 內容如下:
        		apiVersion: v1
        		kind: ReplicationController
        		metadata:
        		  name: myweb
        		spec:
        		  replicas: 2
        		  selector:
        			app: myweb
        		  template:
        			metadata:
        			  labels:
        				app: myweb
        			spec:
        			  containers:
        				- name: myweb
        				  image: kubeguide/tomcat-app:v1
        				  ports:
        				  - containerPort: 8080
        
        完成rc建立和驗證
        	$ kubectl create -f myweb-rc.yaml 
        	replicationcontroller "myweb" created
        	$ kubectl get rc 
        	NAME      DESIRED   CURRENT   READY     AGE
        	mysql     1         1         1         23h
        	myweb     2         2         2         12s
        	$ kubectl get pods
        	NAME          READY     STATUS    RESTARTS   AGE
        	mysql-srd1m   1/1       Running   1          23h
        	myweb-fqlvz   1/1       Running   0          40s
        	myweb-ggrlg   1/1       Running   0          40s
        
      3. 建立對應的Service,檔名(myweb-svc.yaml),內容如下
        	apiVersion: v1
        	kind: Service
        	metadata: 
        	name: myweb
        	spec:
        	type: NodePort
        	ports:
        	- port: 8080
        	nodePort: 30001
        	selector:
        	app: myweb
        
        建立並驗證
        	$ kubectl create -f myweb-svc.yaml 
        	service "myweb" created
        	$ kubectl get services
        	NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
        	kubernetes   10.254.0.1       <none>        443/TCP          8d
        	mysql        10.254.55.41     <none>        3306/TCP         42m
        	myweb        10.254.237.187   <nodes>       8080:30001/TCP   21s