1 安装依赖软件和环境配置
- 配置 Master 和 work 节点的域名
vi /etc/hosts
192.168.99.101 master
192.168.99.102 node1
192.168.99.103 node2
- CPU 设置为 2 核
- 下载阿里云的yum源repo文件:
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- 安装基本软件包
yum install wget net-tools ntp git ‐y
- 同步系统时间
ntpdate 0.asia.pool.ntp.org
- 配置Docker, K8S的阿里云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=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 clean all
yum -y makecache
- 确保 br_netfilter 模块被加载。
通过运行 lsmod | grep br_netfilter 来确认br_netfilter已经加载。若要加载该模块,可执行 sudo modprobe br_netfilter。 - 将桥接的IPv4流量传递到iptables
为了让你的 Linux 节点上的 iptables 能够正确地查看桥接流量,你需要确保在你的 sysctl 配置中将 net.bridge.bridge-nf-call-iptables 设置为 1。执行:
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF
写入配置:
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sudo sysctl --system
- 关闭 防火墙
systemctl stop firewalld
systemctl disable firewalld
- 关闭 SeLinux
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
- 关闭 swap
swapoff -a
yes | cp /etc/fstab /etc/fstab_bak
永久关闭
vi /etc/fstab #注释下面这一行
#/dev/mapper/centos-swap swap swap defaults 0 0
1.1 Master节点安装 kubeadm, kubelet and kubectl
- 首先确保虚拟机的 CPU 为 2 核(Kubeadm init 要求 2 核)
- 修改docker配置文件,使用 systemd 作为 cgroup 的驱动(Kubeadm init 推荐)
- 本课程基于Kubernetes1.19.3 版本进行搭建,大家在安装kubectl和kubeadm的时候一定指定这个版本,否则后续的实践可能出错。
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://registry.cn-hangzhou.aliyuncs.com"]
}
EOF
# Restart Docker
systemctl daemon-reload
systemctl restart docker
systemctl enable docker
- 安装kubelet、kubeadm、kubectl
yum install -y kubelet-1.19.3 kubeadm-1.19.3 kubectl-1.19.3
-
配置系统自动启动 kubelet
systemctl enable kubelet && systemctl start kubelet
- 下载镜像
镜像下载的脚本
Kubernetes几乎所有的安装组件和Docker镜像都放在goolge自己的网站上,直接访问可能会有网络问题,这里的解决办法是从阿里云镜像仓库下载镜像,拉取到本地以后改回默认的镜像tag。
vi image.sh
加入以下代码
#!/bin/bash
url=registry.cn-hangzhou.aliyuncs.com/google_containers
version=v1.19.3
images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
for imagename in ${images[@]} ; do
docker pull $url/$imagename
docker tag $url/$imagename k8s.gcr.io/$imagename
docker rmi -f $url/$imagename
done
-
下载镜像
运行脚本image.sh,下载指定版本的镜像,运行脚本前先赋权。
[root@master ~]# chmod u+x image.sh
[root@master ~]# ./image.sh
[root@master ~]# docker images
目录
1.1 Master节点安装 kubeadm, kubelet and kubectl
2 . 初始化Master 节点
请使用 root账号在 Master 节点上安装,work node 也需要使用 root 账号。
- 设置主机名,管理节点设置主机名为master
hostnamectl set-hostname master
- 初始化主节点
kubeadm init --kubernetes-version=1.19.3 \
--apiserver-advertise-address=192.168.99.101 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
此时会等待镜像拉取,网络 5M 的情况下 1 分钟能完成。
注意:如果你用的是云环境,需要把apiserver-advertise-address改成主节点的内网 ip 地址。
如果启动失败提示:kubelet 没有启动,则可以启动 kubelet ,再执行kubeadm reset, init。
如果出现错误:
[root@bogon kubeblog]# kubectl get nodes
The connection to the server localhost:8080 was refused - did you specify the right host or port?
执行以下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
- 配置KUBECONFIG 环境变量
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
- 安装网络插件 Flannel(只需要在 Master 安装 flannel)
可以使用课程代码库的文件:
git clone https://git.imooc.com/coding-464/kubeblog.git
kubectl apply -f kubeblog/docs/Chapter4/flannel.yaml
拉不下拉,就直接复制下面代码即可 vi flannel.yaml 保存
---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: psp.flannel.unprivileged
annotations:
seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
privileged: false
volumes:
- configMap
- secret
- emptyDir
- hostPath
allowedHostPaths:
- pathPrefix: "/etc/cni/net.d"
- pathPrefix: "/etc/kube-flannel"
- pathPrefix: "/run/flannel"
readOnlyRootFilesystem: false
# Users and groups
runAsUser:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
fsGroup:
rule: RunAsAny
# Privilege Escalation
allowPrivilegeEscalation: false
defaultAllowPrivilegeEscalation: false
# Capabilities
allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
defaultAddCapabilities: []
requiredDropCapabilities: []
# Host namespaces
hostPID: false
hostIPC: false
hostNetwork: true
hostPorts:
- min: 0
max: 65535
# SELinux
seLinux:
# SELinux is unused in CaaSP
rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
rules:
- apiGroups: ['extensions']
resources: ['podsecuritypolicies']
verbs: ['use']
resourceNames: ['psp.flannel.unprivileged']
- 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-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
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-system
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
image: registry.cn-beijing.aliyuncs.com/qingfeng666/flannel:v0.13.0
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: registry.cn-beijing.aliyuncs.com/qingfeng666/flannel:v0.13.0
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
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
- 查看是否成功创建flannel网络
ifconfig |grep flan
flannel.1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1450
等待大约 10 分钟,检查Kubernetes master运行情况:
[root@localhost ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
master Ready master 10m v1.19.3