K8s 入门案例
准备三台服务器现在阿里云申请三台服务器,一台作为主机,两台作为节点k8s-master为控制节点:39.103.231.230、k8s-node1为工作节点:39.103.231.230、k8s-node2为工作节点:39.103.231.200三台服务器都要添加以下配置修改主机名:vim/etc/hostnameuname-nreboot添加主机名与IP对应关系(记得设置主机名):vim/etc
准备三台服务器
现在阿里云申请三台服务器,一台作为主机,两台作为节点
k8s-master为控制节点:39.103.231.230、k8s-node1为工作节点:39.103.231.230、k8s-node2为工作节点:39.103.231.200
三台服务器都要添加以下配置
- 修改主机名:
vim /etc/hostname
uname -n
reboot
-
添加主机名与IP对应关系(记得设置主机名):
vim /etc/hosts
172.21.74.195 k8s-master
172.21.74.194 k8s-node1
172.21.161.186 k8s-node2
-
关闭 swap:
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
swapoff -a
-
关闭防火墙:
systemctl stop firewalld
systemctl disable firewalld
firewall-cmd --state
-
将桥接的IPv4流量传递到 iptables 的链:
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
-
设置仓库:
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2
-
设置淘宝镜像:
sudo yum-config-manager \
--add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-
安装 Docker:
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
yum -y install docker-ce-18.06.1.ce
systemctl enable docker && systemctl start docker
docker --version
-
添加阿里云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=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
-
安装kubeadm,kubelet和kubectl:
yum install -y kubelet-1.15.0 kubeadm-1.15.0 kubectl-1.15.0
systemctl enable kubelet
控制节点服务器添加以下配置
-
创建一个 Master 节点:
kubeadm init \
--apiserver-advertise-address=172.21.74.195 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.15.0 \
--service-cidr=10.1.0.0/16 \
--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 -g) $HOME/.kube/config
-
添加网络:kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
-
查看网络准备状态:kubectl get pods -n kube-system
-
查看节点:kubectl get nodes
工作节点服务器添加以下配置
-
安装Pod网络插件:docker pull quay.io/coreos/flannel:v0.14.0-rc1
-
将一个 Node 节点加入到当前集群中:
kubeadm join 172.21.74.195:6443 --token zs0zmi.av77vl9vk0j1pf4u \
--discovery-token-ca-cert-hash sha256:9e3f1f3db8337dcae6f9338f49e7bca215c24547d75fc6922f874688808300ab
-
安装可视化控制台:
docker search kubernetes-dashboard
docker pull lizhenliang/kubernetes-dashboard-amd64:v1.10.1
k8s控制台配置(在控制节点服务器操作)
-
访问路径:
https://39.103.231.230:30001
-
创建管理员及绑定权限:
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
-
获取 token:kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
部署nginx到k8s 集群中示例(在控制节点服务器操作)
-
部署 nginx:
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
-
扩容三个副本:kubectl scale deployment nginx --replicas=3
-
尝试运行:kubectl expose deployment java-demo --port=80 --target-port=8080 --type=NodePort -o yaml --dry-run >svc.yaml
更多推荐
所有评论(0)