master+ansible 192.168.2.72 # 使用pod 4核4G 20Gi
node1 192.168.2.73 # 使用pod 2核2G 20Gi
node2 192.168.2.94 # 使用pod 2核2G 20Gi
nfs 192.168.2.81 # 不使用pod 1核1G 20Gi
load-balancer 192.168.2.79 # 不使用pod + 编译安装nginx7层负载 2核2G 20Gi
ens33 192.168.2.79
ens37 192.168.2.82
prometheus 192.168.2.75 # 使用pod 2核2G 20Gi
测试 192.168.2.88 # 不使用pod 2核2G 20Gi
免密通道
ssh-keygen -t ecdsa
ssh-copy-id -i ~/.ssh/id_ecdsa.pub 192.168.2.72 # master
ssh-copy-id -i ~/.ssh/id_ecdsa.pub 192.168.2.73 # node1
ssh-copy-id -i ~/.ssh/id_ecdsa.pub 192.168.2.94 # node2
ssh-copy-id -i ~/.ssh/id_ecdsa.pub 192.168.2.81 # nfs-server
ssh-copy-id -i ~/.ssh/id_ecdsa.pub 192.168.2.79 # load-balancer
# prometheus待定
ssh-copy-id -i ~/.ssh/id_ecdsa.pub 192.168.2.** # prometheus
ansible-playbook
yum install epel-release -y
yum install ansible -y
vim /etc/ansible/hosts
[docker-kubeadm]
192.168.2.72
192.168.2.73
192.168.2.94
[nfs]
192.168.2.81
192.168.2.72
192.168.2.73
192.168.2.94
[loadbalancer]
192.168.2.79
vim ansible_playbook.yaml
- hosts: docker-kubeadm
remote_user: root
tasks:
- name: deploy kubeadm
script: ~/install_kubeadm.sh
- hosts: loadbalancer
remote_user: root
tasks:
- name: deploy nginx
script: ~/onekey_install_nginx.sh
- hosts: nfs
remote_user: root
tasks:
- name: install nfs-utils
script: ~/install_nfs.sh
ansible-playbook ansible_playbook.yaml
docker-kubeadm - 部署kubeadm,kubelet和kubectl 脚本
vim install_kubeadm.sh
#!/bin/bash
# 卸载旧版本
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine
yum install -y yum-utils
yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
#配置 Docker 使用 systemd 作为默认 Cgroup 驱动
cat <<EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors" : [
"https://registry.docker-cn.com",
"https://docker.mirrors.ustc.edu.cn",
"http://hub-mirror.c.163.com",
"https://cr.console.aliyun.com/"]
}
EOF
#重启docker
systemctl restart docker
#关闭swap分区
swapoff -a # 临时
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab # 永久
# 修改主机名后使用su - root 重新登录
# su - root
cat >> /etc/hosts << EOF
192.168.2.72 master
192.168.2.73 node1
192.168.2.94 node2
EOF
#添加kubernetes 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
#安装kubeadm,kubelet和kubectl
yum install -y kubelet kubeadm kubectl
#设置开机自启
systemctl enable --now kubelet
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux
setenforce 0 #临时关闭
#永久关闭
sed -i '/^SELINUX/ s/enforcing/disabled/' /etc/selinux/config
load-balancer - 编译安装nginx 脚本
vim onekey_install_nginx.sh
#!/bin/bash
#解决软件的依赖关系,需要安装的软件包
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc gcc-c++ autoconf automake make
#useradd cPen
id cPen || useradd -s /sbin/nologin cPen
#download nginx
mkdir -p /nginx
cd /nginx
curl -O http://nginx.org/download/nginx-1.19.8.tar.gz
#解压 下载的nginx的源码包
tar xf nginx-1.19.8.tar.gz
cd nginx-1.19.8
#生成编译前配置工作 --> Makefile
./configure --prefix=/usr/local/nginx --user=cPen --group=cPen --with-threads --with-http_ssl_module --with-http_realip_module --with-http_v2_module --with-file-aio --with-http_stub_status_module --with-stream
#编译
make -j 2
#编译安装 --> 将编译好的二进制程序安装到指定目录 /usr/local/nginx1
make install
#修改PATH变量
echo "PATH=$PATH:/usr/local/nginx/sbin" >>/root/.bashrc
#执行修改了环境变量的脚本
source /root/.bashrc
#启动nginx
/usr/local/nginx/sbin/nginx
#firewalld and selinux
#stop firewalld和设置下次开机不启动firewalld
service firewalld stop
systemctl disable firewalld
#临时停止selinux和永久停止selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/sysconfig/selinux
#开机自启
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
chmod +x /etc/rc.d/rc.local
下载nfs-utils的脚本
vim install_nfs.sh
#!/bin/bash
yum install nfs-utils -y
service nfs-server start
systemctl enable nfs-server
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/sysconfig/selinux
service firewalld stop
systemctl disable firewalld
kubeadm init \
--apiserver-advertise-address=192.168.2.72 \
--image-repository registry.aliyuncs.com/google_containers \
--service-cidr=10.1.0.0/16 \
--pod-network-cidr=10.244.0.0/16
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
node节点加入
kubeadm join …… 略
vim kube-flannel.yml
---
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: quay.io/coreos/flannel:v0.13.1-rc2
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.13.1-rc2
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
kubectl apply -f kube-flannel.yml
vim /etc/exports
/data 192.168.2.0/24(rw,no_root_squash,no_all_squash,sync)
mkdir /data/
chmod 777 /data/
service nfs-server restart
exportfs -rv
systemctl enable nfs-server
echo "hello,world" >>/data/index.html
mkdir pv-pvc-pod-nfs
cd pv-pvc-pod-nfs/
PV-持久卷
vim pv-nfs.yaml
apiVersion: v1
kind: PersistentVolume #资源类型
metadata:
name: cl-nginx-pv
labels:
type: cl-nginx-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteMany #访问模式,多个客户端读写
persistentVolumeReclaimPolicy: Recycle #回收策略-可以回收
storageClassName: nfs #注:指定存储卷的类型
nfs:
path: "/data"
server: 192.168.2.81 #k8s-nfs
readOnly: false #不是只读
PVC-持久卷消费者
vim pvc-nfs.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: cl-nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: nfs
deployment起pod
vim new-pod-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-nginx
spec:
replicas: 10
selector:
matchLabels:
app: cl
tier: backend-nginx
track: stable
template:
metadata:
labels:
app: cl
tier: backend-nginx
track: stable
spec:
containers:
- name: cl-pv-container-nfs
image: nginx
ports:
- containerPort: 80
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: cl-pv-storage-nfs
volumes:
- name: cl-pv-storage-nfs
persistentVolumeClaim:
claimName: cl-nginx-pvc
创建PV-持久卷、PVC-持久卷消费者、使用deployment起pod
kubectl apply -f pv-nfs.yaml
kubectl apply -f pvc-nfs.yaml
kubectl apply -f new-pod-nginx.yaml
master主机 - 暴露端口
kubectl expose deployment/backend-nginx --type="NodePort" --port 80
vim /usr/local/nginx/conf/nginx.conf
……
upstream myweb {
server 192.168.2.72:端口待定;
server 192.168.2.73:……;
server 192.168.2.94:……;
}
server {
listen 80;
server_name www.cl.com;
location / {
proxy_pass http://myweb;
}
……
nginx -s reload
1
启动集群以后,node节点加入 显示STATUS NotReady 节点一直未准备。
错误原因:CNI插件还未安装,所以状态会是NotReady。
解决方案:
master节点里/etc/cni/net.d目录中拥有10-flannel.conflist文件,将这个文件scp到节点机的/etc/cni/net.d的文件下就解决了。
2
节点加入不进k8s集群
kubeadm join 超时报错
error execution phase kubelet-start: error uploading crisocket: timed out waiting for the condition
节点敲 kubeadm reset
重新再join即可
3
错误(大坑):pod起不来
错误原因:内存不足,副本数起的太多,建议在3-5个。