在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應用。請根據實際需求調整配置和鏡像。