前两个步骤主要是参考文章 《VMware虚拟机部署k8s集群》
最后一个步骤主要是参考文章 《k8s部署单机版mysql5.6》
ifcfg-ens33
cd /etc/sysconfig/network-scripts/ ls
BOOTPROTO=dhcp
和 ONBOOT=yes
vi ifcfg-ens33
service netowrk restart
ping 110.242.68.3
ip addr
# 关闭防火墙 systemctl stop firewalld && systemctl disable firewalld # 关闭 SElinux setenforce 0 sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config # 关闭 swap 分区 swapoff -a sed -ri 's/.*swap.*/#&/' /etc/fstab # 配置 iptables cat <<EOF | tee /etc/modules-load.d/k8s.conf br_netfilter EOF cat <<EOF | tee /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF sysctl --system
# 安装 yum-utils yum install -y yum-utils # 配置 yum docker源 阿里云 yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # 安装 docker yum install -y docker-ce-20.10.7 docker-ce-cli-20.10.7 containerd.io-1.4.6 systemctl enable docker --now # 配置 docker 仓库源 mkdir -p /etc/docker tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://82m9ar63.mirror.aliyuncs.com"], "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } EOF sudo systemctl daemon-reload sudo systemctl restart docker # 检测一下 docker 安装效果,没有报错即为成功 docker info
# 配置 k8s 下载源 cat <<EOF | 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 exclude=kubelet kubeadm kubectl EOF # 安装 kubelet、kubeadm、kubectl yum install -y kubelet-1.20.9 kubeadm-1.20.9 kubectl-1.20.9 --disableexcludes=kubernetes # 启动kubelet sudo systemctl enable --now kubelet
echo "192.168.59.128 cluster_endpoint" >> /etc/hosts
hostnamectl set-hostname cluster_endpoint # 检查一下 hostname
sudo tee ./images.sh <<-'EOF' #!/bin/bash images=( kube-apiserver:v1.20.9 kube-proxy:v1.20.9 kube-controller-manager:v1.20.9 kube-scheduler:v1.20.9 coredns:1.7.0 etcd:3.4.13-0 pause:3.2 ) for imageName in ${images[@]} ; do docker pull registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/$imageName done EOF chmod +x ./images.sh && ./images.sh
# init 只需要修改apiserver的地址即可,其他不用改变 kubeadm init \ --apiserver-advertise-address=192.168.59.128 \ --control-plane-endpoint=cluster-endpoint \ --image-repository registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images \ --kubernetes-version v1.20.9 \ --service-cidr=10.96.0.0/16 \ --pod-network-cidr=192.168.0.0/16
Your Kubernetes control-plane has initialized successfully! 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 Alternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.conf 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/ You can now join any number of control-plane nodes by copying certificate authorities and service account keys on each node and then running the following as root: kubeadm join cluster-endpoint:6443 --token 0bw6u4.gqtvp4m3s86uowkt \ --discovery-token-ca-cert-hash sha256:c01776440ff767075ac2cb84ad9b1db7a579a0273e0ee04c24c4dddeb3b37ae1 \ --control-plane Then you can join any number of worker nodes by running the following on each as root: kubeadm join cluster-endpoint:6443 --token 0bw6u4.gqtvp4m3s86uowkt \ --discovery-token-ca-cert-hash sha256:c01776440ff767075ac2cb84ad9b1db7a579a0273e0ee04c24c4dddeb3b37ae1
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config export KUBECONFIG=/etc/kubernetes/admin.conf
curl https://docs.projectcalico.org/v3.18/manifests/calico.yaml -O kubectl apply -f calico.yaml
# 根据node节点设置 node1 node2 hostnamectl set-hostname k8s-node1
kubeadm token create --print-join-command
kubeadm join cluster-endpoint:6443 --token 0bw6u4.gqtvp4m3s86uowkt --discovery-token-ca-cert-hash sha256:c01776440ff767075ac2cb84ad9b1db7a579a0273e0ee04c24c4dddeb3b37ae1
kubectl get pod --all-namespaces -o wide
kubectl get nodes
通过 PV PVC Deployment Service 来完成部署,PV完成数据的持久化映射
cd mkdir data mkdir mysql_package cd mysql_package
# 新建 PV 配置文件 vi mysql-pv.yaml # mysql-pv.yaml apiVersion: v1 kind: PersistentVolume metadata: name: mysql-pv labels: pv: mysql-pv spec: capacity: storage: 20Gi accessModes: - ReadWriteOnce persistentVolumeReclaimPolicy: Retain hostPath: path: /root/data # 创建PV kubectl create -f mysql-pv.yaml # 检验 kubectl get pv
# 新建 PVC 配置文件 vi mysql-pvc.yaml # cat mysql-pvc.yaml kind: PersistentVolumeClaim apiVersion: v1 metadata: name: mysql-pvc spec: storageClassName: "" accessModes: - ReadWriteOnce resources: requests: storage: 10Gi selector: matchLabels: pv: mysql-pv # 创建 PVC kubectl create -f mysql-pvc.yaml # 校验 kubectl get pvc
# 新建 Deployment 配置文件 vi mysql-deploy.yaml # cat mysql-deploy.yaml apiVersion: apps/v1 kind: Deployment metadata: name: mysql-deploy spec: replicas: 1 selector: matchLabels: name: mysql-ops template: metadata: labels: name: mysql-ops spec: containers: - name: mysql56 image: mysql:5.6 imagePullPolicy: IfNotPresent env: - name: MYSQL_ROOT_PASSWORD value: "123456" ports: - containerPort: 3306 volumeMounts: - name: mysql-persistent-storage mountPath: "/var/lib/mysql" volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pvc # 创建 Deployment kubectl create -f mysql-deploy.yaml # 校验 kubectl get deploy
# 新建 Service 配置文件 vi mysql-service.yaml # cat mysql-service.yaml apiVersion: v1 kind: Service metadata: name: mysql-svc labels: name: mysql-svc spec: type: NodePort ports: - port: 3306 protocol: TCP targetPort: 3306 name: http nodePort: 30056 selector: name: mysql-ops # 创建 Service kubectl create -f mysql-service.yaml # 校验 kubectl get svc
等待一段时间后,k8s集群自动分配Node进行部署MySQL,通过命令查看pod对应的node
kubectl get po -o wide
从图中可以看到,部署在了k8s-node3节点
登录 k8s=node3 机器,查看ip,通过mysql可视化工具连接对应 ip:port,user=root,password=123456
连接成功,也可以通过对应机器上 docker 命令进入容器执行相应操作。
docker ps
# 找到对应的容器ID,进入容器,执行mysql命令 docker exec -it 0f12726aae19 /bin/bash # 执行MySQL命令 mysql -uroot -p123456