0
点赞
收藏
分享

微信扫一扫

运维 - 实践篇(五)- 基础 K8S 单机环境搭建

仲秋花似锦 2022-02-03 阅读 55

运维 - 实践篇(五)- 基础 K8S 单机环境搭建

文章目录

修订时间修订内容备注
2022/02/02完成 K8S 单机环境基础环境搭建create

一、说明

1. 服务器配置介绍

主机名配置公网IP / 网卡IP角色
Tx-Code-424核16G42.192.222.62 / 10.0.4.16Master

二、基础K8S单机环境搭建流程

1. 查看服务器网卡IP

$ ip addr 

2. 修改主机名称

$ hostnamectl set-hostname k8s-master && bash

3. 添加 Host

$ cat > /etc/hosts << EOF
10.0.4.16 k8s-master

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

EOF

4. 关闭防火墙

$ systemctl stop firewalld
$ systemctl disable firewalld

5. 关闭 selinux

$ setenforce 0
$ sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config

6. 关闭 swap 交换区间

# 临时
$ swapoff -a 
# 永久
$ sed -i 's/.*swap.*/#&/' /etc/fstab

7. 配置 Iptables 链路

$ cat > /etc/sysctl.d/k8s.conf <<EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 10
EOF
# 即时生效
$ sysctl --system 

8. 时间同步

yum install ntpdate -y
ntpdate time.windows.com

10. 添加 K8S yum 源

$ cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

11. 安装 kubeadm、kubelet、kubectl

# 安装版本是v1.21.5
yum -y install kubelet-1.21.5-0 kubectl-1.21.5-0 kubeadm-1.21.5-0
systemctl enable kubelet

12. 安装 k8s

  • kubernetes-version :kubernetes 程序组件的版本号,尽量与安装的 kubelet 版本号相同
  • image-repository :指定要使用的镜像仓库 默认: gar.io
  • apiserver-advertise-address :一般为 Master 节点用于集群内通信的IP地址,填主机的网卡IP地址 10.0.4.16,也可以填 0.0.0.0 (这里我自己填的这个)
  • service-cidr :Service 网络的地址范围,其值为 CIDR 格式的网络地址。默认为 10.96.0.0/12
  • pod-network-cidr :Pod 网络的地址范围,其值为 CIDR 格式的网络地址。通常 Flannel 的网络插件默认值为 10.244.0.0/16 ;Calico 插件的默认值为 192.168.0.0/16
kubeadm init \
--apiserver-advertise-address 10.0.4.16 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.21.5 \
--service-cidr 10.96.0.0/12 \
--pod-network-cidr 10.244.0.0/16 \
--ignore-preflight-errors all \
--token-ttl 0
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

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

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 10.0.4.16:6443 --token uu6f09.z498pb30kz87e39m \
        --discovery-token-ca-cert-hash sha256:f8432251857330193c6021b661cb65ca108da8f52034203a2881d24347d4a10f 

13. 配置 kubectl 工具

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

三、配置 k8s 网络插件

1. 安装 calico 网络插件

# 获取 calico.yaml
wget https://docs.projectcalico.org/manifests/calico.yaml 

sed -i "s#192\.168\.0\.0/16#10\.244\.0\.0/16#" calico.yaml

kubectl apply -f calico.yaml

2. 查看结果

kubectl get node

四、K8S单机特殊处理

1. Master 节点污点处理

#执行后看到有输出说明有污点
kubectl get node -o yaml | grep taint -A 5 
#执行这句就行,就是取消污点
kubectl taint nodes --all node-role.kubernetes.io/master-   

2. 安装补全命令的包

yum -y install bash-completion
kubectl completion bash
source /usr/share/bash-completion/bash_completion
kubectl completion bash >/etc/profile.d/kubectl.sh
source /etc/profile.d/kubectl.sh

cat  >>  /root/.bashrc <<EOF
source /etc/profile.d/kubectl.sh
EOF

五、基于K8S单机部署中间服务

$ kubectl create deployment nginx --image=nginx
$ kubectl expose deployment nginx --port=80 --type=NodePort
$ kubectl get pods,svc
举报

相关推荐

k8s运维操作

K8S 运维管理

k8s环境搭建

搭建K8s环境

K8S日常运维手册

k8s 常用运维命令

K8S运维经验分享

0 条评论