单master集群

欠伸展肢体,吟咏心自愉。这篇文章主要讲述单master集群相关的知识,希望能为你提供帮助。
1. 准备工作centos7.6的系统

主机名
IP
配置
角色
k8s-master1
10.0.0.2
4c4g
k8s-master
k8s-master2
10.0.0.3
4c4g
k8s-master,node
k8s-master3
10.0.0.4
4c4g
k8s-master,node
1.解析域名??DNS??
2. 更改主机名
hostnamectl set-hostname k8s-master1
hostnamectl set-hostname k8s-master2
hostnamectl set-hostname k8s-master3
hostnamectl set-hostname k8s-vip

3. 调整系统参数在所有集群机器上操作
3.1 清空iptables规则
iptables -P FORWARD ACCEPT

3.2 关闭swap
swapoff -a
# 防止开机自动挂载 swap 分区
sed -i \'/ swap / s/^\\(.*\\)$/#\\1/g\' /etc/fstab

3.3 关闭selinux和防火墙
sed -ri \'s#(SELINUX=).*#\\1disabled#\' /etc/selinux/config
setenforce 0
systemctl disable firewalld & & systemctl stop firewalld

3.4 修改内核参数
#句柄数 vm.max_map_count=262144
cat < < EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
vm.max_map_count=262144
EOF
sysctl -p /etc/sysctl.d/k8s.conf
modprobe br_netfilter
#加载网桥防火墙

3.5 yum源优化
curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
curl -o /etc/yum.repos.d/Centos-7.repo http://mirrors.aliyun.com/repo/Centos-7.repo
cat < < EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum clean all & & yum makecache

2. 安装docker在所有集群机器上操作
## 查看所有的可用版本
yum list docker-ce --showduplicates | sort -r
## 安装当前源里的最新版本
yum install docker-ce-20.10.6 -y
## 配置docker加速
mkdir -p /etc/docker
# harbor为后面会用到的docker私有仓库
vi /etc/docker/daemon.json
{
"insecure-registries": [
"harbor.hs.com"
],
"registry-mirrors" : [
"https://8xpk5wnt.mirror.aliyuncs.com"
]
}
## 启动docker
systemctl enable docker & & systemctl start docker

3. 安装K8S相关依赖在所有集群机器上操作
yum install -y kubelet-1.19.8 kubeadm-1.19.8 kubectl-1.19.8
systemctl enable kubelet# 可以选择不启动, 等下初始化节点的时候,会替你启动

4. 初始化集群在任意一个master节点操作
kubeadm init --pod-network-cidr 172.16.0.0/16 --service-cidr 10.96.0.0/16--image-repository registry.aliyuncs.com/google_containers

参数
--pod-net* 规定k8s集群内pod可使用的地址
--service-cidr规定k8s内service资源的地址
--image 指定镜像下载地址
1. 创建相关目录初始化完成后,最后的输出内容会包括如下一种
根据指引,创建相应的文件夹和目录。才能正常使用kubectl命令
To start using your cluster, you need to run the following as a regular user:

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

2.安装网络插件在这两段中间,有个提示,告诉你需要安装网络插件。不安装的话,节点状态会是notreday
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/

2.1 安装flannel插件。
具体这些插件,等下会介绍
下载地址:wget ??https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml??
可能会下不来。就贴在下面了
cat kube-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-plugin
image: rancher/mirrored-flannelcni-flannel-cni-plugin:v1.2
command:
- cp
args:
- -f
- /flannel
- /opt/cni/bin/flannel
volumeMounts:
- name: cni-plugin
mountPath: /opt/cni/bin
- name: install-cni
image: quay.io/coreos/flannel:v0.15.0-rc1
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: quay.io/coreos/flannel:v0.15.0-rc1
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-plugin
hostPath:
path: /opt/cni/bin
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg

做如下调整.在上面的文件内 找到对应的模块。大概在第190行附近
containers:
- name: kube-flannel
image: quay.io/coreos/flannel:v0.11.0-amd64
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
- --iface=eth0# 如果机器存在多网卡的话,指定内网网卡的名称,默认不指定的话会找第一块网

