1 service对象定位
2 Service 实践
手工创建Service
根据应用部署资源对象,创建SVC对象
kubectl expose deployment nginx --port=80 --type=NodePort
yaml方式创建Service
nginx-web的service资源清单文件
apiVersion: v1
kind: Service
metadata:
name: sswang-nginx-web
labels:
app: nginx-web
spec:
type: NodePort
selector:
app: nginx-web
ports:
- protocol: TCP
name: http
port: 80
targetPort: 80
nodePort: 31080
tomcat-web的service资源清单文件
apiVersion: v1
kind: Service
metadata:
name: sswang-tomcat-web
labels:
app: tomcat-web
spec:
type: NodePort
selector:
app: tomcat
ports:
- protocol: TCP
name: http
port: 8080
targetPort: 8080
nodePort: 31880
3 外部Service
部署外部mysql环境
准备软件源
]# cat /etc/yum.repos.d/MariaDB.repo
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.3/centos7-amd64
gpgcheck=0
更新系统软件包:
yum makecache fast
安装 MySQL 服务器
yum install mariadb-server mariadb -y
设置 MySQL 服务在启动时自动启动
systemctl start mariadb.service
systemctl enable mariadb.service
开启 MySQL 服务器远程访问能力
]# vim /etc/my.cnf.d/server.cnf
[mysqld]
bind-address = 0.0.0.0
重启 MySQL 服务使配置生效
systemctl restart mariadb.service
配置远程主机登录权限
mysql -uroot -p123456 -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;"
mysql -uroot -p123456 -e "FLUSH PRIVILEGES;"
主库上创建数据库
]# mysql -uroot -p123456 -e "
CREATE DATABASE bookinfo default charset utf8 collate utf8_general_ci;
USE bookinfo;
CREATE TABLE book_info (
id INT AUTO_INCREMENT PRIMARY KEY,
book_name VARCHAR(100),
author VARCHAR(100),
date_of_issue DATE,
isDelete BOOLEAN
);
INSERT INTO book_info (book_name, author, date_of_issue, isDelete) VALUES
('Book 1', 'Author 1', '2022-01-01', FALSE),
('Book 2', 'Author 2', '2022-02-01', FALSE),
('Book 3', 'Author 3', '2022-03-01', TRUE);
"
定制资源清单文件
apiVersion: v1
kind: Namespace
metadata:
name: external-ns
---
apiVersion: v1
kind: Endpoints
metadata:
name: ex-mysql
namespace: external-ns
subsets:
- addresses:
- ip: 10.0.0.18
ports:
- port: 3306
---
apiVersion: v1
kind: Service
metadata:
name: ex-mysql
namespace: external-ns
spec:
type: ClusterIP
ports:
- port: 3306
targetPort: 3306
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: bookinfo
namespace: external-ns
spec:
replicas: 1
selector:
matchLabels:
app: flask-bookinfo
template:
metadata:
labels:
app: flask-bookinfo
spec:
containers:
- name: flask-bookinfo
image: kubernetes-register.sswang.com/sswang/flask_bookinfo:2.3.2
imagePullPolicy: Always
ports:
- containerPort: 5000
env:
- name: DB_HOST
value: "ex-mysql"
- name: DB_USER
value: "root"
- name: DB_PASSWORD
value: "123456"
- name: DB_DATABASE
value: "bookinfo"