美文网首页
k8s部署脚本

k8s部署脚本

作者: proud2008 | 来源:发表于2021-02-23 17:07 被阅读0次

参考

ubuntu 20环境下 使用阿里云镜像

#安装 ssh
sudo apt install openssh-server
service sshd start
# net工具ifconifg
sudo apt install net-tools

# 关闭swap
# 临时
swapoff -a 
# 永久关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab

# 根据规划设置主机名【master节点上操作】
hostnamectl set-hostname k8smaster
# 根据规划设置主机名【node1节点操作】
hostnamectl set-hostname k8snode1
# 根据规划设置主机名【node2节点操作】
hostnamectl set-hostname k8snode2



# 将桥接的IPv4流量传递到iptables的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
# 生效
sysctl --system  

# 时间同步
apt install ntpdate -y
ntpdate time.windows.com

#安装docker
apt install curl
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

# 安装kubeadm,kubelet和kubectl 安装k8s相关命令
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - 

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF  
#更新
apt-get update
apt-get install -y kubelet kubeadm kubectl
# 设置开机启动
systemctl enable kubelet
# 查看版本
kubectl version
# GitVersion:"v1.20.4"

master节点,


# 在master添加hosts
cat >> /etc/hosts << EOF
192.168.19.132 k8smaster
192.168.19.133 k8snode1
192.168.19.134 k8snode2
EOF
kubeadm init --apiserver-advertise-address=192.168.19.132 --image-repository registry.aliyuncs.com/google_containers  --service-cidr=10.96.0.0/12  --pod-network-cidr=10.244.0.0/16
#执行完成后出现下面日志 即成功
#Your Kubernetes control-plane has initialized successfully!
#若失败可通过sudo kubeadm reset 恢复重新操作

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 查看节点情况  不执行上面的语句  会报The connection to the server localhost:8080 was refused - did you specify the right host or port?
kubectl get nodes
#k8smaster   NotReady   control-plane,master   56s   v1.20.4

#部署CNI网络插件
# 下载网络插件配置 [flannel](https://github.com/flannel-io/flannel)
#无法下载可以下载到本地 导入到服务端
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f kube-flannel.yml

# 查看状态 【kube-system是k8s中的最小单元】
kubectl get pods -n kube-system

# 重新生成token
kubeadm token create --print-join-command

node端

#添加到集群
kubeadm join 192.168.19.132:6443 --token 58do6d.5z5pqzyyx3jsgalc     --discovery-token-ca-cert-hash sha256:7843449a7f9250f2ec5e02c70ddca7cecf1a4b9794e53c4770837b7b7f347d32 


测试

# 下载nginx 【会联网拉取nginx镜像】
kubectl create deployment nginx --image=nginx
# 查看状态
kubectl get pod
# 暴露端口
kubectl expose deployment nginx --port=80 --type=NodePort
# 查看一下对外的端口
kubectl get pod,svc
#kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        91m
#nginx        NodePort    10.98.87.231   <none>        80:32562/TCP   3m52s
curl 10.98.87.231:80内部即可访问
外部使用ip:32562
192.168.19.132:32562

问题记录

1、kubeadm init时

[kubelet-check] Initial timeout of 40s passed.

    Unfortunately, an error has occurred:
        timed out waiting for the condition

    This error is likely caused by:
        - The kubelet is not running
        - The kubelet is unhealthy due to a misconfiguration of the node in some way (required cgroups disabled)

    If you are on a systemd-powered system, you can try to troubleshoot the error with the following commands:
        - 'systemctl status kubelet'
        - 'journalctl -xeu kubelet'

    Additionally, a control plane component may have crashed or exited when started by the container runtime.
    To troubleshoot, list all containers using your preferred container runtimes CLI.

    Here is one example how you may list all Kubernetes containers running in docker:
        - 'docker ps -a | grep kube | grep -v pause'
        Once you have found the failing container, you can inspect its logs with:
        - 'docker logs CONTAINERID'

error execution phase wait-control-plane: couldn't initialize a Kubernetes cluster
To see the stack trace of this error execute with --v=5 or higher
root@ubuntu:/home/xin# docker cgroups disabled

通过分析docker 容器的报错信息发现是init后的ip地址写的不正确

相关文章

网友评论

      本文标题:k8s部署脚本

      本文链接:https://www.haomeiwen.com/subject/indrfltx.html