安裝要求:
在開始之前,部署Kubernetes集群機器需要滿足以下幾個條件,官方解釋:
- 一臺或多臺機器,作業系統 CentOS7.x-86_x64
- 硬體配置:2GB或更多RAM,2個CPU或更多CPU,硬碟30GB或更多
- 可以訪問外網,需要拉取鏡像,如果服務器不能上網,需要提前下載鏡像并匯入節點
- 禁止swap磁區
如果嫌麻煩,后面給出了搭建好的集群虛擬機,開箱即用
一、環境準備(虛擬機)
| 角色 | IP |
|---|---|
| master | 192.168.1.21 |
| node1 | 192.168.1.22 |
| node2 | 192.168.1.23 |
- 虛擬機靜態ip配置,參見:Vmware靜態IP配置詳解-避坑指南
- 命令多服務器批量執行,參見:linux命令多服務器分發執行【xcall】
- linux互信配置,參見:linux服務器互信配置
接下來,請按照以下步驟依次執行,master、node1、node2都需要執行
- 關閉防火墻,永久禁用
systemctl disable firewalld
- 關閉selinux,永久關閉
sed -i 's/enforcing/disabled/' /etc/selinux/config
- 關閉swap,永久關閉
sed -ri 's/.*swap.*/#&/' /etc/fstab
- 根據規劃設定主機名
# <hostname>分別修改為:master、node1、node2,分別在對應的這三臺服務器執行
hostnamectl set-hostname <hostname> --static
- 添加hosts
cat >> /etc/hosts << EOF
192.168.1.21 master
192.168.1.22 node1
192.168.1.23 node1
EOF
- 將橋接的IPv4流量傳遞到iptables的鏈
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 生效
sysctl --system
- 時間同步
yum install ntpdate -y
ntpdate time.windows.com
二、所有節點安裝Docker/kubeadm/kubelet
Kubernetes默認CRI(容器運行時)為Docker,因此先安裝Docker,
1. 安裝Docker
- 下載docker包
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
- 安裝docker
yum -y install docker-ce-18.06.1.ce-3.el7
- 啟動后可以通過命令
docker --version查看是否安裝成功,如果顯示Docker version 18.06.1-ce, build e68fc7a則是成功
systemctl enable docker && systemctl start docker
2. 配置阿里鏡像倉庫
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
3. 添加阿里云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
4.安裝kubeadm,kubelet和kubectl
- 由于版本更新頻繁,這里指定版本號部署
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
- 添加到開機自啟
systemctl enable kubelet
三、部署Kubernetes Master
- 在192.168.1.21(Master節點)執行
- 由于默認拉取鏡像地址k8s.gcr.io國內無法訪問,這里指定阿里云鏡像倉庫地址
kubeadm init \
--apiserver-advertise-address=192.168.44.146 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
- 配置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
四、創建永久token
- 由于默認生成的token只有24小時有效期,避免到期后不可用,提前創建永久token
- 本命令為創建token,
--ttl 0表示永不過期 - 命令執行后會輸出最后一行
p4rynu.uj4jaxnzk2s0y9vi就是生成的token
kubeadm token create --ttl 0
W0322 22:42:54.687441 18368 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
p4rynu.uj4jaxnzk2s0y9vi
- 可以通過如下命令,查看token串列
kubeadm token list
- 獲取集群CA證書的HASH值,最后一行輸出為HASH值
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outformder 2>/dev/null|openssl dgst -sha256 -hex | sed 's/^.* //'
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
- 自己創建將node加入master的命令
- 將上面得到的
token值替換如下<token> - 將上面得到的
hash值替換如下<hash> - 將master節點ip值替換如下
<masterIP>
- 將上面得到的
kubeadm join --token <token> <masterIP>:6443 --discovery-token-ca-cert-hash sha256:<hash>
五、加入Kubernetes Node
- 在192.168.1.22/23(Node)執行
- 向集群添加新節點,執行在kubeadm init輸出的kubeadm join命令,不過我們用我們自己上一步自己創建的命令
- 用你自己創建的,別用我的,如下只是一個示例!!!
kubeadm join --token p4rynu.uj4jaxnzk2s0y9vi 192.168.1.21:6443 --discovery-token-ca-cert-hash sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
六、部署CNI網路插件
- 執行以下命令,如果你的網路是通的則用下載的檔案,否則跳過本步,用我給你的吧
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
- 以下是我放好的
kube-flannel.yml,你也可以FQ,在瀏覽器下載好放到一個檔案中
點擊查看kube-flannel.yml檔案
---
kind: Namespace
apiVersion: v1
metadata:
name: kube-flannel
labels:
pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-flannel
labels:
tier: node
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "flannel",
"delegate": {
"hairpinMode": true,
"isDefaultGateway": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds
namespace: kube-flannel
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
hostNetwork: true
priorityClassName: system-node-critical
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni-plugin
#image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)
image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
command:
- cp
args:
- -f
- /flannel
- /opt/cni/bin/flannel
volumeMounts:
- name: cni-plugin
mountPath: /opt/cni/bin
- name: install-cni
#image: flannelcni/flannel:v0.20.1 for ppc64le and mips64le (dockerhub limitations may apply)
image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.1
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
#image: flannelcni/flannel:v0.20.1 for ppc64le and mips64le (dockerhub limitations may apply)
image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.1
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN", "NET_RAW"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: EVENT_QUEUE_DEPTH
value: "5000"
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
- name: xtables-lock
mountPath: /run/xtables.lock
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni-plugin
hostPath:
path: /opt/cni/bin
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
- name: xtables-lock
hostPath:
path: /run/xtables.lock
type: FileOrCreate
- 檔案放到哪里都可以,我們只用一次,在有此檔案的目錄執行以下命令,只在master節點執行
kubectl apply -f kube-flannel.yml
- 執行完畢后,通過以下命令可以查看運行中的網路服務
kubectl get pods -n kube-system
七、測驗kubernetes集群
- 在Kubernetes集群中創建一個pod,驗證是否正常運行:
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
訪問地址:http://NodeIP:Port
八、開箱即用的k8s,新鮮出爐的!
- 使用注意:!!!基于vmware 15搭建,里面4個服務器,base的是主服務器,master、node1、node2是克隆連接,機器ip與本教程完全相同,可放心使用!
鏈接:https://pan.baidu.com/s/1OL2ZqlveMOmRIKEdHU-w4Q?pwd=abcd
提取碼:abcd
--來自百度網盤超級會員V8的分享
- 手機用戶趕緊掃一掃獲取吧!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/537734.html
標籤:其他
