1、CentOS 7 基礎環境準備node
centos 7 默認服務目錄 /usr/lib/systemd/system systemctl服務開機啓動連接存貯目錄: /etc/systemd/system/basic.target.wants/
列出全部開機自啓的服務
systemctl list-unit-files|grep enabled
一、Centos7 防火牆 默認是 firewalllinux
想和centos 6 同樣配置 iptables;能夠直接 # yum update iptables 也能夠直接安裝 # yum install iptables iptables-devel iptables-services iptables-utils
關閉 firewalld 開啓iptables Systemctl stop firewalld Systemctl disable firewalld systemctl restart iptables.service systemctl status iptables.service systemctl enable iptables.service
二、網絡設置networkgit
使用 static 地址和配置DNS
Centos 7 的網卡名稱從默認eth更改成ifcfg-en開頭的; CentOS6 及以前以太網網卡進行順序命名的;多網卡如:eth0,eth1 依次; Centos7 則不一樣,命名規則默認是基於固件、拓撲、位置信息來分配;
查看網卡信息:redis
查看 ip 地址
# ip addr show 若是用戶不習慣能夠更新 ifconfig 而後再查看; # yum update ifconfig 沒有的話 能夠在直接安裝 net-tools # yum install net-tools
三、關閉selinuxdocker
# sed -i '/^SELINUX=/cSELINUX=disabled' /etc/sysconfig/selinux
四、更新 yum 源express
# cat /etc/yum.repos.d/virt7-docker-common-release.repo [virt7-docker-common-release] name=virt7-docker-common-release baseurl=http://cbs.centos.org/repos/virt7-docker-common-release/x86_64/os/ gpgcheck=0
五、時間校驗 時間服務(這個有多種方式)apache
# yum install ntp
systemctl restart ntpd.service
也能夠部署時間服務器進行校驗
六、規劃分佈centos
10.100.10.100 master 10.100.10.105 minion1 (node1) 10.100.10.106 minion2 (node2)
也能夠去綁定主機頭 /etc/hosts
二 、kubernetes api
Kubernetes 用於管理雲平臺中多個主機上的容器化的應用,就是一個容器集羣的管理工具。服務器
在k8s中 全部的容器都是定義在 一個POD 中運行,cloudstack 也有這個概念。一個Pod承載着多個容器,同一個Pod中的容器會部署在同一個物理機器上而且可以共享資源。個Pod也能夠包含O個或者多個磁盤卷組(volumes),這些卷組將會以目錄的形式提供給一個容器,或者被全部Pod中的容器共享,對於用戶建立的每一個Pod,系統會自動選擇那個健康而且有足夠容量的機器,而後建立相似容器的容器,當容器建立失敗的時候,容器會被node agent自動的重啓,這個node agent叫kubelet,可是,若是是Pod失敗或者機器,它不會自動的轉移而且啓動,除非用戶定義了 replication controller。
API對象是K8s集羣中的管理操做單元,因此有不少的容錯性和可擴展性。
Kubernetes的目標是讓部署容器化的應用簡單而且高效(powerful),Kubernetes提供了應用部署,規劃,更新,維護的一種機制。
3、master 服務端:
IP : 10.100.10.100
安裝主程序:
# yum install etcd flannel docker kubernetes
一、Etcd
etcd 是 CoreOS 團隊發起的一個管理配置信息和服務發現(service discovery)的項目;相似zookeeper ,Apache ZooKeeper也是作分佈式系統中進行同步和一致性管理的。由於在分佈式系統中,各類服務的配置信息的管理分享,服務的發現是一個很基本同時也是很重要的問題。
使用 etcd 能夠在多個節點上啓動多個實例,並添加它們爲一個集羣。同一個集羣中的 etcd 實例將會保持彼此信息的一致性。
etcd.conf 文件配置示例 :
# cat etcd.conf ETCD_NAME=default ETCD_DATA_DIR="/var/lib/etcd/default.etcd" ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
啓動 :
systemctl start etcd.services
二、虛擬網絡(能夠供docker虛擬網絡)
可使用 flannel,或者openvswitch
在etcd裏定義建立flannel網絡配置: # etcdctl mk /atomic.io/network/config '{"Network":"172.16.0.0/16"}'
三、etcdctl 經常使用命令;
backup 備份目錄 cluster-health 集羣健康檢測 mk 建立一個鍵值設置屬性 mkdir 建立目錄 rm 刪除 rmdir 若是目錄爲空 刪除全部 get 查看鍵的屬性
四、kubernetes -master 配置;
4.一、config配置示例:
# cat /etc/kubernetes/config |grep -v ^$ |grep -v ^#
KUBE_LOGTOSTDERR="--logtostderr=true" KUBE_LOG_LEVEL="--v=0" KUBE_ALLOW_PRIV="--allow-privileged=false" KUBE_MASTER="--master=http://docker-master:8080"
4.二、apiserver 配置示例:
# cat /etc/kubernetes/apiserver |grep -v ^$ |grep -v ^# KUBE_API_ADDRESS="--address=0.0.0.0" KUBE_API_PORT="--port=8080" KUBE_MASTER="--master=http://docker-master:8080" KUBELET_PORT="--kubelet-port=10250" KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379" KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota" KUBE_API_ARGS=""
4.三、kubelet配置示例:
# cat /etc/kubernetes/kubelet |grep -v ^$ |grep -v ^# KUBELET_ADDRESS="--address=127.0.0.1" KUBELET_HOSTNAME="--hostname-override=127.0.0.1" KUBELET_API_SERVER="--api-servers=http://127.0.0.1:8080" KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest" KUBELET_ARGS=""
五、添加啓動項、啓動、並查看狀態:
# cat start-kube.sh
for SERVICES in etcd docker kube-apiserver kube-controller-manager kube-scheduler; do systemctl enable $SERVICES systemctl restart $SERVICES systemctl status $SERVICES done
七、服務檢測:
1. 檢測端口;ss -tln
2. 查看 docker 網絡
# ifconfig docker
查看 docker 網絡 172.16.0.0/16 網絡
3. master 檢測節點(暫時沒有):
# kubectl get nodes
NAME STATUS AGE
4. 異常排錯:能夠根據提示進行查看啓動運行異常的信息
# journalctl -xe 查看錯誤信息
dhcp 問題
DNS問題
鏡像下載問題
ca 認證問題
4、kubernettes - minion 節點
1. 環境安裝
節點 主程序
yum -y install flannel docker kubernetes
2. 配置 flannel
# cat /etc/sysconfig/flanneld
FLANNEL_ETCD_ENDPOINTS="http://10.100.10.100:2379"
# etcd 節點名稱
FLANNEL_ETCD_PREFIX="/atomic.io/network"
# flannel網絡 能夠設置成master主機IP
一、kubernetes minion 端配置示例參考;
主要也是這個文件 config kubetle apiserver (minion 配置基本同樣的,kubelet 中 KUBELET_HOSTNAME 設置爲本機IP 地址)
1.一、apiserver 文件
# cat apiserver |grep -v ^$ |grep -v ^# KUBE_API_ADDRESS="--address=127.0.0.1" KUBE_ETCD_SERVERS="--etcd_servers=http://10.100.10.100:2379" KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16" KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
1.二、config 文件
# cat config |grep -v ^$ |grep -v ^# KUBE_LOGTOSTDERR="--logtostderr=true" KUBE_LOG_LEVEL="--v=0" KUBE_ALLOW_PRIV="--allow_privileged=false" KUBE_MASTER="--master=http://10.100.10.100:8080" KUBE_ETCD_SERVERS="--etcd-servers=http://10.100.10.100:2379"
1.三、kubelet 文件
# cat kubelet |grep -v ^$ |grep -v ^# KUBELET_ADDRESS="--address=0.0.0.0" KUBELET_PORT="--port=10250" KUBELET_HOSTNAME="--hostname-override=10.100.10.105" # KUBELET_HOSTNAME 設置minion端主機IP (node2 就是設置爲 10.100.10.106) KUBELET_API_SERVER="--api-servers=http://10.100.10.100:8080" KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
二、minion 端添加啓動項、啓動、並查看狀態;
# cat minion-kube.sh for SERVICES in kube-proxy kubelet docker flanneld; do systemctl enable $SERVICES systemctl restart $SERVICES systemctl status $SERVICES done
三、檢測服務
ss -tln
# 檢測進程端口;
# ifconfig docker 查看docker網絡 172.16.0.0/16 網絡;
再返回 master 端檢測節點:
# kubectl get nodes
NAME STATUS AGE
5、Kubernetes Web UI
一、建立kubernetes-dashboard.yaml
從官網下載 yaml 文件; wget https://rawgit.com/kubernetes/dashboard/master/src/deploy/kubernetes-dashboard.yaml
二、編輯 kubernetes-dashboard.yaml 文件;
配置示例(版本不是最新,可按照部署最新進行編輯修改):
# cat kubernetes-dashboard.yaml # Copyright 2015 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # Configuration to deploy release version of the Dashboard UI. # # Example usage: kubectl create -f <this_file> kind: Deployment apiVersion: extensions/v1beta1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: replicas: 1 selector: matchLabels: app: kubernetes-dashboard template: metadata: labels: app: kubernetes-dashboard # Comment the following annotation if Dashboard must not be deployed on master annotations: scheduler.alpha.kubernetes.io/tolerations: | [ { "key": "dedicated", "operator": "Equal", "value": "master", "effect": "NoSchedule" } ] spec: containers: - name: kubernetes-dashboard image: docker.io/mritd/kubernetes-dashboard-amd64 # 若是有網絡問題,images 也能夠本身建立 docker 私有庫;地址寫成本身的; #imagePullPolicy: Always imagePullPolicy: IfNotPresent # 不存在 就下載 ports: - containerPort: 9090 protocol: TCP args: # Uncomment the following line to manually specify Kubernetes API server Host # If not specified, Dashboard will attempt to auto discover the API server and connect # to it. Uncomment only if the default does not work. # - --apiserver-host=http://my-address:port - --apiserver-host=http://10.100.10.100:8080 # master 主機 apiserver livenessProbe: httpGet: path: / port: 9090 initialDelaySeconds: 30 timeoutSeconds: 30 --- kind: Service apiVersion: v1 metadata: labels: app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: type: NodePort ports: - port: 80 targetPort: 9090 selector: app: kubernetes-dashboard
三、建立 Pod (image 位置;設置下載地址有關 須要等一會)
# kubectl create -f kubernetes-dashboard.yaml
# 建立 pod 失敗刪除 # 可使用 kubectl delete -f kubernetes-dashboard.yaml 刪除
四、檢測 pods
pods 檢測
# kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system kubernetes-dashboard-3713835017-4nbkp 1/1 Running 1 5m
services 檢測
# kubectl get services --all-namespaces
NAMESPACE NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE default kubernetes 10.254.0.1 <none> 443/TCP 33m kube-system kubernetes-dashboard 10.254.211.205 <nodes> 80:30491/TCP 5m
五、查看 service 信息
# kubectl describe service/kubernetes-dashboard --namespace="kube-system" Name: kubernetes-dashboard Namespace: kube-system Labels: app=kubernetes-dashboard Selector: app=kubernetes-dashboard Type: NodePort IP: 10.254.211.205 Port: <unset> 80/TCP NodePort: <unset> 30491/TCP Endpoints: 172.16.4.4:9090 Session Affinity: None
六、異常處理
一、查看 pods 信息描述; # kubectl describe pod/kubernetes-dashboard-3713835017-4nbkp --namespace="kubectl-system" 二、查看日誌信息; # kubectl logs -f kubernetes-dashboard-3713835017-4nbkp --namespace=kube-system
七、測試訪問:
K8s 集羣提供虛擬的隔離做用,K8s集羣初始有兩個名字空間,分別是默認名字空間 default 和系統名字空間 kube-system
http://master:8080/ui/
6、kubectl 經常使用命令:也就是 kubectl 進行交互操做執行。
1. 檢測信息命令
# 查看集羣信息 kubectl cluster-info # 查看各組件信息 kubectl -s http://localhost:8080 get componentstatuses # 查看pods所在的運行節點 kubectl get pods -o wide # 查看pods定義的詳細信息 kubectl get pods -o yaml # 查看Replication Controller信息 kubectl get rc # 查看service的信息 kubectl get service # 查看節點信息 kubectl get nodes # 按selector名來查找pod kubectl get pod --selector name=redis # 查看運行的pod的環境變量 kubectl exec pod名 env
二、操做類命令
# 建立 kubectl create -f 文件名 # 重建 kubectl replace -f 文件名 [--force] # 刪除 kubectl delete -f 文件名 kubectl delete pod pod名 kubectl delete rc rc名 kubectl delete service service名 kubectl delete pod --all