1. 准备工作

镜像下载地址: http://mirrors.aliyun.com/centos/7.8.2003/isos/x86_64/

操作系统

IP

主机名

centos7.8 2核4G

192.168.220.143

k8s-master

centos7.8 2核4G

192.168.220.144

k8s-node1

centos7.8 2核4G

192.168.220.145

k8s-node2

关闭CentOS防火墙

systemctl disable firewalld
systemctl stop firewalld

设置主机名

hostnamectl set-hostname 主机名

时间同步

yum install ntp
ntpdate 0.rhel.pool.ntp.org

host绑定

vim /etc/hosts
192.168.1.31    k8s-master
192.168.1.32    k8s-node1
192.168.1.33    k8s-node2

2. 安装docker

master节点和所有node节点都需要执行

安装 Docker CE, 设置仓库, 安装所需包

yum install yum-utils device-mapper-persistent-data lvm2

新增 Docker 仓库。

yum-config-manager \
  --add-repo \
  https://download.docker.com/linux/centos/docker-ce.repo

安装 Docker CE.

yum update -y && yum install -y \
  containerd.io-1.2.13 \
  docker-ce-19.03.11 \
  docker-ce-cli-19.03.11

创建 /etc/docker 目录。

mkdir /etc/docker

设置 daemon。

cat > /etc/docker/daemon.json <<EOF
{
  "exec-opts": ["native.cgroupdriver=systemd"]
}
EOF
mkdir -p /etc/systemd/system/docker.service.d

重启 Docker

systemctl daemon-reload
systemctl restart docker

开机启动

systemctl enable docker

3. 安装并配置 kubectl

master节点和所有node节点都需要执行

配置忽略swap报错

cat <<EOF > /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--fail-swap-on=false"
EOF

解决报错 FileContent–proc-sys-net-bridge-brigge-nf-call-iptables
参考文章:

echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables

配置kubenetesyum仓库(这里使用阿里云仓库)

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
        https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum makecache

安装kubelatkubectlkubeadm

yum -y install kubelet-1.19.2 kubeadm-1.19.2 kubectl-1.19.2

查看安装

rpm -aq kubelet kubectl kubeadm

springbootrocketmq集群配置_centos

4. 初始化master

在master节点上执行

kubeadm init 首先运行一系列预检查以确保机器 准备运行 Kubernetes。这些预检查会显示警告并在错误时退出。然后 kubeadm init 下载并安装集群控制平面组件。这可能会需要几分钟。 输出应如下所示

kubeadm init --kubernetes-version=v1.19.2 --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --ignore-preflight-errors=Swap

成功

springbootrocketmq集群配置_kubernetes_02


记录 kubeadm init 输出的 kubeadm join 命令。 你需要此命令将节点加入集群。

要使非 root 用户可以运行 kubectl,请运行以下命令, 它们也是 kubeadm init 输出的一部分

mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config

安装pod网络附加组件flannel

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

下载不了添加hosts

151.101.76.133 raw.githubusercontent.com

部署

kubectl apply -f kube-flannel.yml

部署完查看节点

kubectl get node

springbootrocketmq集群配置_kubernetes_03


查看节点

kubectl get pods -n kube-system

springbootrocketmq集群配置_centos_04


如果出现kube-flannel-ds-nlgdr 镜像下载失败

参考文章:

5. 加入node

在两个node上执行上边init提示的内容

kubeadm join 192.168.220.146:6443 --token h1tt38.3z8up00useg7w305 \
    --discovery-token-ca-cert-hash sha256:7b69f8acec22d9e2533f5902c9fbbaf03837fdfba66b0acd022960ac8d41307a

查询token命令

kubeadm token list

查询sha256命令

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

6. 检查集群状态

kubectl get nodes

springbootrocketmq集群配置_linux_05

官当故障排除文档: https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/troubleshooting-kubeadm/

7. 部署dashboard

参考文章:

wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml

springbootrocketmq集群配置_kubernetes_06


部署

kubectl apply -f recommended.yaml

查看pod

kubectl get pods -n kubernetes-dashboard

springbootrocketmq集群配置_docker_07


创建create-admin.yaml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard
 
---
 
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
 - kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard

部署

kubectl apply -f create-admin.yaml

生成证书

openssl genrsa -out dashboard.key 2048 
openssl req -new -out dashboard.csr -key dashboard.key -subj '/CN=192.168.220.143'
openssl x509 -req -days 3650 -in dashboard.csr -signkey dashboard.key -out dashboard.crt

kubectl create secret generic kubernetes-dashboard-certs --from-file=dashboard.key --from-file=dashboard.crt -n kubernetes-dashboard

删除老的证书

kubectl delete secret kubernetes-dashboard-certs -n kubernetes-dashboard

查看dashboard

kubectl get pod -n kubernetes-dashboard | grep dashboard

springbootrocketmq集群配置_运维_08


删除原有pod即可(会自动创建新的pod)

kubectl delete pod -n kubernetes-dashboard kubernetes-dashboard-7d8466d688-b9kkg

然后访问

https://192.168.220.143:30001/#/login

springbootrocketmq集群配置_运维_09


查询token

kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

springbootrocketmq集群配置_kubernetes_10


输入token登录

springbootrocketmq集群配置_运维_11

8.外网访问pod中的springboot

jenkins部署springboot 创建pod demo3.yaml

apiVersion: v1
kind: Pod
metadata:
 name: demo3
 labels:
  name: demo3
  app: demo3
spec:
 containers:
 - name: demo3
   image: 192.168.220.143:5000/demo3:0.0.1-SNAPSHOT
   ports:
 - containerPort: 50000
     hostPort: 50000

创建service demo3-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: demo3-svc
spec:
 type: NodePort #新加此行
 ports:
 - port: 50000
   nodePort: 30002 #新加此行
   targetPort: 50000
 selector:
  app: demo3

都部署下

kubectl create -f demo3.yaml
kubectl create -f demo3-svc.yaml

即可使用nodePort部署的springboot项目

springbootrocketmq集群配置_kubernetes_12


这样以后每次升级, 只要修改版本, 提交到git, 使用jenkins构建镜像, 推送到私有仓库, 然后修改pod的版本号

springbootrocketmq集群配置_centos_13


springbootrocketmq集群配置_docker_14


就完成了部署