我是靠谱客的博主 凶狠冬天,最近开发中收集的这篇文章主要介绍【代码】kubenetes集群部署,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

基于kubeadm

配置机器
准备虚拟机vmware
系统镜像为:CentOS-7-x86_64-DVD-1708
CPU 2核
磁盘 20G,
内存 2G
共三台,组成集群

安装选择 Install CentOS 7
软件选择 开发及生产工作站
安装位置选择默认自动分区
禁用Kdump
打开网络

在这里我的虚拟网卡有点问题,总是装不上, 于是就nat模式
只有三点就好:

  1. 能联网
  2. 能互通,
  3. 复制命令, 我直接在一个节点开终端操作, 没想到支持文字复制, 还算ok

命令模式
systemctl set-default multi-user.target
图形模式
systemctl set-default graphical.target

配置网络
查看ip地址:
ip addr
配置静态ip,保证节点之间互通:
vim /etc/sysconfig/network-scripts/ifcfg-ens33

BOOTPROTO=static
IPADDR=192.168.182.5
NETMASK=255.255.255.0
GATEWAY=192.168.182.2
ONBOOT=yes

配置dns
vim /etc/resolv.conf

nameserver 192.168.182.2

配置主机名

vim /etc/hostname
node1

vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=node3

配置host文件

cat >> /etc/hosts << EOF
192.168.182.3 node1
192.168.182.4 node2
192.168.182.5 node3
EOF

保证网络通畅

service network restart
ping baidu.com
ping 192.168.182.4
ping 192.168.182.3

配置yum源

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 
yum makecache

关闭防火墙

systemctl stop firewalld & systemctl disable firewalld

关闭Swap

swapoff -a
编辑/etc/fstab,注释掉包含swap的那一行即可

添加docker仓库

yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache

安装docker

yum install docker-ce -y
docker --version

启动docker

systemctl start docker & systemctl enable docker
docker run --rm hello-world

关闭selinux

setenforce 0

配置kubeadm镜像源

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

设置iptables内核参数

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system

安装kubeadm组件

yum install -y kubelet kubeadm kubectl
yum -y install kubernetes-cni
systemctl enable kubelet && systemctl start kubelet

获取kubeadm镜像
在这里, 前四个都是最新的1.16.0版本
关于使用的版本可直接kubeadm init 后通过报错中的版本查看当前需要的镜像版本

#!/bin/bash
APISERVER=v1.16.0
MANAGER=v1.16.0
SCHEDULER=v1.16.0
PROXY=v1.16.0
PAUSE=3.1
ETCD=3.3.15-0
COREDNS=1.6.2

docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:$APISERVER
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:$MANAGER
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:$SCHEDULER
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:$PROXY
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$PAUSE
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:$ETCD
docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:$COREDNS

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:$APISERVER k8s.gcr.io/kube-apiserver:$APISERVER
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:$MANAGER k8s.gcr.io/kube-controller-manager:$MANAGER
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:$SCHEDULER k8s.gcr.io/kube-scheduler:$SCHEDULER
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:$PROXY k8s.gcr.io/kube-proxy:$PROXY
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:$PAUSE k8s.gcr.io/pause:$PAUSE
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:$ETCD k8s.gcr.io/etcd:$ETCD
docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:$COREDNS k8s.gcr.io/coredns:$COREDNS

报错
Cannot connect to the Docker daemon at unix:

systemctl daemon-reload
service docker restart
service docker status

master 节点初始化

kubeadm init --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12

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

查看错误日志
journalctl -f -u kubelet

发现警告
Warning:detected “cgroupfs” as the Docker cgroup driver. The recommended driver is “systemd”.
vim /etc/docker/daemon.json

{
 "exec-opts":["native.cgroupdriver=systemd"]
}

systemctl restart docker
systemctl status docker

查看, 卸载安装过的包
yum list installed | grep docker
yum -y remove docker-engine.x86_64

安装网络插件
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

查看kube-system状态
kubectl get pods -n kube-system
发现 coredns Pending, 加上"cniVersion": “0.2.0” 即可
vim /etc/cni/net.d/10-flannel.conflist


  "name": "cbr0",
  "cniVersion": "0.2.0",
  "plugins": [
    {
      "type": "flannel",
      "delegate": {
        "hairpinMode": true,
        "isDefaultGateway": true
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    }
  ]
}

安装worker节点
只需要到 安装kubeadm组件 这一步
如果init过, 那就kubeadm reset, 再清理下: rm -rf $HOME/.kube, 然后再join

发现无法 kubectl get no
he connection to the server localhost:8080 was refused - did you specify the right host or port?
需要: kubectl get nodes node2 --kubeconfig=/etc/kubernetes/kubelet.conf

发现 worker NotReady
跟之前一样, 需要加上 cni键
vim /etc/cni/net.d/10-flannel.conflist

给node打label
kubectl label nodes node3 node-role.kubernetes.io/worker=

k8s的helloworld
docker images | grep hello-world
kubectl run helloworld --image=hello-world --replicas=2 --port=80
kubectl delete deploy helloworld

安装dashboard

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
kubectl get pod -owide  --all-namespaces=true
发现ImagePullBackOff
describe pod 发现:
Failed to pull image "k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1"

去dashboard安装的node-3执行

docker pull mirrorgooglecontainers/kubernetes-dashboard-amd64:v1.10.1
docker tag mirrorgooglecontainers/kubernetes-dashboard-amd64:v1.10.1 k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
docker rmi mirrorgooglecontainers/kubernetes-dashboard-amd64:v1.10.1

未成功,pod一直重启

端口转发访问pod

kubectl port-forward my-pod hostport:podport

下载镜像heapster-influxdb-amd64
超时, 于是:

docker pull mirrorgooglecontainers/heapster-influxdb-amd64:v1.5.2
docker tag docker.io/mirrorgooglecontainers/heapster-influxdb-amd64:v1.5.2 k8s.gcr.io/heapster-influxdb-amd64:v1.5.2
docker rmi -f mirrorgooglecontainers/heapster-influxdb-amd64:v1.5.2

kubectl监听资源
kubectl get no --watch

下载源码

 git clone -c core.symlinks=true -b release-1.9 https://github.com/kubernetes/kubernetes.git

Docker 源
修改 /etc/docker/daemon.json 文件并添加上 registry-mirrors 键值。

{  
	"registry-mirrors": ["https://registry.docker-cn.com"]
}

https://7bezldxe.mirror.aliyuncs.com

最后

以上就是凶狠冬天为你收集整理的【代码】kubenetes集群部署的全部内容,希望文章能够帮你解决【代码】kubenetes集群部署所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部