We study associative ternary algebras and describe a general approach which allows us to construct various classes of ternary algebras. Applying this approach to a central bimodule with a covariant derivative we construct a ternary algebra whose ternary multiplication is closely related to the curvature of the covariant derivative. We also apply our approach to a bimodule over two associative (binary) algebras in order to construct a ternary algebra which we use to produce a large class of Lie algebras. We study the calculus of cubic matrices and use this calculus to construct a matrix ternary algebra with associativity of second kind.