文章目录
一、练手:k8s部署部署wordpress+mysql
wordpress是用于快速搭建博客系统。
1、创建wordpress命名空间
# 创建namespace
kubectl create namespace wordpress
# 查看namespace列表
kubectl get ns
2、创建mysql数据库
# 新建wordpress-db.yaml文件
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: mysql-deploy
namespace: wordpress
labels:
app: mysql
spec:
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.6
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3306
name: dbport
env:
- name: MYSQL_ROOT_PASSWORD
value: rootPassW0rd
- name: MYSQL_DATABASE
value: wordpress
- name: MYSQL_USER
value: wordpress
- name: MYSQL_PASSWORD
value: wordpress
volumeMounts:
- name: db
mountPath: /var/lib/mysql
volumes:
- name: db
hostPath:
path: /var/lib/mysql
---
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: wordpress
spec:
selector:
app: mysql
ports:
- name: mysqlport
protocol: TCP
port: 3306
targetPort: dbport
该yaml文件创建一个mysql,并且生成一个service,service对外暴露的端口是3306
# 根据wordpress-db.yaml创建资源[mysql数据库]
kubectl apply -f wordpress-db.yaml
# 查看pod
kubectl get pods -n wordpress -o wide
# 查看详情,查看pod部署进度
kubectl describe pod mysql-deploy-78cd6964bd-smq4k -n wordpress
# 记得获取ip,因为wordpress.yaml文件中要修改
[root@m ~]# kubectl get svc mysql -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.104.214.22 <none> 3306/TCP 42m
kubectl describe svc mysql -n wordpress
3、创建wordpress应用
# 创建wordpress.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: wordpress-deploy
namespace: wordpress
labels:
app: wordpress
spec:
template:
metadata:
labels:
app: wordpress
spec:
containers:
- name: wordpress
image: wordpress
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: wdport
env:
- name: WORDPRESS_DB_HOST
value: mysql:3306 # 改为service的name,不需要每次都指定ip
- name: WORDPRESS_DB_USER
value: wordpress
- name: WORDPRESS_DB_PASSWORD
value: wordpress
---
apiVersion: v1
kind: Service
metadata:
name: wordpress
namespace: wordpress
spec:
type: NodePort # NodePort类型
selector:
app: wordpress
ports:
- name: wordpressport
protocol: TCP
port: 80
targetPort: wdport
# #修改其中mysql的ip地址,其实也可以使用service的name:mysql
kubectl apply -f wordpress.yaml
# 查看详情,查看pod部署进度
kubectl describe pod mysql-deploy-78cd6964bd-smq4k -n wordpress
[root@m ~]# kubectl get pods -n wordpress -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-deploy-78cd6964bd-smq4k 1/1 Running 0 71m 192.168.80.193 w2 <none> <none>
wordpress-deploy-6498447888-c9287 1/1 Running 0 116s 192.168.190.65 w1 <none> <none>
# 获取到转发后的端口,如80端口转到本机的30493端口
[root@m ~]# kubectl get svc -n wordpress
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql ClusterIP 10.104.214.22 <none> 3306/TCP 71m
wordpress NodePort 10.111.92.1 <none> 80:30493/TCP 2m7s
我们发现,搭建成功了,用浏览器访问192.168.56.101:30493,发现访问成功了!
4、小结
在集群中,pod之间可以通过service 的name进行访问,不仅仅是ip,这就意味着,service中不仅帮我们做了负载均衡,而且做了dns处理。
二、实战:部署自己的springboot项目
1、准备一个springboot项目
访问:http://localhost:8080/k8s
先试用maven打成一个jar包:
# maven打包
mvn clean pakcage
# 上传到服务器
[root@m spring-boot]# pwd
/root/spring-boot
[root@m spring-boot]# ll
total 17140
-rw-r--r--. 1 root root 17547894 Jul 5 15:54 springboot-demo-0.0.1-SNAPSHOT.jar
注意!这一步通常来说使用jenkins完成,此处我们是手动完成的。
2、使用docker打成镜像
(1)编写Dockerfile:
FROM openjdk:8-jre-alpine
COPY springboot-demo-0.0.1-SNAPSHOT.jar /springboot-demo.jar
ENTRYPOINT ["java","-jar","/springboot-demo.jar"]
# 根据Dockerfile创建image
docker build -t springboot-demo-image .
# 查看是否成功打成了镜像
docker images
(2)将镜像推送至dockerhub
docker镜像深入学习,docker镜像发布公有云与私有云
# 登录阿里云镜像仓库 # 需要输入密码
docker login --username=沙里沟第二小学扛把子 registry.cn-hangzhou.aliyuncs.com
docker tag springboot-demo-image registry.cn-hangzhou.aliyuncs.com/cxf01/springboot-demo-image:v1.0
docker push registry.cn-hangzhou.aliyuncs.com/cxf01/springboot-demo-image:v1.0
(3)运行测试
# 运行
docker run -d --name s1 springboot-demo-image # 未做端口映射
# 访问
docker inspect s1
curl 172.17.0.2:8080/k8s # 未做端口映射,只能访问容器ip
3、使用k8s部署springboot
# 以Deployment部署Pod
# vi springboot-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: springboot-demo
spec:
selector:
matchLabels:
app: springboot-demo
replicas: 1
template:
metadata:
labels:
app: springboot-demo
spec:
containers:
- name: springboot-demo
image: registry.cn-hangzhou.aliyuncs.com/cxf01/springboot-demo-image:v1.0 # 记得改
ports:
- containerPort: 8080
---
# 创建Pod的Service service的80端口指向pod的8080
apiVersion: v1
kind: Service
metadata:
name: springboot-demo
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: springboot-demo
---
# 创建Ingress,定义访问规则,一定要记得提前创建好nginx ingress controller
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: springboot-demo
spec:
rules:
- host: k8s.demo.cxf
http:
paths:
- path: /
backend:
serviceName: springboot-demo
servicePort: 80
# 启动
kubectl apply -f springboot-demo.yaml
kubectl get pods
kubectl get pods -o wide # 查看pod详细信息 -192.168.80.194
curl 192.168.80.194:8080/k8s
kubectl get svc # 查看service - 10.106.125.90 80:8080
kubectl get ingress # 查看ingress
# 查看详情,查看pod部署进度
kubectl describe pod springboot-demo-6d88c74d5b-l9m7k
kubectl scale deploy springboot-demo --replicas=5 # 扩容为5个
三、实战:部署springcloud项目
1、部署nacos
(1)下载部署nacos server1.0.0
github:https://github.com/alibaba/nacos/releases
01 上传nacos-server-1.0.0.tar.gz到阿里云服务器:/usr/local/nacos
02 解压:tar -zxvf
03 进入到bin目录执行:sh startup.sh -m standalone [需要有java环境的支持]
04 浏览器访问:192.168.56.100:8848/nacos
05 用户名和密码:nacos
2、微服务注册到nacos的地址问题
我们有微服务user、order,在pod注册到nacos上的地址,默认是pod的内部地址。
如果在同一个k8s集群下,这样部署是没问题的,但是不同k8s集群下,微服务之间就互相不可访问了。
(1)使用host模式
...
metadata:
labels:
app: order
spec:
# 主要是加上这句话,注意在order.yaml的位置
hostNetwork: true
containers:
- name: order
image: registry.cn-hangzhou
...
如上,加上hostNetwork: true,就会在nacos上注册的ip端口,会映射成宿主机的ip和端口。