Kubernetes(k8s)是自动化容器操作的开源平台,基于这个平台,你可以进行容器部署,资源调度和集群扩容等操作。如果你曾经用过Docker部署容器,那么可以将Docker看成Kubernetes底层使用的组件,Kubernetes是Docker的上层封装,通过它可以很方便的进行Docker集群的管理。今天我们使用minikube在单机上进行Kubernetes集群的部署,目的是让我们对k8s有个初步的认识。
Minikube 支持以下 Kubernetes 功能:
-
NodePorts
-
ConfigMaps 和 Secrets
-
Dashboards
-
容器运行时: Docker、CRI-O 以及 containerd
-
启用 CNI (容器网络接口)
-
Ingress
-
关闭安全策略
/usr/bin/systemctl stop firewalld /usr/bin/systemctl disable firewalld /usr/sbin/iptables -F && /usr/sbin/iptables -X && /usr/sbin/iptables -F -t nat && /usr/sbin/iptables -X -t nat && /usr/sbin/iptables -P FORWARD ACCEPT /usr/sbin/iptables -P FORWARD ACCEPT /usr/bin/sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0
2. 安装docker
yum install -y epel-release /usr/bin/yum install -y conntrack wget net-tools jq yum-utils ntpdate ntp ipvsadm ipset device-mapper-persistent-data iptables curl sysstat libseccomp wget lrzsz bash-completion yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo yum install -y docker-ce-18.09.4 docker-ce-cli-18.09.4 systemctl start docker systemctl enable docker
3.加载所有节点IPVS模块
usr/sbin/modprobe ip_vs /usr/sbin/modprobe ip_vs_rr /usr/sbin/modprobe ip_vs_wrr /usr/sbin/modprobe ip_vs_sh /usr/sbin/modprobe nf_conntrack_ipv4
4.关闭swap缓存
/usr/sbin/swapoff -a /usr/bin/sed -i '/ swap / s/^/(.*/)$/#/1/g' /etc/fstab
5.优化内核参数
cat >/etc/sysctl.d/k8s.conf <<EOF net.bridge.bridge-nf-call-iptables=1 net.bridge.bridge-nf-call-ip6tables=1 net.ipv4.ip_forward=1 net.ipv4.tcp_tw_recycle=0 vm.swappiness=0 # 禁止使用 swap 空间,只有当系统 OOM 时才允许使用它 vm.overcommit_memory=1 # 不检查物理内存是否够用 vm.panic_on_oom=0 # 开启 OOM vm.swappiness=0 fs.inotify.max_user_instances=8192 fs.inotify.max_user_watches=1048576 fs.file-max=52706963 fs.nr_open=52706963 net.ipv6.conf.all.disable_ipv6=1 net.netfilter.nf_conntrack_max=2310720 EOF modprobe br_netfilter sysctl -p /etc/sysctl.d/k8s.conf
以上步骤就是minikube的初始化环境,同时也是kubeadm的初始化环境,只是运行不同的命令而已,minikube内部就是运行kubeadm。
6.下载软件
wget https://storage.googleapis.com/kubernetes-release/release/v1.16.0/bin/linux/amd64/kubectl chmod +x ./kubectl mv ./kubectl /usr/local/bin/kubectl curl -Lo minikube https://storage.googleapis.com/minikube/releases/v1.4.0/minikube-linux-amd64 / && chmod +x minikube
国外网站,很慢,最好自己找资源(FQ)下载这两个文件,我下载的k8是1.16.0版本,想下载最新版或指定版本就更改版本号就行。
7.运行minikubu
minikube start --image-repository='registry.cn-hangzhou.aliyuncs.com/google_containers' --vm-driver=none
minikube start
命令可用于启动集群。
此命令将创建并配置一台虚拟机,使其运行单节点 Kubernetes
集群。指定国内镜像,可省却国外下载镜像步骤,去网上翻minikube的文章,会发现基本上都是各种报错,我是在一台裸机上部署的,除了开发工具啥都没装,
此命令还会配置您的 kubectl 安装,以便使其能与您的 Kubernetes 集群正确通信。
指定 VM 驱动程序
您可以通过将 --vm-driver=<enter_driver_name>
参数添加到 minikube start
来更改 VM 驱动程序。 例如命令:
minikube start --vm-driver=<driver_name>
Minikube 支持以下驱动程序:
注意: 有关支持的驱动程序以及如何安装插件的详细信息,请参阅驱动程序。
-
virtualbox
-
vmwarefusion
-
kvm2 (驱动安装)
-
hyperkit (驱动安装)
-
hyperv (驱动安装) 请注意,下面的 IP 是动态的,可以更改。可以使用
minikube ip
检索。 -
vmware (驱动安装) (VMware 统一驱动)
-
none (在主机上运行Kubernetes组件,而不是在 VM 中。使用该驱动依赖 Docker (安装 Docker) 和 Linux 环境)
成功显示
* minikube 1.6.2 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.6.2 * To disable this notice, run: 'minikube config set WantUpdateNotification false' ! minikube v1.4.0 on Centos 7.7.1908 * Using image repository registry.cn-hangzhou.aliyuncs.com/google_containers * Running on localhost (CPUs=4, Memory=7521MB, Disk=51175MB) ... * OS release is CentOS Linux 7 (Core) * Preparing Kubernetes v1.16.0 on Docker 18.09.4 ... * Downloading kubeadm v1.16.0 * Downloading kubelet v1.16.0 * Pulling images ... * Launching Kubernetes ... * Configuring local host environment ... * ! The 'none' driver provides limited isolation and may reduce system security and reliability. ! For more information, see: - https://minikube.sigs.k8s.io/docs/reference/drivers/none/ * ! kubectl and minikube configuration will be stored in /root ! To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run: * - sudo mv /root/.kube /root/.minikube $HOME - sudo chown -R $USER $HOME/.kube $HOME/.minikube * * This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true * Waiting for: apiserver proxy etcd scheduler controller dns * Done! kubectl is now configured to use "minikube" 在运行Pulling images的时候另开个终端运行: systemctl enable kubelet
在Pulling images的时候很慢,因为镜像很多,有的很大,比较慢,可以通过docker images查看已经下载完成的镜像,镜像组件功能在这里不做介绍。
registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver v1.16.0 b305571ca60a 4 months ago 217MB registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy v1.16.0 c21b0c7400f9 4 months ago 86.1MB registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager v1.16.0 06a629a7e51c 4 months ago 163MB registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler v1.16.0 301ddc62b80b 4 months ago 87.3MB registry.cn-hangzhou.aliyuncs.com/google_containers/etcd 3.3.15-0 b2756210eeab 4 months ago 247MB registry.cn-hangzhou.aliyuncs.com/google_containers/coredns 1.6.2 bf261d157914 5 months ago 44.1MB registry.cn-hangzhou.aliyuncs.com/google_containers/kube-addon-manager v9.0.2 bd12a212f9dc 5 months ago 83.1MB registry.cn-hangzhou.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 2 years ago 742kB registry.cn-hangzhou.aliyuncs.com/google_containers/storage-provisioner v1.8.1 4689081edb10 2 years ago 80.8MB
8.添加快捷键
source <(kubectl completion bash) echo "source <(kubectl completion bash)" >>~/.bashrc
9.测试
kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready master 2m8s v1.16.0 kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-67c766df46-2n8bz 1/1 Running 1 43m coredns-67c766df46-stttx 1/1 Running 1 43m etcd-minikube 1/1 Running 1 42m kube-addon-manager-minikube 1/1 Running 1 42m kube-apiserver-minikube 1/1 Running 1 42m kube-controller-manager-minikube 1/1 Running 1 42m kube-proxy-2v42d 1/1 Running 1 43m kube-scheduler-minikube 1/1 Running 1 42m storage-provisioner 1/1 Running 1 42m
10. 删除集群
minikube delete rm -rf /root/.minikube
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/aiops/183494.html