Jimmy小站

小明也有大梦想 — 蒋明/铭
当前位置:网站首页 / 未分类 / 正文

Docker自动化部署集群搭建

2021-04-18 / 未分类 / 1409 次围观 / 0 次吐槽

项目环境

  • docker master *1
  • docker slave *2
  • 项目运行中间件环境*1 包含:
    RabbitMQ
    MySQL
    nacos
    Redis
    miniIO oss
  • 持续集成环境*1,可选
    jenkins 不是必须
    harbor 镜像仓库 可使用阿里云私有镜像库代替
    gitlab 源码库 可使用github代替
    ##

关闭不必要的服务

// 关闭防火墙
sudo ufw disable

// 关闭swap
sudo swapoff -a
sudo vim /etc/fstab
注释掉有swap的行,避免开机自动启动

//关闭selinux
sudo apt install -y selinux-utils
setenforce 0
shutdown -r now // 重启
sudo getenforce //检查是否关闭

修改成国内源

ubuntu

sudo vim /etc/apt/sources.list
改成

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ trusty-proposed main restricted universe multiverse

docker

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://y8t1dvc7.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

k8s

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

设置自动启动

sudo systemctl enable kubelet && systemctl start kubelet

配置

vim /etc/host
把主机名和IP地址绑定,方便通过主机名能相互访问

生成配置
kubeadm config print init-defaults ClusterConfiguration > kubeadm.conf
检查配置
kubeadm config images list --config kubeadm.conf
拉取依赖镜像
kubeadm config images pull --config ./kubeadm.conf

尝试修改镜像源imageRepository和master的IPadvertiseAddress和podSubnet

apiVersion: kubeadm.k8s.io/v1beta2
bootstrapTokens:
- groups:
  - system:bootstrappers:kubeadm:default-node-token
  token: abcdef.0123456789abcdef
  ttl: 24h0m0s
  usages:
  - signing
  - authentication
kind: InitConfiguration
localAPIEndpoint:
  advertiseAddress: 192.168.31.20
  bindPort: 6443
nodeRegistration:
  criSocket: /var/run/dockershim.sock
  name: jimmy-virtualbox
  taints:
  - effect: NoSchedule
    key: node-role.kubernetes.io/master
---
apiServer:
  timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta2
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns:
  type: CoreDNS
etcd:
  local:
    dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers
kind: ClusterConfiguration
kubernetesVersion: v1.18.0
networking:
  dnsDomain: cluster.local
  podSubnet: 10.244.0.0/16
  serviceSubnet: 10.96.0.0/12
scheduler: {}

完成之后 初始化

sudo kubeadm init --config ./kubeadm.conf
或者:(推荐前者)
#修改IP地址为master节点的IP地址并配置pod地址
sudo kubeadm init \
--apiserver-advertise-address=192.168.31.20 \
--image-repository registry.aliyuncs.com/google_containers  \
--pod-network-cidr=10.244.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

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/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 192.168.31.20:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:2a074dcbc1bd05675cc7f7b25e09b883d7243965f87dd3d19c68f70e388e0ac4 

下面的命令在master和slave上都要执行,有一个问题,slave刚开始会提示文件不存在。需要从master拷贝过来,貌似等一会会自动同步。

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



systemctl enable kubelet
systemctl start kubelet

结果

jimmy@jimmy-VirtualBox:~$ kubectl get nodes
NAME               STATUS     ROLES    AGE     VERSION
jimmy-virtualbox   NotReady   master   7m43s   v1.18.6

现在master启动了,但是是notready 状态。需要配置master和node之间的网络环境 flannel

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

然后
sudo kubectl apply -f kube-flannel.yml

kube get nodes
结果

jimmy@jimmy-VirtualBox:~$ kubectl get nodes
NAME               STATUS   ROLES    AGE   VERSION
jimmy-virtualbox   Ready    master   21m   v1.18.6

If your cluster was setup to utilize IPVS, run ipvsadm –clear (or similar)
to reset your system’s IPVS tables.

其他

如果不小心在slave安装了kubeadm 可以回撤
sudo kubeadmn reset

从节点
[root@k8snode1 kubernetes]# kubectl get pod
The connection to the server localhost:8080 was refused - did you specify the right host or port

出现这个问题的原因是kubectl命令需要使用kubernetes-admin来运行,解决方法如下,将主节点中的【/etc/kubernetes/admin.conf】文件拷贝到从节点相同目录下,然后配置环境变量:

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source ~/.bash_profile

检查pod状态
sudo kubectl describe pod mysql
报错:open /run/flannel/subnet.env: no such file or directory
在master和slave机器分别新增文件
sudo vim /run/flannel/subnet.env
内容如下

FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true

强行删除,让容器重新启动pod
kubectl delete pod mysql-fh2vm –grace-period=0 –force

再次检查pod状态

yaml部署应用

sudo vim mysql.yml 内容如下

apiVersion: v1
kind: ReplicationController                           
metadata:
  name: mysql                                          
spec:
  replicas: 1 #Pod副本的数量
  selector:
    app: mysql #标签
  template: #根据此模板创建Pod的副本(实例)
    metadata:
      labels:
        app: mysql  #Pod副本拥有的标签,对应RC的Selector
    spec:
      containers:  #Pod内容器的定义部分
      - name: mysql #容器的名称
        image: hub.c.163.com/library/mysql #容器对应的Docker image
        ports: 
        - containerPort: 3306  #容器应用监听的端口号
        env:  #注入容器内的环境变量
        - name: MYSQL_ROOT_PASSWORD 
          value: "123456"

sudo kubectl create -f mysql-rc.yml

推荐您阅读更多有关于“”的文章

[一个Java程序猿的转型之路,读研深造,专注机器学习推荐算法]
本站所有文章如无特别注明均为原创。作者:吉米酱 ,复制或转载请以超链接形式注明转自 Jimmy小站
原文地址《Docker自动化部署集群搭建
额 本文暂时没人评论 来添加一个吧

发表评论

必填

选填

选填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Copyright © Jimmy小站 Allrights Reserved.备案号:桂ICP备 15005996