CentOS 7

CentOS7下的各种应用


CentOS7安装K8S

<p>第2章 kubernetes安装配置指南 2.1 系统要求 2.2 使用kubeadm工具快速安装kubernetes集群 2.2.1 安装kubernetes和相关工具 2.2.2 kubeadm config 2.2.3 下载kubenetes的相关镜像 2.2.4 运行kubeadm init命令安装Master 2.2.5 安装Node,加入集群 2.2.6 安装网络插件 2.2.7 验证kubernets集群是否安装完成</p> <h1>一、环境准备</h1> <h2>验证MAC地址和product_uuid对于每个节点都是唯一的</h2> <p>获取MAC地址</p> <pre><code class="language-bash">ifconfig -a 或 ip link</code></pre> <p>获取product_uui</p> <pre><code class="language-bash">cat /sys/class/dmi/id/product_uuid</code></pre> <p>尽管某些虚拟机可能具有相同的值,但是硬件设备很有可能具有唯一的地址。Kubernetes使用这些值来唯一地标识集群中的节点。如果这些值不是每个节点唯一的,则安装过程可能会失败。</p> <h2>检查网络适配器</h2> <p>如果您有多个网络适配器,并且您的Kubernetes组件在默认路由上不可用,我们建议您添加IP路由,以便Kubernetes群集地址通过适当的适配器。</p> <h2>让iptables查看桥接流量</h2> <p>作为您的Linux节点iptables正确查看桥接流量的要求,您应确保net.bridge.bridge-nf-call-iptables在sysctl配置中将其设置为1 ,即修改内核参数</p> <h3>修改内核参数</h3> <pre><code class="language-bash">cat &lt;&lt;EOF &gt; /etc/sysctl.d/k8s.conf net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 EOF</code></pre> <h3>使命令生效</h3> <p>br_netfilter在执行此步骤之前,请确保已加载模块。这可以通过运行来完成lsmod | grep br_netfilter。要显式加载,请调用sudo modprobe br_netfilter。</p> <pre><code class="language-bash">modprobe br_netfilter sysctl --system</code></pre> <h2>检查所需的端口</h2> <h3>管理节点master</h3> <table> <thead> <tr> <th>协议</th> <th>方向</th> <th>端口范围</th> <th>目的</th> <th>使用者</th> </tr> </thead> <tbody> <tr> <td>TCP协议</td> <td>入站</td> <td>6433*</td> <td>Kubernetes API server</td> <td>All</td> </tr> <tr> <td>TCP协议</td> <td>入站</td> <td>2379-2380</td> <td>etcd server client API</td> <td>kkube-apiserver, etcd</td> </tr> <tr> <td>TCP协议</td> <td>入站</td> <td>10250</td> <td>Kubelet API</td> <td>Self, Control plane</td> </tr> <tr> <td>TCP协议</td> <td>入站</td> <td>10251</td> <td>kube-scheduler</td> <td>Self</td> </tr> <tr> <td>TCP协议</td> <td>入站</td> <td>10252</td> <td>kube-controller-manager</td> <td>Self</td> </tr> </tbody> </table> <h3>工作节点</h3> <table> <thead> <tr> <th>协议</th> <th>方向</th> <th>端口范围</th> <th>目的</th> <th>使用者</th> </tr> </thead> <tbody> <tr> <td>TCP协议</td> <td>入站</td> <td>10250</td> <td>Kubelet API</td> <td>Self, Control plane</td> </tr> <tr> <td>TCP协议</td> <td>入站</td> <td>30000-32767</td> <td>NodePort Services†</td> <td>All</td> </tr> </tbody> </table> <p>† NodePort Service+ 是默认端口范围。 标有*的任何端口号都是可覆盖的,因此您需要确保您提供的任何自定义端口也处于打开状态。 尽管etcd端口包含在控制平面节点中,但是您也可以在外部或自定义端口上托管自己的etcd群集。 您使用的Pod Network插件(请参阅下文)也可能需要打开某些端口。由于每个Pod网络插件的情况不同,因此请参阅插件的文档,以了解所需的端口。</p> <h2>修改各节点hosts信息</h2> <p>3个节点,都是 Centos 7.8 系统,内核版本:3.10.0-1127.8.2.el7.x86_64,在每个节点上添加 hosts 信息:</p> <pre><code class="language-bash">cat /etc/hosts 172.16.3.156 k8s-master 172.16.3.157 k8s-node2 172.16.3.158 k8s-node2</code></pre> <h2>禁用防火墙</h2> <pre><code class="language-bash">systemctl stop firewalld systemctl disable firewalld</code></pre> <h2>禁用Selinux</h2> <h3>临时禁用</h3> <pre><code class="language-bash">setenforce 0</code></pre> <h3>永久禁用</h3> <pre><code class="language-bash">sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config</code></pre> <h2>禁用交换分区</h2> <h3>临时禁用</h3> <pre><code class="language-bash">swapoff -a</code></pre> <h3>永久禁用</h3> <p>修改 /etc/fstab文件,注释掉swap那一行,使用 free -m 确认 swap 已经关闭。</p> <pre><code class="language-bash">sed -i 's/.*swap.*/#&amp;/' /etc/fstab</code></pre> <p>swappiness 参数调整,修改 /etc/sysctl.d/k8s.conf添加下面一行:</p> <pre><code class="language-bash">vm.swappiness=0</code></pre> <p>执行 <code>sysctl -p /etc/sysctl.d/k8s.conf</code>使修改生效。</p> <h2>同步服务器时间</h2> <p>所有节点都需要同步时间,或master同步互联上时钟服务器,其它节点再以master为时钟服务器进行同步。</p> <pre><code class="language-bash">yum install chrony -y systemctl enable chronyd systemctl start chronyd chronyc sources 210 Number of sources = 4 MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 203.107.6.88 2 6 17 8 +263us[-3870ms] +/- 15ms ^- 119.28.183.184 2 6 17 7 -1962us[-1962us] +/- 45ms ^- a.chl.la 2 6 33 3 -7748us[-7748us] +/- 137ms ^- ntp1.ams1.nl.leaseweb.net 2 6 17 7 -915us[ -915us] +/- 224ms [root@k8s-master ~]# date Fri Jul 3 20:37:31 CST 2020</code></pre> <h1>二、安装docker</h1> <h2>安装所需的软件包</h2> <pre><code class="language-bash">yum install -y yum-utils \ device-mapper-persistent-data \ lvm2</code></pre> <h2>设置稳定版的YUM源</h2> <pre><code class="language-bash">yum-config-manager \ --add-repo \ https://download.docker.com/linux/centos/docker-ce.repo</code></pre> <h2>安装最新版本的Docker社区版和containerd</h2> <pre><code class="language-bash">yum install docker-ce docker-ce-cli containerd.io</code></pre> <h2>安装指定版本</h2> <h3>查看版本号</h3> <pre><code class="language-bash">yum list docker-ce --showduplicates | sort -r * updates: mirrors.cn99.com Loading mirror speeds from cached hostfile Loaded plugins: fastestmirror Installed Packages * extras: mirrors.cn99.com docker-ce.x86_64 3:19.03.9-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.8-3.el7 docker-ce-stable docker-ce.x86_64 3:19.03.1-3.el7 docker-ce-stable ............ docker-ce.x86_64 3:18.09.4-3.el7 docker-ce-stable docker-ce.x86_64 3:18.09.3-3.el7 docker-ce-stable ............ docker-ce.x86_64 18.06.0.ce-3.el7 docker-ce-stable docker-ce.x86_64 18.03.1.ce-1.el7.centos docker-ce-stable ............ docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: mirrors.cn99.com Available Packages</code></pre> <h3>安装指定版本</h3> <pre><code class="language-bash">yum install docker-ce-&lt;VERSION_STRING&gt; docker-ce-cli-&lt;VERSION_STRING&gt; containerd.io 例如: yum install docker-ce-18.06.1.ce-3.el7 docker-ce-cli-18.06.1.ce-3.el7 containerd.io</code></pre> <p>说明: docker已安装但尚未启动。 docker组已创建,但没有用户添加到该组。</p> <h2>启动docker</h2> <pre><code class="language-bash">systemctl start docker</code></pre> <h2>设置随机启动</h2> <pre><code class="language-bash">systemctl enable docker</code></pre> <h2>验证docker是否正确安装</h2> <pre><code class="language-bash">$ sudo docker version $ sudo docker info $ sudo docker run hello-world</code></pre> <h2>设置easytong用户sudo免密操作</h2> <p>vim /etc/sudoers</p> <pre><code class="language-bash">your-user ALL=(ALL) NOPASSWD:ALL</code></pre> <h2>以非root用户身份使用Docker</h2> <p>如果您想以非root用户身份使用Docker,则应将用户添加到“ docker”组:</p> <pre><code class="language-bash">usermod -aG docker your-user</code></pre> <p>注意:请记住注销并重新登录才能生效!</p> <h2>卸载Docker社区版</h2> <h3>卸载Docker软件包</h3> <pre><code class="language-bash">yum remove docker-ce</code></pre> <h3>删除相关文件</h3> <p>主机上的镜像,容器,卷或自定义配置文件不会自动删除。 要删除所有镜像,容器和卷:</p> <pre><code class="language-bash">rm -rf /var/lib/docker</code></pre> <h2>安装私有仓库registry</h2> <pre><code class="language-bash">mkdir /registry docker run -p 5000:5000 --restart=always --name registry -v /registry/:/var/lib/registry -d registry</code></pre> <h2>验证registry是否完装成功</h2> <p>验证私有仓库是否搭建成功,使用浏览器访问http://172.16.3.156:5000/v2/ <img src="https://www.showdoc.cc/server/api/common/visitfile/sign/5408eda35e05629aa2888151137d63ab?showdoc=.jpg" alt="" /> 界面显示{},表示docker registry搭建成功</p> <h2>添加阿里加速源,中科大源,配置registry认证方式等</h2> <p>cat /etc/docker/daemon.json</p> <pre><code class="language-bash">{ "registry-mirrors" : [ "https://dekn3ozn.mirror.aliyuncs.com", "https://docker.mirrors.ustc.edu.cn" ], "insecure-registries": [ "172.16.3.156:5000" ] }</code></pre> <p>-- &quot;registry-mirrors&quot; : [ <a href="https://dekn3ozn.mirror.aliyuncs.com">https://dekn3ozn.mirror.aliyuncs.com</a> ] #阿里加速器 -- &quot;registry-mirrors&quot; : [ <a href="https://docker.mirrors.ustc.edu.cn">https://docker.mirrors.ustc.edu.cn</a> ] #中科大源 -- &quot;insecure-registries&quot;: [ &quot;172.16.3.156:5000&quot; ] #配置docker访问registry私有仓库的认证方式;</p> <h2>注意:</h2> <p>1、registry和client都要配置,配置完后需要重启docker,再启动registry。 2、书写格式若不对,也会报如下错误</p> <pre><code class="language-bash">The push refers to repository [172.16.135:5000/kube-proxy] Get https://172.16.135:5000/v2/: dial tcp: lookup 172.16.135: no such host</code></pre> <h1>三、安装master/Node节点</h1> <h2>添加k8s阿里yum源</h2> <p>由于官方k8s源在google,国内无法访问,这里使用阿里云yum的源</p> <pre><code class="language-bash">cat &lt;&lt;EOF &gt; /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</code></pre> <h2>安装kubeadm、kubectl、kubelet</h2> <pre><code class="language-bash">yum install -y kubectl kubeadm kubelet</code></pre> <h2>启动kubelet服务</h2> <pre><code class="language-bash">systemctl enable kubelet systemctl start kubelet</code></pre> <p>实际没有启动成功,忽略,不影响下一步操作。</p> <h2>说明:</h2> <p><code>到这里为止上面所有的操作都需要在所有节点执行配置。</code></p> <h2>初始化集群</h2> <h3>导入镜像/或通过科学上网在线下载/或下载国内源再修改tag</h3> <p>初始化集群前,先把事先准备的images导入到master节点上。</p> <pre><code class="language-bash">docker load &lt; ./kubernetes_v1.18.5_InstallationPackage2.tar.gz</code></pre> <h3>进行初始化操作</h3> <pre><code class="language-bash">kubeadm init --kubernetes-version v1.18.5 \ --apiserver-advertise-address=172.16.3.156 \ --image-repository registry.aliyuncs.com/google_containers \ #如果先导入镜像,此行要注销。 --service-cidr=10.1.0.0/16 \ --pod-network-cidr=10.244.0.0/16</code></pre> <p>上面安装完后,会提示你输入如下命令,复制粘贴过来,执行即可。下面是初始化过程的部分摘记:</p> <pre><code class="language-bash">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 172.16.3.156:6443 --token 9pem8b.xyzdu0mi6ai76kus \ --discovery-token-ca-cert-hash sha256:35670f9aaf35b508e023cbadaf4799d4be3da706dd11170dc6627b46ecb6f696 </code></pre> <h2>拷贝 kubeconfig 文件</h2> <p>要使kubectl适用于非root用户,请运行以下命令,这些命令也是kubeadm init输出的一部分:</p> <pre><code class="language-bash"> mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config</code></pre> <h2>或者,如果您是root用户,则可以运行:</h2> <pre><code class="language-bash">export KUBECONFIG=/etc/kubernetes/admin.conf</code></pre> <h2>查看master节点状态</h2> <p>安装master节点完毕。可以使用下面命令查看此时master处于NotReady状态,暂时不用管。</p> <pre><code class="language-bash">[root@k8s-master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 2m v1.18.5</code></pre> <h1>安装Node节点</h1> <h2>安装kubeadm、kubelet、kubectl</h2> <p>node节点安装,请参考上面的操作(除registry操作)</p> <h2>安装flannel</h2> <p>每个群集只能安装一个Pod网络。您可以在下面找到一些流行的Pod网络插件的安装说明:</p> <h3>下载kube-flannel.ymly文件</h3> <p>由于国内无法访问,我先下载好kube-flannel.yml文件并保存为kube-flannel-v0.12.0.yml,(这样可以不修改官方kube-flannel-v0.12.0.yml文件内容)。 或者: 因为这个官方kube-flannel-v0.12.0.yml文件中配置了一个国内无法访问的地址(quay.io),可以将kube-flannel-v0.12.0.yml文件中的image改为国内可以访问的地址(quay-mirror.qiniu.com)。 下面两个链接都可以通过科学上网下载。</p> <pre><code class="language-bash">https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml</code></pre> <h2>运行命令</h2> <pre><code class="language-bash">kubectl apply -f kube-flannel-v0.12.0.yml</code></pre> <h2>检查CoreDNS Pod是否Running</h2> <pre><code class="language-bash">kubectl get pods --all-namespaces</code></pre> <p>安装Pod网络后,检查CoreDNS Pod是否在Running来确认其正常工作。而且,一旦CoreDNS Pod启动并运行,您就可以通过加入节点来继续。 如果你的网络不能正常工作或CoreDNS不在Running状态,检查出 故障排除指南 进行kubeadm。</p> <h2>查看pod运行状态</h2> <p>命令很快执行完成,耐心等待几分钟,在master节点上隔一会儿查看 Pod 运行状态:</p> <pre><code class="language-bash">kubectl get nodes</code></pre> <p>当看到所有nodes都是ready状态,集群部署完成。</p> <h2>管理master节点隔离</h2> <p>默认情况下,出于安全原因,您的群集不会在控制平面节点上调度Pod。如果您希望能够在控制平面节点上调度Pod,例如针对单机Kubernetes集群进行开发,请运行:</p> <pre><code class="language-bash">kubectl taint nodes --all node-role.kubernetes.io/master-</code></pre> <p>输出是这样:</p> <pre><code class="language-bash">node/k8s-master untainted taint "node-role.kubernetes.io/master" not found taint "node-role.kubernetes.io/master" not found</code></pre> <p>这node-role.kubernetes.io/master将从所有具有污染的节点(包括管理master节点)中删除污染,这意味着调度程序将能够在任何地方调度Pods。</p> <h2>在node节点执行加入集群</h2> <p>节点是您的工作负载(容器和Pod等)运行的地方。要将新节点添加到群集,请对每台计算机执行以下操作: SSH到机器 成为root(例如sudo su -) 运行由输出的命令kubeadm init。例如:</p> <pre><code class="language-bash">kubeadm join 172.16.3.156:6443 --token 9pem8b.xyzdu0mi6ai76kus \ --discovery-token-ca-cert-hash sha256:35670f9aaf35b508e023cbadaf4799d4be3da706dd11170dc6627b46ecb6f696 </code></pre> <p>用同样的方法添加其它的节点即可。</p> <h2>获取node加入集群的命令</h2> <p>上面kubeadm init执行成功后会返回给你node节点加入集群的命令,等会要在node节点上执行,需要保存下来,如果忘记了,可以使用如下命令获取。</p> <pre><code class="language-bash">kubeadm token create --print-join-command</code></pre> <p>默认情况下,令牌会在24小时后过期。如果要在当前令牌过期后将节点加入集群,则可以通过在控制平面节点上运行以下命令来创建新令牌:</p> <h2>查看Node节点状态</h2> <p>执行成功后运行 kubectl get nodes 命令:</p> <pre><code class="language-bash">kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master NotReady master 39m v1.18.5 k8s-node1 NotReady &lt;none&gt; 106s v1.18.5</code></pre> <p>可以看到是 NotReady 状态,这是因为还没有安装网络插件,接下来安装网络插件.这里我们安装flannel。</p> <h2>安装Dashboard UI</h2> <p>有些地址国内无法访问,国内可以使用recommended.yaml,运行命令</p> <pre><code class="language-bash">kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml</code></pre> <p>或运行本地文件(dashboard-v2.0.0.yml,在这里,我先从上链接下载并存为dashboard-v2.0.0.yml)</p> <pre><code class="language-bash">kubectl apply -f dashboard-v2.0.0.yml</code></pre> <p>如果需要删除安装,执行</p> <pre><code class="language-bash">kubectl delete -f dashboard-v2.0.0.yml</code></pre> <h2>修改成NodePort方式访问,才可以远程访问和正确远程鉴权</h2> <pre><code class="language-bash">kubectl --namespace=kubernetes-dashboard get service kubernetes-dashboard NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes-dashboard ClusterIP 10.1.208.8 &lt;none&gt; 443/TCP 28m</code></pre> <h2>编辑kubernetes-dashboard</h2> <pre><code class="language-bash">kubectl --namespace=kubernetes-dashboard edit service kubernetes-dashboard</code></pre> <p>将里面的type: ClusterIP改为type: NodePort即可。 <strong>wq保存</strong>。等一会儿,重新查看,就变为NodePort了。</p> <pre><code class="language-bash">NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes-dashboard NodePort 10.1.208.8 &lt;none&gt; 443:30060/TCP 50m</code></pre> <h2>新建用户获取令牌</h2> <h3>下载文件admin-role.yaml</h3> <p>下载文件admin-role.yaml(或把内容复制保存为文件),没找到v2.0.0</p> <pre><code class="language-bash">https://github.com/rootsongjc/kubernetes-handbook/blob/master/manifests/dashboard-1.7.1/admin-role.yaml</code></pre> <h3>执行命令</h3> <pre><code class="language-bash">kubectl create -f admin-role.yaml</code></pre> <h3>获取token</h3> <pre><code class="language-bash">kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')</code></pre> <h3>使用token登录系统,完成</h3> <p>然后可以通过上面的 30060 端口去访问 Dashboard,<strong>要记住使用 https</strong>,Chrome不生效可以使用Firefox测试:</p> <pre><code class="language-bash">https://172.16.3.156:30060</code></pre> <p>登录界面如下:</p>

页面列表

ITEM_HTML