1. 程式人生 > >kubernetes 集群

kubernetes 集群

log 基礎 images pen 失敗 osc 更新 this rhel7

一、CentOS 7 基礎環境準備

centos 7 默認服務目錄
/usr/lib/systemd/system systemctl服務開機啟動鏈接存貯目錄: /etc/systemd/system/basic.target.wants/ 列出所有開機自啟的服務 systemctl list-unit-files|grep enabled

  1、Centos7 防火墻 默認是 firewall

想和centos 6 一樣配置 iptables;直接 yum update iptables

也可以直接安裝 yum install iptables iptables-server

Systemctl stop firewalld
Systemctl disable firewalld
systemctl restart iptables.service
systemctl status iptables.service
systemctl enable iptables.service

  2、網絡設置network

使用 static 地址和配置DNS
Centos 7 的網卡名稱從默認eth更改為ifcfg-en開頭的
CentOS6 及之前以太網網卡進行順序命名的;多網卡如:eth0,eth1 依次。
Centos7 則不同,命名規則默認是基於固件、拓撲、位置信息來分配。
# ip addr show 
如果用戶不習慣可以更新 ifconfig 然後再查看;
# yum update ifconfig 

  3、關閉selinux

#sed -i ‘/^SELINUX=/cSELINUX=disabled‘ /etc/sysconfig/selinux 

  4、更新 yum 源

# 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

  5、時間校驗

# yum install ntp 
systemctl restart ntpd.service
也可以部署時間服務器進行校驗

  6、規劃分布

10.100.10.100 master
10.100.10.105 minion1 (node1)
10.100.10.106 minion2 (node2)
也可以去綁定主機頭 /etc/hosts

二 、kubernetes

三、master 服務端:

IP : 10.100.10.100
# yum install etcd flannel docker kubernetes

  1、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

  2、虛擬網絡(可以供docker虛擬網絡)
  可以使用 flannel,或者openvswitch

在etcd裏定義創建flannel網絡配置:
# etcdctl mk /atomic.io/network/config ‘{"Network":"172.16.0.0/16"}‘

  3、etcdctl 常用命令;

     backup          備份目錄
     cluster-health  集群健康檢測
     mk              創建一個鍵值設置屬性 
     mkdir           創建目錄
     rm              刪除
     rmdir           如果目錄為空 刪除所有
     get             查看鍵的屬性

  4、kubernetes -master 配置;

    4.1、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.2、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.3、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=""

  5、添加啟動項、啟動、並查看狀態:

# 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

  7、服務檢測:

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認證問題

四、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

1、kubernetes minion 端配置示例參考;

主要也是這個文件 config kubetle apiserver (minion 配置基本一樣的,kubelet 中 KUBELET_HOSTNAME 設置為本機IP 地址)

    1.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.2、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.3、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" 

  2、minion 端添加啟動項、啟動、並查看狀態;

# cat minion-kube.sh
for SERVICES in kube-proxy kubelet docker flanneld; do
systemctl enable $SERVICES
systemctl restart $SERVICES
systemctl status $SERVICES 
done

  3、檢測服務

ss -tln # 檢測進程端口;
# ifconfig docker 
查看docker網絡 172.16.0.0/16 網絡;
再返回 master 端檢測節點:
# kubectl get nodes
NAME STATUS AGE

五、Kubernetes Web UI搭建
  1、創建kubernetes-dashboard.yaml

從官網下載 yaml 文件;
wget https://rawgit.com/kubernetes/dashboard/master/src/deploy/kubernetes-dashboard.yaml

  2、編輯 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

  3、創建 Pod (image 位置;設置下載地址有關 需要等一會)

# kubectl create -f kubernetes-dashboard.yaml
# 創建 pod 失敗刪除
# 可以使用 kuectl delete -f kubernetes-dashboard.yaml 刪除

  4、檢測 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

  5、查看 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

  6、異常處理

可以查看pods信息描述;
# kubectl describe pod/kubernetes-dashboard-3713835017-4nbkp --namespace="kubectl-system"
查看日誌信息;
# kubectl logs -f kubernetes-dashboard-3713835017-4nbkp --namespace=kube-system

  7、測試訪問:

http://master:8080/ui/

技術分享圖片

六、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
2.操作類命令
# 創建
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

  

kubernetes 集群