目的

        继续搭建单应用到k8s集群上,熟悉后逐渐练习集群应用的搭建,这一篇文档,接着上一篇的在k8s上搭建mysql单节点服务并实现客户端访问。

参考文档

示例:使用 Redis 部署 PHP 留言板应用 | Kubernetes

创建redis deployment

#创建应用配置文件存放目录
mkdir -p /root/application/redis
cd /root/application/redis

#编写redis deployment的配置文件(直接从参考文档复制过来,可以根据需要修改)
vim redis-leader-deployment.yaml
# 来源:https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-leader
  labels:
    app: redis
    role: leader
    tier: backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
        role: leader
        tier: backend
    spec:
      containers:
      - name: leader
        image: "docker.io/redis:6.0.5"
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 6379
root@kmaster1:~/application/redis# kubectl get pods
NAME                     READY   STATUS    RESTARTS     AGE
mysql-6fbb5bdc8b-rcj8s   1/1     Running   1 (6d ago)   6d
root@kmaster1:~/application/redis# ls
redis-leader-deployment.yaml
root@kmaster1:~/application/redis# kubectl apply -f redis-leader-deployment.yaml 
deployment.apps/redis-leader created
root@kmaster1:~/application/redis# kubectl get pods
NAME                            READY   STATUS              RESTARTS     AGE
mysql-6fbb5bdc8b-rcj8s          1/1     Running             1 (6d ago)   6d
redis-leader-6cc46676d8-tjfs9   0/1     ContainerCreating   0            12s

创建redis service

注意:这里改为nodeport的目的是将redis的服务提供给k8s集群外主机访问。也可直接用参考文档的yaml,那就是只能k8s集群内访问redis服务

cd /root/application/redis

vim redis-leader-service.yaml
# 来源:https://cloud.google.com/kubernetes-engine/docs/tutorials/guestbook
apiVersion: v1
kind: Service
metadata:
  name: redis-leader
  labels:
    app: redis
    role: leader
    tier: backend
spec:
  type: NodePort #设定端口类型为节点型
  ports:
  - port: 6379
    targetPort: 6379
    nodePort:32323 #新增此行可以将端口放到k8s集群外
  selector:
    app: redis
    role: leader
    tier: backend
root@kmaster1:~/application/redis# vim redis-leader-service.yaml 
root@kmaster1:~/application/redis# kubectl apply -f redis-leader-service.yaml
service/redis-leader created
root@kmaster1:~/application/redis# kubectl get svc
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP          15d
mysql-service   NodePort    10.105.14.54   <none>        3306:31313/TCP   6d1h
redis-leader    NodePort    10.99.207.98   <none>        6379:32323/TCP   16m

redis客户端访问

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