您好,登錄后才能下訂單哦!
kubeadm是官方社區推出的一個用于快速部署kubernetes集群的工具。
在開始之前,部署Kubernetes集群機器需要滿足以下幾個條件:
關閉防火墻:
systemctl stop firewalld
systemctl disable firewalld
關閉selinux:
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
關閉swap:
swapoff -a $ 臨時
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab $ 永久
添加主機名與IP對應關系(記得設置主機名):
$ 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
Kubernetes默認CRI(容器運行時)為Docker,因此先安裝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
$ 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
由于版本更新頻繁,這里指定版本號部署:
yum install -y kubelet-1.16.2 kubeadm-1.16.2 kubectl-1.16.2 bash-completion wget
systemctl enable kubelet
在9.110.187.120(Master)執行。
$ 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
由于默認拉取鏡像地址k8s.gcr.io國內無法訪問,這里指定阿里云鏡像倉庫地址。
使用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
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
如果下載失敗,可以改成這個鏡像地址:lizhenliang/flannel:v0.11.0-amd64
在9.110.187.125/126(Node)執行。
向集群添加新節點,執行在kubeadm init輸出的kubeadm join命令:
$ kubeadm join 9.110.187.120:6443 --token esce21.q6hetwm8si29qxwn \
--discovery-token-ca-cert-hash sha256:00603a05805807501d7181c3d60b478788408cfe6cedefedb1f97569708be9c5
在Kubernetes集群中創建一個pod,驗證是否正常運行:
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
$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta5/aio/deploy/recommended.yaml
修改recommended.yaml文件內容(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
---
#因為自動生成的證書很多瀏覽器無法使用,所以我們自己創建,注釋掉kubernetes-dashboard-certs對象聲明
#apiVersion: v1
#kind: Secret
#metadata:
# labels:
# k8s-app: kubernetes-dashboard
# name: kubernetes-dashboard-certs
# namespace: kubernetes-dashboard
#type: Opaque
---
創建證書
mkdir dashboard-certs
cd dashboard-certs/
#創建命名空間
kubectl create namespace kubernetes-dashboard #yaml里會自動創建,可以不用單獨創建
# 創建key文件
openssl genrsa -out dashboard.key 2048
#證書請求
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
#創建kubernetes-dashboard-certs對象
kubectl create secret generic kubernetes-dashboard-certs --from-file=dashboard.key --from-file=dashboard.crt -n kubernetes-dashboard
部署Dashboard
涉及到的兩個鏡像可以先下載下來
#安裝
kubectl create -f ~/recommended.yaml
#檢查結果
kubectl get pods -A -o wide
kubectl get service -n kubernetes-dashboard -o wide
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
為用戶分配權限:
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
創建service account并綁定默認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。
在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上執行安裝:
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下添加一下內容
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
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。