1 #!/bin/sh 2 #****************************************************************# 3 # ScriptName: environment.sh 4 # Author: xhy 5 # Create Date: 2020-05-13 12:21 6 # Modify Author: xhy 7 # Modify Date: 2020-05-13 12:21 8 # Version: 9 #***************************************************************# 10 11 # 集群 MASTER 机器 IP 数组 12 export MASTER_IPS=(172.24.12.11 172.24.12.12 172.24.12.13) 13 14 # 集群 MASTER IP 对应的主机名数组 15 export MASTER_NAMES=(master01 master02 master03) 16 17 # 集群 NODE 机器 IP 数组 18 export NODE_IPS=(172.24.12.21 172.24.12.22 172.24.12.23) 19 20 # 集群 NODE IP 对应的主机名数组 21 export NODE_NAMES=(worker01 worker02 worker03) 22 23 # 集群所有机器 IP 数组 24 export ALL_IPS=(172.24.12.11 172.24.12.12 172.24.12.13 172.24.12.21 172.24.12.22 172.24.12.23) 25 26 # 集群所有IP 对应的主机名数组 27 export ALL_NAMES=(master01 master02 master03 worker01 worker02 worker03) 28 29 # etcd 集群服务地址列表 30 export ETCD_ENDPOINTS="https://172.24.12.11:2379,https://172.24.12.12:2379,https://172.24.12.13:2379" 31 32 # etcd 集群间通信的 IP 和端口 33 export ETCD_NODES="master01=https://172.24.12.11:2380,master02=https://172.24.12.12:2380,master03=https://172.24.12.13:2380" 34 35 # 节点间互联网络接口名称 36 export IFACE="eth0" 37 38 # etcd 数据目录 39 export ETCD_DATA_DIR="/data/k3s/etcd/data" 40 41 # etcd WAL 目录,建议是 SSD 磁盘分区,或者和 ETCD_DATA_DIR 不同的磁盘分区 42 export ETCD_WAL_DIR="/data/k3s/etcd/wal"
1 #!/bin/sh 2 #****************************************************************# 3 # ScriptName: k3sinit.sh 4 # Author: xhy 5 # Create Date: 2020-05-13 18:56 6 # Modify Author: xhy 7 # Modify Date: 2020-05-13 18:56 8 # Version: 9 #***************************************************************# 10 # Initialize the machine. This needs to be executed on every machine. 11 12 # Disable the SELinux. 13 sed -i ‘s/^SELINUX=.*/SELINUX=disabled/‘ /etc/selinux/config 14 15 # Turn off and disable the firewalld. 16 systemctl stop firewalld 17 systemctl disable firewalld 18 19 # Modify related kernel parameters & Disable the swap. 20 cat > /etc/sysctl.d/k3s.conf << EOF 21 net.ipv4.ip_forward = 1 22 net.bridge.bridge-nf-call-ip6tables = 1 23 net.bridge.bridge-nf-call-iptables = 1 24 net.ipv4.tcp_tw_recycle = 0 25 vm.swappiness = 0 26 vm.overcommit_memory = 1 27 vm.panic_on_oom = 0 28 net.ipv6.conf.all.disable_ipv6 = 1 29 EOF 30 sysctl -p /etc/sysctl.d/k8s.conf >&/dev/null 31 swapoff -a 32 sed -i ‘/ swap / s/^\(.*\)$/#\1/g‘ /etc/fstab 33 modprobe br_netfilter 34 35 # Add ipvs modules 36 cat > /etc/sysconfig/modules/ipvs.modules <<EOF 37 #!/bin/bash 38 modprobe -- ip_vs 39 modprobe -- ip_vs_rr 40 modprobe -- ip_vs_wrr 41 modprobe -- ip_vs_sh 42 modprobe -- nf_conntrack_ipv4 43 EOF 44 chmod 755 /etc/sysconfig/modules/ipvs.modules 45 bash /etc/sysconfig/modules/ipvs.modules 46 47 # Install rpm 48 yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget
1 [root@master01 ~]# cat > etc/hosts << EOF 2 172.24.12.11 master01 3 172.24.12.12 master02 4 172.24.12.13 master03 5 172.24.12.21 worker01 6 172.24.12.22 worker02 7 172.24.12.23 worker03 8 EOF
1 [root@master01 ~]# source /root/environment.sh 2 [root@master01 ~]# for all_ip in ${ALL_IPS[@]} 3 do 4 echo ">>> ${all_ip}" 5 ssh-copy-id -i ~/.ssh/id_rsa.pub root@${all_ip} 6 scp /etc/hosts root@${all_ip}:/etc/hosts 7 scp environment.sh root@${all_ip}:/root/ 8 scp k3sinit.sh root@${all_ip}:/root/ 9 ssh root@${all_ip} "chmod +x /root/environment.sh" 10 ssh root@${all_ip} "chmod +x /root/k3sinit.sh" 11 ssh root@${all_ip} "bash /root/k3sinit.sh &" 12 done
1 [root@master01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 -o /usr/local/bin/cfssl #下载cfssl软件 2 [root@master01 ~]# chmod u+x /usr/local/bin/cfssl 3 [root@master01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64 -o /usr/local/bin/cfssljson #下载json模板 4 [root@master01 ~]# chmod u+x /usr/local/bin/cfssljson 5 [root@master01 ~]# curl -L https://pkg.cfssl.org/R1.2/cfssl-certinfo_linux-amd64 -o /usr/local/bin/cfssl-certinfo 6 [root@master01 ~]# chmod u+x /usr/local/bin/cfssl-certinfo 7 [root@master01 ~]# mkdir /opt/k3s/work 8 [root@master01 ~]# cd /opt/k3s/work 9 [root@master01 cert]# cfssl print-defaults config > config.json 10 [root@master01 cert]# cfssl print-defaults csr > csr.json #创建模版配置json文件
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# cp config.json ca-config.json #复制一份作为CA的配置文件 3 [root@master01 work]# cat > ca-config.json <<EOF 4 { 5 "signing": { 6 "default": { 7 "expiry": "168h" 8 }, 9 "profiles": { 10 "kubernetes": { 11 "expiry": "87600h", 12 "usages": [ 13 "signing", 14 "key encipherment", 15 "server auth", 16 "client auth" 17 ] 18 } 19 } 20 } 21 } 22 EOF
1 [root@master01 work]# cp csr.json ca-csr.json #复制一份作为CA的证书签名请求文件 2 [root@master01 work]# cat > ca-csr.json <<EOF 3 { 4 "CN": "kubernetes", 5 "key": { 6 "algo": "rsa", 7 "size": 2048 8 }, 9 "names": [ 10 { 11 "C": "CN", 12 "ST": "Shanghai", 13 "L": "Shanghai", 14 "O": "k3s", 15 "OU": "System" 16 } 17 ] 18 } 19 EOF
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# for all_ip in ${ALL_IPS[@]} 4 do 5 echo ">>> ${all_ip}" 6 ssh root@${all_ip} "mkdir -p /etc/kubernetes/cert" 7 scp ca*.pem ca-config.json root@${all_ip}:/etc/kubernetes/cert 8 done
1 [root@master01 ~]# wget https://github.com/coreos/etcd/releases/download/v3.4.7/etcd-v3.4.7-linux-amd64.tar.gz 2 [root@master01 ~]# tar -xvf etcd-v3.4.7-linux-amd64.tar.gz
1 [root@master01 ~]# for master_ip in ${MASTER_IPS[@]} 2 do 3 echo ">>> ${master_ip}" 4 scp etcd-v3.4.7-linux-amd64/etcd* root@${master_ip}:/usr/local/bin 5 ssh root@${master_ip} "chmod +x /usr/local/bin/*" 6 ssh root@${master_ip} "mkdir -p /data/k3s/etcd/data" 7 ssh root@${master_ip} "mkdir -p /data/k3s/etcd/wal" 8 done
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 cert]# cat > etcd-csr.json <<EOF 3 { 4 "CN": "etcd", 5 "hosts": [ 6 "127.0.0.1", 7 "localhost", 8 "172.24.12.11", 9 "172.24.12.12", 10 "172.24.12.13" 11 ], 12 "key": { 13 "algo": "rsa", 14 "size": 2048 15 }, 16 "names": [ 17 { 18 "C": "CN", 19 "ST": "Shanghai", 20 "L": "Shanghai", 21 "O": "k3s", 22 "OU": "System" 23 } 24 ] 25 } 26 EOF 27 #创建etcd的CA证书请求文件
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# cfssl gencert -ca=/opt/k3s/work/ca.pem \ 3 -ca-key=/opt/k3s/work/ca-key.pem -config=/opt/k3s/work/ca-config.json 4 -profile=kubernetes etcd-csr.json | cfssljson -bare etcd #生成CA密钥(ca-key.pem)和证书(ca.pem)
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 ssh root@${master_ip} "mkdir -p /etc/etcd/cert" 7 scp etcd*.pem root@${master_ip}:/etc/etcd/cert/ 8 done
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# cat > etcd.service.template <<EOF 4 [Unit] 5 Description=Etcd Server 6 After=network.target 7 After=network-online.target 8 Wants=network-online.target 9 Documentation=https://github.com/coreos 10 11 [Service] 12 Type=notify 13 WorkingDirectory=${ETCD_DATA_DIR} 14 ExecStart=/usr/local/bin/etcd \ 15 --data-dir=${ETCD_DATA_DIR} \ 16 --wal-dir=${ETCD_WAL_DIR} \ 17 --name=##MASTER_NAME## \\ 18 --cert-file=/etc/etcd/cert/etcd.pem \ 19 --key-file=/etc/etcd/cert/etcd-key.pem \ 20 --trusted-ca-file=/etc/kubernetes/cert/ca.pem \ 21 --peer-cert-file=/etc/etcd/cert/etcd.pem \ 22 --peer-key-file=/etc/etcd/cert/etcd-key.pem \ 23 --peer-trusted-ca-file=/etc/kubernetes/cert/ca.pem \ 24 --peer-client-cert-auth \ 25 --client-cert-auth \ 26 --listen-peer-urls=https://##MASTER_IP##:2380 \\ 27 --initial-advertise-peer-urls=https://##MASTER_IP##:2380 \\ 28 --listen-client-urls=https://##MASTER_IP##:2379,http://127.0.0.1:2379 \\ 29 --advertise-client-urls=https://##MASTER_IP##:2379 \\ 30 --initial-cluster-token=etcd-cluster-0 \ 31 --initial-cluster=${ETCD_NODES} \ 32 --initial-cluster-state=new \ 33 --auto-compaction-mode=periodic \ 34 --auto-compaction-retention=1 \ 35 --max-request-bytes=33554432 \ 36 --quota-backend-bytes=6442450944 \ 37 --heartbeat-interval=250 \ 38 --election-timeout=2000 39 Restart=on-failure 40 RestartSec=5 41 LimitNOFILE=65536 42 43 [Install] 44 WantedBy=multi-user.target 45 EOF
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# for (( i=0; i < 3; i++ )) 4 do 5 sed -e "s/##MASTER_NAME##/${MASTER_NAMES[i]}/" -e "s/##MASTER_IP##/${MASTER_IPS[i]}/" etcd.service.template > etcd-${MASTER_IPS[i]}.service 6 done
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 scp etcd-${master_ip}.service root@${master_ip}:/etc/systemd/system/etcd.service 7 done
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 ssh root@${master_ip} "mkdir -p ${ETCD_DATA_DIR} ${ETCD_WAL_DIR}" 7 ssh root@${master_ip} "systemctl daemon-reload && systemctl enable etcd && systemctl restart etcd " & 8 done
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 ssh root@${master_ip} "systemctl status etcd|grep Active" 7 done
1 [root@master01 ~]# cd /opt/k3s/work 2 [root@master01 work]# source /root/environment.sh 3 [root@master01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 ETCDCTL_API=3 /usr/local/bin/etcdctl 7 --endpoints=https://${master_ip}:2379 8 --cacert=/etc/kubernetes/cert/ca.pem 9 --cert=/etc/etcd/cert/etcd.pem 10 --key=/etc/etcd/cert/etcd-key.pem endpoint health 11 done
1 [root@master01 ~]# source /root/environment.sh 2 [root@master01 ~]# ETCDCTL_API=3 /usr/local/bin/etcdctl \ 3 -w table --cacert=/etc/kubernetes/cert/ca.pem 4 --cert=/etc/etcd/cert/etcd.pem 5 --key=/etc/etcd/cert/etcd-key.pem 6 --endpoints=${ETCD_ENDPOINTS} endpoint status
1 [root@master01 ~]# curl -sfL https://docs.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn \ 2 sh -s - server --write-kubeconfig ~/.kube/config 3 --datastore-endpoint=‘https://172.24.12.11:2379,master02=https://172.24.12.12:2379,master03=https://172.24.12.13:2379‘ --datastore-cafile=/etc/kubernetes/cert/ca.pem 4 --datastore-certfile=/etc/etcd/cert/etcd.pem 5 --datastore-keyfile=/etc/etcd/cert/etcd-key.pem 6 --token=x120952576 7 --tls-san=172.24.12.254 8 [root@master01 ~]# echo "source <(kubectl completion bash)" >> ~/.bashrc
1 [root@master01 ~]# kubectl get nodes
1 [root@master01 ~]# kubectl taint node master01 node-role.kubernetes.io/master="":NoSchedule 2 [root@master01 ~]# kubectl taint node master01 node-role.kubernetes.io/master="":NoSchedule 3 [root@master01 ~]# kubectl taint node master01 node-role.kubernetes.io/master="":NoSchedule
1 [root@master01 ~]# for master_ip in ${MASTER_IPS[@]} 2 do 3 echo ">>> ${master_ip}" 4 ssh root@${master_ip} "yum -y install gcc gcc-c++ make libnl libnl-devel libnfnetlink-devel openssl-devel" 5 ssh root@${master_ip} "wget http://down.linuxsb.com:8888/software/keepalived-2.0.20.tar.gz" 6 ssh root@${master_ip} "tar -zxvf keepalived-2.0.20.tar.gz" 7 ssh root@${master_ip} "cd keepalived-2.0.20/ && ./configure --sysconf=/etc --prefix=/usr/local/keepalived && make && make install" 8 ssh root@${master_ip} "systemctl enable keepalived && systemctl start keepalived" 9 done
1 [root@master01 ~]# wget http://down.linuxsb.com:8888/k3s_ha.sh #下载高可用自动配置脚本 2 [root@master01 ~]# vi k3s_ha.sh #其他部分保持默认 3 # master keepalived virtual ip address 4 export K3SHA_VIP=172.24.12.254 5 6 # master01 ip address 7 export K3SHA_IP1=172.24.12.11 8 9 # master02 ip address 10 export K3SHA_IP2=172.24.12.12 11 12 # master03 ip address 13 export K3SHA_IP3=172.24.12.13 14 15 # master01 hostname 16 export K3SHA_HOST1=master01 17 18 # master02 hostname 19 export K3SHA_HOST2=master02 20 21 # master03 hostname 22 export K3SHA_HOST3=master03 23 24 # master01 network interface name 25 export K3SHA_NETINF1=eth0 26 27 # master02 network interface name 28 export K3SHA_NETINF2=eth0 29 30 # master03 network interface name 31 export K3SHA_NETINF3=eth0 32 33 [root@master01 ~]# bash k3s_ha.sh
1 [root@master01 ~]# cat /etc/keepalived/keepalived.conf 2 [root@master01 ~]# cat /etc/keepalived/check_apiserver.sh 确认Keepalived配置 3 [root@master01 ~]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 ssh root@${master_ip} "systemctl restart keepalived.service" 7 ssh root@${master_ip} "systemctl status keepalived.service" 8 ssh root@${master_ip} "ping -c1 172.24.12.254" 9 done
1 [root@master01 ~]# kubectl -n kube-system get pods | grep -E ‘NAME|nginx‘ 2 NAME READY STATUS RESTARTS AGE 3 nginx-lb-2dk6z 1/1 Running 0 2m56s 4 nginx-lb-68s47 1/1 Running 0 2m56s 5 nginx-lb-nbc9l 1/1 Running 0 2m56s
1 [root@master01 ~]# vi /etc/rancher/k3s/k3s.yaml 2 …… 3 server: https://172.24.12.254:16443 4 ……
1 [root@worker01 ~]# curl -sfL https://docs.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn \ 2 sh -s - agent --server https://172.24.12.254:16443 --token x120952576 3 [root@worker01 ~]# echo "source <(kubectl completion bash)" >> ~/.bashrc
1 [root@master01 ~]# k3s server --help #查看k3s server更多参数 2 [root@worker01 ~]# k3s agent --help #查看k3s agent更多参数
1 [root@master01 ~]# yum -y install iscsi-initiator-utils
1 [root@master01 ~]# wget \ 2 https://raw.githubusercontent.com/longhorn/longhorn/master/deploy/longhorn.yaml 3 [root@master01 ~]# vi longhorn.yaml
1 #…… 2 --- 3 kind: Service 4 apiVersion: v1 5 metadata: 6 labels: 7 app: longhorn-ui 8 name: longhorn-frontend 9 namespace: longhorn-system 10 spec: 11 type: NodePort #修改为nodeport 12 selector: 13 app: longhorn-ui 14 ports: 15 - port: 80 16 targetPort: 8000 17 nodePort: 8888 18 --- 19 #……
1 [root@master01 ~]# kubectl get sc 2 NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE 3 …… 4 longhorn driver.longhorn.io Delete Immediate true 15m
1 [root@master01 ~]# vi longhornsc.yaml
1 kind: StorageClass 2 apiVersion: storage.k8s.io/v1 3 metadata: 4 name: longhornsc 5 provisioner: rancher.io/longhorn 6 parameters: 7 numberOfReplicas: "3" 8 staleReplicaTimeout: "30" 9 fromBackup: ""
1 [root@master01 ~]# kubectl create -f longhornsc.yaml
1 [root@master01 ~]# vi longhornpod.yaml
1 apiVersion: v1 2 kind: PersistentVolumeClaim 3 metadata: 4 name: longhorn-pvc 5 spec: 6 accessModes: 7 - ReadWriteOnce 8 storageClassName: longhorn 9 resources: 10 requests: 11 storage: 2Gi 12 --- 13 apiVersion: v1 14 kind: Pod 15 metadata: 16 name: longhorn-pod 17 namespace: default 18 spec: 19 containers: 20 - name: volume-test 21 image: nginx:stable-alpine 22 imagePullPolicy: IfNotPresent 23 volumeMounts: 24 - name: volv 25 mountPath: /data 26 ports: 27 - containerPort: 80 28 volumes: 29 - name: volv 30 persistentVolumeClaim: 31 claimName: longhorn-pvc 32
1 [root@master01 ~]# kubectl create -f longhornpod.yaml
原文:https://www.cnblogs.com/itzgr/p/12886477.html