K8S部署

发布于:2025-07-17 ⋅ 阅读:(27) ⋅ 点赞:(0)

在这里插入图片描述

一、环境准备
软件环境
操作系统:CentOS 7
Docker: 20+
k8s: 1.23.6
1、初始化操作

关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

关闭selinux

sed -i ‘s/enforcing/disabled/’ /etc/selinux/config # 永久
setenforce 0 # 临时

关闭swap

swapoff -a # 临时
sed -ri ‘s/.swap./#&/’ /etc/fstab # 永久

关闭完swap后,一定要重启一下虚拟机!!!

根据规划设置主机名

hostnamectl set-hostname

在master添加hosts

cat >> /etc/hosts << EOF
192.168.0.172 k8s-master
192.168.0.173 k8s-node1
192.168.0.174 k8s-node2
EOF

将桥接的IPv4流量传递到iptables的链

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

sysctl --system # 生效

#新安装的系统可能存在如下问题
[root@localhost ~]# yum install ntpdate -y
Loaded plugins: fastestmirror
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - “Could not resolve host: mirrorlist.centos.org; Unknown error”

One of the configured repositories failed (Unknown),
and yum doesn’t have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work “fix” this:

 1. Contact the upstream for the repository and get them to fix the problem.

 2. Reconfigure the baseurl/etc. for the repository, to point to a working
    upstream. This is most often useful if you are using a newer
    distribution release than is supported by the repository (and the
    packages for the previous distribution release still work).

 3. Run the command with the repository temporarily disabled
        yum --disablerepo=<repoid> ...

 4. Disable the repository permanently, so yum won't use it by default. Yum
    will then just ignore the repository until you permanently enable it
    again or use --enablerepo for temporary usage:

        yum-config-manager --disable <repoid>
    or
        subscription-manager repos --disable=<repoid>

 5. Configure the failing repository to be skipped, if it is unavailable.
    Note that yum will try to contact the repo. when it runs most commands,
    so will have to try and fail each time (and thus. yum will be be much
    slower). If it is a very temporary problem though, this is often a nice
    compromise:

        yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

解决方法:
sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/.repo
sed -i s/^#.baseurl=http/baseurl=http/g /etc/yum.repos.d/.repo
sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/
.repo
sed -i ‘s/mirrorlist/#mirrorlist/g’ /etc/yum.repos.d/CentOS-*
sed -i ‘s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g’ /etc/yum.repos.d/CentOS-*
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache

时间同步

yum install ntpdate -y
ntpdate time.windows.com

2、安装基础软件(所有节点)
2.1 安装Docker

Docker CE 镜像

配置方法

CentOS 7(使用 yum 进行安装)

step 1: 安装必要的一些系统工具

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Step 2: 添加软件源信息

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

PS:如果出现如下错误信息

Loaded plugins: fastestmirror
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
Could not fetch/save url https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to file /etc/yum.repos.d/docker-ce.repo: [Errno 14] curl#60 - “Peer’s Certificate issuer is not recognized.”

注意:
官方软件源默认启用了最新的软件,您可以通过编辑软件源的方式获取各个版本的软件包。例如官方并没有将测试版本的软件源置为可用,您可以通过以下方式开启。同理可以开启各种测试版本等。
vim /etc/yum.repos.d/docker-ee.repo
将[docker-ce-test]下方的enabled=0修改为enabled=1

安装指定版本的Docker-CE:
Step 1: 查找Docker-CE的版本:
yum list docker-ce.x86_64 --showduplicates | sort -r
Loading mirror speeds from cached hostfile
Loaded plugins: branch, fastestmirror, langpacks
docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
docker-ce.x86_64 17.03.1.ce-1.el7.centos @docker-ce-stable
docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
Available Packages
Step2: 安装指定版本的Docker-CE: (VERSION例如上面的17.03.0.ce.1-1.el7.centos)
sudo yum -y install docker-ce-[VERSION]

安装校验
root@iZbp12adskpuoxodbkqzjfZ:$ docker version
Client:
Version: 17.03.0-ce
API version: 1.26
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 07:52:04 2017
OS/Arch: linux/amd64

Server:
Version: 17.03.0-ce
API version: 1.26 (minimum version 1.12)
Go version: go1.7.5
Git commit: 3a232c8
Built: Tue Feb 28 07:52:04 2017
OS/Arch: linux/amd64
Experimental: false

Docker 加速

由于 Docker 默认下载镜像的仓库是在国外的, 因此国内用户访问相对来说会较慢一些, 目前国内一些大公司与学校搭建了一些国内的镜像仓库, 可以让我们在下载镜像时更方便些

阿里云镜像仓库配置

注册阿里云账号,并登陆到阿里云后台,进入控制台面板

进入控制台以后,找到左上方的三横的功能列表按钮,在弹出来的功能列表处选择弹性计算下的容器镜像服务

进入容器镜像服务页面后,点击左侧菜单栏中镜像中心下的镜像加速器菜单获取镜像加速器地址,在操作文档处选择 Docker 所在服务器的操作系统,并按照文档提示完成配置即可

CentOS

针对Docker客户端版本大于 1.10.0 的用户

