- 单节点k8s服务搭建
- 实战
- ISSUS
- ERROR FileContent—proc-sys-net-bridge-bridge-nf-call-iptables 设置错误导致kubeadm安装k8s失败
- kubernetes [ERROR Swap]: running with swap on is not supported. Please disable swap
- [ERROR SystemVerification]: unexpected kernel config: CONFIG_CGROUP_PIDS
- [ERROR SystemVerification]: missing required cgroups: pids
- Exiting due to GUEST_MISSING_CONNTRACK: Sorry, Kubernetes 1.19.2 requires conntrack to be installed in root’s path
- 资料
单节点k8s服务搭建
虚拟机安装
… 省略
设置网络
yum search ifconfig
yum install -y net-tools.x86_64
vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
ONBOOT=yes —-> no 改成 yes
service network restart
默认网关没有找到
Destination Host Unreachable 解决方法)
安装docker
来,我们通过以下命令即可安装和启动 docker 服务
# curl -fsSL https://get.docker.com -o get-docker.sh
# sh get-docker.sh
# systemctl start docker
# systemctl enable docker
再来,验证一下:
# docker version
安装 kubectl
kubectl 是 k8s 的命令行工具,通过它可操作 k8s 集群。安装过程如下:
# curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
# chmod +x ./kubectl
# mv ./kubectl /usr/local/bin/kubectl
来,验证一下:
# kubectl version --client
安装和启动 minikube
# curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 \
&& chmod +x minikube
# mkdir -p /usr/local/bin/
# install minikube /usr/local/bin/
验证一下吧
# minikube version
此时安装的 minikube 版本号是:v1.7.2
接着启动:
# minikube start --vm-driver=none
蛋尼尔:启动等了好久了也没提示成功,看到:VM is unable to access k8s.gcr.io, you may need to configure a proxy or set —image-repository 昨办?
OK,根据提示信息,应该是国内网络的问题。我们再加个参数重新启动:
# minikube start --vm-driver=none --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
没什么意外的话就成功启动了,我们来验证一下:
# minikube status
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
启动 k8s dashboard
# minikube dashboard
蛋尼尔:哎呀,不太顺利啊,又没成功启动,报错:/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ is not accessible: Temporary Error: unexpected response code: 503
OK。那我们来尝试通过查看日志来找蛛丝马迹吧。
先查看下 pods 的运行状态
# kubectl get pods -A
发现 kubernetes-dashboard 的 pod 状态不正常,那我们接着查看该 pod 的运行日志
# kubectl logs kubernetes-dashboard-79d9cd965-kj5ln -n kubernetes-dashboard
蛋尼尔:发现错误信息了:connect: no route to host
恩,本地路由问题。通过不懈努力地google和尝试,最终的解决方法就是 flush iptables,命令如下:
# systemctl stop kubelet
# systemctl stop docker
# iptables --flush
# iptables -tnat --flush
# systemctl start kubelet
# systemctl start docker
重新运行 minikube dashboard 尝试启动。
蛋尼尔:启动成功啦。但在 Windows 主机怎么访问 dashboard 呢?
先停掉 minikube dashboard 命令,我们改成 kubectl proxy 来启动代理服务,使得外面可通过 IP 来访问 k8s dashboard
// 其中 --address 是你虚拟机的IP,可通过 ifconfig 获得
# kubectl proxy --port=8001 --accept-hosts='^.*' --address='192.168.31.136'
访问地址:http://192.168.31.136:8001/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview?namespace=default
如果你仍然在 Windows 主机的浏览器中无法访问 dashboard,有可能是 CentOS 端口的暴露问题,通过以下增加防火墙规则即可
# firewall-cmd --zone=public --add-port=8001/tcp --permanent
success
# firewall-cmd --reload
success
自启动
最终还是没能通过 /etc/rc.d/rc.local 增加执行脚本的方式自启动,总是会出现各种问题,所以最后写了一个启动脚本 start-k8s.sh,内容如下:
iptables --flush
iptables -tnat --flush
systemctl start docker
minikube start --vm-driver=none --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers
sleep 10
kubectl proxy --port=8001 --accept-hosts='^.*' --address='192.168.31.136'
每次启动 CentOS 后,再手动执行这个脚本即可:sh start-k8s.sh
OK,搞定,可以重启试一下了。
这里没再花时间去折腾,可以优化的空间还是挺大的,有能搞定系统启动时自启动 minikube 的同学,欢迎把解决方案告诉我哈
实战
nginx
cd /opt/k8s/
mkdir pods
cd pods
touch pod-nginx.yaml
apiVersion: v1
kind: Pod # 创建的是Pod类型
metadata: # 元数据, 一些基础信息
name: nginx # 名称
labels:
app: MyApp # 标签
spec:
containers:
- name: pod-nginx # 容器名称
image: nginx:latest # 使用的镜像
ports:
- containerPort: 80 # 容器监听的端口号
# 创建
kubectl create -f pod-nginx.yaml
# 发布
kubectl expose pod nginx --port=80 --type=NodePort
# 查看nginx服务信息
kubectl get svc nginx
java应用
ISSUS
ERROR FileContent—proc-sys-net-bridge-bridge-nf-call-iptables 设置错误导致kubeadm安装k8s失败
echo “1”>/proc/sys/net/bridge/bridge-nf-call-iptables
echo “1”>/proc/sys/net/bridge/bridge-nf-call-ip6tables
kubernetes [ERROR Swap]: running with swap on is not supported. Please disable swap
1. 关掉swapoff
2. 注释掉配置
vi /etc/fstab
重启虚拟机
reboot now
[ERROR SystemVerification]: unexpected kernel config: CONFIG_CGROUP_PIDS
你要在cat /boot/config-uname -r
| grep CGROUP这个文件里面加CONFIG_CGROUP_PIDS=y
[ERROR SystemVerification]: missing required cgroups: pids
//更新yum源
yum update
//安装grub2(centos7)
yum install kernel grub2
//配置grub
//这是未修改前的配置:
[root@localhost ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
//修改/etc/default/grub,将GRUB_CMDLINE_LINUX后面的单词quiet删除,其他不变,保存退出:
[root@localhost ~]# cat /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb"
GRUB_DISABLE_RECOVERY="true"
//配置内核启动文件
mkdir /boot/grub
ln -s /boot/grub2/grub.cfg /boot/grub/grub.cfg
grub2-mkconfig -o /boot/grub/grub.cfg
touch /.autorelabel
//重启等待完成
[root@localhost ~]# reboot
[root@localhost ~]# uname -r
3.10.0-957.27.2.el7.x86_64
Exiting due to GUEST_MISSING_CONNTRACK: Sorry, Kubernetes 1.19.2 requires conntrack to be installed in root’s path
yum install conntrack