虚拟机vmware:Centos7
ip:192.168.146.132
内存:1G
硬盘:20G
1、关闭CentOS自带的防火墙
systemctl disable firewalld
systemctl stop firewalld
2、安装etcd和Kubernetes软件(会自动安装Docker软件)
yum install -y etcd kubernetes
3、修改配置文件
修改/etc/sysconfig/docker,修改为:
OPTIONS='--selinux-enabled=false --insecure-registry gcr.io'
Kubernetes apiserver配置文件/etc/kubernetes/apiserver中,把--admission_control参数中的ServiceAccount删除
4、配置CentOS证书
参考内容:Kubernetes创建pod一直处于ContainerCreating排查和解决
因为拉取gcr.io的镜像需要redhat的证书,但是系统默认是没有的,所以,这里我们自己添加。
安装rhsm
yum install -y *rhsm*
通过python-rhsm-certificates的rpm包获得证书:
wget 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
安装完成后,执行一下docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
5、配置docker阿里云镜像加速
这个镜像仓库是我个人在阿里云申请的:
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://ckq4qobq.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
systemctl deamon-reload
systemctl restart docker
6、启动所有服务
systemctl start etcd
systemctl start docker
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy
从github上下载需要的yaml文件:
需要用到的是 java_web_app 文件夹内内容
下载地址:https://github.com/bestlope/k8s_practice
或者:git clone https://github.com/bestlope/k8s_practice.git
1、搭建mysql服务
cd k8s_practice/java_web_app/mysql
#启动mysql的RC服务
kubectl create -f mysql-rc.yaml
#查看刚刚创建的RC
kubectl get rc
#查看pod创建的情况
kubectl get pods
#启动mysql的SVC服务
kubectl create -f mysql-svc.yml
#查看刚刚创建的service
kubectl get svc
2、启动tomcat应用
cd k8s_practice/java_web_app/tomcat
#创建tomcat的RC服务
kubectl create -f myweb-rc.yaml
#创建tomcat的SVC服务
kubectl create -f myweb-svc.yaml
3、通过浏览器访问网页
访问:http://192.168.146.132:30001/
成功看到跳转到Tomcat页面,即成功单机版 K8S 搭建。
开发一个spring boot 项目 基于JPA操作MySQL的CRUD.
核心配置文件:
Dockerfile:
FROM openjdk:8-jdk-alpine
#将本地文件挂到到/tmp目录
VOLUME /tmp
#复制文件到容器
ADD spring-boot-docker-1.0.jar /app.jar
#暴露8080端口
EXPOSE 8081
#配置启动容器后执行的命令
ENTRYPOINT ["java","-jar","/app.jar"]
k8s-app-mysql.yaml
apiVersion: v1
kind: Service
metadata:
name: k8s-app
namespace: default
labels:
app: k8s-app
spec:
type: NodePort
ports:
- port: 8081
nodePort: 30002
selector:
app: k8s-app
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: k8s-app
labels:
app: k8s-app
spec:
replicas: 1
selector:
matchLabels:
app: k8s-app
template:
metadata:
labels:
app: k8s-app
spec:
containers:
- name: k8s-app
image: k8s-mysql:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8081
livenessProbe:
httpGet:
port: 8081
path: /
scheme: HTTP
periodSeconds: 15
initialDelaySeconds: 30
pom.xml : 配置项目依赖与Docker插件
4.0.0
com.neo
spring-boot-docker
1.0
jar
spring-boot-docker
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
UTF-8
1.8
springboot
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
5.1.34
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
com.spotify
docker-maven-plugin
1.0.0
${docker.image.prefix}/${project.artifactId}
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
后台部署:
docker build -t k8s-mysql . 构建镜像
docker images 查询构建的镜像
kubectl create -f k8s-app-mysql.yaml 创建k8s管理的service 、deployment 对外暴露服务
最终效果先展示一下: