在CentOS上部署Kubernetes(k8s)应用通常涉及以下几个步骤:准备环境、安装Docker、安装kubeadm、kubelet和kubectl,以及初始化Kubernetes集群。以下是一个基本的指南,帮助你在CentOS 7上部署一个简单的Kubernetes集群,并部署一个应用。
配置主机名:
hostnamectl set-hostname k8s-master
关闭防火墙:
systemctl stop firewalld && systemctl disable firewalld
关闭swap:
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
关闭SELinux:
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
设置时区:
timedatectl set-timezone Asia/Shanghai
时间同步:
yum -y install ntp
ntpdate time.windows.com
hwclock --systohc
配置网络:
编辑 /etc/sysctl.d/k8s.conf
文件,添加以下内容:
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
然后应用配置:
sysctl --system
添加Docker镜像源:
curl https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
安装Docker:
yum list docker-ce --showduplicates | sort -r
yum install -y docker-ce-20.10.6-3.el7
systemctl start docker
systemctl enable docker
添加Kubernetes镜像源:
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
安装kubeadm、kubelet和kubectl:
yum list kubelet --showduplicates | sort -r
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet
systemctl start kubelet
初始化Master节点:
kubeadm init --pod-network-cidr=10.244.0.0/16
配置kubectl:
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
安装网络插件(例如Calico):
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
假设你要部署一个简单的Nginx应用,可以按照以下步骤进行:
制作Docker镜像:
创建一个 Dockerfile
:
FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
构建镜像:
docker build -t nginx .
推送镜像到Harbor或其他仓库:
docker tag nginx:latest your-registry/nginx:latest
docker push your-registry/nginx:latest
在Kubernetes集群中部署Nginx:
创建一个 deployment.yaml
文件:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: your-registry/nginx:latest
ports:
- containerPort: 80
应用配置:
kubectl apply -f deployment.yaml
验证部署:
kubectl get pods
通过以上步骤,你可以在CentOS上成功部署一个Kubernetes集群,并部署一个简单的Nginx应用。请根据实际需求调整配置和镜像。