Kubernetes 集群部署脚本 此实验是在三台虚拟机上进行部署 三台虚拟的脚本第一部分
此脚本用于在 CentOS 7 系统上安装和配置 Kubernetes 集群。脚本包括主机名设置、网络配置、系统优化、安全配置、Docker 和 CRI-Dockerd 安装、Kubernetes 组件安装以及集群初始化。
脚本内容1 (此脚本三台都可以适用,三台的IP和hosts需要更改) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 hostnamectl set-hostname wangbiao-master nmcli con mod eth0 connection.autoconnect yes ipv4.method manual ipv4.addresses 192.168.44.130/24 ipv4.gateway 192.168.44.2 ipv4.dns 223.5.5.5 nmcli con up eth0 cat <<EOF >> /etc/hosts 192.168.44.130 wangbiao-master 192.168.44.131 wangbiao-node1 192.168.44.132 wangbiao-node2 EOF echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.confsystemctl stop firewalld systemctl disable firewalld systemctl mask firewalld sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0 swapoff -a sed -i 's/.*swap.*/#&/' /etc/fstab cat >/etc/modules-load.d/k8s.conf <<EOF overlay br_netfilter EOF modprobe overlay modprobe br_netfilter cat >> /etc/sysctl.conf <<EOF net.ipv4.ip_forward = 1 net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl -p cd /root/k8syum install *.rpm -y sed -i '4,6d' /etc/chrony.conf sed -i 's/0.centos.pool.ntp.org/ntp.aliyun.com/g' /etc/chrony.conf systemctl start chronyd chronyc sources -v cat >/etc/docker/daemon.json{ "registry-mirrors" : ["https://z5d2yy4c.mirror.aliyuncs.com" ],"exec-opts" : ["native.cgroupdriver=systemd" ]} EOF systemctl enable docker.service --now cd /root sed -i '10s/$/ --pod-infra-container-image=registry.aliyuncs.com\/google_containers\/pause:3.9/' /usr/lib/systemd/system/cri-docker.service systemctl enable cri-docker.service systemctl start cri-docker.service cat >/etc/sysconfig/modules/ipvs.modules <<EOF modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack_ipv4 EOF bash /etc/sysconfig/modules/ipvs.modules lsmod | grep ip_vs sed -i 's/^KUBELET_EXTRA_ARGS=/KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"/' /etc/sysconfig/kubelet systemctl enable kubelet && systemctl start kubelet cd /root/k8s/tar zxf k8s.tar.gz docker load -i k8s.tar kubeadm init --image-repository=registry.aliyuncs.com/google_containers --apiserver-advertise-address=192.168.44.130 --kubernetes-version=v1.28.2 --cri-socket=unix:///var/run/cri-dockerd.sock mkdir -p $HOME /.kubesudo cp -i /etc/kubernetes/admin.conf $HOME /.kube/config sudo chown $(id -u):$(id -g) $HOME /.kube/config export KUBECONFIG=/etc/kubernetes/admin.confkubectl get nodes kubectl get pods -A cd /root/k8skubectl apply -f calico.yaml kubeadm token create --print-join-command > worker-int sed -i '/^kubeadm/{s/$/& --cri-socket=unix:\/\/\/var\/run\/cri-dockerd.sock/}' worker-int
小结:
主机名和网络配置:设置主机名、配置网络接口、更新 /etc/hosts 文件以便于主机之间的通信。 2.系统优化:启用 IP 转发、关闭防火墙、禁用 SELinux、关闭 swap 分区。 3.内核模块加载和系统配置:加载 Kubernetes 需要的内核模块、配置 sysctl 参数。 4.软件安装:安装 Kubernetes 和 Docker 相关的软件包。 5.NTP 配置:配置 NTP 服务,确保时间同步。 6.Docker 配置:配置 Docker 使用阿里云镜像和 cgroup 驱动。 7.CRI-Dockerd 配置:配置和启动 CRI-Dockerd 服务。 8.IPVS 配置:配置 IPVS 以支持负载均衡。 9.Kubernetes 组件安装:安装 kubelet、kubeadm 和 kubectl。 10.集群初始化:初始化 Kubernetes 集群,配置 kubeconfig,检查集群状态。 11.部署网络插件:部署 Calico 网络插件以支持 Pod 网络。 12.工作节点加入:生成并配置工作节点加入集群的命令。
脚本内容2 (此脚本是2个 使用了NFS 共享) 1.脚本1
1 2 3 4 5 6 7 8 9 10 11 12 13 mkdir /web mount wangbiao-master:/web /web echo "wangbiao-master:/web /web nfs defaults 0 0" >> /etc/fstabmount -a echo 'source <(kubectl completion bash)' >> ~/.bashrcsource ~/.bashrc
2.脚本2 (此脚本还包含了一些配置yaml文件)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 mkdir /webcat <<EOF >> /etc/exports /web *(rw,sync,no_root_squash) EOF systemctl enable nfs-server.service --now kubectl completion bash >> ~/.bashrc source ~/.bashrckubectl completion bash >> .bashrc source .bashrckubectl create namespace wangbiao kubectl apply -f mysql-deploy.yaml kubectl apply -f tomcat-deploy.yaml kubectl apply -f nginx.yaml kubectl apply -f financial-deploy.yaml kubectl expose deployment nginx --port 80 --type NodePort -n wangbiao kubectl expose deployment tomcat --port 8080 --type NodePort -n wangbiao kubectl expose deployment mysql --port 3306 --type NodePort -n wangbiao kubectl expose deployment -n wangbiao financial --port 9090 --target-port 8080 kubectl get service -n wangbiao mkdir /web/financialcp financial.jar /web/financial/
mysql-deploy.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: mysql name: mysql namespace: wangbiao spec: replicas: 1 selector: matchLabels: app: mysql strategy: {} template: metadata: creationTimestamp: null labels: app: mysql spec: containers: - image: mysql name: mysql env : - name: MYSQL_ROOT_PASSWORD value: "123" volumeMounts: - name: mysql-data mountPath: /var/lib/mysql - name: mysql-conf mountPath: /etc/mysql/conf.d resources: {} volumes: - name: mysql-data hostPath: path: /web/mysql-data - name: mysql-conf hostPath: path: /web/my-conf status: {}
tomcat-deploy.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: tomcat name: tomcat namespace: wangbiao spec: replicas: 1 selector: matchLabels: app: tomcat strategy: {} template: metadata: creationTimestamp: null labels: app: tomcat spec: containers: - image: tomcat:9.0.78-jdk11-corretto name: tomcat volumeMounts: - name: tomcat-data mountPath: /usr/local/tomcat/webapps resources: {} volumes: - name: tomcat-data hostPath: path: /web/tomcat-data status: {}
nginx.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: nginx name: nginx namespace: wangbiao spec: replicas: 1 selector: matchLabels: app: nginx strategy: {} template: metadata: creationTimestamp: null labels: app: nginx spec: containers: - image: nginx name: nginx volumeMounts: - name: nginx-data mountPath: /usr/share/nginx/html - name: nginx-conf mountPath: /etc/nginx/conf.d resources: {} volumes: - name: nginx-data hostPath: path: /web/nginx-data - name: nginx-conf hostPath: path: /web/nginx-conf/conf.d status: {}
financial-deploy.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: financial name: financial namespace: wangbiao spec: replicas: 1 selector: matchLabels: app: financial strategy: {} template: metadata: creationTimestamp: null labels: app: financial spec: containers: - image: openjdk:11 name: openjdk command : ["bash" ,"-c" ,"java -jar /application/*.jar" ] volumeMounts: - name: financial-app mountPath: /application resources: {} volumes: - name: financial-app hostPath: path: /web/financial status: {}
其他信息
使用包有financial.jar financial_dist.zip jforum-2.7.0.war financial.sql(这个数据库手动在数据库中进行运行还没有写入脚本,手动操作是创建一个financial数据库 再将这个sql文件在数据库中运行)