1. 程式人生 > >Centos7安裝Kubernetes1.11叢集及Dashboard工具

Centos7安裝Kubernetes1.11叢集及Dashboard工具

1. 測試節點

暫時一個Master節點,一個Node節點

192.168.1.148 Master
192.168.1.6 Node

建議首先清空iptables路由規則:

# (清理filter表規則,不加–t預設為filter表)
iptables -X && iptables -F && iptables -Z 
iptables -X -t nat && iptables -F -t nat && iptables -Z -t nat

2. Master及Node節點安裝環境

# 關掉 selinux
setenforce 0 sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux # 關掉防火牆 systemctl stop firewalld systemctl disable firewalld # 關閉 swap swapoff -a sed -i 's/.*swap.*/#&/' /etc/fstab # 配置轉發引數 cat <<EOF > /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables =
1 EOF sysctl --system # 設定國內 yum 源 cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/ enabled=1 gpgcheck=1 repo_gpgcheck=1 gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg EOF # 安裝一些必備的工具
yum install -y epel-release yum install -y net-tools wget vim ntpdate # 安裝Docker yum install -y docker systemctl enable docker && systemctl start docker systemctl enable docker.service # 安裝kubeadm、kubectl、kubelet yum install -y kubelet kubeadm kubectl kubernetes-cni systemctl enable kubelet && systemctl start kubelet

3. Master節點安裝環境

  • 將以下shell寫入指令碼檔案,並執行:
#!/bin/bash
images=(kube-proxy-amd64:v1.11.0 kube-scheduler-amd64:v1.11.0 kube-controller-manager-amd64:v1.11.0 kube-apiserver-amd64:v1.11.0
etcd-amd64:3.2.18 coredns:1.1.3 pause-amd64:3.1 kubernetes-dashboard-amd64:v1.8.3 k8s-dns-sidecar-amd64:1.14.9 k8s-dns-kube-dns-amd64:1.14.9
k8s-dns-dnsmasq-nanny-amd64:1.14.9 )
for imageName in ${images[@]} ; do
  docker pull registry.cn-hangzhou.aliyuncs.com/k8sth/$imageName
  docker tag registry.cn-hangzhou.aliyuncs.com/k8sth/$imageName k8s.gcr.io/$imageName
done

docker tag da86e6ba6ca1 k8s.gcr.io/pause:3.1

docker pull docker.io/mirrorgooglecontainers/k8s-dns-sidecar-amd64:1.14.9
docker tag docker.io/mirrorgooglecontainers/k8s-dns-sidecar-amd64:1.14.9 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.9
docker rmi docker.io/mirrorgooglecontainers/k8s-dns-sidecar-amd64:1.14.9
docker tag docker.io/mirrorgooglecontainers/k8s-dns-kube-dns-amd64:1.14.9 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.9
docker rmi docker.io/mirrorgooglecontainers/k8s-dns-kube-dns-amd64:1.14.9
docker pull docker.io/mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.9
docker tag docker.io/mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.9 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.9
docker rmi docker.io/mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.9

docker pull registry.cn-shenzhen.aliyuncs.com/cp_m/flannel:v0.10.0-amd64
docker tag registry.cn-shenzhen.aliyuncs.com/cp_m/flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64
  • 初始化Master節點
[root[email protected] ~]# kubeadm init --kubernetes-version=v1.11.0 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.1.148
  • 安裝Flannel網路