拉取镜像
docker pull quay.io/coreos/flannel:v0.14.0-amd64

启动flannel
kubectl apply -f kube-flannel.yml

3. 将其他节点加入集群
Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 10.0.0.2:6443 --token dugy0z.mvvpejvbjc95vn8m \\
--discovery-token-ca-cert-hash sha256:548076fcd6c70d4a903a14c67e28ad3709cf0ec56019032b3cb74bd61f1c596a

5. 验证集群1 取消master节点的污点
kubectl taint node k8s-master node-role.kubernetes.io/master:NoSchedule-

2 验证状态
kubectl get nodes
NAMESTATUSROLESAGEVERSION
k8s-master1Readymaster14mv1.19.8
k8s-master2Readymaster10mv1.19.8
k8s-master3Readymaster6m27sv1.19.8
kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAMESTATUSMESSAGEERROR
controller-managerHealthyok
schedulerHealthyok
etcd-0Healthy{"health":"true"}

3 验证可用性
kubectl runtest-nginx --image=nginx:alpine
kubectl get po -o wide#看到running即可
NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODEREADINESS GATES
test-nginx-5bd8859b98-5nnnw1/1Running09s172.16.1.2k8s-slave1< none> < none>
curl172.16.1.2
...
< h1> Welcome to nginx!< /h1>
< p> If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.< /p>

< p> For online documentation and support please refer to
< a href="http://nginx.org/"> nginx.org< /a> .< br/>
Commercial support is available at
< a href="http://nginx.com/"> nginx.com< /a> .< /p>

< p> < em> Thank you for using nginx.< /em> < /p>
< /body>
< /html>

5. 部署dashboard5.1 获取yaml文件
wget kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml

5.2 更改部分参数
vi recommended.yaml
# 修改Service为NodePort类型,文件的45行上下
......
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
type: NodePort# 加上type=NodePort变成NodePort类型的服务
......

5.3 部署服务
kubectl apply -f recommended.yaml

5.4 查看服务状态
$ kubectl -n kubernetes-dashboard get svc
NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE
dashboard-metrics-scraperClusterIP172.16.62.124< none> 8000/TCP31m
kubernetes-dashboardNodePort172.16.74.46< none> 443:30133/TCP31m

5.5 创建登录秘钥
vi dashboard-admin.conf
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: admin
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
subjects:
- kind: ServiceAccount
name: admin
namespace: kubernetes-dashboard

---
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin
namespace: kubernetes-dashboard

kubectl apply -f dashboard-admin.conf #创建服务

5.6 获取秘钥
kubectl -n kubernetes-dashboard get secret |grep admin-token
admin-token-fqdpfkubernetes.io/service-account-token37m17s
kubectl -n kubernetes-dashboard get secret admin-token-fqdpf -o jsonpath={.data.token}|base64 -d
eyJhbGciOiJSUzI1NiIsImtpZCI6Ik1rb2xHWHMwbWFPMjJaRzhleGRqaExnVi1BLVNRc2txaEhETmVpRzlDeDQifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi10b2tlbi1mcWRwZiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJhZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjYyNWMxNjJlLTQ1ZG...

5.7登录访问

访问地址
??https://10.0.0.2://30133??
单master集群

文章图片

单master集群

文章图片

6. 清理环境如果搭建过程中遇到了问题
# 在全部集群节点执行
kubeadm reset
ifconfig cni0 down & & ip link delete cni0
ifconfig flannel.1 down & & ip link delete flannel.1
rm -rf /run/flannel/subnet.env
rm -rf /var/lib/cni/
mv /etc/kubernetes/ /tmp
mv /var/lib/etcd /tmp
mv ~/.kube /tmp
iptables -F
iptables -t nat -F
ip link del dummy0

7. 设置kubectl自动补全

操作节点:??k8s-master??
【单master集群】

yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source < (kubectl completion bash)
echo "source < (kubectl completion bash)" > > ~/.bashrc


    推荐阅读