對于剛接觸到k8s的同學(xué)來說可能有熟悉過其基本概念,但是總在安裝集群這里卡殼,我寫這篇文章的目的是快速的構(gòu)建一個試驗(yàn)可用的k8s單主集群,并不針對生產(chǎn); 文章中盡可能詳細(xì)的向同學(xué)們描述安裝的過程; 該集群構(gòu)建在阿里云ECS中,不具備條件的同學(xué)可使用虛擬機(jī); 步驟及設(shè)置規(guī)則基本相同; 特殊之處會有詳細(xì)的說明;
環(huán)境說明
OS system | roles&ipaddress | hardware&configure |
---|---|---|
centos 7.7 | master&172.26.61.54 | cpu: 2 core Memory: 8G |
centos 7.7 | node&172.26.61.55 | cpu: 2 core Memory: 4G |
centos 7.7 | node&172.26.61.56 | cpu: 2 core Memory: 4G |
所有節(jié)點(diǎn)系統(tǒng)層面準(zhǔn)備工作
# 開啟路由轉(zhuǎn)發(fā)功能及網(wǎng)橋設(shè)置
$ vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
$ sysctl -p
# 加載內(nèi)核lvs相關(guān)內(nèi)核模塊
$ cat >/etc/sysconfig/modules/ipvs.modules<<EOF
#!/bin/bash
modprobe br_netfilter
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack_ipv4
EOF
$ chmod +x /etc/sysconfig/modules/ipvs.modules
$ source /etc/sysconfig/modules/ipvs.modules
所有節(jié)點(diǎn)安裝docker及kubeadm相關(guān)軟件
# step 1: 安裝必要的一些系統(tǒng)工具
$ yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加軟件源信息
$ yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安裝Docker-CE
$ yum makecache fast
$ yum -y install docker-ce
# Step 4: 開啟Docker服務(wù)
$ service docker start
$ chkconfig docker on
# Step 5: 開啟docker鏡像加速
$ tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://aoewjvel.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
$ systemctl daemon-reload && systemctl restart docker
# Step 6: 安裝kubeadm工具
$ 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=0
EOF
$ yum install -y kubelet kubeadm kubectl
$ systemctl enable kubelet && systemctl start kubelet
注意: 根據(jù)官方說明, 在未初始化之前kubelet是無限重啟的狀態(tài),只有初始化后才會真的存活于啟動狀態(tài); 所以kubelet未啟動先不用管!
Master節(jié)點(diǎn): 使用kubeadm初始化
$ kubeadm config print init-defaults >> kubeinit.yml
$ vim kubeinit.yml
localAPIEndpoint:
advertiseAddress: 172.26.61.54 # 本地IP地址
bindPort: 6443
......
......
imageRepository: registry.aliyuncs.com/google_containers # 修改為國內(nèi)鏡像
kind: ClusterConfiguration
kubernetesVersion: v1.17.0 # 可調(diào)整安裝的版本
networking:
dnsDomain: cluster.local
serviceSubnet: 10.96.0.0/12
podSubnet: 10.244.0.0/16 # 增加pod的節(jié)點(diǎn)ip范圍
......
......
# 根據(jù)配置文件下載所需鏡像
$ kubeadm config images pull --config=kubeinit/kubeinit.yml
# 使用配置文件進(jìn)行初始化(要注意保留token字段值, 待會node加入節(jié)點(diǎn)要用到)
$ kubeadm init --config=kubeinit/kubeinit.yml
# 根據(jù)初始化后的輸出進(jìn)行的設(shè)置
$ mkdir -p $HOME/.kube
$ cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ chown $(id -u):$(id -g) $HOME/.kube/config
# 安裝flannel網(wǎng)絡(luò)插件(加載的時間可能會較長,耐心等待即可)
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 最后查看所有組件的運(yùn)行狀態(tài)
$ kubectl get nodes --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-9d85f5447-4g25h 1/1 Running 0 41m
kube-system coredns-9d85f5447-5xggq 1/1 Running 0 41m
kube-system etcd-kubemaster 1/1 Running 0 42m
kube-system kube-apiserver-kubemaster 1/1 Running 0 42m
kube-system kube-controller-manager-kubemaster 1/1 Running 0 42m
kube-system kube-flannel-ds-amd64-7bqgb 1/1 Running 0 39m
kube-system kube-flannel-ds-amd64-b2ps4 1/1 Running 0 17m
kube-system kube-proxy-946mp 1/1 Running 0 17m
kube-system kube-proxy-b7mmc 1/1 Running 0 41m
kube-system kube-scheduler-kubemaster 1/1 Running 0 42m
Node節(jié)點(diǎn): 加入集群 kubeadm join
$ vim joinmaster.yml:
apiVersion: kubeadm.k8s.io/v1beta1
kind: JoinConfiguration
discovery:
bootstraToken:
apiServerEndpoint: 172.26.61.54:6443
token: abcdef.0123456789abcdef #這就是剛剛保存的token
unsafeSkipCAVerification: true
tlsBootstrapToken: abcdef.0123456789abcdef
$ kubeadm join --config=joinmaster.yml
最后查看集群的運(yùn)行狀態(tài)
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
kubemaster Ready master 16h v1.17.3
kubenode Ready <none> 16h v1.17.3
至此集群的安裝已經(jīng)完畢! 下面我們來配置dashboard!
Master節(jié)點(diǎn): 配置Dashboard
# 使用 wget下載dashboard的配置文件到本地更改如下字段
$ wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-rc5/aio/deploy/recommended.yaml
$ vim recommended.yaml
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort # 使用nodePort方式把pod的端口映射到節(jié)點(diǎn)上
ports:
- port: 443
targetPort: 8443
nodePort: 30443 # 節(jié)點(diǎn)端口
selector:
k8s-app: kubernetes-dashboard
# 構(gòu)建dashboard
$ kubectl apply -f recommended.yaml
# 查看dashboard啟動情況
$ kubectl get pods --all-namespaces
......
kubernetes-dashboard dashboard-metrics-scraper-7b8b58dc8b-n6x77 1/1 Running 0 90s
kubernetes-dashboard kubernetes-dashboard-866f987876-zr9zf 1/1 Running 0 90s
注意: 確定dashboard為Running狀態(tài)后,使用火狐瀏覽器訪問時會拒絕連接, 原因是不安全, 我們點(diǎn)擊繼續(xù)訪問即可
生成訪問用戶
$ vim dashboard_config.yml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kube-system
$ kubectl apply -f dashboard_config.yml
# 獲取用戶的token用于登錄
$ kubectl -n kube-system describe secret admin-user-token-n8w8l
# 將輸出的token復(fù)制到瀏覽器中即可訪問dashboard了