您可以通过修改daemon配置文件/etc/docker/daemon.json来使用加速器

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["此处修改成你自己的加速 url"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

2.2 添加阿里云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

2.3 安装 kubeadm、kubelet、kubectl
yum install -y kubelet-1.23.6 kubeadm-1.23.6 kubectl-1.23.6
systemctl enable kubelet

配置关闭 Docker 的 cgroups,修改 /etc/docker/daemon.json,加入以下内容

“exec-opts”: [“native.cgroupdriver=systemd”]

重启 docker

systemctl daemon-reload
systemctl restart docker

  1. 部署 Kubernetes Master

在 Master 节点下执行

kubeadm init
–apiserver-advertise-address=192.168.0.172
–image-repository registry.aliyuncs.com/google_containers
–kubernetes-version v1.23.6
–service-cidr=10.96.0.0/12
–pod-network-cidr=10.244.0.0/16

安装成功后,复制如下配置并执行

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

  1. 加入 Kubernetes Node
    分别在 k8s-node1 和 k8s-node2 执行

下方命令可以在 k8s master 控制台初始化成功后复制 join 命令

kubeadm join 192.168.113.120:6443 --token w34ha2.66if2c8nwmeat9o7 --discovery-token-ca-cert-hash sha256:20e2227554f8883811c01edd850f0cf2f396589d32b57b9984de3353a7389477

如果初始化的 token 不小心清空了,可以通过如下命令获取或者重新申请

如果 token 已经过期,就重新申请

token 没有过期可以通过如下命令获取

kubeadm token list

获取 --discovery-token-ca-cert-hash 值,得到值后需要在前面拼接上 sha256:

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null |
openssl dgst -sha256 -hex | sed ‘s/^.* //’

  1. 部署 CNI 网络插件

在 master 节点上执行

下载 calico 配置文件,可能会网络超时

curl https://docs.projectcalico.org/manifests/calico.yaml -O

修改 calico.yaml 文件中的 CALICO_IPV4POOL_CIDR 配置,修改为与初始化的 cidr 相同

修改 IP_AUTODETECTION_METHOD 下的网卡名称

删除镜像 docker.io/ 前缀,避免下载过慢导致失败

sed -i ‘s#docker.io/##g’ calico.yaml

kubectl apply -f calico.yaml

kubectl describe po calico-kube-controller^C6dfcd885bf-l2zl7 -n kube-system

  1. 测试 kubernetes 集群

创建部署

kubectl create deployment nginx --image=nginx

暴露端口

kubectl expose deployment nginx --port=80 --type=NodePort

查看 pod 以及服务信息

kubectl get pod,svc

journalctl -xefu kubelet

pod异常查询思路
查看所有pod状态
kubectl get po -n kube-system
查看指定pod状态
kubectl get po calico-kube-controllers-6dfcd885bf-l2zl7 -n kube-system
查看指定pod运行日志 -n 指定命名空间
kubectl describe po calico-kube-controllers-6dfcd885bf-l2zl7 -n kube-system

在任意节点使用kubectl

1. 将 master 节点中 /etc/kubernetes/admin.conf 拷贝到需要运行的服务器的 /etc/kubernetes 目录中

scp /etc/kubernetes/admin.conf root@k8s-node1:/etc/kubernetes

2. 在对应的服务器上配置环境变量

echo “export KUBECONFIG=/etc/kubernetes/admin.conf” >> ~/.bash_profile
source ~/.bash_profile

通过kubectl执行相关命令到pod
kubectl exec -it nginx-po -c nginx – more /etc/nginx/conf.d/default.conf

创建nginx yaml的demo
apiVersion: v1 #api文档版本
kind: Pod #资源对象类型,也可以配置为Deployment、Statefuleset这一类的对象
metadata: #Pod相关的元数据 用于描述pod的数据
name: nginx-po #Pod名称
labels: #定义pod的标签
type: app #自定义label的标签,名字为type,值为app
version: v1.0.0 #自定义的pod版本号
namespace: ‘default’ #命名空间的配置
spec:
selector:
matchLables:
app: nginx
replicas: 3
spec: #期望POD按照这里面的描述进行构建
containers: #对于pod中的容器描述

  • name: nginx #容器名称
    image: nginx:1.7.9 #指定容器的镜像
    imagePullPolicy: IfNotPresent #镜像拉取策略,指定如果本地有就用本地,如果没有就远程拉取
    startupProbe: #应用启动探针配置
    tcpSocket:
    port: 80 #请求端口
    failureThreshold: 3 #失败多少次才算正在失败
    periodSeconds: 10 #间隔时间
    successThreshold: 1 #多少次检测成功算成功
    timeoutSeconds: 5 #请求超市时间
    workingDir: /usr/share/nginx/html #容器启动后的工作目录,即进入容器后的目录
    ports:
    • name: http #端口名称
      containerPort: 8080 #描述容器内要堡垒的端口
      protocol: TCP #描述该端口是基于哪种协议通信的
      env: #环境变量
    • name: JVM_OPTS #环境变量的名称
      value: ‘Xms128m -Xmx128m’ #环境变量的值
      resources: #指定资源限制和资源请求的值(这里开始就是设置容器的资源上限)
      requests: #必须要多少资源
      cpu: 100m #限制cpu最少使用一个核心的十分之一 1000m等于一个核心
      memory: 128Mi #限制内存最少使用128兆
      limits: #最多使用多少资源
      cpu: 200m #cpu最多使用一个核心的十分之二
      memory: 128Mi #最多使用512兆
      restartPolicy: OnFailure #重启策略只要失败的情况才会重启

网站公告

今日签到

点亮在社区的每一天
去签到