溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Kubernetes V1.16.2部署Dashboard V2.0(beta5)

發(fā)布時(shí)間:2020-05-03 08:14:49 來源:網(wǎng)絡(luò) 閱讀:1724 作者:有功夫 欄目:云計(jì)算

kubeadm是官方社區(qū)推出的一個(gè)用于快速部署kubernetes集群的工具。

1.安裝要求

在開始之前,部署Kubernetes集群機(jī)器需要滿足以下幾個(gè)條件:

  • 一臺(tái)或多臺(tái)機(jī)器,操作系統(tǒng) CentOS7.6-86_x64
  • 硬件配置:4GB或更多RAM,4個(gè)CPU或更多CPU,硬盤30GB或更多
  • 集群中所有機(jī)器之間網(wǎng)絡(luò)互通
  • 禁止swap分區(qū)

2. 準(zhǔn)備環(huán)境

關(guān)閉防火墻:
systemctl stop firewalld
systemctl disable firewalld

關(guān)閉selinux:
sed -i 's/enforcing/disabled/' /etc/selinux/config 
setenforce 0

關(guān)閉swap:
swapoff -a  $ 臨時(shí)
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab  $ 永久

添加主機(jī)名與IP對(duì)應(yīng)關(guān)系(記得設(shè)置主機(jī)名):
$ cat >> /etc/hosts<<EOF
9.110.187.120 k8s-master
9.110.187.125 k8s-node1
9.110.187.126 k8s-node2
EOF
將橋接的IPv4流量傳遞到iptables的鏈:
$ cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
$ sysctl --system

3. 所有節(jié)點(diǎn)安裝Docker/kubeadm/kubelet

Kubernetes默認(rèn)CRI(容器運(yùn)行時(shí))為Docker,因此先安裝Docker。

3.1 安裝Docker

$ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
$ yum -y install docker-ce-18.06.1.ce-3.el7
$ systemctl enable docker && systemctl start docker
$ docker --version
Docker version 18.06.1-ce, build e68fc7a
$ yum list docker-ce --showduplicates | sort -r

3.2 添加阿里云YUM軟件源

$ cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

3.3 安裝kubeadm,kubelet和kubectl

由于版本更新頻繁,這里指定版本號(hào)部署:

yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 bash-completion wget
systemctl enable kubelet

4. 部署Kubernetes Master

在9.110.187.120(Master)執(zhí)行。

$ kubeadm init \
  --apiserver-advertise-address=9.110.187.120 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.16.2 \
  --service-cidr=10.1.0.0/16 \
  --pod-network-cidr=10.244.0.0/16

由于默認(rèn)拉取鏡像地址k8s.gcr.io國內(nèi)無法訪問,這里指定阿里云鏡像倉庫地址。

使用kubectl工具:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
$ kubectl get nodes

5. 安裝Pod網(wǎng)絡(luò)插件(CNI)

$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

如果下載失敗,可以改成這個(gè)鏡像地址:lizhenliang/flannel:v0.11.0-amd64

6. 加入Kubernetes Node

在9.110.187.125/126(Node)執(zhí)行。

向集群添加新節(jié)點(diǎn),執(zhí)行在kubeadm init輸出的kubeadm join命令:

$ kubeadm join 9.110.187.120:6443 --token esce21.q6hetwm8si29qxwn \
    --discovery-token-ca-cert-hash sha256:00603a05805807501d7181c3d60b478788408cfe6cedefedb1f97569708be9c5

7. 測試kubernetes集群

在Kubernetes集群中創(chuàng)建一個(gè)pod,驗(yàn)證是否正常運(yùn)行:

kubectl create deployment nginx --image=nginx.1.16
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pods -A  -o wide
kubectl describe pod nginx-86c57db685-frwwt

訪問地址:http://NodeIP:Port

8. Kubernetes V1.16.2部署Dashboard V2.0(beta5)

$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml

修改recommended.yaml文件內(nèi)容(vi recommended.yaml):

---
#增加直接訪問端口
kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
spec:
  type: NodePort #增加
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30001 #增加
  selector:
    k8s-app: kubernetes-dashboard

---
#因?yàn)樽詣?dòng)生成的證書很多瀏覽器無法使用,所以我們自己創(chuàng)建,注釋掉kubernetes-dashboard-certs對(duì)象聲明
#apiVersion: v1
#kind: Secret
#metadata:
#  labels:
#    k8s-app: kubernetes-dashboard
#  name: kubernetes-dashboard-certs
#  namespace: kubernetes-dashboard
#type: Opaque

---
創(chuàng)建證書
mkdir dashboard-certs
cd dashboard-certs/
#創(chuàng)建命名空間
kubectl create namespace kubernetes-dashboard    #yaml里會(huì)自動(dòng)創(chuàng)建,可以不用單獨(dú)創(chuàng)建

# 創(chuàng)建key文件
openssl genrsa -out dashboard.key 2048

#證書請(qǐng)求
openssl req -days 36000 -new -out dashboard.csr -key dashboard.key -subj '/CN=dashboard-cert'

#自簽證書
openssl x509 -req -in dashboard.csr -signkey dashboard.key -out dashboard.crt

#創(chuàng)建kubernetes-dashboard-certs對(duì)象
kubectl create secret generic kubernetes-dashboard-certs --from-file=dashboard.key --from-file=dashboard.crt -n kubernetes-dashboard

部署Dashboard

涉及到的兩個(gè)鏡像可以先下載下來
#安裝
kubectl create -f  ~/recommended.yaml
#檢查結(jié)果
kubectl get pods -A  -o wide

kubectl get service -n kubernetes-dashboard  -o wide

9.創(chuàng)建dashboard管理員

cat >> dashboard-admin.yaml<<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: dashboard-admin
  namespace: kubernetes-dashboard
EOF

kubectl create -f dashboard-admin.yaml

為用戶分配權(quán)限:
cat >>dashboard-admin-bind-cluster-role.yaml<<EOF
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: dashboard-admin-bind-cluster-role
  labels:
    k8s-app: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: dashboard-admin
  namespace: kubernetes-dashboard
EOF

kubectl create -f dashboard-admin-bind-cluster-role.yaml

訪問地址:http://NodeIP:30001

創(chuàng)建service account并綁定默認(rèn)cluster-admin管理員集群角色:

$ kubectl create serviceaccount dashboard-admin -n kube-system
$ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
$ kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')

使用輸出的token登錄Dashboard。

10.安裝metrics-server

在Node1/Node2上下載鏡像文件:

docker pull bluersw/metrics-server-amd64:v0.3.6
docker tag bluersw/metrics-server-amd64:v0.3.6 k8s.gcr.io/metrics-server-amd64:v0.3.6  

在Master上執(zhí)行安裝:

git clone https://github.com/kubernetes-incubator/metrics-server.git
cd metrics-server/deploy/1.8+/
修改metrics-server-deployment.yaml
image: k8s.gcr.io/metrics-server-amd64  #在image下添加一下內(nèi)容
        command:
        - /metrics-server
        - --metric-resolution=30s
        - --kubelet-insecure-tls
        - --kubelet-preferred-address-types=InternalIP

查找runAsNonRoot: true  修改為runAsNonRoot: false

kubectl create -f .

如果不能獲取不到鏡像可以更改image: mirrorgooglecontainers/metrics-server-amd64:v0.3.6

Kubernetes V1.16.2部署Dashboard V2.0(beta5)
Kubernetes V1.16.2部署Dashboard V2.0(beta5)
Kubernetes V1.16.2部署Dashboard V2.0(beta5)

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI