一、基础环境设置
// 所有节点hosts文件全部改
cat >> /etc/hosts <<EOF
192.168.111.128 k8s-master1
192.168.111.129 k8s-node1
EOF
systemctl disable --now NetworkManager (centos8的话就不需要)
systemctl disable --now firewalld
systemctl disable --now dnsmasq
setenforce 0
vim /etc/sysconfig/selinux
SELINUX=disabled
swapoff -a && sysctl -w vm.swappiness=0
sed -ri 's/.*swap.*/#&/' /etc/fstab
vim /etc/fstab
#/dev/mapper/cl-swap swap (如果有这一行的话就注释一下)
// 设置时间服务器
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' > /etc/timezone
ntpdate time2.aliyun.com
// 定时同步时间
crontab -e
*/5 * * * * ntpdate time2.aliyun.com
ulimit -SHn 65535
# 配置k8s的yum源地址
cat <<EOF | sudo tee /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
#安装 kubelet,kubeadm,kubectl
yum install -y kubelet-1.23.5 kubeadm-1.23.5 kubectl-1.23.5
systemctl daemon-reload
systemctl enable kubelet && systemctl start kubelet
#初始化master节点(主节点执行)
kubeadm init \
--apiserver-advertise-address=192.168.111.128 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.23.5 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
// 根据安装结果提示执行
#查询join token
kubeadm token create --print-join-command
#安装calico
curl https://docs.projectcalico.org/v3.18/manifests/calico.yaml -O
kubectl apply -f calico.yaml
#查看pod详细信息
kubectl get pod -n kube-system -o wide
#启动deployment
kubectl create deployment test-deployment --image=webdevops/php-nginx:7.3-alpine --port=80 --replicas=3 -n namespace
#查看svc
kubectl get svc
#暴露deployment端口,外部才能访问
kubectl expose deployment test-deployment --port=80 --target-port=80
#修改deployment的svc type
kubectl edit svc test-deployment
ClusterIP改为NodePort(会将80端口映射到宿主机的某个端口通过kubectl get svc查看)
#修改pod数量
kubectl scale --replicas=2 deployment/test-deployment
#安装k8s-dashboard
# 下载并应用文件
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.3.1/aio/deploy/recommended.yaml
kubectl apply -f recommended.yaml
#修改TYPE为NodePort
kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard
#创建认证文件
vim dashboard-svc-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: dashboard-admin
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: dashboard-admin
subjects:
- kind: ServiceAccount
name: dashboard-admin
namespace: kube-system
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
# 执行
kubectl apply -f dashboard-svc-account.yaml
#令牌获取
kubectl get secret -n kube-system |grep admin|awk '{print $1}'
#token获取
kubectl describe secret dashboard-admin-token-qt2n5 -n kube-system|grep '^token'|awk '{print $2}'