说明:K8S单机安装可能遇到很多坑,网上目前的大部分教程已经不适用最新版了。踩过坑后与大家分享一下方法
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
yum install -y etcd kubernetes
配置修改参考: https://lihaoquan.me/2017/2/25/create-kubernetes-single-node-mode.html
systemctl start etcd
systemctl start docker
systemctl start kube-apiserver.service
systemctl start kube-controller-manager.service
systemctl start kube-scheduler.service
systemctl start kubelet.service
systemctl start kube-proxy.service
如果遇到错误,则使用journalctl -xe查看错误信息,可能遇到需要设置防火墙,设置代理等问题
journalctl -xe
docker pull mysql:5.7
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql
spec:
replicas: 1
selector:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456"
kubectl create -f mysql-rc.yaml
#使用mysql 5.7,直接pull mysql会拉到mysql 8版本,老的JDBC可能不兼容
查看mysql具体启动情况,也会包含错误信息
kubectl describe pod mysql
wget [http://mirror.centos.org/centos/7/os/x86\_64/Packages/python-rhsm-certificates-1.19.10-1.el7\_4.x86\_64.rpm](http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm)
rpm2cpio python-rhsm-certificates-1.19.10-1.el7\_4.x86\_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem
#rpm2cpio命令用于将rpm软件包转换为cpio格式的文件,cpio命令主要是用来建立或者还原备份档的工具程序,cpio命令可以复制文件到归档包中,或者从归档包中复制文件。
删除并重新生成mysql的rc
kubectl delete -f mysql-rc.yaml
kubectl get pods
kubectl create -f mysql-rc.yaml
kubectl describe pod mysql
kubectl get pods
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
kubectl create -f mysql-svc.yaml
docker pull kubeguide/tomcat-app:v1
apiVersion: v1
kind: ReplicationController
metadata:
name: myweb
spec:
replicas: 5
selector:
app: myweb
template:
metadata:
labels:
app: myweb
spec:
containers:
- name: myweb
image: docker.io/kubeguide/tomcat-app:v1
ports:
- containerPort: 8080
env:
- name: MYSQL_SERVICE_HOST
value: "10.254.65.209" #<===这里的mysql地址需要填写service的mysql地址,执行kubectl get svc 查看
- name: MYSQL_SERVICE_PORT
value: "3306"
apiVersion: v1
kind: Service
metadata:
name: myweb
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30001 #外部端口映射
selector:
app: myweb
kubectl create -f myweb-rc.yaml
kubectl create -f myweb-svc.yaml
访问:http://127.0.0.1:30001/demo/index.jsp 进行操作
可以进入容器查看mysql中已经有数据了
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。