之前看了很多文章,都是部署后一直報錯,百度解決后下次又忘了,這次決定把從頭到尾的過程記錄下來方便下次再看,部署參考文章尚硅谷Kubernetes(k8s)視頻學習筆記_尚硅谷k8s筆記_溯光旅者的博客-CSDN博客
1、先去下載vmware虛擬機安裝,我安裝的是這個版本VMware Workstation 16 Player
2、去阿里云網(wǎng)站下載centos7鏡像
centos-7-isos-x86_64安裝包下載_開源鏡像站-阿里云
3、新增兩臺虛擬機用來部署,創(chuàng)建步驟都是一樣的
wxsmaster? 4G 4核? 30G
wxsnode1??4G 4核? 30G
?
這里點完成之后一直等待,直到系統(tǒng)創(chuàng)建成功?
?4、ifconfig命令查看ip后開始按順序執(zhí)行下面步驟,都整理好了
#設置主機名分別在wxsmaster執(zhí)行和wxsnode1執(zhí)行,設置完后用hostname可以查看名稱
hostnamectl set-hostname wxsmaster
hostnamectl set-hostname wxsnode1
#關閉防火墻,用systemctl status firewalld查看是否關閉
systemctl stop firewalld
systemctl disable firewalld
#關閉 selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config # 永久
setenforce 0 # 臨時
#關閉 swap,關閉后用free -m命令查看是否關閉
swapoff -a # 臨時
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久
#時間同步,同步后可用date查看
yum install ntpdate -y
ntpdate time.windows.com
#在 master 添加 hosts
cat >> /etc/hosts << EOF
192.168.254.135 wxsmaster
192.168.254.136 wxsnode1
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 # 生效
#安裝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-3.el7
systemctl enable docker && systemctl start docker
docker --version
#配置docker的驅(qū)動和阿里云加速器
cat > /etc/docker/daemon.json << EOF
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}
EOF
#重新加載配置參數(shù),重新啟動docker服務,啟動后用docker info | grep Cgrou 查看docker驅(qū)動是否為systemd
systemctl daemon-reload
systemctl restart docker
#添加阿里云 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
#安裝 kubeadm, kubelet 和 kubectl
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
systemctl enable kubelet
5、初始化,只有wxsmaster執(zhí)行這個命令,下面的apiserver-advertise-address值要改成master節(jié)點的ip
kubeadm init \
--apiserver-advertise-address=192.168.254.135 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.18.0 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
6、初始化完成后,拷貝出圖片里面的命令,在wxsnode1節(jié)點執(zhí)行
kubeadm join 192.168.254.135:6443 --token 23zsua.gsa9vwevq3ee3kdn \
--discovery-token-ca-cert-hash sha256:eb3eba970a429129748ad936d859ac7694e7ea6ed5dc7c8cf5220c8a4d3061ed
7、kubectl get nodes查看節(jié)點發(fā)現(xiàn)報錯了?,兩臺機器加入環(huán)境變量解決
#兩臺機器都加入環(huán)境變量
vim /etc/profile
export KUBECONFIG=/etc/kubernetes/admin.conf
source /etc/profile
#wxsnode1節(jié)點加入后還是報錯,缺少文件,需要從wxsmaster節(jié)點拷貝過來
scp root@192.168.254.135:/etc/kubernetes/admin.conf /etc/kubernetes
8、kubectl get nodes查看節(jié)點狀態(tài)NotReady,用journalctl -xefu kubelet查看運行狀態(tài),發(fā)現(xiàn)報錯了,需要安裝Pod 網(wǎng)絡插件( CNI)(只有wxsmaster主機安裝)
?本地新增一個文件kube-flannel.yaml,貼入一下內(nèi)容,或者去這個網(wǎng)址下載https://github.com/flannel-io/flannel/blob/master/Documentation/kube-flannel.yml
---
kind: Namespace
apiVersion: v1
metadata:
name: kube-flannel
labels:
pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-flannel
labels:
tier: node
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"cniVersion": "0.3.1",
"plugins": [
{
"type": "flannel",
"delegate": {
"hairpinMode": true,
"isDefaultGateway": true
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: kube-flannel-ds
namespace: kube-flannel
labels:
tier: node
app: flannel
spec:
selector:
matchLabels:
app: flannel
template:
metadata:
labels:
tier: node
app: flannel
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/os
operator: In
values:
- linux
hostNetwork: true
priorityClassName: system-node-critical
tolerations:
- operator: Exists
effect: NoSchedule
serviceAccountName: flannel
initContainers:
- name: install-cni-plugin
#image: flannelcni/flannel-cni-plugin:v1.1.0 for ppc64le and mips64le (dockerhub limitations may apply)
image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
command:
- cp
args:
- -f
- /flannel
- /opt/cni/bin/flannel
volumeMounts:
- name: cni-plugin
mountPath: /opt/cni/bin
- name: install-cni
#image: flannelcni/flannel:v0.20.0 for ppc64le and mips64le (dockerhub limitations may apply)
image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.0
command:
- cp
args:
- -f
- /etc/kube-flannel/cni-conf.json
- /etc/cni/net.d/10-flannel.conflist
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
containers:
- name: kube-flannel
#image: flannelcni/flannel:v0.20.0 for ppc64le and mips64le (dockerhub limitations may apply)
image: docker.io/rancher/mirrored-flannelcni-flannel:v0.20.0
command:
- /opt/bin/flanneld
args:
- --ip-masq
- --kube-subnet-mgr
resources:
requests:
cpu: "100m"
memory: "50Mi"
limits:
cpu: "100m"
memory: "50Mi"
securityContext:
privileged: false
capabilities:
add: ["NET_ADMIN", "NET_RAW"]
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: EVENT_QUEUE_DEPTH
value: "5000"
volumeMounts:
- name: run
mountPath: /run/flannel
- name: flannel-cfg
mountPath: /etc/kube-flannel/
- name: xtables-lock
mountPath: /run/xtables.lock
volumes:
- name: run
hostPath:
path: /run/flannel
- name: cni-plugin
hostPath:
path: /opt/cni/bin
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
- name: xtables-lock
hostPath:
path: /run/xtables.lock
type: FileOrCreate
用xftp將這個文件傳入master某個目錄,我是這個
?安裝命令:kubectl apply -f kube-flannel.yaml,安裝完后狀態(tài)變ready了
?9、本地打包springboot的jar包,本地最好java -jar啟動一下看是否能成功,不然后面報錯找問題找半天(經(jīng)驗之談)
?10、去阿里云服務器新增命名空間和鏡像倉庫
?11、創(chuàng)建完后上傳jar包到虛擬機,用上圖第3步驟將docker生成的鏡像上傳到阿里云服務器
#創(chuàng)建Dockerfile,并上傳jar包到里面配置目錄
vim Dockerfile
#輸入下面內(nèi)容
FROM openjdk:8-jdk-alpine
ADD wxstest.jar /home/wxs/wxstest/wxstest.jar
ENTRYPOINT ["java", "-jar", "/home/wxs/wxstest/wxstest.jar"]
#制作鏡像
docker build -t wxstest:1.0.0 .
#查看鏡像
docker images
#重命名鏡像
docker tag 30a9f619627f registry.cn-hangzhou.aliyuncs.com/wxsnamespace/wxstest:1.0.0
#推送到阿里云倉庫
docker push registry.cn-hangzhou.aliyuncs.com/wxsnamespace/wxstest:1.0.0
#拉取鏡像
docker pull registry.cn-hangzhou.aliyuncs.com/wxsnamespace/wxstest:1.0.0
#先用docker部署下看看是否成功,不然后面報錯要重搞好久
docker run -d -p 8088:8088 registry.cn-hangzhou.aliyuncs.com/wxsnamespace/wxstest:1.0.0 -t
12、創(chuàng)建應用,我的目錄是/home/wxs/wxstest
#創(chuàng)建應用
kubectl create deployment wxstest --image=registry.cn-hangzhou.aliyuncs.com/wxsnamespace/wxstest:1.0.0
#刪除命令kubectl delete deployment wxstest
#把上面打印出來的內(nèi)容保存到 wxstest.yaml中,并執(zhí)行下面命令,執(zhí)行完后用這個命令看是否成功kubectl get deployments
kubectl expose deployment wxstest --port=8088 --target-port=8088 --type=NodePort
#刪除命令kubectl delete svc wxstest
#查看應用
kubectl get services
#本機訪問看是否能正常返回頁面
curl http://10.100.143.1:8088/mylogin.html
#查看容器名稱
kubectl get pods -o wide
#查看容器日志
kubectl logs -f wxstest-64686ddd65-dhvtc
13、外網(wǎng)請求虛擬機地址看看
http://192.168.254.135:32057/mylogin.html
http://192.168.254.136:32057/mylogin.html
14、重啟了虛擬機后發(fā)現(xiàn)兩臺機器ip地址變了,需要改成靜態(tài)ip,配置從本機拿
?文章來源:http://www.zghlxwxcb.cn/news/detail-769892.html
#進入配置文件
vi /etc/sysconfig/network-scripts/ifcfg-ens33
#改成如下配置,有的不需要改
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="static"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="285c38d7-6392-478d-ba1d-131066ba5e4d"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.254.135"
NETMASK="255.255.255.0"
GATEWAY="192.168.254.2"
DNS1="8.8.8.8"
#重啟
reboot
15、完事了~~?文章來源地址http://www.zghlxwxcb.cn/news/detail-769892.html
到了這里,關于centos7搭建k8s環(huán)境并部署springboot項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!