[[email protected] ~]# sysctl net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-iptables = 1
[[email protected] ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.extensions/kube-flannel-ds created
  • 檢視Master節點詳情
[[email protected] ~]# export KUBECONFIG=/etc/kubernetes/admin.conf 
[[email protected] ~]# kubectl get nodes
NAME                        STATUS    ROLES     AGE       VERSION
ecs-master.novalocal   Ready     master    1h        v1.11.2
[[email protected] ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                                READY     STATUS    RESTARTS   AGE
kube-system   coredns-78fcdf6894-bwbwv                            1/1       Running   0          4m
kube-system   coredns-78fcdf6894-vtd2n                            1/1       Running   0          4m
kube-system   etcd-ecs-master.novalocal                      1/1       Running   0          3m
kube-system   kube-apiserver-ecs-master.novalocal            1/1       Running   0          3m
kube-system   kube-controller-manager-ecs-master.novalocal   1/1       Running   0          3m
kube-system   kube-flannel-ds-n4tvh                               1/1       Running   0          26s
kube-system   kube-proxy-gflxf                                    1/1       Running   0          4m
kube-system   kube-scheduler-ecs-master.novalocal            1/1       Running   0          3m

4. Node節點加入叢集

  • 在node節點中安裝docker相關服務
docker pull registry.cn-hangzhou.aliyuncs.com/k8sth/kube-proxy-amd64:v1.11.0
docker pull registry.cn-hangzhou.aliyuncs.com/k8sth/pause-amd64:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/k8sth/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1
docker tag registry.cn-hangzhou.aliyuncs.com/k8sth/kube-proxy-amd64:v1.11.0 k8s.gcr.io/kube-proxy-amd64:v1.11.0
docker tag registry.cn-hangzhou.aliyuncs.com/k8sth/pause-amd64:3.1 k8s.gcr.io/pause:3.1
  • 根據Master節點初始化時的提示加入叢集(根據初始化Master時提示的實際情況輸入token和hash)
[[email protected] ~]# kubeadm join 192.168.1.148:6443 --token wkj0bo.pzibll6rd9gyi5z8 --discovery-token-ca-cert-hash sha256:51985223a369a1f8c226f3ccdcf97f4ad5ff201a7c8c708e1636eea0739c0f05
  • 在master節點上檢視叢集當前狀態
[[email protected] ~]# kubectl get nodes
NAME                        STATUS    ROLES     AGE       VERSION
ecs-master.novalocal   Ready     master    6m        v1.11.2
ecs-node.novalocal   Ready     <none>    1m        v1.11.2
[[email protected] ~]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                                READY     STATUS    RESTARTS   AGE
kube-system   coredns-78fcdf6894-bwbwv                            1/1       Running   0          6m
kube-system   coredns-78fcdf6894-vtd2n                            1/1       Running   0          6m
kube-system   etcd-ecs-master.novalocal                      1/1       Running   0          5m
kube-system   kube-apiserver-ecs-master.novalocal            1/1       Running   0          5m
kube-system   kube-controller-manager-ecs-master.novalocal   1/1       Running   0          5m
kube-system   kube-flannel-ds-kcrnq                               1/1       Running   0          1m
kube-system   kube-flannel-ds-n4tvh                               1/1       Running   0          1m
kube-system   kube-proxy-gflxf                                    1/1       Running   0          6m
kube-system   kube-proxy-l8vzr                                    1/1       Running   0          1m
kube-system   kube-scheduler-ecs-master.novalocal            1/1       Running   0          5m

5. 安裝Dashboard視覺化工具

  • 使用第三方指令碼(大神的github)
git clone https://github.com/cookcodeblog/k8s-deploy.git
cd k8s-deploy/k8s_dashboard/
chmod u+x *.sh
./deploy_k8s_dashboard.sh
  • 修改kubernetes-dashboard.yaml,不用代理方式訪問WebUI

停止dashboard相關容器:

[[email protected] k8s_dashboard]#kubectl delete -f kubernetes-dashboard.yaml

修改kubernetes-dashboard.yaml內容:

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  # 新增Service的type為NodePort
  type: NodePort
  ports:
    - port: 443
      targetPort: 8443
      # (k8s只支援30000以上的埠)新增對映到host的埠
      nodePort: 30001
  selector:
    k8s-app: kubernetes-dashboard

重啟dashboard服務:

[[email protected] k8s_dashboard]#kubectl apply -f kubernetes-dashboard.yaml
  • 通過瀏覽器訪問https//<master-ip>:<nodePort>/

正常情況下會彈出以下頁面:

這裡寫圖片描述

可以看到這裡支援兩種方式登入:KubeConfig和token

這裡的token就是在master初始化時或安裝dashboard時提示的token字串,如果忘記了可以這樣獲取:

[[email protected] ~]# kubectl -n kube-system describe $(kubectl -n kube-system get secret -n kube-system -o name | grep namespace) | grep token

而KubeConfig的製作也很簡單,將master節點中的/etc/kubernetes/admin.conf的內容複製到一個新的檔案,在這個新的檔案內容最後加一項token的內容進去即可,如:

- name: kubernetes-admin
  user:
    client-certificate-data: 已有的xxxxxxxx
    client-key-data: 已有的xxxxxxx
    # 在這裡加多一項
    token: 在這裡貼上你實際獲得的token

將這個檔案拷貝到你本地,在瀏覽器中選擇Kubeconfig這一項,然後選擇拷貝回來的kubeconfig檔案,即可登入。

登入完成後主頁如下所示:

這裡寫圖片描述

參考資料:

http://www.cnblogs.com/cocowool/p/kubeadm_install_kubernetes.html
https://blog.csdn.net/nklinsirui/article/details/80806131